twfx
twfx

Reputation: 1694

trace variable change using valgrind and gdb

I have a program which SIGABRT after >5hrs of execution. It is most likely cause by memory leak after checking by valgrind, but I have problem trace down onto which variable actually causes this issue based on valgrind report (which simply contains addresses and ???).

I try to use valgrind and gdb to step through. However since it takes 5hrs to reach the leak (after looping for 428 rounds), I would like to set a breakpoint, let say, when loop=428, and step into the codes. How can I do that?

Based on a simple program below, may I know,

a) how to trace change of value in variable 'a'?

b) how to set a breakpoint when loop = 428?


typedef struct data_attr {
   int a[2500];
}stdata;

typedef struct pcfg{
    stdata *data;
}stConfig;

int funcA(stConfig* pt){  

    int loop = 0;

    while (loop < NUM_NODE){  
        pt->data->a[0] = 1000;
        pt->data->a[0] = 1001;
        loop++;
    }
    return 0;
}

int main(){
    stConfig *p;

    p = (stConfig*) malloc(sizeof(stConfig));
    p->data = (stdata*) malloc (sizeof(stdata));

    funcA(p);

    free(p->data);
    free (p);

    return 0;
}

I am using valgrind 3.7 on ubuntu 10.04

@ valgrind terminal,

valgrind -v --vgdb=yes --vgdb-error=0 --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=40 --track-origins=yes --log-file=mr3m1n2500_valgrind_0717_1155.txt ./pt m >& mr3m1n2500_logcheck_0717_1155.txt

@ gdb terminal I tried to get address of 'p' but it returns void, why?

> gdb ./pt
(gdb) target remote | vgdb
Remote debugging using | vgdb
relaying data between gdb and process 12857
Reading symbols from /lib/ld-linux.so.2...Reading symbols from /usr/lib/debug/lib/ld-2.11.1.so...done.
done.
Loaded symbols for /lib/ld-linux.so.2
[Switching to Thread 12857]
0x04000850 in _start () from /lib/ld-linux.so.2
(gdb) p $p
$1 = void
(gdb) bt 10
#0  0x04000850 in _start () from /lib/ld-linux.so.2

Upvotes: 1

Views: 5917

Answers (4)

user1129237
user1129237

Reputation:

  1. To trace the change in the value of a variable, you can set watch-point on that variable.

    For your case, use: watch p->data->a[index]

  2. To break at the required condition, you can use break break if loop_counter==428

Upvotes: 2

Derui Si
Derui Si

Reputation: 1105

For your first question, how to trace change of value in variable 'a'? Please use "watch",

watch [-l|-location] expr [thread threadnum] [mask maskvalue]

Set a watchpoint for an expression. gdb will break when the expression expr is written into by the program and its value changes. The simplest (and the most popular) use of this command is to watch the value of a single variable:

      (gdb) watch foo

Joachim Pileborg have the answer of your second question.

For your third question, you need to set a break at the line

 p->data = (stdata*) malloc (sizeof(stdata));

and then try to print the value of "p".

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

From help break in GDB:

(gdb) help break
Set breakpoint at specified line or function.
break [LOCATION] [thread THREADNUM] [if CONDITION]
LOCATION may be a line number, function name, or "*" and an address.
If a line number is specified, break at start of code for that line.
If a function is specified, break at start of code for that function.
If an address is specified, break at that exact address.
With no LOCATION, uses current execution address of selected stack frame.
This is useful for breaking on return to a stack frame.

THREADNUM is the number from "info threads".
CONDITION is a boolean expression.

Multiple breakpoints at one place are permitted, and useful if conditional.

Do "help breakpoints" for info on other commands dealing with breakpoints.

To set a breakpoint on a condition, use break if condition, in your case break if loop_counter == 428 or similar.

Upvotes: 1

petermlm
petermlm

Reputation: 940

a) To set a break point of that loop if can do something like:

if(loop == 428)
    int nop = 0;

And then set the break point for the line int nop = 0. Like this the program only stops when that line is executed which happens in loop 428.

b) I am not sure about this one. Where are you trying to examine the value of 'p'?.

Upvotes: 0

Related Questions