Astral Zhang
Astral Zhang

Reputation: 39

No error during debug but error during normal execution

I'm doing a multi-threading program and at times i received segmentation fault. Hence, in order to effectively debug this fault, i would make use of the gdb function.

Usually, i would write all my object file in the a shell script file as followed.

g++ ObjectFile.o ObjectFile2.o LogicFile.cpp -lpthread LogicExe

In order to debug, i just added a -g extension behind.

g++ ObjectFile.o ObjectFile2.o LogicFile.cpp -lpthread LogicExe -g

Next, i would use this command gdb LogicExe, followed by run LogicExe.

In spite of running multiple times, there's no segmentation fault at all while it happens otherwise when i compile without the gdb and -g function.

Why does the debugger not detect the segmentation fault like the normal execution would?

Upvotes: 3

Views: 765

Answers (1)

Mark Plotnick
Mark Plotnick

Reputation: 10271

When you run a program under gdb, the execution will differ in a few ways compared to running it directly from a shell:

  • gdb will, by default, disable Address Space Layout Randomization (ASLR) in order to give you more reproducible results on each run. But this may mask memory corruption errors in your program. You can disable this feature by typing (gdb) set disable-randomization off before starting your program.

  • gdb will set LINES and COLUMNS in your program's environment, creating them if they were not present. This will alter the size of the environment, thus the base of the stack of the program will be different when run under gdb. You can remove those variables from the environment by typing (gdb) unset environment COLUMNS and (gdb) unset environment LINES before starting your program.

  • gdb monitors dynamic library events and thread creation, briefly stopping execution when they occur.

Upvotes: 2

Related Questions