Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ios - lldb python basic - print value of a global array while inside a breakpoint in a function

(Some background: I am not experienced with lldb or python and don't work on them frequently, but currently need to make some basic scripts for debugging an iphone program)

I am currently stopped at a breakpoint in side a function, and want to check the value of an array that has been accessed inside this function

This array is declared as

Float32 my_array[128];  

and has global scope. I can view the array using print command, but I would like to make a python script so that I have more control over the output formatting and possibly plot the array elements as a graph using matplolib later on.

I am looking at the sample python code given in this question, and using the python given there I have verified that I can view local variables in this function (where currently I am stopped at a break point). For example, if I change 'base' in base=frame.FindVariable('base') to my local variable 'k' (the local variable is not an array) ,

base=frame.FindVariable('k')  

then print base I can see the value of k. However, if I try this,

base=frame.FindVariable('my_array')   

and do print base it gives me No value. How can I write a python command to get the values of any kind of variable currently in scope? Preferably it works for normal variables (int, float), arrays, and pointers, but if not, finding values of arrays are more important at the moment.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SBFrame.FindVariable searches among the variables local to that frame. It doesn't search among the global variables.

For that you need to use a search with a wider scope. If you know that the global variable is in the binary image containing the your frame's code - lldb calls that binary image a Module - then you can find the module containing that frame and use SBModule.FindGlobalVariables. If that's not true, you can search the whole target using SBTarget.FindGlobalVariables. If you know that only one global variable of that name exists, you can use FindFirstGlobalVariable variant.

All these commands will find variables of any type, and they all consistently return SBValues so you can format them in a consistent manner regardless of how you find them. For statically allocated arrays, the array elements are its children, so you can fetch individual elements with SBValue.GetChildAtIndex.

You can get to a SBFrame's module like:

module = frame.module

and its target:

target = frame.thread.process.target

lldb separates the contexts in which to search for variables primarily for efficiency. If SBFrame.FindVariable searched for globals as well as locals, a mistyped variable name would be a much more expensive mistake. But it also makes the call more predictable since you will never get some random global from some shared library that the system loaded on your behalf.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...