简体   繁体   中英

How do I use gdb for multi threaded networking program

I am working on a networking programming using epoll. I am getting a segmentation fault error but since it's running on multi thread, it is hard to find where it exactly gets the error by using logs.

I was trying to using gdb so I can see the stack trace. If I run this on gdb then I am getting this error from the epoll_wait. If I connected to the server from different clients then it doesn't work at all.

How do I fix this so I can use gdb to find out where it gets the segmentation fault error Thanks in advance..

epoll_wait error
: Interrupted system call

You need to fix your program to handle EINTR correctly. EINTR ("Interrupted system call") is NOT a fatal error; it just means "please retry that system call again". So your code that calls epoll_wait() should detect it and just silently retry the call. Something like this:

int rv;
do {
    rv = epoll_wait(epfd, events, maxevents, timeout);
} while (rv == -1 && errno == EINTR);

Or, if you have a fixed timeout you need to recalculate it every call:

int rv;
rv = epoll_wait(epfd, events, maxevents, timeout);
while (rv == -1 && errno == EINTR) {
    ...TODO: recalculate timeout here...
    rv = epoll_wait(epfd, events, maxevents, timeout);
}

If you didn't know about this, you probably have the same bug in your calls to other system calls. Especially read() and write(), but also a lot of other calls - check the man pages for the calls you use, and see if they list EINTR as a possible error.

It's usually not practical to prevent EINTR from happening - if you use any library that uses signals, or if you use signals yourself, then you can get EINTR. Last time I looked, the Linux threading libraries used signals.

Enable core dump saving in your environment. Run command ulimit -c unlimited and rerun your program. When it crashes, load generated core dump in gdb and look backtraces of crash. In case of multi threaded program it is convenient to obtain backtraces from all threads by one command at once: (gdb) thread apply all bt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM