Reputation: 4085
With lldb
as the debugger, can you pause a single thread, while other threads continue?
A simple C
multi-threaded example with pthreads
below.
#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;
}
Upvotes: 2
Views: 3469
Reputation: 27110
Depends on what you are asking. You can't pause and examine the state of one thread while other threads are running. Once the process is running you have to pause it to read memory, variables, etc.
But you can resume the process, but only allow some threads to run. One way to do this is use:
(lldb) thread continue <LIST OF THREADS TO CONTINUE>
Another is to use the lldb.SBThread.Suspend()
API from Python to keep some thread or threads from running till you resume them with lldb.SBThread.Resume()
. We didn't make command line commands for this because we thought it was too easy to get yourself deadlocked when you did this, so we forced you to go through the SB API's to show you knew what you were doing... But if you need to do this often, it would be easy to make a Python based command that did it. See:
https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function
for details.
Upvotes: 5