I have an application that I recently split to run in separate processes that communicate with each other via local sockets. I split it out in hopes of increasing stability, as the core "watcher" process can detect a failure and restart the afflicted sub-process.
However, now my watcher process frequently crashes with only the message "Segmentation Fault". I've surrounded all threaded operations in try/catch blocks to try to dump any output, but I still get the same results.
I have been unable to get the debugger to work in MonoDevelop (so development has been difficult enough without these ghost issues).
Isn't Mono supposed to be in a managed environment to prevent issues like this?
Is there any way I can narrow down the source of the issue?
Segmentation faults must (1) be debugged using gdb. To debug mono using gdb you first need to read this.
Once that's done, start your program, run ps auxf
to find the pid of your program, and then execute:
gdb program PID
This will attach gdb to your program. You should be presented with a gdb prompt:
$ (gdb)
execute the following (from the link you should have read by now):
$ (gdb) handle SIGXCPU SIG33 SIG35 SIGPWR nostop noprint
$ (gdb) continue
and now wait until your program stops responding. When that happens, return to gdb, and you'll hopefully find that your program has stopped at the segmentation fault (SIGSEGV), and you should be able to get more information about the crash. In particular this is useful:
$ (gdb) thread apply all backtrace
which will show the stack trace for all threads.
(1) You can also use the more brute way of sprinkling your code with calls to Console.WriteLine. This is your last resort when everything else fails :)
Mono is a managed environment, which makes your question really peculiar to me. Under what circumstances are you dealing with memory in a way to actually cause a seg fault? Do you have any portions of code marked unsafe
? If you do, I would look there first. Otherwise, there is a way to attach a debugger to an already running process. I would attempt to do that if you aren't debugging out of Monodevelop.
The more I think about it, the seg fault can't be in your code if you never do any memory manipulation yourself. It is probably in the runtime, and if so, try/catches aren't going to help you.
my suggestion would be to make good use of text and/or console logging and get a line by line detailed look at whats going on. Or hop in visual studio and do some traces.
Well, it must have been something with my threading of sockets. I switched to using the Begin/End functions instead of handling the threading myself and that fixed the issue.