以lldb
作为调试器,您可以暂停一个线程,而其他线程继续?
一个简单的C
多线程示例,在下面的pthreads
。
#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: %#08xn", 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: %dn", 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;
}
取决于您的要求。在其他线程运行时,您不能暂停并检查一个线程的状态。该过程运行后,您必须暂停它才能读取内存,变量等。
但是您可以恢复该过程,但只允许某些线程运行。一种方法是使用:
(lldb) thread continue <LIST OF THREADS TO CONTINUE>
另一个是使用Python的lldb.SBThread.Suspend()
API来防止某些线程或线程运行,直到使用lldb.SBThread.Resume()
恢复它们为止。我们之所需要经常这样做,很容易做出这样做的基于Python的命令。请参阅:
https://lldb.llvm.org/use/python-reference.html#create-a-a-new-new-lldb-command-command-using-a-a-python function
有关详细信息。