Group Group Group Group Group Group Group Group Group

LLDB - count the number of steps to execute a thread or to execute a function eg. main(), and skip to Xth step

(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 an answer 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.

However, what if the code has loops? Say:

for (i = 1; i < 11; ++i)

  {

    printf("%d ", i);

 d 

  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?

@lolgrep Can you please help with this when you get a chance? Thank you - much appreciated! :]

@junxiang99, if by “steps”, you mean source code lines, yes, that is possible. Note that a source line will take up a varied amount of assembly instructions. You could for instance have that whole code snippet be on one line.

For the above, I’d recommend you check out LLDB’s scripted thread stepping APIs–Either via Python’s SWIG classes discussed in the book, or the C++ API underneath.

For help on scripted stepping there’s a chapter in the book that discusses this–though I forget which one off the top of my head. Here’s a free alternative https://knight.sc/debugging/2019/05/24/lldb-step-scripted.html

ok noted with thanks