Skip to content

Commit

Permalink
Fix gdb backtraces on OSX.
Browse files Browse the repository at this point in the history
We can't redirect output from gdb: since gdb will suspend us we won't
be able to read from the pipe we've redirected output to, and we'll
end up deadlocking if gdb outputs a lot of data (see bug mono#2548).
  • Loading branch information
rolfbjarne authored and duncanmak committed Jan 30, 2012
1 parent 1281853 commit 9435de9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 36 deletions.
7 changes: 2 additions & 5 deletions mono/mini/mini-darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,15 @@ mono_runtime_syscall_fork ()
return (pid_t) fork ();
}

gboolean
void
mono_gdb_render_native_backtraces (pid_t crashed_pid)
{
return FALSE; // disable for now due to #2548.
const char *argv [5];
char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";

argv [0] = g_find_program_in_path ("gdb");
if (argv [0] == NULL) {
return FALSE;
return;
}

if (mkstemp (gdb_template) != -1) {
Expand All @@ -257,6 +256,4 @@ mono_gdb_render_native_backtraces (pid_t crashed_pid)

unlink (gdb_template);
}

return TRUE;
}
29 changes: 3 additions & 26 deletions mono/mini/mini-exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2170,16 +2170,10 @@ mono_handle_native_sigsegv (int signal, void *ctx)
#if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && defined(SYS_fork)
if (!mini_get_debug_options ()->no_gdb_backtrace && !mono_debug_using_mono_debugger ()) {
/* From g_spawn_command_line_sync () in eglib */
int res;
int stdout_pipe [2] = { -1, -1 };
pid_t pid;
int status;
char buffer [1024];
pid_t crashed_pid = getpid ();

res = pipe (stdout_pipe);
g_assert (res != -1);

//pid = fork ();
/*
* glibc fork acquires some locks, so if the crash happened inside malloc/free,
Expand All @@ -2188,31 +2182,14 @@ mono_handle_native_sigsegv (int signal, void *ctx)
pid = mono_runtime_syscall_fork ();

if (pid == 0) {
close (stdout_pipe [0]);
dup2 (stdout_pipe [1], STDOUT_FILENO);

for (i = getdtablesize () - 1; i >= 3; i--)
close (i);

if (!mono_gdb_render_native_backtraces (crashed_pid))
close (STDOUT_FILENO);
dup2 (STDERR_FILENO, STDOUT_FILENO);

mono_gdb_render_native_backtraces (crashed_pid);
exit (1);
}

close (stdout_pipe [1]);

fprintf (stderr, "\nDebug info from gdb:\n\n");

while (1) {
int nread = read (stdout_pipe [0], buffer, 1024);

if (nread <= 0)
break;
write (STDERR_FILENO, buffer, nread);
}

waitpid (pid, &status, WNOHANG);
waitpid (pid, &status, 0);
}
#endif
/*
Expand Down
6 changes: 2 additions & 4 deletions mono/mini/mini-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,15 @@ mono_runtime_syscall_fork ()
#endif
}

gboolean
void
mono_gdb_render_native_backtraces (pid_t crashed_pid)
{
const char *argv [9];
char buf1 [128];

argv [0] = g_find_program_in_path ("gdb");
if (argv [0] == NULL) {
return FALSE;
return;
}

argv [1] = "-ex";
Expand All @@ -657,8 +657,6 @@ mono_gdb_render_native_backtraces (pid_t crashed_pid)
argv [8] = 0;

execv (argv [0], (char**)argv);

return TRUE;
}
#endif
#endif /* __native_client__ */
Expand Down
2 changes: 1 addition & 1 deletion mono/mini/mini.h
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,7 @@ void mono_runtime_setup_stat_profiler (void) MONO_INTERNAL;
void mono_runtime_shutdown_stat_profiler (void) MONO_INTERNAL;
void mono_runtime_posix_install_handlers (void) MONO_INTERNAL;
pid_t mono_runtime_syscall_fork (void) MONO_INTERNAL;
gboolean mono_gdb_render_native_backtraces (pid_t crashed_pid) MONO_INTERNAL;
void mono_gdb_render_native_backtraces (pid_t crashed_pid) MONO_INTERNAL;

/*
* Signal handling
Expand Down

0 comments on commit 9435de9

Please sign in to comment.