OLD | NEW |
1 /* Linux-specific PROCFS manipulation routines. | 1 /* Linux-specific PROCFS manipulation routines. |
2 Copyright (C) 2009-2012 Free Software Foundation, Inc. | 2 Copyright (C) 2009-2012 Free Software Foundation, Inc. |
3 | 3 |
4 This file is part of GDB. | 4 This file is part of GDB. |
5 | 5 |
6 This program is free software; you can redistribute it and/or modify | 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 | 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 | 8 the Free Software Foundation; either version 3 of the License, or |
9 (at your option) any later version. | 9 (at your option) any later version. |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #else | 21 #else |
22 #include "defs.h" | 22 #include "defs.h" |
23 #include "gdb_string.h" | 23 #include "gdb_string.h" |
24 #endif | 24 #endif |
25 | 25 |
26 #include "linux-procfs.h" | 26 #include "linux-procfs.h" |
27 | 27 |
28 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not | 28 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not |
29 found. */ | 29 found. */ |
30 | 30 |
| 31 static int |
| 32 linux_proc_get_int (pid_t lwpid, const char *field) |
| 33 { |
| 34 size_t field_len = strlen (field); |
| 35 FILE *status_file; |
| 36 char buf[100]; |
| 37 int retval = -1; |
| 38 |
| 39 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid); |
| 40 status_file = fopen (buf, "r"); |
| 41 if (status_file == NULL) |
| 42 { |
| 43 warning (_("unable to open /proc file '%s'"), buf); |
| 44 return -1; |
| 45 } |
| 46 |
| 47 while (fgets (buf, sizeof (buf), status_file)) |
| 48 if (strncmp (buf, field, field_len) == 0 && buf[field_len] == ':') |
| 49 { |
| 50 retval = strtol (&buf[field_len + 1], NULL, 10); |
| 51 break; |
| 52 } |
| 53 |
| 54 fclose (status_file); |
| 55 return retval; |
| 56 } |
| 57 |
| 58 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not |
| 59 found. */ |
| 60 |
31 int | 61 int |
32 linux_proc_get_tgid (int lwpid) | 62 linux_proc_get_tgid (pid_t lwpid) |
33 { | 63 { |
34 FILE *status_file; | 64 return linux_proc_get_int (lwpid, "Tgid"); |
35 char buf[100]; | 65 } |
36 int tgid = -1; | |
37 | 66 |
38 snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid); | 67 /* See linux-procfs.h. */ |
39 status_file = fopen (buf, "r"); | 68 |
40 if (status_file != NULL) | 69 pid_t |
| 70 linux_proc_get_tracerpid (pid_t lwpid) |
| 71 { |
| 72 return linux_proc_get_int (lwpid, "TracerPid"); |
| 73 } |
| 74 |
| 75 /* Return non-zero if 'State' of /proc/PID/status contains STATE. */ |
| 76 |
| 77 static int |
| 78 linux_proc_pid_has_state (pid_t pid, const char *state) |
| 79 { |
| 80 char buffer[100]; |
| 81 FILE *procfile; |
| 82 int retval; |
| 83 int have_state; |
| 84 |
| 85 xsnprintf (buffer, sizeof (buffer), "/proc/%d/status", (int) pid); |
| 86 procfile = fopen (buffer, "r"); |
| 87 if (procfile == NULL) |
41 { | 88 { |
42 while (fgets (buf, sizeof (buf), status_file)) | 89 warning (_("unable to open /proc file '%s'"), buffer); |
43 » { | 90 return 0; |
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 } | 91 } |
53 | 92 |
54 return tgid; | 93 have_state = 0; |
| 94 while (fgets (buffer, sizeof (buffer), procfile) != NULL) |
| 95 if (strncmp (buffer, "State:", 6) == 0) |
| 96 { |
| 97 » have_state = 1; |
| 98 » break; |
| 99 } |
| 100 retval = (have_state && strstr (buffer, state) != NULL); |
| 101 fclose (procfile); |
| 102 return retval; |
55 } | 103 } |
| 104 |
| 105 /* Detect `T (stopped)' in `/proc/PID/status'. |
| 106 Other states including `T (tracing stop)' are reported as false. */ |
| 107 |
| 108 int |
| 109 linux_proc_pid_is_stopped (pid_t pid) |
| 110 { |
| 111 return linux_proc_pid_has_state (pid, "T (stopped)"); |
| 112 } |
| 113 |
| 114 /* See linux-procfs.h declaration. */ |
| 115 |
| 116 int |
| 117 linux_proc_pid_is_zombie (pid_t pid) |
| 118 { |
| 119 return linux_proc_pid_has_state (pid, "Z (zombie)"); |
| 120 } |
OLD | NEW |