Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

(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?

like image 253
Neil Traft Avatar asked Jan 15 '14 08:01

Neil Traft


People also ask

Is LLDB compatible with GDB?

The standard LLDB installation provides you with an extensive set of commands designed to be compatible with familiar GDB commands. In addition to using the standard configuration, you can easily customize LLDB to suit your needs. Both GDB and LLDB are of course excellent debuggers without doubt.

Is LLDB better than GDB?

The main difference between LLDB and GDB is that in LLDB, the programmer can debug programs written in C, Objective C and C++ while, in GDB, the programmer can debug programs written in Ada, C, C++, Objective C, Pascal, FORTRAN and Go.

Can I use GDB with clang?

You can use GDB or lldb with clang.

Does gcc have a debugger?

gcc is a debugger by GNU project. Gdb can step through your source code line-by-line or even instruction by instruction. You may also watch the value of any variable at run-time.


1 Answers

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.

like image 81
Oak Avatar answered Oct 05 '22 04:10

Oak