Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes a Sigtrap in a Debug Session

Tags:

c++

debugging

gdb

In my c++ program I'm using a library which will "send?" a Sigtrap on a certain operations when I'm debugging it (using gdb as a debugger). I can then choose whether I wish to Continue or Stop the program. If I choose to continue the program works as expected, but setting custom breakpoints after a Sigtrap has been caught causes the debugger/program to crash.

So here are my questions:

  1. What causes such a Sigtrap? Is it a leftover line of code that can be removed, or is it caused by the debugger when he "finds something he doesn't like" ?
  2. Is a sigtrap, generally speaking, a bad thing, and if so, why does the program run flawlessly when I compile a Release and not a Debug Version?
  3. What does a Sigtrap indicate?

This is a more general approach to a question I posted yesterday Boost Filesystem: recursive_directory_iterator constructor causes SIGTRAPS and debug problems.
I think my question was far to specific, and I don't want you to solve my problem but help me (and hopefully others) to understand the background.

Thanks a lot.

like image 665
zitroneneis Avatar asked Aug 13 '10 08:08

zitroneneis


People also ask

What causes a SIGTRAP?

The SIGTRAP signal is sent to a process when an exception (or trap) occurs: a condition that a debugger has requested to be informed of — for example, when a particular function is executed, or when a particular variable changes value. Indeed, debuggers use this signal to set their breakpoints.

What is a breakpoint's role in the debugger?

Breakpoints are one of the most important debugging techniques in your developer's toolbox. You set breakpoints wherever you want to pause debugger execution. For example, you may want to see the state of code variables or look at the call stack at a certain breakpoint.

How do I stop debugging in Debug view?

To terminate all debugging sessions, right-click in the Debug view and choose Terminate All from the pop-up menu. If you have terminated one or more sessions and would like to remove them from the Debug view, right-click in the Debug view and choose Remove All Terminated from the pop-up menu.

What is a Debug session?

A debugging session begins when the engine acquires a target and continues until all of the targets have been discarded. A debugging session is inaccessible while the targets are executing and accessible when the current target is suspended.


2 Answers

With processors that support instruction breakpoints or data watchpoints, the debugger will ask the CPU to watch for instruction accesses to a specific address, or data reads/writes to a specific address, and then run full-speed.

When the processor detects the event, it will trap into the kernel, and the kernel will send SIGTRAP to the process being debugged. Normally, SIGTRAP would kill the process, but because it is being debugged, the debugger will be notified of the signal and handle it, mostly by letting you inspect the state of the process before continuing execution.

With processors that don't support breakpoints or watchpoints, the entire debugging environment is probably done through code interpretation and memory emulation, which is immensely slower. (I imagine clever tricks could be done by setting pagetable flags to forbid reading or writing, whichever needs to be trapped, and letting the kernel fix up the pagetables, signaling the debugger, and then restricting the page flags again. This could probably support near-arbitrary number of watchpoints and breakpoints, and run only marginally slower for cases when the watchpoint or breakpoint aren't frequently accessed.)

The question I placed into the comment field looks apropos here, only because Windows isn't actually sending a SIGTRAP, but rather signaling a breakpoint in its own native way. I assume when you're debugging programs, that debug versions of system libraries are used, and ensure that memory accesses appear to make sense. You might have a bug in your program that is papered-over at runtime, but may in fact be causing further problems elsewhere.

I haven't done development on Windows, but perhaps you could get further details by looking through your Windows Event Log?

like image 64
sarnold Avatar answered Sep 20 '22 14:09

sarnold


While working in Eclipse with minGW/gcc compiler, I realized it's reacting very bad with vectors in my code, resulting to an unclear SIGTRAP signal and sometimes even showing abnormal debugger behavior (i.e. jumping somewhere up in the code and continuing execution of the code in reverse order!).

I have copied the files from my project into the VisualStudio and resolved the issues, then copied the changes back to eclipse and voila, worked like a charm. The reasons were like vector initialization differences with reserve() and resize() functions, or trying to access elements out of the bounds of the vector array.

Hope this will help someone else.

like image 28
Hack06 Avatar answered Sep 23 '22 14:09

Hack06