gdb/nat/linux-procfs.c - gdb

Functions defined

Source code

  1. /* Linux-specific PROCFS manipulation routines.
  2.    Copyright (C) 2009-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "common-defs.h"
  15. #include "linux-procfs.h"
  16. #include "filestuff.h"
  17. #include <dirent.h>

  18. /* Return the TGID of LWPID from /proc/pid/status.  Returns -1 if not
  19.    found.  */

  20. static int
  21. linux_proc_get_int (pid_t lwpid, const char *field, int warn)
  22. {
  23.   size_t field_len = strlen (field);
  24.   FILE *status_file;
  25.   char buf[100];
  26.   int retval = -1;

  27.   snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid);
  28.   status_file = gdb_fopen_cloexec (buf, "r");
  29.   if (status_file == NULL)
  30.     {
  31.       if (warn)
  32.         warning (_("unable to open /proc file '%s'"), buf);
  33.       return -1;
  34.     }

  35.   while (fgets (buf, sizeof (buf), status_file))
  36.     if (strncmp (buf, field, field_len) == 0 && buf[field_len] == ':')
  37.       {
  38.         retval = strtol (&buf[field_len + 1], NULL, 10);
  39.         break;
  40.       }

  41.   fclose (status_file);
  42.   return retval;
  43. }

  44. /* Return the TGID of LWPID from /proc/pid/status.  Returns -1 if not
  45.    found.  */

  46. int
  47. linux_proc_get_tgid (pid_t lwpid)
  48. {
  49.   return linux_proc_get_int (lwpid, "Tgid", 1);
  50. }

  51. /* See linux-procfs.h.  */

  52. pid_t
  53. linux_proc_get_tracerpid_nowarn (pid_t lwpid)
  54. {
  55.   return linux_proc_get_int (lwpid, "TracerPid", 0);
  56. }

  57. /* Fill in BUFFER, a buffer with BUFFER_SIZE bytes with the 'State'
  58.    line of /proc/PID/status.  Returns -1 on failure to open the /proc
  59.    file, 1 if the line is found, and 0 if not found.  If WARN, warn on
  60.    failure to open the /proc file.  */

  61. static int
  62. linux_proc_pid_get_state (pid_t pid, char *buffer, size_t buffer_size,
  63.                           int warn)
  64. {
  65.   FILE *procfile;
  66.   int have_state;

  67.   xsnprintf (buffer, buffer_size, "/proc/%d/status", (int) pid);
  68.   procfile = gdb_fopen_cloexec (buffer, "r");
  69.   if (procfile == NULL)
  70.     {
  71.       if (warn)
  72.         warning (_("unable to open /proc file '%s'"), buffer);
  73.       return -1;
  74.     }

  75.   have_state = 0;
  76.   while (fgets (buffer, buffer_size, procfile) != NULL)
  77.     if (strncmp (buffer, "State:", 6) == 0)
  78.       {
  79.         have_state = 1;
  80.         break;
  81.       }
  82.   fclose (procfile);
  83.   return have_state;
  84. }

  85. /* See linux-procfs.h declaration.  */

  86. int
  87. linux_proc_pid_is_gone (pid_t pid)
  88. {
  89.   char buffer[100];
  90.   int have_state;

  91.   have_state = linux_proc_pid_get_state (pid, buffer, sizeof buffer, 0);
  92.   if (have_state < 0)
  93.     {
  94.       /* If we can't open the status file, assume the thread has
  95.          disappeared.  */
  96.       return 1;
  97.     }
  98.   else if (have_state == 0)
  99.     {
  100.       /* No "State:" line, assume thread is alive.  */
  101.       return 0;
  102.     }
  103.   else
  104.     {
  105.       return (strstr (buffer, "Z (") != NULL
  106.               || strstr (buffer, "X (") != NULL);
  107.     }
  108. }

  109. /* Return non-zero if 'State' of /proc/PID/status contains STATE.  If
  110.    WARN, warn on failure to open the /proc file.  */

  111. static int
  112. linux_proc_pid_has_state (pid_t pid, const char *state, int warn)
  113. {
  114.   char buffer[100];
  115.   int have_state;

  116.   have_state = linux_proc_pid_get_state (pid, buffer, sizeof buffer, warn);
  117.   return (have_state > 0 && strstr (buffer, state) != NULL);
  118. }

  119. /* Detect `T (stopped)' in `/proc/PID/status'.
  120.    Other states including `T (tracing stop)' are reported as false.  */

  121. int
  122. linux_proc_pid_is_stopped (pid_t pid)
  123. {
  124.   return linux_proc_pid_has_state (pid, "T (stopped)", 1);
  125. }

  126. /* Return non-zero if PID is a zombie.  If WARN, warn on failure to
  127.    open the /proc file.  */

  128. static int
  129. linux_proc_pid_is_zombie_maybe_warn (pid_t pid, int warn)
  130. {
  131.   return linux_proc_pid_has_state (pid, "Z (zombie)", warn);
  132. }

  133. /* See linux-procfs.h declaration.  */

  134. int
  135. linux_proc_pid_is_zombie_nowarn (pid_t pid)
  136. {
  137.   return linux_proc_pid_is_zombie_maybe_warn (pid, 0);
  138. }

  139. /* See linux-procfs.h declaration.  */

  140. int
  141. linux_proc_pid_is_zombie (pid_t pid)
  142. {
  143.   return linux_proc_pid_is_zombie_maybe_warn (pid, 1);
  144. }

  145. /* See linux-procfs.h declaration.  */

  146. char *
  147. linux_proc_pid_get_ns (pid_t pid, const char *ns)
  148. {
  149.   char buf[100];
  150.   char nsval[64];
  151.   int ret;
  152.   xsnprintf (buf, sizeof (buf), "/proc/%d/ns/%s", (int) pid, ns);
  153.   ret = readlink (buf, nsval, sizeof (nsval));
  154.   if (0 < ret && ret < sizeof (nsval))
  155.     {
  156.       nsval[ret] = '\0';
  157.       return xstrdup (nsval);
  158.     }

  159.   return NULL;
  160. }

  161. /* See linux-procfs.h.  */

  162. void
  163. linux_proc_attach_tgid_threads (pid_t pid,
  164.                                 linux_proc_attach_lwp_func attach_lwp)
  165. {
  166.   DIR *dir;
  167.   char pathname[128];
  168.   int new_threads_found;
  169.   int iterations;

  170.   if (linux_proc_get_tgid (pid) != pid)
  171.     return;

  172.   xsnprintf (pathname, sizeof (pathname), "/proc/%ld/task", (long) pid);
  173.   dir = opendir (pathname);
  174.   if (dir == NULL)
  175.     {
  176.       warning (_("Could not open /proc/%ld/task.\n"), (long) pid);
  177.       return;
  178.     }

  179.   /* Scan the task list for existing threads.  While we go through the
  180.      threads, new threads may be spawned.  Cycle through the list of
  181.      threads until we have done two iterations without finding new
  182.      threads.  */
  183.   for (iterations = 0; iterations < 2; iterations++)
  184.     {
  185.       struct dirent *dp;

  186.       new_threads_found = 0;
  187.       while ((dp = readdir (dir)) != NULL)
  188.         {
  189.           unsigned long lwp;

  190.           /* Fetch one lwp.  */
  191.           lwp = strtoul (dp->d_name, NULL, 10);
  192.           if (lwp != 0)
  193.             {
  194.               ptid_t ptid = ptid_build (pid, lwp, 0);

  195.               if (attach_lwp (ptid))
  196.                 new_threads_found = 1;
  197.             }
  198.         }

  199.       if (new_threads_found)
  200.         {
  201.           /* Start over.  */
  202.           iterations = -1;
  203.         }

  204.       rewinddir (dir);
  205.     }

  206.   closedir (dir);
  207. }