0

I'm trying to run a Python debug script in LLDB, but I'm getting invalid thread errors. I'm not sure why.

Here are my programs:

main.c:

#include <stdio.h>
#include <stdlib.h>

int addNumbs(int a, int b)
{
    return a+b;
}

    int main()
    {
        int a =5;
        int b= 24;
    
        int total = addNumbs(a,b);
    
        printf("%d\n", total);
    
        return 0;
    }

My python debugging script debugersample.py:

import lldb

debugger = lldb.debugger

debugger.HandleCommand("b main")
debugger.HandleCommand("r")

for i in range(0,3):                #execute next 3 times
    debugger.HandleCommand("n")

debugger.HandleCommand("bt")

Running LLDB:

clang -g main -o main
lldb ./main
(lldb) command script import debugsample.py

Breakpoint 1: where = main`main + 15 at main.c:11:9, address = 0x0000000100003f0f
Process 5344 launched: '/Users/xxxx/CLionProjects/C_Practice/main' (x86_64)
error: invalid thread
error: invalid thread
error: invalid thread
error: invalid thread
Process 5344 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100003f0f main`main at main.c:11:9
   8    
   9    int main()
   10   {
-> 11       int a =5;
   12       int b= 24;
   13   
   14       int* ptr = (int*)malloc(sizeof(int));
Target 0: (main) stopped.

Note: When I run these commands in LLDB Python's interactive interpreter line by line, it works fine.

1 Answer 1

1

This is because the debugger was running in "asynchronous" mode while running your python code. There's some discussion of the two modes here:

Continue after python-lldb script has finished

It's also mentioned in the docs (look for SetAsync):

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

In asynchronous mode, all API that make the debugee run return immediately, then script execution continues - expecting you to call WaitNextEvent to wait on the process stopping. That's why your other calls fail with "no thread" because the process hasn't stopped yet, and we don't access the thread data of running processes.

Event handling is overkill for little scripts like this; that's what sync mode is for. In sync mode, an API that makes the debugee continue stalls until the process stops again, which is what you want.

So, just run your script thusly:

old_async = debugger.GetAsync()
debugger.SetAsync(False)
<do work>
debugger.SetAsync(old_async)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.