Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breakpoints out of nowhere when debugging with gdb, inside ntdll

Tags:

debugging

gdb

I made a very simple program which automates some things for me.I wrote it in c++ and it runs on Windows. While debugging it with GDB from inside the Codeblocks IDE , I get many breakpoints out of nowhere. I have no idea what might be causing this problem. The breakpoints seem to be related with memory issues ... since when I fixed a memory leak I had detected, the breakpoints number got significantly less.

The exact thing that gdb tells me is:

 Program received signal SIGTRAP, Trace/breakpoint trap.
 In ntdll!TpWaitForAlpcCompletion () (C:\Windows\system32\ntdll.dll)

I get this many many times inside my program. I think that I might be doing something very wrong, even though the program seems to run just fine and it accomplishes what I want it to do. Can anyone tell me what is the problem since I don't know where to look? Also if it is not a problem, then does anyone know how to disable it since this prevents me from getting to the breakpoints I set myself?

Thanks in advance!

EDIT: (Adding the output of GDB's where command): Where can I check what each of these functions do, so I can see what I am doing wrong?

#0  0x76fefadd in ntdll!TpWaitForAlpcCompletion () from C:\Windows\system32\ntdll.dll
#1  0x0028e894 in ?? ()
#2  0x76fb272c in ntdll!RtlCreateUserStack () from C:\Windows\system32\ntdll.dll
#3  0x00657fb8 in ?? ()
#4  0x00657fb8 in ?? ()
#5  0x76f4b76a in ntdll!RtlDowncaseUnicodeChar () from C:\Windows\system32\ntdll.dll
#6  0x02070005 in ?? ()
#7  0x00000b10 in ?? ()
#8  0x0028e8dc in ?? ()
#9  0x76ff0b37 in ntdll!TpQueryPoolStackInformation () from C:\Windows\system32\ntdll.dll
#10 0x038b0000 in ?? ()
#11 0x00657fb8 in ?? ()
#12 0x76f4b76a in ntdll!RtlDowncaseUnicodeChar () from C:\Windows\system32\ntdll.dll
#13 0x6e6e9a5e in ?? ()
#14 0x038b0000 in ?? ()
#15 0x038b0000 in ?? ()
#16 0x00000000 in ?? ()
like image 249
Lefteris Avatar asked Oct 25 '09 14:10

Lefteris


People also ask

How do I remove a break point in GDB?

With the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers. It is not necessary to delete a breakpoint to proceed past it.

How do breakpoints work in GDB?

Software Breakpoint They work by patching the code you are trying to execute with an instruction that triggers a debug event in some fashion. This is accomplished by injecting a breakpoint instruction or when that is not supported by inserting an instruction that causes a fault that halts the core.

What is breakpoint trap?

Breakpoint trap just means the processor has hit a breakpoint. There are two possibilities for why this is happening. Most likely, your initialization code is being hit because your CPU is resetting and hitting the breakpoint again.

What is the GDB command for setting a break point with GDB?

To do this, just type "break [functionname]". gdb will stop your program just before that function is called. Breakpoints stay set when your program ends, so you do not have to reset them unless you quit gdb and restart it.


4 Answers

Wow, you sent me back 5 years to when I used gdb on the linux platform :)

you can prevent gdb to break when receiving SIGTRAP with the following command:

handle SIGTRAP nostop

But I'm with steve here, Try WinDbg instead. It was built especially for windows.

like image 163
Moshe Levi Avatar answered Oct 13 '22 20:10

Moshe Levi


I just experienced this crash using gcc/mingw with Qt Creator.

I had other problems, too, including variables that seemed to be wrong (offset by 6 bytes). In my case, it turned out to be a pragma pack that wreaked havoc on the code.

I had this:

#pragma pack(1)
typedef struct SomeStruct
{
... // structure
} SomeStruct;

... but I didn't terminate it with a pragma pack() call to end packing of 1 after the structure and return to the default byte alignment/packing. Adding that fixed it:

#pragma pack(1)
typedef struct SomeStruct
{
... // structure
} SomeStruct;
#pragma pack() // <<-- with this line the heap corruption problem was stopped
like image 33
Peter Avatar answered Oct 13 '22 18:10

Peter


While the question is quite old now, here are some points that might help someone that would come here after a search like I did:

I just encountered that problem while testing on Win7 an app developed by me on WinXP. In my case it is related both to Windows 7 memory management monitoring and a bad memory allocation in my app.

To make the story short, in the app code, a memory block that was malloced by error (instead of using GlobalAlloc()) and was freed with a GlobalFree() (which is wrong, as the system heap was accessed with a pointer from the C runtime memory pool). While this is causing a (very small in that case) memory leak, it was completely unnoticed while testing on WinXP and the whole program was running apparently correctly.

Now when executed on Win7, a memory monitoring feature called Fault Tolerant Heap (FTH) detects this bug of the app (that is causing an exception) :

  • At the same time it is outputting some info via OutputDebugString() (or maybe DbgPrint()) that can be viewed using the simple DebugView tool, or by any debugger when tracing the application. Thus just before the received signal you can see something like that in the messages :

    warning: HEAP[name_of_your.exe]:

    warning: Invalid address specified to RtlFreeHeap( 006B0000, 006A3698 )

  • And (in the case the app is being debugged) it outputs a breakpoint that has no effect outside of a debugger, but else that should help to point the problem. That breakpoint is shown as the SIGTRAP signal by GDB.

At this point you have 2 alternatives :

  • try to walk the call stack to find the faulty instruction in your code (unfortunately in that case, the bt or where gdb commands cannot show far enough to see where in my code the heap was wrongly freed - if someone knows how to display the correct call stack from the starting module instead of ntdll, let me know)
  • try to continue as FTH has the ability to automagically patch in memory as an attempt to work around your bug (that automagic patch can also occur in advance on the next run of the app)

For not stopping at the time of the heap problem, as Moshe Levi said, you can set a handle SIGTRAP nostop at the GDB prompt before starting the app.

So in short : yes you did have something wrong in your code relative to memory management but in some cases it can run without crashing. But in debug mode the kernel tries to put you on the path of the problem.

like image 23
Seki Avatar answered Oct 13 '22 19:10

Seki


I'd recommend to use WinDBG, the native windows debugger. This will give better stack traces especially for the symbols when it transitions to kernel mode.

like image 35
steve Avatar answered Oct 13 '22 19:10

steve