lldb 暂停一个线程,而其他线程继续

lldb pause a thread while other threads continue

使用 lldb 作为调试器,是否可以暂停单个线程,而其他线程继续?

下面是 pthreads 的简单 C 多线程示例。

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

/*********************************************************************************
 Start two background threads.
 Both print a message to logs.
 Goal: pause a single thread why the other thread continues
 *********************************************************************************/

typedef struct{
    int count;
    char *message;
}Chomper;

void *hello_world(void *voidptr) {
    uint64_t tid;
    unsigned int microseconds = 10;
    assert(pthread_threadid_np(NULL, &tid)== 0);
    printf("Thread ID: dec:%llu hex: %#08x\n", tid, (unsigned int) tid);
    Chomper *chomper = (Chomper *)voidptr;  // help the compiler map the void pointer to the actual data structure

    for (int i = 0; i < chomper->count; i++) {
        usleep(microseconds);
        printf("%s: %d\n", chomper->message, i);
    }
    return NULL;
}

int main() {

        pthread_t myThread1 = NULL, myThread2 = NULL;

        Chomper *shark = malloc(sizeof(*shark));
        shark->count = 5;
        shark->message = "hello";

        Chomper *jellyfish = malloc(sizeof(*jellyfish));
        jellyfish->count = 20;
        jellyfish->message = "goodbye";

        assert(pthread_create(&myThread1, NULL, hello_world, (void *) shark) == 0);
        assert(pthread_create(&myThread2, NULL, hello_world, (void *) jellyfish) == 0);
        assert(pthread_join(myThread1, NULL) == 0);
        assert(pthread_join(myThread2, NULL) == 0);

        free(shark);
        free(jellyfish);
        return 0;
}

取决于你问的是什么。您不能在其他线程 运行ning 时暂停并检查一个线程的状态。一旦进程 运行ning 您必须暂停它以读取内存、变量等。

但是你可以恢复进程,但只允许一些线程运行。一种方法是使用:

(lldb) thread continue <LIST OF THREADS TO CONTINUE>

另一种方法是使用 Python 中的 lldb.SBThread.Suspend() API 从 运行 保留一些线程或线程,直到您使用 lldb.SBThread.Resume() 恢复它们。我们没有为此制作命令行命令,因为我们认为这样做很容易让自己陷入僵局,所以我们强迫您通过 SB API 来证明您知道自己在做什么...但是如果您需要经常这样做,那么很容易制作一个基于 Python 的命令来完成它。参见:

https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function

了解详情。