LLDB - 计算执行线程或执行函数的步骤数,例如。 main(), 跳到第X步

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

(1) 有没有办法计算使用 LLDB 执行函数或线程所需的步骤数?

例如,

#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
}

上述代码的注释中提供了步数。而且我只计算步数。

每次执行代码时,步数都是唯一的,如果命令行参数和环境变量发生变化并影响程序的控制流,预计会发生变化。

如果有循环,每次迭代都意味着重新计算循环中的行数。因此,如果每次迭代有 2 个步骤并且有 10 个迭代,那么该循环有 20 个步骤。

(2) (虽然我只计算跨步,但我很欣赏那些告诉我如何配置以在需要时包括步骤,或在不需要时排除它们的答案。)

(3) 另外,有没有办法跳转到该步数中的特定步骤?至此,我想到了 jump 命令和 .

但是,如果代码有循环怎么办?说:

for (i = 1; i < 11; ++i)
  {
    printf("%d ", i);
  }
  return 0;
}

有 6 行静态代码(包括所有只有大括号的代码)。但是,步数可能是 21。我希望我可以跳到第 10 步,例如。 lldb 可以吗?

在大多数情况下,您可以使用断点,而不是计算步骤并执行计算的步骤数。例如,在 for 循环示例中,可以将断点配置为仅在正确的条件下停止。

对于这些示例,我假设 printffile.c 的第 23 行。

要在循环中途停止,而不是执行多个 next 命令,可以使用断点条件仅在循环索引 i 是所需数量时停止:

b file.c:23
breakpoint modify --condition 'i == 5'

条件表达式由 lldb 计算,因此可以根据需要更复杂。

在这种情况下,由于条件是一个计数,另一种方法是在断点上设置一个忽略计数:

b file.c:23
breakpoint modify --ignore-count 5

注意:除了 --condition--ignore-count,您还可以使用 -c-i

另一种计算步数的方法是编写一个脚本化的步进计划,不断推动新的 "step in" 或 "step over" 计划,直到达到任何终止条件,并更新一些 python 每次结束一个单独的步骤时都会发生变化。有关编写脚本化线程计划的更多信息,请参阅:

https://lldb.llvm.org/use/python-reference.html#using-the-python-api-to-create-custom-stepping-logic

这里有几个脚本化步进计划的例子:

https://github.com/llvm/llvm-project/blob/master/lldb/examples/python/scripted_step.py

我不太清楚你所说的 "jumping to a specific step" 是什么意思。如果你的意思是你只是在那个点停止什么而不必手动干预,那么根据你想要的任何结束条件(第 10 次你到达第 20 行,或其他)来制定一个脚本化的步进计划将是相当简单的。

但是,如果您的意思是 "get to that stage in the execution w/o going through the intervening code",那可不是小问题。 lldb 怎么知道在到达那个点的过程中修改了什么状态?例如,要到达循环的某个迭代,lldb 必须知道将循环计数器设置为它在该点具有的任何值,而它无法知道。