1 /* Linux-specific PROCFS manipulation routines.
2 Copyright (C) 2009-2022 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 "gdbsupport/common-defs.h"
20 #include "linux-procfs.h"
21 #include "gdbsupport/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
);
35 snprintf (buf
, sizeof (buf
), "/proc/%d/status", (int) lwpid
);
36 gdb_file_up status_file
= gdb_fopen_cloexec (buf
, "r");
37 if (status_file
== NULL
)
40 warning (_("unable to open /proc file '%s'"), buf
);
44 while (fgets (buf
, sizeof (buf
), status_file
.get ()))
45 if (strncmp (buf
, field
, field_len
) == 0 && buf
[field_len
] == ':')
47 retval
= strtol (&buf
[field_len
+ 1], NULL
, 10);
54 /* Return the TGID of LWPID from /proc/pid/status. Returns -1 if not
58 linux_proc_get_tgid (pid_t lwpid
)
60 return linux_proc_get_int (lwpid
, "Tgid", 1);
63 /* See linux-procfs.h. */
66 linux_proc_get_tracerpid_nowarn (pid_t lwpid
)
68 return linux_proc_get_int (lwpid
, "TracerPid", 0);
71 /* Process states as discovered in the 'State' line of
72 /proc/PID/status. Not all possible states are represented here,
73 only those that we care about. */
77 /* Some state we don't handle. */
80 /* Stopped on a signal. */
84 PROC_STATE_TRACING_STOP
,
93 /* Parse a PROC_STATE out of STATE, a buffer with the state found in
94 the 'State:' line of /proc/PID/status. */
96 static enum proc_state
97 parse_proc_status_state (const char *state
)
99 state
= skip_spaces (state
);
104 return PROC_STATE_TRACING_STOP
;
106 /* Before Linux 2.6.33, tracing stop used uppercase T. */
107 if (strcmp (state
, "T (stopped)\n") == 0)
108 return PROC_STATE_STOPPED
;
109 else /* "T (tracing stop)\n" */
110 return PROC_STATE_TRACING_STOP
;
112 return PROC_STATE_DEAD
;
114 return PROC_STATE_ZOMBIE
;
117 return PROC_STATE_UNKNOWN
;
121 /* Fill in STATE, a buffer with BUFFER_SIZE bytes with the 'State'
122 line of /proc/PID/status. Returns -1 on failure to open the /proc
123 file, 1 if the line is found, and 0 if not found. If WARN, warn on
124 failure to open the /proc file. */
127 linux_proc_pid_get_state (pid_t pid
, int warn
, enum proc_state
*state
)
132 xsnprintf (buffer
, sizeof (buffer
), "/proc/%d/status", (int) pid
);
133 gdb_file_up procfile
= gdb_fopen_cloexec (buffer
, "r");
134 if (procfile
== NULL
)
137 warning (_("unable to open /proc file '%s'"), buffer
);
142 while (fgets (buffer
, sizeof (buffer
), procfile
.get ()) != NULL
)
143 if (startswith (buffer
, "State:"))
146 *state
= parse_proc_status_state (buffer
+ sizeof ("State:") - 1);
152 /* See linux-procfs.h declaration. */
155 linux_proc_pid_is_gone (pid_t pid
)
158 enum proc_state state
;
160 have_state
= linux_proc_pid_get_state (pid
, 0, &state
);
163 /* If we can't open the status file, assume the thread has
167 else if (have_state
== 0)
169 /* No "State:" line, assume thread is alive. */
173 return (state
== PROC_STATE_ZOMBIE
|| state
== PROC_STATE_DEAD
);
176 /* Return non-zero if 'State' of /proc/PID/status contains STATE. If
177 WARN, warn on failure to open the /proc file. */
180 linux_proc_pid_has_state (pid_t pid
, enum proc_state state
, int warn
)
183 enum proc_state cur_state
;
185 have_state
= linux_proc_pid_get_state (pid
, warn
, &cur_state
);
186 return (have_state
> 0 && cur_state
== state
);
189 /* Detect `T (stopped)' in `/proc/PID/status'.
190 Other states including `T (tracing stop)' are reported as false. */
193 linux_proc_pid_is_stopped (pid_t pid
)
195 return linux_proc_pid_has_state (pid
, PROC_STATE_STOPPED
, 1);
198 /* Detect `t (tracing stop)' in `/proc/PID/status'.
199 Other states including `T (stopped)' are reported as false. */
202 linux_proc_pid_is_trace_stopped_nowarn (pid_t pid
)
204 return linux_proc_pid_has_state (pid
, PROC_STATE_TRACING_STOP
, 1);
207 /* Return non-zero if PID is a zombie. If WARN, warn on failure to
208 open the /proc file. */
211 linux_proc_pid_is_zombie_maybe_warn (pid_t pid
, int warn
)
213 return linux_proc_pid_has_state (pid
, PROC_STATE_ZOMBIE
, warn
);
216 /* See linux-procfs.h declaration. */
219 linux_proc_pid_is_zombie_nowarn (pid_t pid
)
221 return linux_proc_pid_is_zombie_maybe_warn (pid
, 0);
224 /* See linux-procfs.h declaration. */
227 linux_proc_pid_is_zombie (pid_t pid
)
229 return linux_proc_pid_is_zombie_maybe_warn (pid
, 1);
232 /* See linux-procfs.h. */
235 linux_proc_tid_get_name (ptid_t ptid
)
237 #define TASK_COMM_LEN 16 /* As defined in the kernel's sched.h. */
239 static char comm_buf
[TASK_COMM_LEN
];
241 const char *comm_val
;
242 pid_t pid
= ptid
.pid ();
243 pid_t tid
= ptid
.lwp_p () ? ptid
.lwp () : ptid
.pid ();
245 xsnprintf (comm_path
, sizeof (comm_path
),
246 "/proc/%ld/task/%ld/comm", (long) pid
, (long) tid
);
248 gdb_file_up comm_file
= gdb_fopen_cloexec (comm_path
, "r");
249 if (comm_file
== NULL
)
252 comm_val
= fgets (comm_buf
, sizeof (comm_buf
), comm_file
.get ());
254 if (comm_val
!= NULL
)
258 /* Make sure there is no newline at the end. */
259 for (i
= 0; i
< sizeof (comm_buf
); i
++)
261 if (comm_buf
[i
] == '\n')
272 /* See linux-procfs.h. */
275 linux_proc_attach_tgid_threads (pid_t pid
,
276 linux_proc_attach_lwp_func attach_lwp
)
280 int new_threads_found
;
283 if (linux_proc_get_tgid (pid
) != pid
)
286 xsnprintf (pathname
, sizeof (pathname
), "/proc/%ld/task", (long) pid
);
287 dir
= opendir (pathname
);
290 warning (_("Could not open /proc/%ld/task."), (long) pid
);
294 /* Scan the task list for existing threads. While we go through the
295 threads, new threads may be spawned. Cycle through the list of
296 threads until we have done two iterations without finding new
298 for (iterations
= 0; iterations
< 2; iterations
++)
302 new_threads_found
= 0;
303 while ((dp
= readdir (dir
)) != NULL
)
308 lwp
= strtoul (dp
->d_name
, NULL
, 10);
311 ptid_t ptid
= ptid_t (pid
, lwp
);
313 if (attach_lwp (ptid
))
314 new_threads_found
= 1;
318 if (new_threads_found
)
330 /* See linux-procfs.h. */
333 linux_proc_task_list_dir_exists (pid_t pid
)
338 xsnprintf (pathname
, sizeof (pathname
), "/proc/%ld/task", (long) pid
);
339 return (stat (pathname
, &buf
) == 0);
342 /* See linux-procfs.h. */
345 linux_proc_pid_to_exec_file (int pid
)
347 static char buf
[PATH_MAX
];
351 xsnprintf (name
, PATH_MAX
, "/proc/%d/exe", pid
);
352 len
= readlink (name
, buf
, PATH_MAX
- 1);
361 /* See linux-procfs.h. */
364 linux_proc_init_warnings ()
366 static bool warned
= false;
374 if (stat ("/proc/self", &st
) != 0)
375 warning (_("/proc is not accessible."));