From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id E07903858C62; Tue, 8 Nov 2022 21:52:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E07903858C62 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667944363; bh=oD0oUN6zGkRni9NXUnDDhAJrfpMIxq0m6OZIgqdqHuw=; h=From:To:Subject:Date:From; b=f3+XjeQuumiWzMkwlEOpUnsHllLxu4EWBRRQTjKemgakbRJZhWo2zcbjBS2x3tYP6 uIuDiOSqqLk2x1Uz75glSR1teeR4cqDr5Gc8AEj+Gw3N/xNm1ywVZ3BdRAgINVRVt6 qYBCsULoQVrNkxNoFL0HXcLnuU/TqLXCApPRkgGo= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Simon Marchi To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdbsupport, gdb: add read_text_file_to_string, use it in linux_common_core_of_thread X-Act-Checkin: binutils-gdb X-Git-Author: Simon Marchi X-Git-Refname: refs/heads/master X-Git-Oldrev: 7dacb40b89b19382bb2597c5e26f347985d4edb5 X-Git-Newrev: 7a283d9cf5ccf26321f33812e79cf1515288ac94 Message-Id: <20221108215243.E07903858C62@sourceware.org> Date: Tue, 8 Nov 2022 21:52:43 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D7a283d9cf5cc= f26321f33812e79cf1515288ac94 commit 7a283d9cf5ccf26321f33812e79cf1515288ac94 Author: Simon Marchi Date: Fri Nov 4 09:39:12 2022 -0400 gdbsupport, gdb: add read_text_file_to_string, use it in linux_common_c= ore_of_thread =20 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. =20 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. =20 Approved-By: Tom Tromey Change-Id: I1793fda72a82969c28b944a84acb953f74c9230a Diff: --- gdb/nat/linux-osdata.c | 52 ++++++++++++++++++++-------------------------= ---- gdbsupport/filestuff.cc | 37 +++++++++++++++++++++++++++++++++++ gdbsupport/filestuff.h | 4 ++++ 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 3c31ee6fe50..f9c43f6691e 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -62,49 +62,39 @@ int linux_common_core_of_thread (ptid_t ptid) { char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN]; - char *content =3D NULL; - char *p; - char *ts =3D 0; - int content_read =3D 0; - int i; int core; =20 sprintf (filename, "/proc/%lld/task/%lld/stat", (PID_T) ptid.pid (), (PID_T) ptid.lwp ()); - gdb_file_up f =3D gdb_fopen_cloexec (filename, "r"); - if (!f) - return -1; =20 - for (;;) - { - int n; - content =3D (char *) xrealloc (content, content_read + 1024); - n =3D fread (content + content_read, 1, 1024, f.get ()); - content_read +=3D n; - if (n < 1024) - { - content[content_read] =3D '\0'; - break; - } - } + gdb::optional content =3D read_text_file_to_string (filenam= e); + if (!content.has_value ()) + return -1; =20 /* ps command also relies on no trailing fields ever contain ')'. */ - p =3D strrchr (content, ')'); - if (p !=3D NULL) - p++; + std::string::size_type pos =3D content->find_last_of (')'); + if (pos =3D=3D std::string::npos) + return -1; =20 /* 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. */ - if (p !=3D NULL) - p =3D strtok_r (p, " ", &ts); - for (i =3D 0; p !=3D NULL && i !=3D 36; ++i) - p =3D strtok_r (NULL, " ", &ts); + the field with index 36 (so, the 37th). There's no constant for that + anywhere. */ + for (int i =3D 0; i < 37; ++i) + { + /* Find separator. */ + pos =3D content->find_first_of (' ', pos); + if (pos =3D=3D std::string::npos) + return {}; + + /* Find beginning of field. */ + pos =3D content->find_first_not_of (' ', pos); + if (pos =3D=3D std::string::npos) + return {}; + } =20 - if (p =3D=3D NULL || sscanf (p, "%d", &core) =3D=3D 0) + if (sscanf (&(*content)[pos], "%d", &core) =3D=3D 0) core =3D -1; =20 - xfree (content); - return core; } =20 diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc index 2dfae5a48c5..cf5fb13bd0c 100644 --- a/gdbsupport/filestuff.cc +++ b/gdbsupport/filestuff.cc @@ -501,3 +501,40 @@ mkdir_recursive (const char *dir) component_start =3D component_end; } } + +/* See gdbsupport/filestuff.h. */ + +gdb::optional +read_text_file_to_string (const char *path) +{ + gdb_file_up file =3D gdb_fopen_cloexec (path, "r"); + if (file =3D=3D nullptr) + return {}; + + std::string res; + for (;;) + { + std::string::size_type start_size =3D res.size (); + constexpr int chunk_size =3D 1024; + + /* Resize to accomodate CHUNK_SIZE bytes. */ + res.resize (start_size + chunk_size); + + int n =3D fread (&res[start_size], 1, chunk_size, file.get ()); + if (n =3D=3D 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; +} diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h index 4bc9249dcbf..33362901ab8 100644 --- a/gdbsupport/filestuff.h +++ b/gdbsupport/filestuff.h @@ -129,4 +129,8 @@ extern bool is_regular_file (const char *name, int *err= no_ptr); =20 extern bool mkdir_recursive (const char *dir); =20 +/* Read the entire content of file PATH into an std::string. */ + +extern gdb::optional read_text_file_to_string (const char *pa= th); + #endif /* COMMON_FILESTUFF_H */