Is it possible to debug a gcc-compiled program using lldb, or debug a clang-compiled program using gdb?
Asked Answered
P

1

55

(Preface: I'm pretty new to C/C++ and I don't really know how debugging in native code actually works.)

Some sources say that gdb and lldb can debug any program compiled to machine code. Others say that to debug with gdb you must compile in gcc with the -g flag. The documentation for gcc itself suggests this is optional, and that in fact if you use it, it can cause problems for debuggers other than gdb. Clang also has a -g flag and the documentation basically just says "Generate debug information."

So are these debuggers restricted to their own toolchains (GNU and LLVM), or are they somehow independent of the compiler used?

Psycho answered 15/1, 2014 at 8:13 Comment(0)
F
55

In theory you should be able to debug a GCC-built program with lldb and an LLVM-built program with gdb. In both cases you should compile with -g.

This is because both compilers generate object files in the same format (e.g., on Linux, both will generate ELF files with DWARF debug info) and both debuggers know how to parse that format.

In practice, both compilers push some data into the debug info that only their respective debugger knows how to consume. However:

  1. LLVM-generated data should not hinder gdb in any way.
  2. GCC-generated data should not hinder lldb, but if it does you can specifically ask gcc to not add non-standard data. For example, on Linux, using -gdwarf-2 over -g should only generate standard-compliant DWARF.

Notice that you can also debug programs without debug info (not compiled with -g), but you'll be limited to low-level information in the debugger - assembly code, memory and registers - and will not be able to see high level constructs such as line numbers, function names, mapping between variable names and their content, etc.

Fascinating answered 15/1, 2014 at 9:18 Comment(3)
One small clarification about -gdwarf-2 - this tells gcc to emit debug info that conforms to an older version of the DWARF debug format specification. The current version is 4. Sometimes consumers (debuggers) won't understand all the newest features in the later DWARF standards so it's necessary to have the producer (compilers) not emit all the fanciest/newest constructs. But these problems are usually short-lived as the new features are supported in the consumers soon enough and everybody can live with plain old -g and get along.Millwright
Even with -g I'm not able to see high level information using lldb (lldb-310.2.37) on Mac OS X for programs compiled with g++ (MacPorts gcc47 4.7.3_3).Trumantrumann
Is this because the extra GCC baggage stops LLDB being able to interpret the debugging stuff?Stacte

© 2022 - 2024 — McMap. All rights reserved.