From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ET2eLSzoX2qzCyIAWB0awg (envelope-from ) for ; Tue, 21 Jul 2026 17:44:12 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=GrecjDnG; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id A636E1E033; Tue, 21 Jul 2026 17:44:12 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id AA0271E033 for ; Tue, 21 Jul 2026 17:44:11 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1D0414BA2E0E for ; Tue, 21 Jul 2026 21:44:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1D0414BA2E0E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=GrecjDnG Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 5A2E04BA543C for ; Tue, 21 Jul 2026 21:43:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A2E04BA543C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5A2E04BA543C Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1784670226; cv=none; b=JGc55h2o76evTo/C6HyDe0QIIsblRZt4J4UMk8vcLxr8BTLEhhIvr90/FtfKdsZcafQz4Y9DdN7sskuIPZVnxLdHgXNh/C7nCztCcpkAq4rsG70JhRehvELLICv5L6f4vO2gOR+aQestUjBGaJs8Nn+lZLBqVr7E3IPpFDXKVlE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1784670226; c=relaxed/simple; bh=pv0Ta3dsI4ph79A1On6GhlGSJjuWE4ecG9AwBl93ltU=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=jHH3zoQRJgp+0aqKG4N1Nl6CmiFm8L9Zd+OK7VnW2vyYFohAN+FpF5uMcTlb0JjtrbFZW+8qCVzbtcsD6MEg4Bi9CSXFVfK3NSRSMpEVV3hOb2A7B7wdLiBQPOhQB3gDSd3gR5AO2yVU99y3AA85nVjECI9BglZuP+/NpcW6sN0= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=GrecjDnG DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A2E04BA543C Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-476a130c138so8287989f8f.0 for ; Tue, 21 Jul 2026 14:43:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784670225; x=1785275025; darn=sourceware.org; h=content-transfer-encoding:content-type:in-reply-to:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to:content-type; bh=ztRBUOxcpCQ2S8CR6+BJLSoHq2ai/qXlXFKvNsqRuDw=; b=GrecjDnG+pfEQ4EggNNqjv7iGO3W/gsONITXDKoS2/eBAC3BBtRMKsC8O9kAjImR5v 6RAyRFPISgfuIUlkeEfRFS363YKqO3XBbTRkxGCXQQYlXpy3aPbvOgTsCcOhbvl7IiX6 eXFudUwcnqqRw0gTIjUmhB3ekukYgb0OjV2OzWqfajP/XvjlhwCWkontLFr4ti73YJkr WggGO/ACwV8AGrfx7BLaDLA86hRT5ae3Kr5ZRjgFQ1dujx+Lb2fSHOVIg2gByv5cEh1O Cx1b866xVpBOm/7FmM/QsW7mvCkL3Q8okifGNXu+/M+otup0PZlU0WSk+e+2C0dKWj9P cVog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784670225; x=1785275025; h=content-transfer-encoding:content-type:in-reply-to:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ztRBUOxcpCQ2S8CR6+BJLSoHq2ai/qXlXFKvNsqRuDw=; b=tPq6N65EPpBPjqD9UA/SWVfE5D1UeE+dFPfT7em/CnaU9wlABkWQrcPsoHlrSVcYTb EecRSRpB+qIRjmbQAt5dlRpKpI/vRANNZk4J098GpYynwVKjuMLHIrnp7CWTQKyAIh2p xpi4zZB5Fztcin+2gLstKtvSPvOcwiaRAWNleHCboN9qiwYoCEXJMy/XjOngoK9C9dwL D9Oz7XKmbcTle56BvWwnqxiBTw95ENx/ux7MKCIcxGIP+GMMWTevq5QX5Fh3xfrodGDb iuOOk9GCI3nhStkWswQiSwGAd8yN7MytkGoTCO7DAzerlojJm2FyThW6PPUUQna6upgS /FOA== X-Forwarded-Encrypted: i=1; AHgh+Rp3c0AyFBMJ0BeEaKD6ODuEaBsnSc6GHJIvo6GEB4M5WraM4gSUJyUZLNJMd36Ar7Wke7UFjMwXRyIhUQ==@sourceware.org X-Gm-Message-State: AOJu0YzLlxTF1GgFg6G+lp1mayyr78OHMFpfZJD6pKTQcYkjOlOwslg0 G4f3LKI5DvXr2eMY3UQofa9NUmaxrCD7AFBsGtMjJRcve3LJuiAWwi8P X-Gm-Gg: AR+sD12NWp6n0Qx4ncKoYEQoMVyv6WHA6YvhmnQlNKwkQk8iP7hwLCRa4j9nEALxCQG stacmStupmtbW7Gg7gf3av1xgi343OmT6amkTu4JtkJbD94OF1tQDLT5SAoIEAzwT5WYW+PaY59 ZTWAO6zW6NzXUTxWCQStnjProbA5mLD5R+9OvUymVbhuW5bKLKI7q55GA/5YPGHRhBMLPUT+DET v8F3SnDq7nqhQa+gNFX+QmLtEL7swSKfJD4DiqS0W9Jjn9wD9SNhKkebcFaY5cSCNKJMHNgaASB GP/u945WclEuXjE9TExGrOBRD04zap2tUWKjQqwhNIhn9S0H6UNa+uEUCZLAIGn77HJMprB7uyx mx3RAn//DUTEKr8h2NM1KKF1tpbsDbpbxHKELr1BJNbHv4LuMWP4RzcU06jMZXeVtKRgGxITLoK 9GpyQbnhwmbtUhzow= X-Received: by 2002:a05:6000:25ee:b0:47f:83df:fc6 with SMTP id ffacd0b85a97d-47f83df0fecmr2540368f8f.55.1784670225063; Tue, 21 Jul 2026 14:43:45 -0700 (PDT) Received: from [192.168.0.38] ([86.12.216.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47f63e49ce4sm42553193f8f.8.2026.07.21.14.43.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Jul 2026 14:43:44 -0700 (PDT) Message-ID: Date: Tue, 21 Jul 2026 22:43:42 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 06/10] gdb/linux-tdep: migrate linux_info_proc to file_reader_t Content-Language: en-US To: Matthieu Longo , gdb-patches@sourceware.org Cc: Luis Machado , Andrew Burgess , Yury Khrustalev , Pedro Alves , Tom Tromey References: <20260707154900.94542-1-matthieu.longo@arm.com> <20260707154900.94542-7-matthieu.longo@arm.com> From: Luis In-Reply-To: <20260707154900.94542-7-matthieu.longo@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Drive-by review. On 07/07/2026 16:48, Matthieu Longo wrote: > The patch migratse the code of linux_info_proc to use file_reader_t to Typo: migratse > read the procfs files. > The availability of array_views allows to also simplify the logic in > several places, where null-terminating characters are replaced by spaces, > or where the file content is iterated line by line. > In the last case, a new helper function, extract_string_view_from_buffer, > encapsulates the logic for such iterations where string are separated by > tokens. > --- > gdb/linux-tdep.c | 120 +++++++++++++++++++++++++++-------------------- > 1 file changed, 69 insertions(+), 51 deletions(-) > > diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c > index a12a69f03a2..a2af1586d35 100644 > --- a/gdb/linux-tdep.c > +++ b/gdb/linux-tdep.c > @@ -836,6 +836,27 @@ dump_note_entry_p (filter_flags filterflags, const smaps_data &map) > return true; > } > > +/* In a character buffer where entries are separated by a SEPARATOR character, > + extract the string view starting at START. > + Return the extracted view and the iterator to the next entry. */ > + > +static std::pair, gdb::array_view::iterator> > +extract_string_view_from_buffer (gdb::array_view &buffer, > + gdb::array_view::iterator start, > + char separator = '\0') > +{ > + if (start < buffer.begin () || start >= buffer.end ()) > + return std::make_pair (gdb::array_view (), buffer.end ()); > + > + auto it = std::find (start, buffer.end (), separator); > + if (it == buffer.end ()) > + return std::make_pair (gdb::array_view (), buffer.end ()); > + > + auto next_start = std::next (it); > + return std::make_pair > + (gdb::array_view (start, next_start), next_start); > +} > + > /* Implement the "info proc" command. */ > > static void > @@ -878,25 +899,20 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, > gdb_printf (_("process %d\n"), ptid.pid ()); > if (cmdline_f) > { > - xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", ptid.lwp ()); > - gdb_byte *buffer; > - LONGEST len = target_fileio_read_alloc (nullptr, filename, &buffer); > - > - if (len > 0) > + file_reader_t cmdline_freader > + (string_printf ("/proc/%ld/cmdline", ptid.lwp ())); > + if (cmdline_freader) > { > - gdb::unique_xmalloc_ptr cmdline ((char *) buffer); > - ssize_t pos; > - > - for (pos = 0; pos < len - 1; pos++) > - { > - if (buffer[pos] == '\0') > - buffer[pos] = ' '; > - } > - buffer[len - 1] = '\0'; > - gdb_printf ("cmdline = '%s'\n", buffer); > + gdb::array_view cmdline = cmdline_freader.cast_view (); > + gdb_assert (cmdline[ cmdline.size () - 1] == '\0'); Formatting: Stray space before cmdline.size () > + /* Replace null characters splitting the arguments in the command > + line by spaces, except for the last one. */ > + gdb::replace (cmdline.slice (0, cmdline.size () - 1), '\0', ' '); > + gdb_printf ("cmdline = '%s'\n", cmdline.data ()); > } > else > - warning (_("unable to open /proc file '%s'"), filename); > + warning (_("unable to open /proc file '%s'"), > + cmdline_freader.c_filepath()); > } > if (cwd_f) > { > @@ -910,27 +926,25 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, > } > if (environ_f) > { > - xsnprintf (filename, sizeof filename, "/proc/%ld/environ", ptid.lwp ()); > - gdb_byte *buffer; > - LONGEST len = target_fileio_read_alloc (nullptr, filename, &buffer); > - > - if (len > 0) > + file_reader_t environ_freader > + (string_printf ("/proc/%ld/environ", ptid.lwp ())); > + if (environ_freader) > { > - gdb::unique_xmalloc_ptr dealloc ((char *) buffer); > gdb_printf (_("Environment variables:\n\n")); > - > + gdb::array_view buffer = environ_freader.cast_view (); > /* Entries are separated by the null character. > Print each environment variable, line by line. */ > - gdb_byte *buffer_end = buffer + len; > - while (buffer < buffer_end) > + for (auto it = buffer.begin (); it != buffer.end ();) > { > - gdb_printf (" %s\n", buffer); > - /* +1 for the null character. */ > - buffer += strlen ((char *) buffer) + 1; > + auto [ntbs, next_start] > + = extract_string_view_from_buffer (buffer, it, '\0'); > + gdb_printf (" %s\n", ntbs.data ()); > + it = next_start; > } > } > else > - warning (_("unable to open /proc file '%s'"), filename); > + warning (_("unable to open /proc file '%s'"), > + environ_freader.c_filepath()); > } > if (exe_f) > { > @@ -944,10 +958,9 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, > } > if (mappings_f) > { > - xsnprintf (filename, sizeof filename, "/proc/%ld/maps", ptid.lwp ()); > - gdb::unique_xmalloc_ptr map > - = target_fileio_read_stralloc (NULL, filename); > - if (map != NULL) > + file_reader_t map_freader > + (string_printf ("/proc/%ld/maps", ptid.lwp ())); > + if (map_freader) > { > gdb_printf (_("Mapped address spaces:\n\n")); > ui_out_emit_table emitter (current_uiout, 6, -1, "ProcMappings"); > @@ -961,12 +974,16 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, > current_uiout->table_header (0, ui_left, "objfile", "File"); > current_uiout->table_body (); > > - char *saveptr; > - for (const char *line = strtok_r (map.get (), "\n", &saveptr); > - line != nullptr; > - line = strtok_r (nullptr, "\n", &saveptr)) > + auto content = map_freader.view (); > + for (auto it = content.begin (); it != content.end ();) > { > - struct mapping m = read_mapping (line); > + auto [line, next_line_begin] > + = extract_string_view_from_buffer (content, it, '\n'); > + it = next_line_begin; > + Is there a risk we will drop a final chunk of the data when the buffer does not end in \n here (or more generally, does not end in whatever separator we're looking for), comparing it with the old strtok_r behavior? It's a corner case, but I thought I´d check. > + /* read_mapping() expects a null-terminated string. */ > + *std::prev (it) = '\0'; > + struct mapping m = read_mapping (line.data ()); > > ui_out_emit_tuple tuple_emitter (current_uiout, nullptr); > current_uiout->field_core_addr ("start", gdbarch, m.addr); > @@ -985,26 +1002,26 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, > } > } > else > - warning (_("unable to open /proc file '%s'"), filename); > + warning (_("unable to open /proc file '%s'"), > + map_freader.c_filepath ()); > } > if (status_f) > { > - xsnprintf (filename, sizeof filename, "/proc/%ld/status", ptid.lwp ()); > - gdb::unique_xmalloc_ptr status > - = target_fileio_read_stralloc (NULL, filename); > - if (status) > - gdb_puts (status.get ()); > + file_reader_t status_freader > + (string_printf ("/proc/%ld/status", ptid.lwp ())); > + if (status_freader) > + gdb_puts (status_freader.data ()); > else > - warning (_("unable to open /proc file '%s'"), filename); > + warning (_("unable to open /proc file '%s'"), > + status_freader.c_filepath ()); > } > if (stat_f) > { > - xsnprintf (filename, sizeof filename, "/proc/%ld/stat", ptid.lwp ()); > - gdb::unique_xmalloc_ptr statstr > - = target_fileio_read_stralloc (NULL, filename); > - if (statstr) > + file_reader_t stat_freader > + (string_printf ("/proc/%ld/stat", ptid.lwp ())); > + if (stat_freader) > { > - const char *p = statstr.get (); > + const char *p = stat_freader.data (); > > gdb_printf (_("Process: %s\n"), > pulongest (strtoulst (p, &p, 10))); > @@ -1131,7 +1148,8 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args, > #endif > } > else > - warning (_("unable to open /proc file '%s'"), filename); > + warning (_("unable to open /proc file '%s'"), > + stat_freader.c_filepath()); Formatting: Space before parens. Multiple cases. > } > } >