static std::string
i386_windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
{
  if (ptid.lwp () != 0)
    return string_printf ("Thread 0x%lx", ptid.lwp ());

  return normal_pid_to_str (ptid);
}
static void
store_debug_register (ptid_t ptid, int idx, long val)
{
  int tid;

  tid = ptid.lwp ();
  if (tid == 0)
    tid = ptid.pid ();

  (void) ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) (PT_DBR + 8 * idx), val);
}
Exemple #3
0
pid_t
get_ptrace_pid (ptid_t ptid)
{
  pid_t pid;

  /* If we have an LWPID to work with, use it.  Otherwise, we're
     dealing with a non-threaded program/target.  */
  pid = ptid.lwp ();
  if (pid == 0)
    pid = ptid.pid ();
  return pid;
}
int
linux_common_core_of_thread (ptid_t ptid)
{
  char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN];
  char *content = NULL;
  char *p;
  char *ts = 0;
  int content_read = 0;
  int i;
  int core;

  sprintf (filename, "/proc/%lld/task/%lld/stat",
	   (PID_T) ptid.pid (), (PID_T) ptid.lwp ());
  gdb_file_up f = gdb_fopen_cloexec (filename, "r");
  if (!f)
    return -1;

  for (;;)
    {
      int n;
      content = (char *) xrealloc (content, content_read + 1024);
      n = fread (content + content_read, 1, 1024, f.get ());
      content_read += n;
      if (n < 1024)
	{
	  content[content_read] = '\0';
	  break;
	}
    }

  /* ps command also relies on no trailing fields ever contain ')'.  */
  p = strrchr (content, ')');
  if (p != NULL)
    p++;

  /* If the first field after program name has index 0, then core number is
     the field with index 36.  There's no constant for that anywhere.  */
  if (p != NULL)
    p = strtok_r (p, " ", &ts);
  for (i = 0; p != NULL && i != 36; ++i)
    p = strtok_r (NULL, " ", &ts);

  if (p == NULL || sscanf (p, "%d", &core) == 0)
    core = -1;

  xfree (content);

  return core;
}
Exemple #5
0
std::string
core_target::pid_to_str (ptid_t ptid)
{
  struct inferior *inf;
  int pid;

  /* The preferred way is to have a gdbarch/OS specific
     implementation.  */
  if (m_core_gdbarch != nullptr
      && gdbarch_core_pid_to_str_p (m_core_gdbarch))
    return gdbarch_core_pid_to_str (m_core_gdbarch, ptid);

  /* Otherwise, if we don't have one, we'll just fallback to
     "process", with normal_pid_to_str.  */

  /* Try the LWPID field first.  */
  pid = ptid.lwp ();
  if (pid != 0)
    return normal_pid_to_str (ptid_t (pid));

  /* Otherwise, this isn't a "threaded" core -- use the PID field, but
     only if it isn't a fake PID.  */
  inf = find_inferior_ptid (ptid);
  if (inf != NULL && !inf->fake_pid_p)
    return normal_pid_to_str (ptid);

  /* No luck.  We simply don't have a valid PID to print.  */
  return "<main task>";
}
Exemple #6
0
static gdb::byte_vector
rs6000_ptrace_ldinfo (ptid_t ptid)
{
  const int pid = ptid.pid ();
  gdb::byte_vector ldi (1024);
  int rc = -1;

  while (1)
    {
      if (ARCH64 ())
	rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi.data (),
			      ldi.size (), NULL);
      else
	rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi.data (),
			      ldi.size (), NULL);

      if (rc != -1)
	break; /* Success, we got the entire ld_info data.  */

      if (errno != ENOMEM)
	perror_with_name (_("ptrace ldinfo"));

      /* ldi is not big enough.  Double it and try again.  */
      ldi.resize (ldi.size () * 2);
    }

  return ldi;
}
Exemple #7
0
bool
fbsd_nat_target::thread_alive (ptid_t ptid)
{
  if (ptid.lwp_p ())
    {
      struct ptrace_lwpinfo pl;

      if (ptrace (PT_LWPINFO, ptid.lwp (), (caddr_t) &pl, sizeof pl)
	  == -1)
	return false;
#ifdef PL_FLAG_EXITED
      if (pl.pl_flags & PL_FLAG_EXITED)
	return false;
#endif
    }

  return true;
}
Exemple #8
0
void
inf_ptrace_target::post_startup_inferior (ptid_t pid)
{
  ptrace_event_t pe;

  /* Set the initial event mask.  */
  memset (&pe, 0, sizeof pe);
  pe.pe_set_event |= PTRACE_FORK;
  if (ptrace (PT_SET_EVENT_MASK, pid.pid (),
	      (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
    perror_with_name (("ptrace"));
}
Exemple #9
0
ptid_t
rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
			 int options)
{
  pid_t pid;
  int status, save_errno;

  do
    {
      set_sigint_trap ();

      do
	{
	  pid = waitpid (ptid.pid (), &status, 0);
	  save_errno = errno;
	}
      while (pid == -1 && errno == EINTR);

      clear_sigint_trap ();

      if (pid == -1)
	{
	  fprintf_unfiltered (gdb_stderr,
			      _("Child process unexpectedly missing: %s.\n"),
			      safe_strerror (save_errno));

	  /* Claim it exited with unknown signal.  */
	  ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
	  ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
	  return inferior_ptid;
	}

      /* Ignore terminated detached child processes.  */
      if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
	pid = -1;
    }
  while (pid == -1);

  /* AIX has a couple of strange returns from wait().  */

  /* stop after load" status.  */
  if (status == 0x57c)
    ourstatus->kind = TARGET_WAITKIND_LOADED;
  /* signal 0.  I have no idea why wait(2) returns with this status word.  */
  else if (status == 0x7f)
    ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
  /* A normal waitstatus.  Let the usual macros deal with it.  */
  else
    store_waitstatus (ourstatus, status);

  return ptid_t (pid);
}
static unsigned long
i386_gnu_dr_get_reg (ptid_t ptid, int regnum)
{
  struct i386_debug_state regs;
  struct proc *thread;

  /* Make sure we know about new threads.  */
  inf_update_procs (gnu_current_inf);

  thread = inf_tid_to_thread (gnu_current_inf, ptid.lwp ());
  i386_gnu_dr_get (&regs, thread);

  return regs.dr[regnum];
}
Exemple #11
0
static int
find_one_thread (ptid_t ptid)
{
  td_thrhandle_t th;
  td_thrinfo_t ti;
  td_err_e err;
  struct lwp_info *lwp;
  struct thread_db *thread_db = current_process ()->priv->thread_db;
  int lwpid = ptid.lwp ();

  thread_info *thread = find_thread_ptid (ptid);
  lwp = get_thread_lwp (thread);
  if (lwp->thread_known)
    return 1;

  /* Get information about this thread.  */
  err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
  if (err != TD_OK)
    error ("Cannot get thread handle for LWP %d: %s",
	   lwpid, thread_db_err_str (err));

  err = thread_db->td_thr_get_info_p (&th, &ti);
  if (err != TD_OK)
    error ("Cannot get thread info for LWP %d: %s",
	   lwpid, thread_db_err_str (err));

  if (debug_threads)
    debug_printf ("Found thread %ld (LWP %d)\n",
		  (unsigned long) ti.ti_tid, ti.ti_lid);

  if (lwpid != ti.ti_lid)
    {
      warning ("PID mismatch!  Expected %ld, got %ld",
	       (long) lwpid, (long) ti.ti_lid);
      return 0;
    }

  /* If the new thread ID is zero, a final thread ID will be available
     later.  Do not enable thread debugging yet.  */
  if (ti.ti_tid == 0)
    return 0;

  lwp->thread_known = 1;
  lwp->th = th;
  lwp->thread_handle = ti.ti_tid;

  return 1;
}
Exemple #12
0
void
fbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
{
#if defined(TDP_RFPPWAIT) && !defined(PTRACE_VFORK)
  pid_t pid;

  /* Don't PT_CONTINUE a process which has a pending vfork done event.  */
  if (minus_one_ptid == ptid)
    pid = inferior_ptid.pid ();
  else
    pid = ptid.pid ();
  if (fbsd_is_vfork_done_pending (pid))
    return;
#endif

  if (debug_fbsd_lwp)
    fprintf_unfiltered (gdb_stdlog,
			"FLWP: fbsd_resume for ptid (%d, %ld, %ld)\n",
			ptid.pid (), ptid.lwp (),
			ptid.tid ());
  if (ptid.lwp_p ())
    {
      /* If ptid is a specific LWP, suspend all other LWPs in the process.  */
      struct thread_info *tp;
      int request;

      ALL_NON_EXITED_THREADS (tp)
        {
	  if (tp->ptid.pid () != ptid.pid ())
	    continue;

	  if (tp->ptid.lwp () == ptid.lwp ())
	    request = PT_RESUME;
	  else
	    request = PT_SUSPEND;

	  if (ptrace (request, tp->ptid.lwp (), NULL, 0) == -1)
	    perror_with_name (("ptrace"));
	}
    }
  else
    {
namespace ptid {

/* Check that the ptid_t class is POD.

   This is a requirement for as long as we have ptids embedded in
   structures allocated with malloc. */

static_assert (std::is_pod<ptid_t>::value, "ptid_t is POD");

/* We want to avoid implicit conversion from int to ptid_t.  */

static_assert (!std::is_convertible<int, ptid_t>::value,
	       "constructor is explicit");

/* Build some useful ptids.  */

static constexpr ptid_t pid = ptid_t (1);
static constexpr ptid_t lwp = ptid_t (1, 2, 0);
static constexpr ptid_t tid = ptid_t (1, 0, 2);
static constexpr ptid_t both = ptid_t (1, 2, 2);

/* Build some constexpr version of null_ptid and minus_one_ptid to use in
   static_assert.  Once the real ones are made constexpr, we can get rid of
   these.  */

static constexpr ptid_t null = ptid_t::make_null ();
static constexpr ptid_t minus_one = ptid_t::make_minus_one ();

/* Verify pid.  */

static_assert (pid.pid () == 1, "pid's pid is right");
static_assert (lwp.pid () == 1, "lwp's pid is right");
static_assert (tid.pid () == 1, "tid's pid is right");
static_assert (both.pid () == 1, "both's pid is right");

/* Verify lwp_p.  */

static_assert (!pid.lwp_p (), "pid's lwp_p is right");
static_assert (lwp.lwp_p (), "lwp's lwp_p is right");
static_assert (!tid.lwp_p (), "tid's lwp_p is right");
static_assert (both.lwp_p (), "both's lwp_p is right");

/* Verify lwp.  */

static_assert (pid.lwp () == 0, "pid's lwp is right");
static_assert (lwp.lwp () == 2, "lwp's lwp is right");
static_assert (tid.lwp () == 0, "tid's lwp is right");
static_assert (both.lwp () == 2, "both's lwp is right");

/* Verify tid_p.  */

static_assert (!pid.tid_p (), "pid's tid_p is right");
static_assert (!lwp.tid_p (), "lwp's tid_p is right");
static_assert (tid.tid_p (), "tid's tid_p is right");
static_assert (both.tid_p (), "both's tid_p is right");

/* Verify tid.  */

static_assert (pid.tid () == 0, "pid's tid is right");
static_assert (lwp.tid () == 0, "lwp's tid is right");
static_assert (tid.tid () == 2, "tid's tid is right");
static_assert (both.tid () == 2, "both's tid is right");

/* Verify is_pid.  */

static_assert (pid.is_pid (), "pid is a pid");
static_assert (!lwp.is_pid (), "lwp isn't a pid");
static_assert (!tid.is_pid (), "tid isn't a pid");
static_assert (!both.is_pid (), "both isn't a pid");
static_assert (!null.is_pid (), "null ptid isn't a pid");
static_assert (!minus_one.is_pid (), "minus one ptid isn't a pid");

/* Verify operator ==.  */

static_assert (pid == ptid_t (1, 0, 0), "pid operator== is right");
static_assert (lwp == ptid_t (1, 2, 0), "lwp operator== is right");
static_assert (tid == ptid_t (1, 0, 2), "tid operator== is right");
static_assert (both == ptid_t (1, 2, 2), "both operator== is right");

/* Verify operator !=.  */

static_assert (pid != ptid_t (2, 0, 0), "pid isn't equal to a different pid");
static_assert (pid != lwp, "pid isn't equal to one of its thread");
static_assert (lwp != tid, "lwp isn't equal to tid");
static_assert (both != lwp, "both isn't equal to lwp");
static_assert (both != tid, "both isn't equal to tid");

/* Verify matches against minus_one.  */

static_assert (pid.matches (minus_one), "pid matches minus one");
static_assert (lwp.matches (minus_one), "lwp matches minus one");
static_assert (tid.matches (minus_one), "tid matches minus one");
static_assert (both.matches (minus_one), "both matches minus one");

/* Verify matches against pid.  */

static_assert (pid.matches (pid), "pid matches pid");
static_assert (lwp.matches (pid), "lwp matches pid");
static_assert (tid.matches (pid), "tid matches pid");
static_assert (both.matches (pid), "both matches pid");
static_assert (!ptid_t (2, 0, 0).matches (pid), "other pid doesn't match pid");
static_assert (!ptid_t (2, 2, 0).matches (pid), "other lwp doesn't match pid");
static_assert (!ptid_t (2, 0, 2).matches (pid), "other tid doesn't match pid");
static_assert (!ptid_t (2, 2, 2).matches (pid), "other both doesn't match pid");

/* Verify matches against exact matches.  */

static_assert (!pid.matches (lwp), "pid doesn't match lwp");
static_assert (lwp.matches (lwp), "lwp matches lwp");
static_assert (!tid.matches (lwp), "tid doesn't match lwp");
static_assert (!both.matches (lwp), "both doesn't match lwp");
static_assert (!ptid_t (2, 2, 0).matches (lwp), "other lwp doesn't match lwp");

static_assert (!pid.matches (tid), "pid doesn't match tid");
static_assert (!lwp.matches (tid), "lwp doesn't match tid");
static_assert (tid.matches (tid), "tid matches tid");
static_assert (!both.matches (tid), "both doesn't match tid");
static_assert (!ptid_t (2, 0, 2).matches (tid), "other tid doesn't match tid");

static_assert (!pid.matches (both), "pid doesn't match both");
static_assert (!lwp.matches (both), "lwp doesn't match both");
static_assert (!tid.matches (both), "tid doesn't match both");
static_assert (both.matches (both), "both matches both");
static_assert (!ptid_t (2, 2, 2).matches (both),
	       "other both doesn't match both");


} /* namespace ptid */
Exemple #14
0
int
ptid_tid_p (const ptid_t &ptid)
{
  return ptid.tid_p ();
}
Exemple #15
0
int
ptid_lwp_p (const ptid_t &ptid)
{
  return ptid.lwp_p ();
}
Exemple #16
0
int
ptid_is_pid (const ptid_t &ptid)
{
  return ptid.is_pid ();
}
Exemple #17
0
long
ptid_get_tid (const ptid_t &ptid)
{
  return ptid.tid ();
}
Exemple #18
0
long
ptid_get_lwp (const ptid_t &ptid)
{
  return ptid.lwp ();
}
Exemple #19
0
int
ptid_get_pid (const ptid_t &ptid)
{
  return ptid.pid ();
}
Exemple #20
0
int
ptid_match (const ptid_t &ptid, const ptid_t &filter)
{
  return ptid.matches (filter);
}
Exemple #21
0
bool
inf_ptrace_target::thread_alive (ptid_t ptid)
{
  /* ??? Is kill the right way to do this?  */
  return (::kill (ptid.pid (), 0) != -1);
}
Exemple #22
0
ptid_t
inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
			 int options)
{
  pid_t pid;
  int status, save_errno;

  do
    {
      set_sigint_trap ();

      do
	{
	  pid = waitpid (ptid.pid (), &status, 0);
	  save_errno = errno;
	}
      while (pid == -1 && errno == EINTR);

      clear_sigint_trap ();

      if (pid == -1)
	{
	  fprintf_unfiltered (gdb_stderr,
			      _("Child process unexpectedly missing: %s.\n"),
			      safe_strerror (save_errno));

	  /* Claim it exited with unknown signal.  */
	  ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
	  ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
	  return inferior_ptid;
	}

      /* Ignore terminated detached child processes.  */
      if (!WIFSTOPPED (status) && pid != inferior_ptid.pid ())
	pid = -1;
    }
  while (pid == -1);

#ifdef PT_GET_PROCESS_STATE
  if (WIFSTOPPED (status))
    {
      ptrace_state_t pe;
      pid_t fpid;

      if (ptrace (PT_GET_PROCESS_STATE, pid,
		  (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
	perror_with_name (("ptrace"));

      switch (pe.pe_report_event)
	{
	case PTRACE_FORK:
	  ourstatus->kind = TARGET_WAITKIND_FORKED;
	  ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);

	  /* Make sure the other end of the fork is stopped too.  */
	  fpid = waitpid (pe.pe_other_pid, &status, 0);
	  if (fpid == -1)
	    perror_with_name (("waitpid"));

	  if (ptrace (PT_GET_PROCESS_STATE, fpid,
		      (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
	    perror_with_name (("ptrace"));

	  gdb_assert (pe.pe_report_event == PTRACE_FORK);
	  gdb_assert (pe.pe_other_pid == pid);
	  if (fpid == inferior_ptid.pid ())
	    {
	      ourstatus->value.related_pid = ptid_t (pe.pe_other_pid);
	      return ptid_t (fpid);
	    }

	  return ptid_t (pid);
	}
    }
#endif

  store_waitstatus (ourstatus, status);
  return ptid_t (pid);
}