mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-04 15:54:25 +08:00
gdbsupport, gdb: add read_text_file_to_string, use it in linux_common_core_of_thread
I would like to add more code to nat/linux-osdata.c that reads an entire file from /proc or /sys and processes it as a string afterwards. I would like to avoid duplicating the somewhat error-prone code that reads an entire file to a buffer. I think we should have a utility function that does that. Add read_file_to_string to gdbsupport/filestuff.{c,h}, and make linux_common_core_of_thread use it. I want to make the new function return an std::string, and because strtok doesn't play well with std::string (it requires a `char *`, std::string::c_str returns a `const char *`), change linux_common_core_of_thread to use std::string methods instead. Approved-By: Tom Tromey <tom@tromey.com> Change-Id: I1793fda72a82969c28b944a84acb953f74c9230a
This commit is contained in:
parent
7dacb40b89
commit
7a283d9cf5
@ -62,49 +62,39 @@ int
|
|||||||
linux_common_core_of_thread (ptid_t ptid)
|
linux_common_core_of_thread (ptid_t ptid)
|
||||||
{
|
{
|
||||||
char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN];
|
char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN];
|
||||||
char *content = NULL;
|
|
||||||
char *p;
|
|
||||||
char *ts = 0;
|
|
||||||
int content_read = 0;
|
|
||||||
int i;
|
|
||||||
int core;
|
int core;
|
||||||
|
|
||||||
sprintf (filename, "/proc/%lld/task/%lld/stat",
|
sprintf (filename, "/proc/%lld/task/%lld/stat",
|
||||||
(PID_T) ptid.pid (), (PID_T) ptid.lwp ());
|
(PID_T) ptid.pid (), (PID_T) ptid.lwp ());
|
||||||
gdb_file_up f = gdb_fopen_cloexec (filename, "r");
|
|
||||||
if (!f)
|
gdb::optional<std::string> content = read_text_file_to_string (filename);
|
||||||
|
if (!content.has_value ())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
int n;
|
|
||||||
content = (char *) xrealloc (content, content_read + 1024);
|
|
||||||
n = fread (content + content_read, 1, 1024, f.get ());
|
|
||||||
content_read += n;
|
|
||||||
if (n < 1024)
|
|
||||||
{
|
|
||||||
content[content_read] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ps command also relies on no trailing fields ever contain ')'. */
|
/* ps command also relies on no trailing fields ever contain ')'. */
|
||||||
p = strrchr (content, ')');
|
std::string::size_type pos = content->find_last_of (')');
|
||||||
if (p != NULL)
|
if (pos == std::string::npos)
|
||||||
p++;
|
return -1;
|
||||||
|
|
||||||
/* If the first field after program name has index 0, then core number is
|
/* If the first field after program name has index 0, then core number is
|
||||||
the field with index 36. There's no constant for that anywhere. */
|
the field with index 36 (so, the 37th). There's no constant for that
|
||||||
if (p != NULL)
|
anywhere. */
|
||||||
p = strtok_r (p, " ", &ts);
|
for (int i = 0; i < 37; ++i)
|
||||||
for (i = 0; p != NULL && i != 36; ++i)
|
{
|
||||||
p = strtok_r (NULL, " ", &ts);
|
/* Find separator. */
|
||||||
|
pos = content->find_first_of (' ', pos);
|
||||||
|
if (pos == std::string::npos)
|
||||||
|
return {};
|
||||||
|
|
||||||
if (p == NULL || sscanf (p, "%d", &core) == 0)
|
/* Find beginning of field. */
|
||||||
|
pos = content->find_first_not_of (' ', pos);
|
||||||
|
if (pos == std::string::npos)
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sscanf (&(*content)[pos], "%d", &core) == 0)
|
||||||
core = -1;
|
core = -1;
|
||||||
|
|
||||||
xfree (content);
|
|
||||||
|
|
||||||
return core;
|
return core;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -501,3 +501,40 @@ mkdir_recursive (const char *dir)
|
|||||||
component_start = component_end;
|
component_start = component_end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* See gdbsupport/filestuff.h. */
|
||||||
|
|
||||||
|
gdb::optional<std::string>
|
||||||
|
read_text_file_to_string (const char *path)
|
||||||
|
{
|
||||||
|
gdb_file_up file = gdb_fopen_cloexec (path, "r");
|
||||||
|
if (file == nullptr)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
std::string res;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
std::string::size_type start_size = res.size ();
|
||||||
|
constexpr int chunk_size = 1024;
|
||||||
|
|
||||||
|
/* Resize to accomodate CHUNK_SIZE bytes. */
|
||||||
|
res.resize (start_size + chunk_size);
|
||||||
|
|
||||||
|
int n = fread (&res[start_size], 1, chunk_size, file.get ());
|
||||||
|
if (n == chunk_size)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
gdb_assert (n < chunk_size);
|
||||||
|
|
||||||
|
/* Less than CHUNK means EOF or error. If it's an error, return
|
||||||
|
no value. */
|
||||||
|
if (ferror (file.get ()))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
/* Resize the string according to the data we read. */
|
||||||
|
res.resize (start_size + n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
@ -129,4 +129,8 @@ extern bool is_regular_file (const char *name, int *errno_ptr);
|
|||||||
|
|
||||||
extern bool mkdir_recursive (const char *dir);
|
extern bool mkdir_recursive (const char *dir);
|
||||||
|
|
||||||
|
/* Read the entire content of file PATH into an std::string. */
|
||||||
|
|
||||||
|
extern gdb::optional<std::string> read_text_file_to_string (const char *path);
|
||||||
|
|
||||||
#endif /* COMMON_FILESTUFF_H */
|
#endif /* COMMON_FILESTUFF_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user