1 /* Linux-specific PROCFS manipulation routines.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "common-defs.h"
20 #include "linux-procfs.h"
21 #include "filestuff.h"
23 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
27 linux_proc_get_int (pid_t lwpid
, const char *field
)
29 size_t field_len
= strlen (field
);
34 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) lwpid
);
35 status_file
= gdb_fopen_cloexec (buf
, "r");
36 if (status_file
== NULL
)
38 warning (_("unable to open /proc file '%s'"), buf
);
42 while (fgets (buf
, sizeof (buf
), status_file
))
43 if (strncmp (buf
, field
, field_len
) == 0 && buf
[field_len
] == ':')
45 retval
= strtol (&buf
[field_len
+ 1], NULL
, 10);
53 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
57 linux_proc_get_tgid (pid_t lwpid
)
59 return linux_proc_get_int (lwpid
, "Tgid");
62 /* See linux-procfs.h. */
65 linux_proc_get_tracerpid (pid_t lwpid
)
67 return linux_proc_get_int (lwpid
, "TracerPid");
70 /* Return non-zero if 'State' of /proc/PID/status contains STATE. */
73 linux_proc_pid_has_state (pid_t pid
, const char *state
)
80 xsnprintf (buffer
, sizeof (buffer
), "/proc/%d/status", (int) pid
);
81 procfile
= gdb_fopen_cloexec (buffer
, "r");
84 warning (_("unable to open /proc file '%s'"), buffer
);
89 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
90 if (strncmp (buffer
, "State:", 6) == 0)
95 retval
= (have_state
&& strstr (buffer
, state
) != NULL
);
100 /* Detect `T (stopped)' in `/proc/PID/status'.
101 Other states including `T (tracing stop)' are reported as false. */
104 linux_proc_pid_is_stopped (pid_t pid
)
106 return linux_proc_pid_has_state (pid
, "T (stopped)");
109 /* See linux-procfs.h declaration. */
112 linux_proc_pid_is_zombie (pid_t pid
)
114 return linux_proc_pid_has_state (pid
, "Z (zombie)");
This page took 0.033607 seconds and 5 git commands to generate.