0

I'm working for a company, that does not have a habit of adding log entries in their source code.

Hence, if something goes wrong, the amount of logs, explaining what might have happened, is too small to make any real analysis.

Therefore I'm looking for a tool which can do the following:

  • Attach to a running process and link to the symbols file.
  • Follow all lines of source code which are executed.
  • After a certain key is pressed (like "Ctrl+C"), produce a report which looks as follows:

[]

file1.c:010:  function1(1, 2, 5)
file1.c:011:    sum(1,2)
file1.c:020:      return 3;
file1.c:012:    sum(3,5);
file1.c:020:      return 8;
file1.c:012:    return 8;

I can imagine this question sounding very naïve, but if I can have something which just approaches this result, it might be very useful.

Does anybody know if this can be achieved using windbg, cdb, Visual Studio or any other means?

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • I guess the closest you could achieve with windbg (and without too much hassle) would be the [wt](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/wt--trace-and-watch-data-) command starting at `main()`and restricting the trace to your module (`-m`option) and tweaking the depth option (`-l`). Don't forget to have the output forward to a logfile (`.logopen` command). Be prepared for a really big output (depending on your program). – Neitsa Nov 16 '17 at 17:16
  • Is it optimized code? Is it .NET code? IMHO we have too little information to answer the question reliably. – Thomas Weller Nov 16 '17 at 20:43
  • Time Travel Debugging records all the information that you would need, though I don't think there's an extension to then show it as source lines executed (might be possible to write one) https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/time-travel-debugging-overview – snoone Nov 22 '17 at 14:27

1 Answers1

2

do you have the source code and symbols for your exe if yes windbg can step and print source lines

demo below for a simple recv sample

start an executable whose pdb with src info is available

:\>cdb recv

Microsoft (R) Windows Debugger Version 10.0.16299.15 X86

windbg breaks on system breakpoint

ntdll!LdrpDoDebuggerBreak+0x2c:
771a05a6 cc              int     3

enable loading of line information enable stepping in source mode enable printing of src lines

0:000> .lines
Line number information will be loaded
0:000> l+t
Source options are 1:
     1/t - Step/trace by source line
0:000> l+s
Source options are 5:
     1/t - Step/trace by source line
     4/s - List source code at prompt

disallow all other output except src

0:000> .prompt_allow -reg -dis -sym -ea
Allow the following information to be displayed at the prompt:
(Other settings can affect whether the information is actually displayed)
   src - Source info for current instruction
Do not allow the following information to be displayed at the prompt:
   sym - Symbol for current instruction
   dis - Disassembly of current instruction
    ea - Effective address for current instruction
   reg - Register state

go to main and step 10 times you will see each step is showing the src

read and use controlling the target in windbg help to know about various execution methods like step until return , step until branch etc

0:000> g recv!main
ModLoad: 69f50000 69f53000   C:\Windows\system32\api-ms-win-core-synch-l1-2-0.DLL
>   13: int __cdecl main() {
0:000> p 10
>   24:     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
>   25:     if (iResult != NO_ERROR) {
>   30:     ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
>   31:     if (ConnectSocket == INVALID_SOCKET) {
>   38:     clientService.sin_family = AF_INET;
>   39:     clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
>   40:     clientService.sin_port = htons( 27015 );
>   42:     iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
>   43:     if ( iResult == SOCKET_ERROR) {
>   44:         closesocket (ConnectSocket);
>   45:         printf("Unable to connect to server: %ld\n", WSAGetLastError());

Unable to connect to server: 0
>   66:         WSACleanup();
>   67:         return 1;
>   88: }
*** The C++ standard library and CRT step filter can be enabled to skip this fun
ction. Run .settings set Sources.SkipCrtCode = true">.settings set Sources.SkipC
rtCode = true to enable it. ***
blabb
  • 8,674
  • 1
  • 18
  • 27
  • 2
    cdb is not a requirement you can do that with windbg also the help file for windbg and cdb is the same one debugger.chm – blabb Nov 17 '17 at 10:09