diff options
author | Thiago Jung Bauermann <thiago.bauermann@linaro.org> | 2024-03-19 00:21:58 -0300 |
---|---|---|
committer | Thiago Jung Bauermann <thiago.bauermann@linaro.org> | 2024-04-29 23:14:06 -0300 |
commit | 16a447bec542b9217710d5e37f14b15a82b5273d (patch) | |
tree | 94a858ed57cf582197ec3d79f33fb0e3f27fd5b8 /gdb/nat/linux-procfs.c | |
parent | 3de4256ca3e80fe1ab9531585b9a0fee3920921d (diff) | |
download | binutils-16a447bec542b9217710d5e37f14b15a82b5273d.zip binutils-16a447bec542b9217710d5e37f14b15a82b5273d.tar.gz binutils-16a447bec542b9217710d5e37f14b15a82b5273d.tar.bz2 |
gdb/nat: Factor linux_proc_get_stat_field out of linux_common_core_of_thread
The new function will be used in a subsequent patch to read a different
stat field.
The new code is believed to be equivalent to the old code, so there
should be no change in GDB behaviour. The only material change was to
use std::string and string_printf rather than a fixed char array to
build the path to the stat file.
Also, take the opportunity to move the function's documentation comment to
the header file, to conform with GDB practice.
Reviewed-By: Luis Machado <luis.machado@arm.com>
Approved-By: Pedro Alves <pedro@palves.net>
Diffstat (limited to 'gdb/nat/linux-procfs.c')
-rw-r--r-- | gdb/nat/linux-procfs.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c index e208695..081a82b 100644 --- a/gdb/nat/linux-procfs.c +++ b/gdb/nat/linux-procfs.c @@ -230,6 +230,50 @@ linux_proc_pid_is_zombie (pid_t pid) /* See linux-procfs.h. */ +std::optional<std::string> +linux_proc_get_stat_field (ptid_t ptid, int field) +{ + /* We never need to read PID from the stat file, and there's + command_from_pid to read the comm field. */ + gdb_assert (field >= LINUX_PROC_STAT_STATE); + + std::string filename = string_printf ("/proc/%ld/task/%ld/stat", + (long) ptid.pid (), (long) ptid.lwp ()); + + std::optional<std::string> content + = read_text_file_to_string (filename.c_str ()); + if (!content.has_value ()) + return {}; + + /* ps command also relies on no trailing fields ever containing ')'. */ + std::string::size_type pos = content->find_last_of (')'); + if (pos == std::string::npos) + return {}; + + /* The first field after program name is LINUX_PROC_STAT_STATE. */ + for (int i = LINUX_PROC_STAT_STATE; i <= field; ++i) + { + /* Find separator. */ + pos = content->find_first_of (' ', pos); + if (pos == std::string::npos) + return {}; + + /* Find beginning of field. */ + pos = content->find_first_not_of (' ', pos); + if (pos == std::string::npos) + return {}; + } + + /* Find end of field. */ + std::string::size_type end_pos = content->find_first_of (' ', pos); + if (end_pos == std::string::npos) + return content->substr (pos); + else + return content->substr (pos, end_pos - pos); +} + +/* See linux-procfs.h. */ + const char * linux_proc_tid_get_name (ptid_t ptid) { |