0

Is there a way to print what the condition of an if statement evaluates to in LLDB ? Or the value value of any conditionnal expression at some point in of the execution of the program ?

I know how to print a variable with var or print, how to print an array's values with parray 10 arr, how to get the return value of a function by stepping-out of it and thread info but I don't know how to get the value of a conditional expression.

Any debugging tips much appreciated.

Edit : I just learned from a comment below that one can just use print with some conditionnal expression to see what it evaluates to. Still, is there somme command that allows to see that without typing the whole condition and have lldb evalluate it again from the state of the variables but to print what a specific condition has evaluated to at some specific point in the program ?

cassepipe
  • 371
  • 6
  • 16
  • A conditional expression `x ? y : z` is just an expression, try printing it and see what happens. A statement on the other hand doesn't evaluate to anything, it only has side effects. – n. m. could be an AI Dec 04 '20 at 12:32
  • @n.'pronouns'm. Thanks for the precision. You are right I am not looking for the value of the statement but of the condition of an if statement. Also thank you, I am glad to learn that I can print conditional expression. But still I am curious : Is there a way for not to write the whole `print (!strncmp(token_table[i].token, input , token_table[i].len)))` and get the value it evaluates to ? I guess there must be some kind of command for that. – cassepipe Dec 04 '20 at 13:33
  • What kind of command would satisfy you? – n. m. could be an AI Dec 04 '20 at 14:37
  • @n.'pronouns'm. Something like `evaluate line line_number` I don't know. I am not familiar with how debuggers work and I can feel the sarcastic tone. Are you suggesting such a command does not exist ? – cassepipe Dec 04 '20 at 14:52

1 Answers1

1

There isn't such a command.

It would be pretty hard to write one that would be fully general since individual source lines are very often not complete statements, and we'd have to do something sensible in that case, etc...

Also, I don't think you actually want "evaluate this line as an expression", you want something more complex. I imagine you have a source line like:

   if (!strncmp(token_table[i].token, input , token_table[i].len))) {

That's not an expression in C, so we couldn't evaluate the whole source line directly. You really want "find anything that looks like it's a conditional in this source line, pull it out and evaluate that." That looks tractable in simple cases like the one above, but in the general case this starts to get complicated, and it would be hard to get it right.

OTOH, it would be pretty straightforward to write a Python command that only handles simple instances of an if conditional, using the SB API's to pull out the source line, then some Python parsing to pull out the condition, then SBFrame.EvaluateExpression to evaluate it. So you could certainly use the Python API's to whip up something that's good enough for your purposes.

More details about the Python affordances in lldb are here:

https://lldb.llvm.org/use/python-reference.html

and the lldb API's are documented here:

https://lldb.llvm.org/python_reference/index.html

Jim Ingham
  • 25,260
  • 2
  • 55
  • 63