From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1879) id DD9F03858437; Thu, 24 Feb 2022 12:29:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD9F03858437 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] gdb/linux-tdep: make read_mapping return a structure X-Act-Checkin: binutils-gdb X-Git-Author: Simon Marchi X-Git-Refname: refs/heads/master X-Git-Oldrev: 7190ae0d393d09c3c1f24add83d1e90727402749 X-Git-Newrev: 11659552768f6b915a6bf5aa98dfb11ba0f004d0 Message-Id: <20220224122929.DD9F03858437@sourceware.org> Date: Thu, 24 Feb 2022 12:29:29 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Feb 2022 12:29:30 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D11659552768f= 6b915a6bf5aa98dfb11ba0f004d0 commit 11659552768f6b915a6bf5aa98dfb11ba0f004d0 Author: Simon Marchi Date: Wed Feb 23 16:26:16 2022 -0500 gdb/linux-tdep: make read_mapping return a structure =20 Change read_mapping to return a structure instead of taking many output parameters. Change the string + length output parameters (permissions and device) to be gdb::string_view, since that's what string_view is for (a non-NULL terminated view on a string). No changes in behavior expected. =20 Change-Id: I86e627d84d3dda8c9b835592b0f4de8d90d12112 Diff: --- gdb/linux-tdep.c | 109 +++++++++++++++++++++++++++++----------------------= ---- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 060f60e753a..83bd4237286 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -441,42 +441,55 @@ linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_= t ptid) return normal_pid_to_str (ptid); } =20 +/* Data from one mapping from /proc/PID/maps. */ + +struct mapping +{ + ULONGEST addr; + ULONGEST endaddr; + gdb::string_view permissions; + ULONGEST offset; + gdb::string_view device; + ULONGEST inode; + + /* This field is guaranteed to be NULL-terminated, hence it is not a + gdb::string_view. */ + const char *filename; +}; + /* Service function for corefiles and info proc. */ =20 -static void -read_mapping (const char *line, - ULONGEST *addr, ULONGEST *endaddr, - const char **permissions, size_t *permissions_len, - ULONGEST *offset, - const char **device, size_t *device_len, - ULONGEST *inode, - const char **filename) +static mapping +read_mapping (const char *line) { + struct mapping mapping; const char *p =3D line; =20 - *addr =3D strtoulst (p, &p, 16); + mapping.addr =3D strtoulst (p, &p, 16); if (*p =3D=3D '-') p++; - *endaddr =3D strtoulst (p, &p, 16); + mapping.endaddr =3D strtoulst (p, &p, 16); =20 p =3D skip_spaces (p); - *permissions =3D p; + const char *permissions_start =3D p; while (*p && !isspace (*p)) p++; - *permissions_len =3D p - *permissions; + mapping.permissions =3D {permissions_start, (size_t) (p - permissions_st= art)}; =20 - *offset =3D strtoulst (p, &p, 16); + mapping.offset =3D strtoulst (p, &p, 16); =20 p =3D skip_spaces (p); - *device =3D p; + const char *device_start =3D p; while (*p && !isspace (*p)) p++; - *device_len =3D p - *device; + mapping.device =3D {device_start, (size_t) (p - device_start)}; =20 - *inode =3D strtoulst (p, &p, 10); + mapping.inode =3D strtoulst (p, &p, 10); =20 p =3D skip_spaces (p); - *filename =3D p; + mapping.filename =3D p; + + return mapping; } =20 /* Helper function to decode the "VmFlags" field in /proc/PID/smaps. @@ -895,34 +908,27 @@ linux_info_proc (struct gdbarch *gdbarch, const char = *args, line; line =3D strtok_r (NULL, "\n", &saveptr)) { - ULONGEST addr, endaddr, offset, inode; - const char *permissions, *device, *mapping_filename; - size_t permissions_len, device_len; - - read_mapping (line, &addr, &endaddr, - &permissions, &permissions_len, - &offset, &device, &device_len, - &inode, &mapping_filename); + struct mapping m =3D read_mapping (line); =20 if (gdbarch_addr_bit (gdbarch) =3D=3D 32) { printf_filtered ("\t%10s %10s %7.5s %10s %10s %s\n", - paddress (gdbarch, addr), - paddress (gdbarch, endaddr), - permissions, - hex_string (endaddr - addr), - hex_string (offset), - *mapping_filename ? mapping_filename : ""); + paddress (gdbarch, m.addr), + paddress (gdbarch, m.endaddr), + m.permissions.data (), + hex_string (m.endaddr - m.addr), + hex_string (m.offset), + m.filename); } else { printf_filtered (" %18s %18s %7.5s %10s %10s %s\n", - paddress (gdbarch, addr), - paddress (gdbarch, endaddr), - permissions, - hex_string (endaddr - addr), - hex_string (offset), - *mapping_filename ? mapping_filename : ""); + paddress (gdbarch, m.addr), + paddress (gdbarch, m.endaddr), + m.permissions.data (), + hex_string (m.endaddr - m.addr), + hex_string (m.offset), + m.filename); } } } @@ -1322,19 +1328,15 @@ parse_smaps_data (const char *data, =20 while (line !=3D NULL) { - ULONGEST addr, endaddr, offset, inode; - const char *permissions, *device, *filename; struct smaps_vmflags v; - size_t permissions_len, device_len; int read, write, exec, priv; int has_anonymous =3D 0; int mapping_anon_p; int mapping_file_p; =20 memset (&v, 0, sizeof (v)); - read_mapping (line, &addr, &endaddr, &permissions, &permissions_len, - &offset, &device, &device_len, &inode, &filename); - mapping_anon_p =3D mapping_is_anonymous_p (filename); + struct mapping m =3D read_mapping (line); + mapping_anon_p =3D mapping_is_anonymous_p (m.filename); /* If the mapping is not anonymous, then we can consider it to be file-backed. These two states (anonymous or file-backed) seem to be exclusive, but they can actually @@ -1347,9 +1349,12 @@ parse_smaps_data (const char *data, mapping_file_p =3D !mapping_anon_p; =20 /* Decode permissions. */ - read =3D (memchr (permissions, 'r', permissions_len) !=3D 0); - write =3D (memchr (permissions, 'w', permissions_len) !=3D 0); - exec =3D (memchr (permissions, 'x', permissions_len) !=3D 0); + auto has_perm =3D [&m] (char c) + { return m.permissions.find (c) !=3D gdb::string_view::npos; }; + read =3D has_perm ('r'); + write =3D has_perm ('w'); + exec =3D has_perm ('x'); + /* 'private' here actually means VM_MAYSHARE, and not VM_SHARED. In order to know if a mapping is really private or not, we must check the flag "sh" in the @@ -1359,7 +1364,7 @@ parse_smaps_data (const char *data, not have the VmFlags there. In this case, there is really no way to know if we are dealing with VM_SHARED, so we just assume that VM_MAYSHARE is enough. */ - priv =3D memchr (permissions, 'p', permissions_len) !=3D 0; + priv =3D has_perm ('p'); =20 /* Try to detect if region should be dumped by parsing smaps counters. */ @@ -1421,9 +1426,9 @@ parse_smaps_data (const char *data, /* Save the smaps entry to the vector. */ struct smaps_data map; =20 - map.start_address =3D addr; - map.end_address =3D endaddr; - map.filename =3D filename; + map.start_address =3D m.addr; + map.end_address =3D m.endaddr; + map.filename =3D m.filename; map.vmflags =3D v; map.read =3D read? true : false; map.write =3D write? true : false; @@ -1432,8 +1437,8 @@ parse_smaps_data (const char *data, map.has_anonymous =3D has_anonymous; map.mapping_anon_p =3D mapping_anon_p? true : false; map.mapping_file_p =3D mapping_file_p? true : false; - map.offset =3D offset; - map.inode =3D inode; + map.offset =3D m.offset; + map.inode =3D m.inode; =20 smaps.emplace_back (map); }