gdb python how to output integer for examine memory

W

Wesley

Hi all,
I am trying to use gdb debug python script.
I am using gdb7.7 and python2.7.6, here is my simple test script:
import time

def next(i):
time.sleep(10)
i = 1 - i

i = 1
while True:
next(i)
When this script running, gdb attach to it, and here is snippet:

(gdb) py-bt
#5 Frame 0x201e130, for file test.py, line 6, in next (i=1)
Python Exception <type 'exceptions.IOError'> (2, '\xe6\xb2\xa1\xe6\x9c\x89\xe9\x82\xa3\xe4\xb8\xaa\xe6\x96\x87\xe4\xbb\xb6\xe6\x88\x96\xe7\x9b\xae\xe5\xbd\x95', 'test.py'):
Error occurred in Python command: (2, '\xe6\xb2\xa1\xe6\x9c\x89\xe9\x82\xa3\xe4\xb8\xaa\xe6\x96\x87\xe4\xbb\xb6\xe6\x88\x96\xe7\x9b\xae\xe5\xbd\x95', 'test.py')
(gdb) frame 5
#5 0x00000000004d01a7 in PyEval_EvalFrameEx (f=Frame 0x201e130, for file test.py, line 6, in next (i=1), throwflag=0) at Python/ceval.c:2666
2666 x = call_function(&sp, oparg);
(gdb) py-locals
i = 1
(gdb) pyo i
No symbol "i" in current context.
(gdb)
No symbol "i" in current context.
(gdb) p f->f_localsplus
$1 = {1}
(gdb) p f->f_localsplus[0]
$2 = 1
(gdb) p &(f->f_localsplus[0])
$3 = (PyObject **) 0x201e2b8
(gdb) x/d 0x201e2b8
0x201e2b8: 31304528
(gdb) p sizeof(f->f_localsplus[0])
$4 = 8
(gdb) x/dg 0x201e2b8
0x201e2b8: 31304528
(gdb) x/dw 0x201e2b8
0x201e2b8: 31304528
(gdb)

So, the latter several commands, I wannted to check memory content, but , how to output integer 1?

Thanks.
Wesley
 
D

Dave Angel

Wesley said:
Hi all,
I am trying to use gdb debug python script.
I am using gdb7.7 and python2.7.6, here is my simple test script:
import time

def next(i):
time.sleep(10)
i = 1 - i

i = 1
while True:
next(i)
When this script running, gdb attach to it, and here is snippet:

I cannot help with gdb, but I can point out that you have two
separate variables here. Decrement ing the local has no effect on
the global value.

The preferred way is to return any values from the function that
you want to use after it exits.
def next(i):
time.sleep(10)
i = 1 - i
return i

i = 1
while True:
i =next(i)

Another possibility, generally a bad idea, is declaring i global
in the function.
 
W

Wesley

Hi Dave,
Thanks for your response.
It's just a simple script for test:)
My concern is use gdb to monitor variable in memory within python process.
For details, in my origin post, just wanna why cannot output interger valuefrom the address.

Maybe here is not right for gdb python question..but seems I cannot post question at another gdb group.

So, post here, since it's also related to python,in case anyone knowns this..
Sorry for that.

Wesley

在 2014å¹´3月24日星期一UTC+8下åˆ8æ—¶22分59秒,Dave Angel写é“:
 
D

dieter

Wesley said:
I am trying to use gdb debug python script.
I am using gdb7.7 and python2.7.6, here is my simple test script:
import time

def next(i):
time.sleep(10)
i = 1 - i

i = 1
while True:
next(i)
When this script running, gdb attach to it, and here is snippet:

...
(gdb) frame 5
#5 0x00000000004d01a7 in PyEval_EvalFrameEx (f=Frame 0x201e130, for file test.py, line 6, in next (i=1), throwflag=0) at Python/ceval.c:2666
2666 x = call_function(&sp, oparg);
(gdb) py-locals
i = 1
(gdb) pyo i
No symbol "i" in current context.

Quite a lot of time has passed since I last had to debug Python
processes at C level -- thus, my memory may be unreliable.

When I remember right, then "pyo" is used to interprete
a C level variable as a Python object (and print it) -- not
a Python level variable. In your case, "i" is a Python level variable.

You must carefully distinguish between the C level and the Python level.
Some commands expect C level names/objects;
others may expect Python level names/objects.

To learn how you can obtain the value of a Python variable,
I see two approaches: look through the list of provided commands
(and their documentation) and try to figure out which might be applicable
and then may some tests; or look at the implementation of "py-locals"
and use this knowledge to define you own command (for this,
you will also need to understand the gdb language to define commands).
 
W

Wesley

在 2014å¹´3月25日星期二UTC+8下åˆ3æ—¶49分09秒,dieter写é“:
Quite a lot of time has passed since I last had to debug Python

processes at C level -- thus, my memory may be unreliable.



When I remember right, then "pyo" is used to interprete

a C level variable as a Python object (and print it) -- not

a Python level variable. In your case, "i" is a Python level variable.



You must carefully distinguish between the C level and the Python level.

Some commands expect C level names/objects;

others may expect Python level names/objects.



To learn how you can obtain the value of a Python variable,

I see two approaches: look through the list of provided commands

(and their documentation) and try to figure out which might be applicable

and then may some tests; or look at the implementation of "py-locals"

and use this knowledge to define you own command (for this,

you will also need to understand the gdb language to define commands).

Hi Dieter,
Thanks.
Actually, I can now see the varialbe names at Python level and C level.
I just want to verify x command to monitor the memory content.
So, in my origin post, I can get variable i's address, and see the value is1,
then, I wanna have a try x command, the issue is, when use x/format i's address, the output is not 1, but other things:-(
 
D

dieter

Wesley said:
...
Actually, I can now see the varialbe names at Python level and C level.
I just want to verify x command to monitor the memory content.
So, in my origin post, I can get variable i's address, and see the value is 1,
then, I wanna have a try x command, the issue is, when use x/format i's address, the output is not 1, but other things:-(

All Python objects start (at C level) with a header (containing
the reference count, a pointer to the associated type and
maybe other things); the "real" value starts behind this header.

This means, to see the "1" in your example, you must skip the
associated object header -- either in the output or by adding
the size of the header to the initial address.
 
W

Wesley

在 2014å¹´3月26日星期三UTC+8下åˆ3æ—¶10分23秒,dieter写é“:
All Python objects start (at C level) with a header (containing

the reference count, a pointer to the associated type and

maybe other things); the "real" value starts behind this header.



This means, to see the "1" in your example, you must skip the

associated object header -- either in the output or by adding

the size of the header to the initial address.

Most like this.
I will try later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top