GDB: how to determine the line number of a variable declaration of a specific variable for the current function in gdb

How can I determine the line number of a variable declaration for a specific variable for the current function in gdb ??

Here is a sample code:

1 #include<stdio.h> 2 3 void func(int*); 4 5 void main() 6 { 7 int x; 8 char c[5]; 9 int* p; 10 char *g; 11 char *ptr; 12 13 p = &x; 14 g = &c[3]; 15 func(&x); 16 ptr = &c[1]; 17 c[1]='f'; 18 x=12; 19 20 } 21 22 void func(int *l){ 23 int x; 24 unsigned int ggg; 25 ggg =100; 26 27 x = 3; 28 *l = x; 29 } 30 

 Wanted output: for main() variable x, line 7 variable c, line 8 variable p, line 9 variable g, line 10 variable ptr, line 11 for func() variable x, line 23 variable ggg, line 24 

Assuming I'm in the main () function now. I can get local variables using " info locals ", then parse its variable names.

 (gdb) info locals x = 4200592 c = "\000ε\03 0@ " p = 0x7ffd4000 g = 0x40 <Address 0x40 out of bounds> ptr = 0x13cf304 <Address 0x13cf304 out of bounds> 

And using the list command, I managed to get the current scope of the variable, but not the whole body of the function. Below is the result of the list command, and the declaration variable ' x ' seems to be missing. I also tried setting the list size to 20 and got lines from 8 to 27, but still didn't help.

 (gdb) l 8 char c[5]; 9 int* p; 10 char *g; 11 char *ptr; 12 13 p = &x; 14 g = &c[3]; 15 func(&x); 16 ptr = &c[1]; 17 c[1]='f'; 

If I check all the lines of code in the source code, I can meet the same variable declarations from different functions ... help

+6
source share
1 answer

It is easy. You can use the list command to find the corresponding line.

 (gdb) set listsize 1 (gdb) list x 7 int x; (gdb) 

This will show that x is on line 7;

Or use the l shortcut command for the list command

 (gdb) lx 7 int x; 
+6
source

Source: https://habr.com/ru/post/915767/


All Articles