是否可以使用lldb调试gcc编译的程序,或使用gdb调试一个clang编译的程序? [英] Is it possible to debug a gcc-compiled program using lldb, or debug a clang-compiled program using gdb?

查看:1497
本文介绍了是否可以使用lldb调试gcc编译的程序,或使用gdb调试一个clang编译的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(前言:我很熟悉C / C ++,我真的不知道如何在本机代码中调试。)



gdb和lldb可以调试编译为机器代码的任何程序。其他人说,使用gdb调试您必须在gcc 中使用 -g 标志。 gcc本身的文档表明这是可选的,事实上如果你使用它,它可能会导致除了 gdb之外的调试器的问题。 Clang还有一个 -g 标志,文档基本上只是说生成调试信息。



解决方案

在理论上,你可以使用自己的工具链(GNU和LLVM)应该能够使用lldb和一个使用gdb的LLVM内置程序调试GCC构建的程序。在这两种情况下,都应该使用 -g 编译。



这是因为两个编译器都生成相同格式的目标文件(例如,在Linux上,两者都将生成 ELF 文件,其中包含 DWARF 调试信息),并且两个调试器都知道如何解析该格式。



实际上,两个编译器都将一些数据推入调试信息,只有他们各自的调试器知道如何消费。但是:


  1. LLVM生成的数据不应以任何方式阻碍gdb。

  2. GCC生成数据不应阻止lldb,但如果是,您可以专门请求gcc到 >不添加非标准数据。例如,在Linux上,使用 -gdwarf-2 over -g 只能生成符合标准的DWARF。注意,你也可以调试没有调试信息的程序(不编译 -g ),但是您将被限制为调试器中的低级信息 - 汇编代码,内存和寄存器 - 并且将无法查看高级结构,如行号,函数名称,变量名称及其内容之间的映射,等等。


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

    解决方案

    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.

    这篇关于是否可以使用lldb调试gcc编译的程序,或使用gdb调试一个clang编译的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆