remove gdb_string.h
[deliverable/binutils-gdb.git] / gdb / common / linux-procfs.c
CommitLineData
13da1c97 1/* Linux-specific PROCFS manipulation routines.
28e7fd62 2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
13da1c97
LM
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19#ifdef GDBSERVER
20#include "server.h"
21#else
22#include "defs.h"
0e9f083f 23#include <string.h>
13da1c97
LM
24#endif
25
26#include "linux-procfs.h"
614c279d 27#include "filestuff.h"
13da1c97
LM
28
29/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
30 found. */
31
87b0bb13
JK
32static int
33linux_proc_get_int (pid_t lwpid, const char *field)
13da1c97 34{
87b0bb13 35 size_t field_len = strlen (field);
13da1c97
LM
36 FILE *status_file;
37 char buf[100];
87b0bb13 38 int retval = -1;
13da1c97
LM
39
40 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid);
614c279d 41 status_file = gdb_fopen_cloexec (buf, "r");
87b0bb13 42 if (status_file == NULL)
13da1c97 43 {
87b0bb13
JK
44 warning (_("unable to open /proc file '%s'"), buf);
45 return -1;
13da1c97
LM
46 }
47
87b0bb13
JK
48 while (fgets (buf, sizeof (buf), status_file))
49 if (strncmp (buf, field, field_len) == 0 && buf[field_len] == ':')
50 {
51 retval = strtol (&buf[field_len + 1], NULL, 10);
52 break;
53 }
54
55 fclose (status_file);
56 return retval;
13da1c97 57}
644cebc9 58
87b0bb13
JK
59/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
60 found. */
644cebc9
PA
61
62int
87b0bb13 63linux_proc_get_tgid (pid_t lwpid)
644cebc9 64{
87b0bb13
JK
65 return linux_proc_get_int (lwpid, "Tgid");
66}
644cebc9 67
87b0bb13
JK
68/* See linux-procfs.h. */
69
70pid_t
71linux_proc_get_tracerpid (pid_t lwpid)
72{
73 return linux_proc_get_int (lwpid, "TracerPid");
644cebc9 74}
5f572dec 75
87b0bb13 76/* Return non-zero if 'State' of /proc/PID/status contains STATE. */
5f572dec 77
87b0bb13
JK
78static int
79linux_proc_pid_has_state (pid_t pid, const char *state)
5f572dec
JK
80{
81 char buffer[100];
82 FILE *procfile;
83 int retval;
84 int have_state;
85
86 xsnprintf (buffer, sizeof (buffer), "/proc/%d/status", (int) pid);
614c279d 87 procfile = gdb_fopen_cloexec (buffer, "r");
5f572dec
JK
88 if (procfile == NULL)
89 {
90 warning (_("unable to open /proc file '%s'"), buffer);
91 return 0;
92 }
93
94 have_state = 0;
95 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
96 if (strncmp (buffer, "State:", 6) == 0)
97 {
98 have_state = 1;
99 break;
100 }
87b0bb13 101 retval = (have_state && strstr (buffer, state) != NULL);
5f572dec
JK
102 fclose (procfile);
103 return retval;
104}
87b0bb13
JK
105
106/* Detect `T (stopped)' in `/proc/PID/status'.
107 Other states including `T (tracing stop)' are reported as false. */
108
109int
110linux_proc_pid_is_stopped (pid_t pid)
111{
112 return linux_proc_pid_has_state (pid, "T (stopped)");
113}
114
115/* See linux-procfs.h declaration. */
116
117int
118linux_proc_pid_is_zombie (pid_t pid)
119{
120 return linux_proc_pid_has_state (pid, "Z (zombie)");
121}
This page took 0.189861 seconds and 4 git commands to generate.