简体   繁体   中英

LLDB print a variable named class

I have a C programm in which a variable called class is used.

I'm trying to debug it with LLDB but I'm encountering the following problem:

(lldb) print class
error: warning: declaration does not declare anything
error: declaration of anonymous class must be a definition
error: 1 errors parsing expression

I believe this problem occurs because class is a reserved keyword in C++ and LLDB interprets the code passed to print as C++. Is there still a way to print the content of my variable?

(Please do not advise me to rename the variable, I would have come up with this myself, if it was possible)

The problem is that the lldb expression parser uses C++ references to implement the job of finding & extracting results from the expressions we run. So we currently have to compile the expressions as C++ expressions, and as you guessed, you can't use "class" in a C++ expression. At some point, we have to teach clang how to do "C with references" and then we'll be able to compile & execute real C expressions.

However, provided you have debug information for "class", you can print the value of the variable using the "frame variable" command, ie:

(lldb) frame variable class

The "frame variable" command does not use the expression parser, it goes directly to the debug information, extracts the type & location of the variable, and prints that directly. So it doesn't suffer this restriction. If "class" is a global variable, not a frame local, use target variable instead.

frame variable does support a limited set of "expression-like" features, you can say:

(lldb) frame variable class.member

or

(lldb) frame variable *class

but you can't use it to call functions or pass the variable to a function call.

If you need to do that you can run the command:

(lldb) frame variable -L class

which will print the location of the variable. Usually that's some address, in which case you can use

 (TypeOfClass *) <Address From Frame Variable>

in your expression in place of "class". If the location turns out to be a register, then use "$" appropriately cast in your expression. If you are going to use the variable in a number of expressions, remember you can do:

(lldb) expr TypeOfClass *$class = (TypeOfClass *) <Address From Frame Variable>

and then just use $class in your subsequent expressions. If you got super-motivated, you could even write a Python command that automates these steps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM