11

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.

Jon Taylor
  • 181
  • 1
  • 9
  • 2
    Perhaps helpful: [How to debug a crash before main?](https://stackoverflow.com/a/30142423/7582247) – Ted Lyngmo Apr 23 '19 at 16:49
  • Check all global variables with constructors or significant initialization logic. I assume you have a smurfton of code and a [mcve] is out of the question? – user4581301 Apr 23 '19 at 16:50
  • 2
    Possible duplicate of [How to debug a crash before main?](https://stackoverflow.com/q/7808769/608639), [How to debug program crashing before main()](https://stackoverflow.com/q/42056867/608639), etc. – jww Apr 23 '19 at 16:50
  • 1
    Why are you writing in two completely different languages. The problem is probably caused during the construction of global variables. Why do you have globals? Which probably means you have a bug in the initialization order of your globals. Look up how to sequence the order of your global variables. – Martin York Apr 23 '19 at 17:18
  • @user4581301, most of the code was not written by me, and you are right, it is a very large amount of code, and I have no idea what part of it (if any) is causing this problem.... – Jon Taylor Apr 23 '19 at 17:45
  • @Martin York, I am writing in two languages because this is a userspace-targeted version of an embedded stack with an RTOS, libc and libstdc++ all included in a statically-linked binary. – Jon Taylor Apr 23 '19 at 17:47
  • @jww, I took a look at the two links you mentioned. The second one was about porting something from Linux to Windows, which I am not doing. The other suggested using the LD_DEBUG environment variable to debug dynamic library loading, which A) Should not be relevant since my executable is statically linked, and B) I tried and got no additional output other than 'Segmentation fault'. – Jon Taylor Apr 23 '19 at 17:54
  • 1
    Please show your code, concentrate on the minimal code that causes the segfault. Others might learn more from your actual mistake (as in what your code does wrong), then how you use gdb or other techniques to identify cause, but both will be more helpful than neither. – 2785528 Apr 23 '19 at 17:58
  • Can you post the command lines for compiling and linking, along with your toolchain component versions please. – Maxim Egorushkin Apr 23 '19 at 19:03
  • Can you build dynamically linked binary and see if the crash still happens? Can you post strace output on statically-linked binary? – ks1322 Apr 24 '19 at 15:50
  • Also could be duplicate of https://stackoverflow.com/q/33710113/72178. – ks1322 Apr 24 '19 at 16:09

1 Answers1

2

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.

Lord Alveric
  • 327
  • 2
  • 4