参考链接https://zhuanlan.zhihu.com/p/1961833750619988266https://blog.csdn.net/zhixingheyi_tian/article/details/96836387 我的gdb 调试在使用 GDB 运行程序时如果您需要传递命令行参数给您的程序可以使用 -args 选项。这样做可以让 GDB 将后续的参数作为您的程序的命令行参数传递并且在启动 GDB 后立即运行您的程序。例如如果您希望在调试时将 your_program 运行时的参数设为 arg1 arg2您可以使用如下命令gdb -args your_program arg1 arg2编译 -g 选项g”标志是对程序进行调试性编译时常用的选项。我们需要给每一个需要调试的源文件都加上这个选项。它将使用特殊版本的C语言标准库完成编译和链接操作给库函数加上程序调试方面的支持。编译器会把这些标志自动传给链接器。加上-g选项以后gcc在编译是会做以下额外的操作创建符号表符号表包含了程序中使用的变量名称的列表。sharedlibrary demo 测试demo_function.cpp$ cat demo_function.cpp #include iostream void demo_function() { int x 10; int y 20; int result x y; std::cout The result is: result std::endl; std::cout Inside demo function std::endl; }demo.cpp$ cat demo.cpp #include iostream void demo_function(); int main() { demo_function(); return 0; }编译g -shared -fPIC -o libdemo_function.so demo_function.cpp g -o demo demo.cpp -L. -ldemo_function倘若不带 “-g” 编译 demo.cpp$ gdb ./demo ... Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...(no debugging symbols found)...done. (gdb) break 5 No symbol table is loaded. Use the file command. (gdb) r带 “-g” 编译 demo.cpp$ gdb ./demo ... Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb)倘若不带 “-g” 编译 demo_function.cppReading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) s The program is not being run. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s The result is: 30 Inside demo function 6 return 0;带 “-g” 编译 demo_function.cppReading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s demo_function () at demo_function.cpp:5 5 int x 10; (gdb) display(x) 1: (x) 32767 (gdb) n 6 int y 20; 1: (x) 10 (gdb) display(x) 2: (x) 10 (gdb)倘若$ mv demo_function.cpp demo_function.cpp.bak则Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s demo_function () at demo_function.cpp:5 5 demo_function.cpp: No such file or directory. (gdb)指定 source file 目录$ gdb ./demo -d /home/hadoop/Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s demo_function () at demo_function.cpp:5 warning: Source file is more recent than executable. 5 int x 10; (gdb) n 6 int y 20; (gdb) display(x) 1: (x) 10 (gdb)keyword override基类中定义的虚函数使用override 关键字。派生类中必须重新实现arrow/cpp/src/arrow/dataset/file_orc.h:64:35: error: ‘arrow::Futurenonstd::optional_lite::optionallong int arrow::dataset::OrcFileFormat::CountRows(const std::shared_ptrarrow::dataset::FileFragment, arrow::dataset::Expression, const std::shared_ptrarrow::dataset::ScanOptions)’ marked ‘override’, but does not override宏这是表示同一行有两个 “\”/home/shen/benchmark/Gazelle_plugin_benchmark/native-sql-engine/cpp/src/operators/columnar_to_row_converter.cc:468:93: error: stray ‘\’ in program auto numeric_array std::static_pointer_castarrow::ARRAYTYPE(array); \gdb 调试gdb -args ./release/parquet-arrow-parquet-scan-string-benchmark --iterations 1 --threads 1 --file /mnt/DP_disk1/part-00000-35695359-20b7-407d-a582-e6f13547768f-c000.gz.parquet --cpu 0 b /home/shen/parquetScan/tmp/arrow_parquet_reading_optimized/cpp/src/arrow/array/validate.cc:208GDB 调试(gdb) break (b)在源代码指定的某一行设置断点其中xxx用于指定具体打断点位置(gdb) run (r执行被调试的程序其会自动在第一个断点处暂停执行。(gdb) continue (c当程序在某一断点处停止后用该指令可以继续执行直至遇到断点或者程序结束。(gdb) next (n)令程序一行代码一行代码的执行。(gdb) steps如果有调用函数进入调用的函数内部否则和 next 命令的功能一样。(gdb) until (u)(gdb) until (u) location当你厌倦了在一个循环体内单步跟踪时单纯使用 until 命令可以运行程序直到退出循环体。until n 命令中n 为某一行代码的行号该命令会使程序运行至第 n 行代码处停止。(gdb) print (p打印指定变量的值其中 xxx 指的就是某一变量名。(gdb) list (l)显示源程序代码的内容包括各行代码所在的行号。(gdb) finishfi结束当前正在执行的函数并在跳出函数后暂停程序的执行。(gdb) returnreturn结束当前调用函数并返回指定值到上一层函数调用处停止程序执行。(gdb) jumpj)使程序从当前要执行的代码处直接跳转到指定位置处继续执行后续的代码。(gdb) quit (q)终止调试。GDB print(gdb) print value.data() $106 0x7fffffff9ef4 Accounting(gdb) print *((int*)values[0]) $11 9998GDB demodemo.cpp#include iostream int main() { int x 10; int y 20; int result x y; std::cout The result is: result std::endl; return 0; }编译该程序并确保包含调试信息g -g demo.cpp -o demogdb ./demo(gdb) break 6(gdb) run(gdb) print x使用 next 命令来逐行执行程序。(gdb) next使用 backtrace 命令来查看函数调用堆栈。(gdb) backtrace完成调试时可以使用 quit 命令退出 GDB 调试界面。动态共享库demodemofunction.cpp#include iostream void demo_function() { std::cout Inside demo function std::endl; }g -shared -fPIC -o libdemofunc.so demofunction.cpp g -shared -fPIC -o libdemofuncg.so demofunction.cpp -g-rwxrwxr-x 1 hadoop hadoop 19192 May 29 19:15 libdemofuncg.so -rwxrwxr-x 1 hadoop hadoop 8648 May 29 19:15 libdemofunc.so使用该命令objdump -h libdemofuncg.solibdemofuncg.so 会被 libdemofunc.so 多出以下 debug信息24 .debug_aranges 00000030 0000000000000000 0000000000000000 00001075 2**0 CONTENTS, READONLY, DEBUGGING 25 .debug_info 000014d7 0000000000000000 0000000000000000 000010a5 2**0 CONTENTS, READONLY, DEBUGGING 26 .debug_abbrev 00000371 0000000000000000 0000000000000000 0000257c 2**0 CONTENTS, READONLY, DEBUGGING 27 .debug_line 0000024b 0000000000000000 0000000000000000 000028ed 2**0 CONTENTS, READONLY, DEBUGGING 28 .debug_str 00000c77 0000000000000000 0000000000000000 00002b38 2**0 CONTENTS, READONLY, DEBUGGING编译参数-Wl,-rpath 选项用于在编译链接时指定程序运行时动态链接器搜索共享库文件的路径。具体来说-Wl 选项用于传递参数给链接器在这里是 ld而 -rpath 选项用于指定程序运行时搜索共享库文件的路径。这个路径会被写入可执行文件的运行时搜索路径列表中这样在程序运行时动态链接器就会在指定的路径中搜索共享库文件。-Wl,-rpath 选项可以跟随多个目录。你可以在编译链接时使用多个 -Wl,-rpath 选项来指定程序运行时动态链接器搜索共享库文件的路径。g your_program.cpp -o your_program -Wl,-rpath,/path/to/library1 -Wl,-rpath,/path/to/library2判断有没有 “-g” 编译objdump -h使用该命令.首推该命令进行检测objdump -h libdemofuncg.solibdemofuncg.so 会被 libdemofunc.so 多出以下 debug信息24 .debug_aranges 00000030 0000000000000000 0000000000000000 00001075 2**0 CONTENTS, READONLY, DEBUGGING 25 .debug_info 000014d7 0000000000000000 0000000000000000 000010a5 2**0 CONTENTS, READONLY, DEBUGGING 26 .debug_abbrev 00000371 0000000000000000 0000000000000000 0000257c 2**0 CONTENTS, READONLY, DEBUGGING 27 .debug_line 0000024b 0000000000000000 0000000000000000 000028ed 2**0 CONTENTS, READONLY, DEBUGGING 28 .debug_str 00000c77 0000000000000000 0000000000000000 00002b38 2**0 CONTENTS, READONLY, DEBUGGINGobjdump --debugging 命令# objdump --debugging /root/test/libmyjni_nog.so | grep debug # objdump --debugging /root/development/projects/JNITEST/libmyjni.so | grep debug Contents of the .debug_aranges section: Offset into .debug_info: 0x0 Contents of the .debug_info section: 7d08 DW_AT_name : (indirect string, offset: 0x1ba6): __debug b5c4 DW_AT_name : (indirect string, offset: 0x960f): __gnu_debug Contents of the .debug_abbrev section: Raw dump of debug contents of section .debug_line: 11 /usr/include/c/8/debug 36 11 0 0 debug.h Contents of the .debug_str section: 0x00001ba0 456d6945 6c005f5f 64656275 67005f5a EmiEl.__debug._Z 0x00009610 5f676e75 5f646562 7567005f 5a4e374a _gnu_debug._ZN7J Contents of the .debug_ranges section:nm 命令# nm --h Usage: nm [option(s)] [file(s)] List symbols in [file(s)] (a.out by default). The options are: -a, --debug-syms Display debugger-only symbols -A, --print-file-name Print name of the input file before every symbol -B Same as --formatbsd -C, --demangle[STYLE] Decode low-level symbol names into user-level names The STYLE, if specified, can be auto (the default), gnu, lucid, arm, hp, edg, gnu-v3, java or gnat# nm -C /root/development/projects/JNITEST/libmyjni.so | grep nativeMethod 0000000000003299 T Java_MyJNITest_nativeMethod# nm -a /root/test/libmyjni_nog.so libmyjni_nog_nm # nm -a /root/test/libmyjni_nog.so libmyjni_nog_nm# diff libmyjni_nog_nm libmyjni_nm 14a15,20 0000000000000000 N .debug_abbrev 0000000000000000 N .debug_aranges 0000000000000000 N .debug_info 0000000000000000 N .debug_line 0000000000000000 N .debug_ranges 0000000000000000 N .debug_strattach/detach假设你知道某个进程的进程 ID 为 1234你可以这样附加到它(gdb) attach 1234Pstack参考链接https://www.cnblogs.com/kongzhongqijing/articles/7685699.htmlinfo threads参考链接https://blog.csdn.net/m0_66491750/article/details/137162955info threadst 49bt
C++ 编译, 调试,及问题汇总 (GDB)
参考链接https://zhuanlan.zhihu.com/p/1961833750619988266https://blog.csdn.net/zhixingheyi_tian/article/details/96836387 我的gdb 调试在使用 GDB 运行程序时如果您需要传递命令行参数给您的程序可以使用 -args 选项。这样做可以让 GDB 将后续的参数作为您的程序的命令行参数传递并且在启动 GDB 后立即运行您的程序。例如如果您希望在调试时将 your_program 运行时的参数设为 arg1 arg2您可以使用如下命令gdb -args your_program arg1 arg2编译 -g 选项g”标志是对程序进行调试性编译时常用的选项。我们需要给每一个需要调试的源文件都加上这个选项。它将使用特殊版本的C语言标准库完成编译和链接操作给库函数加上程序调试方面的支持。编译器会把这些标志自动传给链接器。加上-g选项以后gcc在编译是会做以下额外的操作创建符号表符号表包含了程序中使用的变量名称的列表。sharedlibrary demo 测试demo_function.cpp$ cat demo_function.cpp #include iostream void demo_function() { int x 10; int y 20; int result x y; std::cout The result is: result std::endl; std::cout Inside demo function std::endl; }demo.cpp$ cat demo.cpp #include iostream void demo_function(); int main() { demo_function(); return 0; }编译g -shared -fPIC -o libdemo_function.so demo_function.cpp g -o demo demo.cpp -L. -ldemo_function倘若不带 “-g” 编译 demo.cpp$ gdb ./demo ... Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...(no debugging symbols found)...done. (gdb) break 5 No symbol table is loaded. Use the file command. (gdb) r带 “-g” 编译 demo.cpp$ gdb ./demo ... Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb)倘若不带 “-g” 编译 demo_function.cppReading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) s The program is not being run. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s The result is: 30 Inside demo function 6 return 0;带 “-g” 编译 demo_function.cppReading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s demo_function () at demo_function.cpp:5 5 int x 10; (gdb) display(x) 1: (x) 32767 (gdb) n 6 int y 20; 1: (x) 10 (gdb) display(x) 2: (x) 10 (gdb)倘若$ mv demo_function.cpp demo_function.cpp.bak则Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s demo_function () at demo_function.cpp:5 5 demo_function.cpp: No such file or directory. (gdb)指定 source file 目录$ gdb ./demo -d /home/hadoop/Reading symbols from /home/hadoop/testlibhdfs/sharedlibrary/demo...done. (gdb) break 5 Breakpoint 1 at 0x400771: file demo.cpp, line 5. (gdb) r Starting program: /home/hadoop/testlibhdfs/sharedlibrary/./demo Breakpoint 1, main () at demo.cpp:5 5 demo_function(); (gdb) s demo_function () at demo_function.cpp:5 warning: Source file is more recent than executable. 5 int x 10; (gdb) n 6 int y 20; (gdb) display(x) 1: (x) 10 (gdb)keyword override基类中定义的虚函数使用override 关键字。派生类中必须重新实现arrow/cpp/src/arrow/dataset/file_orc.h:64:35: error: ‘arrow::Futurenonstd::optional_lite::optionallong int arrow::dataset::OrcFileFormat::CountRows(const std::shared_ptrarrow::dataset::FileFragment, arrow::dataset::Expression, const std::shared_ptrarrow::dataset::ScanOptions)’ marked ‘override’, but does not override宏这是表示同一行有两个 “\”/home/shen/benchmark/Gazelle_plugin_benchmark/native-sql-engine/cpp/src/operators/columnar_to_row_converter.cc:468:93: error: stray ‘\’ in program auto numeric_array std::static_pointer_castarrow::ARRAYTYPE(array); \gdb 调试gdb -args ./release/parquet-arrow-parquet-scan-string-benchmark --iterations 1 --threads 1 --file /mnt/DP_disk1/part-00000-35695359-20b7-407d-a582-e6f13547768f-c000.gz.parquet --cpu 0 b /home/shen/parquetScan/tmp/arrow_parquet_reading_optimized/cpp/src/arrow/array/validate.cc:208GDB 调试(gdb) break (b)在源代码指定的某一行设置断点其中xxx用于指定具体打断点位置(gdb) run (r执行被调试的程序其会自动在第一个断点处暂停执行。(gdb) continue (c当程序在某一断点处停止后用该指令可以继续执行直至遇到断点或者程序结束。(gdb) next (n)令程序一行代码一行代码的执行。(gdb) steps如果有调用函数进入调用的函数内部否则和 next 命令的功能一样。(gdb) until (u)(gdb) until (u) location当你厌倦了在一个循环体内单步跟踪时单纯使用 until 命令可以运行程序直到退出循环体。until n 命令中n 为某一行代码的行号该命令会使程序运行至第 n 行代码处停止。(gdb) print (p打印指定变量的值其中 xxx 指的就是某一变量名。(gdb) list (l)显示源程序代码的内容包括各行代码所在的行号。(gdb) finishfi结束当前正在执行的函数并在跳出函数后暂停程序的执行。(gdb) returnreturn结束当前调用函数并返回指定值到上一层函数调用处停止程序执行。(gdb) jumpj)使程序从当前要执行的代码处直接跳转到指定位置处继续执行后续的代码。(gdb) quit (q)终止调试。GDB print(gdb) print value.data() $106 0x7fffffff9ef4 Accounting(gdb) print *((int*)values[0]) $11 9998GDB demodemo.cpp#include iostream int main() { int x 10; int y 20; int result x y; std::cout The result is: result std::endl; return 0; }编译该程序并确保包含调试信息g -g demo.cpp -o demogdb ./demo(gdb) break 6(gdb) run(gdb) print x使用 next 命令来逐行执行程序。(gdb) next使用 backtrace 命令来查看函数调用堆栈。(gdb) backtrace完成调试时可以使用 quit 命令退出 GDB 调试界面。动态共享库demodemofunction.cpp#include iostream void demo_function() { std::cout Inside demo function std::endl; }g -shared -fPIC -o libdemofunc.so demofunction.cpp g -shared -fPIC -o libdemofuncg.so demofunction.cpp -g-rwxrwxr-x 1 hadoop hadoop 19192 May 29 19:15 libdemofuncg.so -rwxrwxr-x 1 hadoop hadoop 8648 May 29 19:15 libdemofunc.so使用该命令objdump -h libdemofuncg.solibdemofuncg.so 会被 libdemofunc.so 多出以下 debug信息24 .debug_aranges 00000030 0000000000000000 0000000000000000 00001075 2**0 CONTENTS, READONLY, DEBUGGING 25 .debug_info 000014d7 0000000000000000 0000000000000000 000010a5 2**0 CONTENTS, READONLY, DEBUGGING 26 .debug_abbrev 00000371 0000000000000000 0000000000000000 0000257c 2**0 CONTENTS, READONLY, DEBUGGING 27 .debug_line 0000024b 0000000000000000 0000000000000000 000028ed 2**0 CONTENTS, READONLY, DEBUGGING 28 .debug_str 00000c77 0000000000000000 0000000000000000 00002b38 2**0 CONTENTS, READONLY, DEBUGGING编译参数-Wl,-rpath 选项用于在编译链接时指定程序运行时动态链接器搜索共享库文件的路径。具体来说-Wl 选项用于传递参数给链接器在这里是 ld而 -rpath 选项用于指定程序运行时搜索共享库文件的路径。这个路径会被写入可执行文件的运行时搜索路径列表中这样在程序运行时动态链接器就会在指定的路径中搜索共享库文件。-Wl,-rpath 选项可以跟随多个目录。你可以在编译链接时使用多个 -Wl,-rpath 选项来指定程序运行时动态链接器搜索共享库文件的路径。g your_program.cpp -o your_program -Wl,-rpath,/path/to/library1 -Wl,-rpath,/path/to/library2判断有没有 “-g” 编译objdump -h使用该命令.首推该命令进行检测objdump -h libdemofuncg.solibdemofuncg.so 会被 libdemofunc.so 多出以下 debug信息24 .debug_aranges 00000030 0000000000000000 0000000000000000 00001075 2**0 CONTENTS, READONLY, DEBUGGING 25 .debug_info 000014d7 0000000000000000 0000000000000000 000010a5 2**0 CONTENTS, READONLY, DEBUGGING 26 .debug_abbrev 00000371 0000000000000000 0000000000000000 0000257c 2**0 CONTENTS, READONLY, DEBUGGING 27 .debug_line 0000024b 0000000000000000 0000000000000000 000028ed 2**0 CONTENTS, READONLY, DEBUGGING 28 .debug_str 00000c77 0000000000000000 0000000000000000 00002b38 2**0 CONTENTS, READONLY, DEBUGGINGobjdump --debugging 命令# objdump --debugging /root/test/libmyjni_nog.so | grep debug # objdump --debugging /root/development/projects/JNITEST/libmyjni.so | grep debug Contents of the .debug_aranges section: Offset into .debug_info: 0x0 Contents of the .debug_info section: 7d08 DW_AT_name : (indirect string, offset: 0x1ba6): __debug b5c4 DW_AT_name : (indirect string, offset: 0x960f): __gnu_debug Contents of the .debug_abbrev section: Raw dump of debug contents of section .debug_line: 11 /usr/include/c/8/debug 36 11 0 0 debug.h Contents of the .debug_str section: 0x00001ba0 456d6945 6c005f5f 64656275 67005f5a EmiEl.__debug._Z 0x00009610 5f676e75 5f646562 7567005f 5a4e374a _gnu_debug._ZN7J Contents of the .debug_ranges section:nm 命令# nm --h Usage: nm [option(s)] [file(s)] List symbols in [file(s)] (a.out by default). The options are: -a, --debug-syms Display debugger-only symbols -A, --print-file-name Print name of the input file before every symbol -B Same as --formatbsd -C, --demangle[STYLE] Decode low-level symbol names into user-level names The STYLE, if specified, can be auto (the default), gnu, lucid, arm, hp, edg, gnu-v3, java or gnat# nm -C /root/development/projects/JNITEST/libmyjni.so | grep nativeMethod 0000000000003299 T Java_MyJNITest_nativeMethod# nm -a /root/test/libmyjni_nog.so libmyjni_nog_nm # nm -a /root/test/libmyjni_nog.so libmyjni_nog_nm# diff libmyjni_nog_nm libmyjni_nm 14a15,20 0000000000000000 N .debug_abbrev 0000000000000000 N .debug_aranges 0000000000000000 N .debug_info 0000000000000000 N .debug_line 0000000000000000 N .debug_ranges 0000000000000000 N .debug_strattach/detach假设你知道某个进程的进程 ID 为 1234你可以这样附加到它(gdb) attach 1234Pstack参考链接https://www.cnblogs.com/kongzhongqijing/articles/7685699.htmlinfo threads参考链接https://blog.csdn.net/m0_66491750/article/details/137162955info threadst 49bt