将遇到的gdb调试的技巧慢慢总结下。
1. gdb 调试程序设置参数
set args 可以在gdb调试时指定运行时的参数.
show args 命令可以查看设置好的运行参数.
#include <stdio.h> int main(int argc, char *argv[]) { printf("argc = %d\n", argc); for (int i = 0;i < argc;++i) { printf("argv[%d]=%s\n", i, argv[i]); } return 0; }
编译:gcc -std=c99 -g main.c -o main
运行:./main
结果: argc = 1
argv[0]=./main
gdb调试: gdb main
(gdb) set args arg1 arg2 arg3
(gdb) show args
Argument list to give program being debugged when it is started is "arg1 arg2 arg3".
(gdb) r
Starting program: ./main arg1 arg2 arg3
argc = 4
argv[0]=./main
argv[1]=arg1
argv[2]=arg2
argv[3]=arg3