1 /* Linux-specific PROCFS manipulation routines.
2 Copyright (C) 2009-2016 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"
25 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
29 linux_proc_get_int (pid_t lwpid
, const char *field
, int warn
)
31 size_t field_len
= strlen (field
);
36 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) lwpid
);
37 status_file
= gdb_fopen_cloexec (buf
, "r");
38 if (status_file
== NULL
)
41 warning (_("unable to open /proc file '%s'"), buf
);
45 while (fgets (buf
, sizeof (buf
), status_file
))
46 if (strncmp (buf
, field
, field_len
) == 0 && buf
[field_len
] == ':')
48 retval
= strtol (&buf
[field_len
+ 1], NULL
, 10);
56 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
60 linux_proc_get_tgid (pid_t lwpid
)
62 return linux_proc_get_int (lwpid
, "Tgid", 1);
65 /* See linux-procfs.h. */
68 linux_proc_get_tracerpid_nowarn (pid_t lwpid
)
70 return linux_proc_get_int (lwpid
, "TracerPid", 0);
73 /* Process states as discovered in the 'State' line of
74 /proc/PID/status. Not all possible states are represented here,
75 only those that we care about. */
79 /* Some state we don't handle. */
82 /* Stopped on a signal. */
86 PROC_STATE_TRACING_STOP
,
95 /* Parse a PROC_STATE out of STATE, a buffer with the state found in
96 the 'State:' line of /proc/PID/status. */
98 static enum proc_state
99 parse_proc_status_state (const char *state
)
101 state
= skip_spaces_const (state
);
106 return PROC_STATE_TRACING_STOP
;
108 /* Before Linux 2.6.33, tracing stop used uppercase T. */
109 if (strcmp (state
, "T (tracing stop)") == 0)
110 return PROC_STATE_TRACING_STOP
;
112 return PROC_STATE_STOPPED
;
114 return PROC_STATE_DEAD
;
116 return PROC_STATE_ZOMBIE
;
119 return PROC_STATE_UNKNOWN
;
123 /* Fill in STATE, a buffer with BUFFER_SIZE bytes with the 'State'
124 line of /proc/PID/status. Returns -1 on failure to open the /proc
125 file, 1 if the line is found, and 0 if not found. If WARN, warn on
126 failure to open the /proc file. */
129 linux_proc_pid_get_state (pid_t pid
, int warn
, enum proc_state
*state
)
135 xsnprintf (buffer
, sizeof (buffer
), "/proc/%d/status", (int) pid
);
136 procfile
= gdb_fopen_cloexec (buffer
, "r");
137 if (procfile
== NULL
)
140 warning (_("unable to open /proc file '%s'"), buffer
);
145 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
146 if (startswith (buffer
, "State:"))
149 *state
= parse_proc_status_state (buffer
+ sizeof ("State:") - 1);
156 /* See linux-procfs.h declaration. */
159 linux_proc_pid_is_gone (pid_t pid
)
162 enum proc_state state
;
164 have_state
= linux_proc_pid_get_state (pid
, 0, &state
);
167 /* If we can't open the status file, assume the thread has
171 else if (have_state
== 0)
173 /* No "State:" line, assume thread is alive. */
177 return (state
== PROC_STATE_ZOMBIE
|| state
== PROC_STATE_DEAD
);
180 /* Return non-zero if 'State' of /proc/PID/status contains STATE. If
181 WARN, warn on failure to open the /proc file. */
184 linux_proc_pid_has_state (pid_t pid
, enum proc_state state
, int warn
)
187 enum proc_state cur_state
;
189 have_state
= linux_proc_pid_get_state (pid
, warn
, &cur_state
);
190 return (have_state
> 0 && cur_state
== state
);
193 /* Detect `T (stopped)' in `/proc/PID/status'.
194 Other states including `T (tracing stop)' are reported as false. */
197 linux_proc_pid_is_stopped (pid_t pid
)
199 return linux_proc_pid_has_state (pid
, PROC_STATE_STOPPED
, 1);
202 /* Detect `t (tracing stop)' in `/proc/PID/status'.
203 Other states including `T (stopped)' are reported as false. */
206 linux_proc_pid_is_trace_stopped_nowarn (pid_t pid
)
208 return linux_proc_pid_has_state (pid
, PROC_STATE_TRACING_STOP
, 1);
211 /* Return non-zero if PID is a zombie. If WARN, warn on failure to
212 open the /proc file. */
215 linux_proc_pid_is_zombie_maybe_warn (pid_t pid
, int warn
)
217 return linux_proc_pid_has_state (pid
, PROC_STATE_ZOMBIE
, warn
);
220 /* See linux-procfs.h declaration. */
223 linux_proc_pid_is_zombie_nowarn (pid_t pid
)
225 return linux_proc_pid_is_zombie_maybe_warn (pid
, 0);
228 /* See linux-procfs.h declaration. */
231 linux_proc_pid_is_zombie (pid_t pid
)
233 return linux_proc_pid_is_zombie_maybe_warn (pid
, 1);
236 /* See linux-procfs.h. */
239 linux_proc_tid_get_name (ptid_t ptid
)
241 #define TASK_COMM_LEN 16 /* As defined in the kernel's sched.h. */
243 static char comm_buf
[TASK_COMM_LEN
];
246 const char *comm_val
;
247 pid_t pid
= ptid_get_pid (ptid
);
248 pid_t tid
= ptid_lwp_p (ptid
) ? ptid_get_lwp (ptid
) : ptid_get_pid (ptid
);
250 xsnprintf (comm_path
, sizeof (comm_path
),
251 "/proc/%ld/task/%ld/comm", (long) pid
, (long) tid
);
253 comm_file
= gdb_fopen_cloexec (comm_path
, "r");
254 if (comm_file
== NULL
)
257 comm_val
= fgets (comm_buf
, sizeof (comm_buf
), comm_file
);
260 if (comm_val
!= NULL
)
264 /* Make sure there is no newline at the end. */
265 for (i
= 0; i
< sizeof (comm_buf
); i
++)
267 if (comm_buf
[i
] == '\n')
278 /* See linux-procfs.h. */
281 linux_proc_attach_tgid_threads (pid_t pid
,
282 linux_proc_attach_lwp_func attach_lwp
)
286 int new_threads_found
;
289 if (linux_proc_get_tgid (pid
) != pid
)
292 xsnprintf (pathname
, sizeof (pathname
), "/proc/%ld/task", (long) pid
);
293 dir
= opendir (pathname
);
296 warning (_("Could not open /proc/%ld/task."), (long) pid
);
300 /* Scan the task list for existing threads. While we go through the
301 threads, new threads may be spawned. Cycle through the list of
302 threads until we have done two iterations without finding new
304 for (iterations
= 0; iterations
< 2; iterations
++)
308 new_threads_found
= 0;
309 while ((dp
= readdir (dir
)) != NULL
)
314 lwp
= strtoul (dp
->d_name
, NULL
, 10);
317 ptid_t ptid
= ptid_build (pid
, lwp
, 0);
319 if (attach_lwp (ptid
))
320 new_threads_found
= 1;
324 if (new_threads_found
)
336 /* See linux-procfs.h. */
339 linux_proc_task_list_dir_exists (pid_t pid
)
344 xsnprintf (pathname
, sizeof (pathname
), "/proc/%ld/task", (long) pid
);
345 return (stat (pathname
, &buf
) == 0);
348 /* See linux-procfs.h. */
351 linux_proc_pid_to_exec_file (int pid
)
353 static char buf
[PATH_MAX
];
357 xsnprintf (name
, PATH_MAX
, "/proc/%d/exe", pid
);
358 len
= readlink (name
, buf
, PATH_MAX
- 1);