0

I'm debugging a nasty problem where #includeing a file(not anything I wrote, for the record) causes a crash in my program. This means, I have working and broken, with only one C(++) include statement changed. Some of the libraries I'm using don't have debugging information.

What I would like to do is get GDB to output every line of C++ executed for the program run, and x86 instructions where not available to a textfile in such a format that I can diff the two outputs and hopefully figure out what went wrong.

Is this easily possible in GDB?

3 Answers 3

5

You can check the difference between the pre-processed output in each version. For example:

gcc -dD -E a.cc -o a.pre
gcc -dD -E b.cc -o b.pre
diff -u a.pre b.pre

You can experiment with different "-d" settings to make that more verbose/concise. Maybe something in the difference of listings will be obvious. It's usually something like a struct which changes size depending on include files.

Failing that, if you really want to mess with per-instruction or line traces, you could probably use valgrind and see where the paths diverge, but I think you may be in for a world of pain. In fact you'll probably find valgrind finds your bug and then 100 you didn't know about :) I expect the problem is just a struct or other data size difference, and you won't need to bother.

You could get gdb to automate line tracing. It would be quite painful. Basically you'd need to script it to run "n" (next line) repeatedly until a crash, then check the logs. If you can script "b main", then "run", then infinite "n" that would do it. There's probably a built-in command to do it but I'm not aware of it.

Sign up to request clarification or add additional context in comments.

4 Comments

As well as #if/#else variations on structs etc., there could be some static initialisation that's buggy. If there's some dependency, the #include might change the order of other things it includes, resulting in the crash. Or, the #include file might add a static object whose initialisation fails. gcc -E output is a good way to start investigating those issues too. It might minimise diff noise if the failing #include is put last (so the order of other includes isn't changed), then see if it still breaks.
Just played with some combinations to reduce diff spam. This may work better: gcc -E a.cc -o a.pre; ...; diff -uw -I'^#' a.pre b.pre
Thanks for the suggestion. The problem ended up being that a header file(two levels deep!) had #pragma pack(1) with no corresponding #pragma pack(pop) at the end, leading to the STL libraries being packed, which apparently GCC doesn't like. Removing the pragma and switching to __attribute__((packed)) for the affected structs solved the issue. I ended up finding this by just removing stuff until my program didn't crash, then doing BFS search on the header files included at each level once I knew it was a header issue.
STL won't like it because the static/shared libraries will be compiled without packed structs. So your code will use structs which are packed, mismatching what the precompiled STL lib thinks size and offsets are. It's a shame there's no mechanism in GNU binutils/gcc to detect this happening - you just have to be careful that all headers are interpreted in the same way in all compile units.
0

I don't think GDB can do this; maybe a profile will help, though? Are you compiling with gcc? Look at the -p and -pf commands, I think those might be useful.

Comments

0

The disassemble command at the gdb prompt will disassemble the current function you are stopped in, but I don't think outputting the entire execution path is feasible.

What library are you including? If it is open source, you can recompile it with debugging symbols enabled. Also, if you're using Linux, most distributions have -dbg versions of packages for common libraries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.