Debug is very difficult when you develop a multi-thread program.
This article will show you how to use LLDB to do it.(the example is TinyWebServer)
about the project
TinyWebServer will create A acceptor thread,2 worker thread(or you can set the number of worker in web.json) and a ThreadPool which contains some threads(the number of threads is equal to the number of CPUs)
1. Set the breakpoint
First, you should set the breakpoint
the command of lldb to set breakpoint is:
1 | 1.use line |
First, we can set a breakpoint for acceptor, just like this
1 | br s -f kqueue_net_event.cpp -n KqueueAcceptEvent::Handle |
and then, run the Server and connect to localhost:10086
the debugger will stop on the breakpoint, now the TCP request is waitting for handling
2.Get the information of threads
Now, we input continue
to make the Server to continue with running
when we debug the multi-thread program, we want to know the status of threads
so, we can use thread list
to get the information of threads
1 | (lldb) thread list |
we can see the #1
is main thread, and #2,#3,#4
is calling the kevent function,#5,6,7,8
is waitting
so, we can guess #2,#3,#4
are a acceptor thread and 2 worker threads, #5,6,7,8
are the threads which in ThreadPool
3.Get the information of bug
When we upload a big file to the server, server will crash(This is a bug)
1 | Process 87773 stopped |
now, we can see the thread#6 has crashed, from information we know
- the code is runned by ThreadPool
- error: EXC_BAD_ACCESS