2012-02-27 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / common / linux-procfs.c
CommitLineData
13da1c97 1/* Linux-specific PROCFS manipulation routines.
0b302171 2 Copyright (C) 2009-2012 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"
23#include "gdb_string.h"
24#endif
25
26#include "linux-procfs.h"
27
28/* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
29 found. */
30
31int
32linux_proc_get_tgid (int lwpid)
33{
34 FILE *status_file;
35 char buf[100];
36 int tgid = -1;
37
38 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid);
39 status_file = fopen (buf, "r");
40 if (status_file != NULL)
41 {
42 while (fgets (buf, sizeof (buf), status_file))
43 {
44 if (strncmp (buf, "Tgid:", 5) == 0)
45 {
46 tgid = strtoul (buf + strlen ("Tgid:"), NULL, 10);
47 break;
48 }
49 }
50
51 fclose (status_file);
52 }
53
54 return tgid;
55}
644cebc9
PA
56
57/* Detect `T (stopped)' in `/proc/PID/status'.
58 Other states including `T (tracing stop)' are reported as false. */
59
60int
61linux_proc_pid_is_stopped (pid_t pid)
62{
63 FILE *status_file;
64 char buf[100];
65 int retval = 0;
66
67 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) pid);
68 status_file = fopen (buf, "r");
69 if (status_file != NULL)
70 {
71 int have_state = 0;
72
73 while (fgets (buf, sizeof (buf), status_file))
74 {
75 if (strncmp (buf, "State:", 6) == 0)
76 {
77 have_state = 1;
78 break;
79 }
80 }
81 if (have_state && strstr (buf, "T (stopped)") != NULL)
82 retval = 1;
83 fclose (status_file);
84 }
85 return retval;
86}
This page took 0.059283 seconds and 4 git commands to generate.