darwin_inferior *darwin_inf = get_darwin_inferior (inf);
std::vector<darwin_thread_t *> new_thread_vec;
+ if (darwin_inf == nullptr)
+ return;
+
/* Get list of threads. */
kret = task_threads (darwin_inf->task, &thread_list, &new_nbr);
MACH_CHECK_ERROR (kret);
{
darwin_inferior *priv = get_darwin_inferior (inf);
- return priv->task == *(task_t *)port_ptr;
+ return priv != nullptr && priv->task == *(task_t *)port_ptr;
}
static int
{
darwin_inferior *priv = get_darwin_inferior (inf);
- for (darwin_thread_t *t : priv->threads)
- {
- if (t->gdb_port == thread)
- return t;
- }
+ if (priv != nullptr)
+ for (darwin_thread_t *t : priv->threads)
+ {
+ if (t->gdb_port == thread)
+ return t;
+ }
return NULL;
}
{
darwin_inferior *priv = get_darwin_inferior (inf);
- if (!priv->suspended)
+ if (priv != nullptr && !priv->suspended)
{
kern_return_t kret;
{
darwin_inferior *priv = get_darwin_inferior (inf);
- if (priv->suspended)
+ if (priv != nullptr && priv->suspended)
{
kern_return_t kret;
inf = darwin_find_inferior_by_task (task_port);
*pinf = inf;
- darwin_inferior *priv = get_darwin_inferior (inf);
-
/* Check message destination. */
- if (inf != NULL && hdr->msgh_local_port != priv->notify_port)
- return -4;
+ if (inf != NULL)
+ {
+ darwin_inferior *priv = get_darwin_inferior (inf);
+ if (hdr->msgh_local_port != priv->notify_port)
+ return -4;
+ }
return 0;
}
priv->pending_messages--;
}
+/* Wrapper around the __pthread_kill syscall. We use this instead of the
+ pthread_kill function to be able to send a signal to any kind of thread,
+ including GCD threads. */
+
+static int
+darwin_pthread_kill (darwin_thread_t *thread, int nsignal)
+{
+ DIAGNOSTIC_PUSH;
+ DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS;
+ int res = syscall (SYS___pthread_kill, thread->gdb_port, nsignal);
+ DIAGNOSTIC_POP;
+ return res;
+}
+
static void
darwin_resume_thread (struct inferior *inf, darwin_thread_t *thread,
int step, int nsignal)
{
- kern_return_t kret;
- int res;
-
inferior_debug
(3, _("darwin_resume_thread: state=%d, thread=0x%x, step=%d nsignal=%d\n"),
thread->msg_state, thread->gdb_port, step, nsignal);
&& thread->event.ex_data[0] == EXC_SOFT_SIGNAL)
{
/* Either deliver a new signal or cancel the signal received. */
- res = PTRACE (PT_THUPDATE, inf->pid,
- (caddr_t) (uintptr_t) thread->gdb_port, nsignal);
+ int res = PTRACE (PT_THUPDATE, inf->pid,
+ (caddr_t) (uintptr_t) thread->gdb_port, nsignal);
if (res < 0)
inferior_debug (1, _("ptrace THUP: res=%d\n"), res);
}
{
/* Note: ptrace is allowed only if the process is stopped.
Directly send the signal to the thread. */
- res = syscall (SYS___pthread_kill, thread->gdb_port, nsignal);
+ int res = darwin_pthread_kill (thread, nsignal);
inferior_debug (4, _("darwin_resume_thread: kill 0x%x %d: %d\n"),
thread->gdb_port, nsignal, res);
thread->signaled = 1;
break;
case DARWIN_STOPPED:
- kret = thread_resume (thread->gdb_port);
+ kern_return_t kret = thread_resume (thread->gdb_port);
MACH_CHECK_ERROR (kret);
thread->msg_state = DARWIN_RUNNING;
{
darwin_inferior *priv = get_darwin_inferior (inf);
- for (darwin_thread_t *thread : priv->threads)
- darwin_resume_thread (inf, thread, step, nsignal);
+ if (priv != nullptr)
+ for (darwin_thread_t *thread : priv->threads)
+ darwin_resume_thread (inf, thread, step, nsignal);
}
struct resume_inferior_threads_param
inferior_debug
(2, _("darwin_resume: pid=%d, tid=0x%lx, step=%d, signal=%d\n"),
- ptid_get_pid (ptid), ptid_get_tid (ptid), step, signal);
+ ptid.pid (), ptid.tid (), step, signal);
if (signal == GDB_SIGNAL_0)
nsignal = 0;
ptid = inferior_ptid;
/* minus_one_ptid is RESUME_ALL. */
- if (ptid_equal (ptid, minus_one_ptid))
+ if (ptid == minus_one_ptid)
{
struct resume_inferior_threads_param param;
else
{
struct inferior *inf = find_inferior_ptid (ptid);
- long tid = ptid_get_tid (ptid);
+ long tid = ptid.tid ();
/* Stop the inferior (should be useless). */
darwin_suspend_inferior (inf);
if (breakpoint_inserted_here_p (regcache->aspace (), pc))
{
inferior_debug (4, "cancel_breakpoint for thread 0x%lx\n",
- (unsigned long) ptid_get_tid (ptid));
+ (unsigned long) ptid.tid ());
/* Back up the PC if necessary. */
if (gdbarch_decr_pc_after_break (gdbarch))
inferior_debug
(2, _("darwin_wait: waiting for a message pid=%d thread=%lx\n"),
- ptid_get_pid (ptid), ptid_get_tid (ptid));
+ ptid.pid (), ptid.tid ());
/* Handle fake stop events at first. */
if (darwin_inf_fake_stop != NULL)
darwin_dump_message (hdr, darwin_debug_flag > 11);
res = darwin_decode_message (hdr, &thread, &inf, status);
- if (ptid_equal (res, minus_one_ptid))
+ if (res == minus_one_ptid)
continue;
/* Early return in case an inferior has exited. */
int status;
int res;
- if (ptid_equal (inferior_ptid, null_ptid))
+ if (inferior_ptid == null_ptid)
return;
gdb_assert (inf != NULL);
if (res == 0)
{
+ /* On MacOS version Sierra, the darwin_restore_exception_ports call
+ does not work as expected.
+ When the kill function is called, the SIGKILL signal is received
+ by gdb whereas it should have been received by the kernel since
+ the exception ports have been restored.
+ This behavior is not the expected one thus gdb does not reply to
+ the received SIGKILL message. This situation leads to a "busy"
+ resource from the kernel point of view and the inferior is never
+ released, causing it to remain as a zombie process, even after
+ GDB exits.
+ To work around this, we mark all the threads of the inferior as
+ signaled thus darwin_decode_message function knows that the kill
+ signal was sent by gdb and will take the appropriate action
+ (cancel signal and reply to the signal message). */
+ darwin_inferior *priv = get_darwin_inferior (inf);
+ for (darwin_thread_t *thread : priv->threads)
+ thread->signaled = 1;
+
darwin_resume_inferior (inf);
ptid = darwin_wait (inferior_ptid, &wstatus);
}
CATCH (ex, RETURN_MASK_ALL)
{
- inf->pid = 0;
- inf->priv.reset ();
+ exit_inferior (inf);
inferior_ptid = null_ptid;
throw_exception (ex);
posix_spawnp (NULL, argv[0], NULL, &attr, argv, env);
}
+/* Read kernel version, and return TRUE on Sierra or later. */
+
+static bool
+should_disable_startup_with_shell ()
+{
+ char str[16];
+ size_t sz = sizeof (str);
+ int ret;
+
+ ret = sysctlbyname ("kern.osrelease", str, &sz, NULL, 0);
+ if (ret == 0 && sz < sizeof (str))
+ {
+ unsigned long ver = strtoul (str, NULL, 10);
+ if (ver >= 16)
+ return true;
+ }
+ return false;
+}
+
void
darwin_nat_target::create_inferior (const char *exec_file,
const std::string &allargs,
char **env, int from_tty)
{
+ gdb::optional<scoped_restore_tmpl<int>> restore_startup_with_shell;
+
+ if (startup_with_shell && should_disable_startup_with_shell ())
+ {
+ warning (_("startup-with-shell not supported on this macOS version,"
+ " disabling it."));
+ restore_startup_with_shell.emplace (&startup_with_shell, 0);
+ }
+
/* Do the hard work. */
fork_inferior (exec_file, allargs, env, darwin_ptrace_me,
darwin_ptrace_him, darwin_pre_ptrace, NULL,
darwin_inferior *priv = get_darwin_inferior (inf);
- darwin_check_osabi (priv, ptid_get_tid (inferior_ptid));
+ darwin_check_osabi (priv, inferior_ptid.tid ());
darwin_setup_fake_stop_event (inf);
void
darwin_nat_target::detach (inferior *inf, int from_tty)
{
- pid_t pid = ptid_get_pid (inferior_ptid);
+ pid_t pid = inferior_ptid.pid ();
darwin_inferior *priv = get_darwin_inferior (inf);
kern_return_t kret;
int res;
darwin_nat_target::pid_to_str (ptid_t ptid)
{
static char buf[80];
- long tid = ptid_get_tid (ptid);
+ long tid = ptid.tid ();
if (tid != 0)
{
snprintf (buf, sizeof (buf), _("Thread 0x%lx of process %u"),
- tid, ptid_get_pid (ptid));
+ tid, ptid.pid ());
return buf;
}
set_enable_mach_exceptions (const char *args, int from_tty,
struct cmd_list_element *c)
{
- if (!ptid_equal (inferior_ptid, null_ptid))
+ if (inferior_ptid != null_ptid)
{
struct inferior *inf = current_inferior ();
darwin_inferior *priv = get_darwin_inferior (inf);
for (darwin_thread_t *t : priv->threads)
{
if (t->inf_port == lwp)
- return ptid_t (ptid_get_pid (inferior_ptid), 0, t->gdb_port);
+ return ptid_t (inferior_ptid.pid (), 0, t->gdb_port);
}
/* Maybe the port was never extract. Do it now. */
names_count * sizeof (mach_port_t));
if (res)
- return ptid_t (ptid_get_pid (inferior_ptid), 0, res);
+ return ptid_t (inferior_ptid.pid (), 0, res);
else
return null_ptid;
}