Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug an executable with gdb when it crashes during startup?

Tags:

c++

c

linux

gdb

I have a C-and-C++-based project I just got to build and link for the first time, and it segfaults on execution. I tried running it in gdb to get a backtrace, and saw this:

gdb) run
Starting program: /home/jon/controlix-code/bin/controlix 
During startup program terminated with signal SIGSEGV, Segmentation fault.
(gdb) bt
No stack.
(gdb) 

I assume it is crashing before main() is called, but beyond that I don't have a clue. I haven't been able to find much about this type of situation on Google, so I thought I'd ask here.

like image 611
Jon Taylor Avatar asked Apr 23 '19 16:04

Jon Taylor


People also ask

How do I set a breakpoint in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

What command is used to start up GDB?

Use the run command to start your program under gdb.

How do I restart GDB?

When you're doing a usual gdb session on an executable file on the same computer, you can give the run command and it will start the program over again.


1 Answers

One approach is to catch all exceptions before running:

catch throw
run

And if that does not help, you may have to single-step through the assembly from the very beginning. But before you do that,

break main
run

and single-step through the code using step and next should lead you to the culprit.

like image 139
Lord Alveric Avatar answered Sep 23 '22 00:09

Lord Alveric