首页 > 其他分享 >【gdb】打印函数局部变量的值

【gdb】打印函数局部变量的值

时间:2023-10-16 23:01:09浏览次数:40  
标签:full 打印函数 void 局部变量 bt gdb fun

打印函数局部变量的值

1.例子:

#include <stdio.h>

void fun_a(void)
{
	int a = 0;
	printf("%d\n", a);
}

void fun_b(void)
{
	int b = 1;
	fun_a();
	printf("%d\n", b);
}

void fun_c(void)
{
	int c = 2;
	fun_b();
	printf("%d\n", c);
}

void fun_d(void)
{
	int d = 3;
	fun_c();
	printf("%d\n", d);
}

int main(void)
{
	int var = -1;
	fun_d();
	return 0;
}

如果要打印函数局部变量的值,可以使用“bt full”命令(bt是backtrace的缩写)。首先我们在函数fun_a里打上断点,当程序断住时,显示调用栈信息:

(gdb) bt
#0  fun_a () at a.c:6
#1  0x000109b0 in fun_b () at a.c:12
#2  0x000109e4 in fun_c () at a.c:19
#3  0x00010a18 in fun_d () at a.c:26
#4  0x00010a4c in main () at a.c:33

接下来,用“bt full”命令显示各个函数的局部变量值:

(gdb) bt full
#0  fun_a () at a.c:6
        a = 0
#1  0x000109b0 in fun_b () at a.c:12
        b = 1
#2  0x000109e4 in fun_c () at a.c:19
        c = 2
#3  0x00010a18 in fun_d () at a.c:26
        d = 3
#4  0x00010a4c in main () at a.c:33
        var = -1

也可以使用如下“bt full n”,意思是从内向外显示n个栈桢,及其局部变量,例如:

(gdb) bt full 2
#0  fun_a () at a.c:6
        a = 0
#1  0x000109b0 in fun_b () at a.c:12
        b = 1
(More stack frames follow...)

而“bt full -n”,意思是从外向内显示n个栈桢,及其局部变量,例如:

(gdb) bt full -2
#3  0x00010a18 in fun_d () at a.c:26
        d = 3
#4  0x00010a4c in main () at a.c:33
        var = -1

如果只是想打印当前函数局部变量的值,可以使用如下命令:

(gdb) info locals
a = 0

 

参考资料

1. gdb手册

2. 打印函数局部变量的值

标签:full,打印函数,void,局部变量,bt,gdb,fun
From: https://www.cnblogs.com/sunbines/p/17768645.html

相关文章

  • 【gdb】在匿名空间设置断点
    在匿名空间设置断点1.例子namespaceFoo{voidfoo(){}}namespace{voidbar(){}}在gdb中,如果要对namespaceFoo中的foo函数设置断点,可以使用如下命令:(gdb)bFoo::foo如果要对匿名空间中的bar函数设置断点,可以使用如下命令:(gdb)b(anonymousnames......
  • 【gdb】为fork调用设置catchpoint
    为fork调用设置catchpoint1.例子:#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>intmain(void){pid_tpid;pid=fork();if(pid<0){exit(1);}elseif(pid>0){exit(......
  • 【gdb】为exec调用设置catchpoint
    为exec调用设置catchpoint1.例子:#include<unistd.h>intmain(void){execl("/bin/ls","ls",NULL);return0;}使用gdb调试程序时,可以用“catchexec”命令为exec系列系统调用设置catchpoint,以上面程序为例:[root@node01demo]#gccdemo.c-g[root@node01dem......
  • 【gdb】为vfork调用设置catchpoint
    为vfork调用设置catchpoint1.例子:#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>intmain(void){pid_tpid;pid=vfork();if(pid<0){exit(1);}elseif(pid>0){exi......
  • 【gdb】同时调试父进程和子进程
    同时调试父进程和子进程1.   参考资料1.gdb手册2.同时调试父进程和子进程......
  • 【gdb】设置读观察点
    设置读观察点1.例子#include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;void*thread1_func(void*p_arg){while(1){a++;sleep(10);}}void*thread2_func(void*p_arg){while(1){a++;sleep(10......
  • 【gdb】设置读写观察点
    设置读写观察点1.例子:#include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;void*thread1_func(void*p_arg){while(1){a++;sleep(10);}}void*thread2_func(void*p_arg){while(1){a++;sleep(1......
  • 【gdb】
      #include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;void*thread1_func(void*p_arg){while(1){a++;sleep(10);}}void*thread2_func(void*p_arg){while(1){a++;sleep(10);}}......
  • 【gdb】输出信息多时不会暂停输出
    输出信息多时不会暂停输出有时当gdb输出信息较多时,gdb会暂停输出,并会打印“---Type<return>tocontinue,orq<return>toquit---”这样的提示信息,如下面所示:81process26391020xff04af84in__lwp_park()from/usr/lib/libc.so.180process25735660xff04......
  • 【gdb】设置观察点
    设置观察点1.例子#include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;void*thread1_func(void*p_arg){ while(1) { a++; sleep(10); }}intmain(intargc,char*argv[]){ pthread_tt1; pthread_create(&t1,NUL......