llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

lldb python API is not working correctly

Open iamanonymouscs opened this issue 3 years ago • 3 comments

$ cat small.c

#include <stdio.h>

int main ()
{
  printf("Hello, world!");
  return 0;
}

$ cat script.py

# -*- coding: utf-8 -*-

#!/usr/bin/env python3

import lldb

print("debugger: ", lldb.debugger)
print("target: ", lldb.target)
print("process: ", lldb.process)

$ clang -O0 -g small.c $ lldb --batch --one-line 'process launch --stop-at-entry' --one-line 'command script import script.py' --file a.out

(lldb) target create "a.out"
Current executable set to 'a.out' (x86_64).
(lldb) process launch --stop-at-entry
Process 34338 launched: 'a.out' (x86_64)
(lldb) command script import script.py
debugger:  Debugger (instance: "debugger_1", id: 1)
target:  None
process:  None

We can found that the lldb python API lldb.target and lldb.process is not working correctly as lldb.debugger

$ lldb --version lldb version 17.0.0 $ clang --version

Ubuntu clang version 17.0.0 (++20230412091519+75ef84bf5260-1~exp1~20230412091628.1170)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

iamanonymouscs avatar Apr 14 '23 14:04 iamanonymouscs

@llvm/issue-subscribers-lldb

llvmbot avatar Apr 14 '23 14:04 llvmbot

That's expected. We have a section on the website that explains why:

While extremely convenient, these variables have a couple caveats that you should be aware of. First of all, they hold the values of the selected objects on entry to the embedded interpreter. They do not update as you use the LLDB API’s to change, for example, the currently selected stack frame or thread.

Moreover, they are only defined and meaningful while in the interactive Python interpreter. There is no guarantee on their value in any other situation, hence you should not use them when defining Python formatters, breakpoint scripts and commands (or any other Python extension point that LLDB provides). For the latter you’ll be passed an SBDebugger, SBTarget, SBProcess, SBThread or SBFrame instance and you can use the functions from the “Equivalent” column to navigate between them.

JDevlieghere avatar Apr 14 '23 15:04 JDevlieghere

That's expected. We have a section on the website that explains why:

While extremely convenient, these variables have a couple caveats that you should be aware of. First of all, they hold the values of the selected objects on entry to the embedded interpreter. They do not update as you use the LLDB API’s to change, for example, the currently selected stack frame or thread.

Moreover, they are only defined and meaningful while in the interactive Python interpreter. There is no guarantee on their value in any other situation, hence you should not use them when defining Python formatters, breakpoint scripts and commands (or any other Python extension point that LLDB provides). For the latter you’ll be passed an SBDebugger, SBTarget, SBProcess, SBThread or SBFrame instance and you can use the functions from the “Equivalent” column to navigate between them.

Thank you very much for the detailed explanation.

iamanonymouscs avatar Apr 14 '23 16:04 iamanonymouscs