(1) Is there a way to count the number of steps it takes to execute a function or a thread with LLDB?
As an example,
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); // step count: 1
int number;
int number2;
int number3;
number = 50; // step count: 2 (lldb skips declarations)
number = 60; // step count: 3
/*
Comment comment
*/
if(number < 100) // step count: 4 (lldb skips comments)
printf("Number is less than 100!\n"); // step count: 5
else if(number == 100)
printf("Number is 100!\n");
else
printf("Number is greater than 100!\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n"); // step count: 6 (the lines that are not executed bec of branching are not counted.)
return 0; // step count: 7
}
The step count is provided in the comments in the code above. And I only count step-overs.
The step count is unique to each execution of the code, and is expected to change if command line arguments and environment variables change and affect the control flow of the program.
If there are loops, each iteration will mean counting the lines in the loop again. So if there are 2 steps per iteration and there are 10 iterations, then there are 20 steps for that loop.
(2) (Although I only count step-overs, I appreciate answers that also tell me how I can configure to include step-ins when I need them, or exclude them when I don't need them.)
(3)
In addition, is there a way to jump to a specific step in that step count? By this, I think of the jump
command and How to skip a couple of lines code with lldb?.
However, what if the code has loops? Say:
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
There are 6 static lines of code (counting all those with only the curly brackets as well). However, the number of steps is probably 21. I wish I could jump to the 10th step, for example. Is it possible with lldb?
Another way to count steps is to write a scripted stepping plan that just keeps pushing new "step in" or "step over" plans till you reach whatever your terminating condition is, and updates some python variable each time an individual step is concluded. For more info on writing scripted thread plans, see:
https://lldb.llvm.org/use/python-reference.html#using-the-python-api-to-create-custom-stepping-logic
and there are several examples of scripted stepping plans here:
https://github.com/llvm/llvm-project/blob/master/lldb/examples/python/scripted_step.py
I'm not quite sure what you mean by "jumping to a specific step". If you mean you just what to stop at that point without having to manually intervene, it would be fairly straightforward to make a scripted stepping plan with whatever end conditions you want (the 10th time you hit line 20, or whatever).
But if you mean "get to that stage in the execution w/o going through the intervening code" that's not a trivial problem. How would lldb know what state was modified along the way to that point? For instance to get to a certain iteration of a loop, lldb would have to know to set the loop counter to whatever value it has at that point, which it has no way of knowing.