This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

bad function parameter values for C++ code in backtrace


Hi,

I find problem with gdb backtrace for  printing incorrect function
argument values in backtrace.  Please let me know if it is some
limitation of gdb or a bug?

The parameter values printed by the "bt" command in gdb is incorrect.
I've tried on  x86 as well on ARM, the result is same (and incorrect
parameter values).  For example, the down-most frame's function
parameter values is shown in the upper frame's function also.

 The test program to generate the condition is pasted below.

Program is (test2.cpp):
-----------------------------------------------------------------
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>

int g_len;
int g_value;
void foo();

void do_pqr(int len)
{
    int v_do_pqr = 1;
    printf("%s: len= %d\n", __FUNCTION__, len);

    // Note: Put backtrace before this line if wish to see backtrace
without crashing.
    *((volatile int*)0) = 1234;
    v_do_pqr++;
    g_value = v_do_pqr + len + (unsigned)&len;
}

void pqr(int len)
{
    int v_pqr = 2;
    int a = 11;
    int b = 12;
    ++a;
    ++b;
    printf("%s: len= %d\n", __FUNCTION__, len);
    len += 5;
    g_len = len;
    foo();//do_pqr(len);
    v_pqr++;
    g_value = v_pqr + a + b + (unsigned)&len;
}

void foo()
{
   int v_foo = 3;
   int c = 13;
   ++c;
   do_pqr(g_len);
   v_foo++;
   g_value = v_foo - c;
}

void xyz(int len)
{
    int v_xyz = 4;
    int d = 14;
    ++d;
    printf("%s: len= %d\n", __FUNCTION__, len);
    len += 3;
    pqr(len);
    v_xyz++;
    g_value = v_xyz + d + ++len + (unsigned)&len;
}


char* abc(int len)
{
    int v_abc = 5;
    printf("%s: len= %d\n", __FUNCTION__, len);
    len = len  + 2;
    xyz(len);
    char* ptr = 0;
    v_abc++;
    g_value += v_abc + len;
    return ptr + len + (unsigned)&len;
}

char* check(int len)
{
    int v_check = 6;
    printf("%s: len= %d\n", __FUNCTION__, len);
    static int i = 0;
    ++len;
    abc(++i);
    len += 10;
    i += 2;
    v_check++;
    g_value = v_check + i + len;
    if(g_value >  1000)
    {
        len = v_check - i * 2;
    }
    i = len + g_value;
    g_value = i + v_check + (unsigned)&len;

    return 0;
}

int main(void)
{
    int v_main = 7;
    int len = 200;
    len = (int)check(len);
    v_main++;
    g_value += len;
}

-----------------------------------------------------------------

Compilation step:

             g++ -o test2 test2.cpp -W -Wall -g

The backtrace dump with gdb is:

[user@localhost]$ gdb test2
GNU gdb Red Hat Linux (6.5-25.el5rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db lib
rary "/lib/i686/nosegneg/libthread_db.so.1".
(gdb) br do_pqr
Breakpoint 1 at 0x804846a: file test2.cpp, line 12.
(gdb) r
Starting program: /home/user/test2
check: len= 200
abc: len= 1
xyz: len= 3
pqr: len= 6
Breakpoint 1, do_pqr (len=11) at test2.cpp:12
12          int v_do_pqr = 1;
(gdb) where
#0  do_pqr (len=11) at test2.cpp:12
#1  0x080484d5 in foo () at test2.cpp:40
#2  0x0804853c in pqr (len=11) at test2.cpp:30
#3  0x0804859f in xyz (len=6) at test2.cpp:52
#4  0x08048606 in abc (len=3) at test2.cpp:63
#5  0x08048681 in check (len=201) at test2.cpp:76
#6  0x08048722 in main () at test2.cpp:95
(gdb) quit
The program is running.  Exit anyway? (y or n) y

Thanks
Lalit


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]