Skip to content

Instantly share code, notes, and snippets.

@lambdageek
Created February 20, 2020 18:50
Show Gist options
  • Save lambdageek/ca5cf60b8274acf52df8fefe4230a724 to your computer and use it in GitHub Desktop.
Save lambdageek/ca5cf60b8274acf52df8fefe4230a724 to your computer and use it in GitHub Desktop.
lldb attach to parent hang
#! /bin/sh
exec /Applications/Xcode.app/Contents/Developer/usr/bin/lldb --debug --batch --no-lldbinit --attach-pid $1 --source /tmp/aleksey.txt < /dev/null 2>&1
#include <fcntl.h>
#include <sys/errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main (void)
{
const char *commands_filename = "/tmp/aleksey.txt";
int commands_handle = open (commands_filename, O_TRUNC | O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (commands_handle < 0) {
printf ("open failed: %s\n", strerror (errno));
return 1;
}
pid_t self_pid = getpid ();
//dprintf (commands_handle, "process attach --pid %ld\n", (long) self_pid);
dprintf (commands_handle, "thread list\n");
dprintf (commands_handle, "thread backtrace all\n");
dprintf (commands_handle, "detach\n");
dprintf (commands_handle, "quit\n");
close (commands_handle);
char pid_str[50];
sprintf (pid_str, "%ld", (long) self_pid);
pid_t child_pid = fork ();
if (child_pid == 0) {
const char *args[] = {
"/Applications/Xcode.app/Contents/Developer/usr/bin/lldb",
"--debug",
"--no-use-colors",
"--batch",
"--no-lldbinit",
"--attach-pid",
pid_str,
"--source",
commands_filename,
(char*)0
};
#if 0
const char *args[] = {
"./run-lldb.sh",
pid_str,
(char*)0
};
#endif
#if 0
pid_t pid2 = fork ();
if (pid2 < 0) {
perror ("fork 2");
_exit (1);
} else if (pid2 > 0)
_exit (0);
#else
if (setsid () < 0)
perror ("setsid");
#endif
//if (setpgid (0,0) < 0)
// perror ("setpgid");
signal (SIGTTIN, SIG_IGN);
signal (SIGTTOU, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
signal (SIGHUP, SIG_IGN);
dup2 (STDERR_FILENO, STDOUT_FILENO);
close (STDIN_FILENO);
printf ("running lldb\n");
execv (args[0], (char**)args);
printf ("exec failed: %s\n", strerror (errno));
_exit (-1);
} else if (child_pid > 0) {
int status;
//setpgid (child_pid, child_pid);
printf ("sleeping for 10 seconds in the parent\n");
sleep (10);
printf ("waiting in parent\n");
waitpid (child_pid, &status, 0);
printf ("woke in parent\n");
while (0) {
int i;
printf ("ping\n");
for (i = 0; i < 12345678; ++i) {
//if (i % 500000 == 0)
// printf ("ping\n");
}
sleep (5);
}
return 0;
} else {
printf ("fork failed: %s\n", strerror (errno));
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment