From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id onM8DeBAT2rBSi0AWB0awg (envelope-from ) for ; Thu, 09 Jul 2026 02:34:08 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=jn6ipNjg; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 254051E098; Thu, 09 Jul 2026 02:34:08 -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,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::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 627C51E04F for ; Thu, 09 Jul 2026 02:34:07 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D8F64BA23C4 for ; Thu, 9 Jul 2026 06:34:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D8F64BA23C4 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=jn6ipNjg Received: from mail-yw1-x112d.google.com (mail-yw1-x112d.google.com [IPv6:2607:f8b0:4864:20::112d]) by sourceware.org (Postfix) with ESMTPS id D1F574BA2E39 for ; Thu, 9 Jul 2026 06:33:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D1F574BA2E39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D1F574BA2E39 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783578807; cv=none; b=HPpL90IEIy1iLUDlEdG0SRDTcs9dT8HaY2NMVwlZeYn+I7Pnx+LZ9YRmNr3tfwGaaSKKVRo/06A/XkGBiwq1SL921UBt6gUZ9eEXLEJJXWvl42GJ2f6qqbzVEVxpjP4l3ZBNpSazIgcHEheCE9Nwh4T0dmn9UQmSygf5ifg8vpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783578807; c=relaxed/simple; bh=8RBwL9rTca+0215/E8YMq1HxJcOLokCCLYB+gynhwOE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WRZkOgDtNIZXLyQSPNxdSLV6HefC3n54ktqd/cv39LFTI+CQXfUio2HCw+hrTua5NDRK6ADvNYvkB8sriC9JvtIsDTMIXy4rKf9STYHcPYbbWxJ4xbQG8nPeTtYZsGDJLqaC5yVDg2WaJtyjomiiUHrWG9X1y7dh51948r0btPM= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=jn6ipNjg DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D1F574BA2E39 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-80cbb0688c8so23674667b3.1 for ; Wed, 08 Jul 2026 23:33:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1783578806; x=1784183606; darn=sourceware.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :user-agent:references:in-reply-to:subject:cc:to:from:from:to:cc :subject:date:message-id:reply-to:content-type; bh=IcB44Zr2CZEq1CoVSNwAy/mSQBTooC4LAipKDPG2DGU=; b=jn6ipNjgqp78UJw6EMKNBwT7wGyKWEmhBol8evn8w18nnumYYNBxddcb1qSv7e0M9X pYHfDQ+HA5HvwETfz2fzCUNUxdJsRLL3L3QKJRIy74H7uzNPtMacyadgluPre5+7X+6s SPFfDqUEWazbPJUqldhcMPOcFHUueM6BbQ6Cdck0lrAlwcnl6ujtiT2RflQRuzrw2xZC liAAhfyjGFB0FEDpiwXiDpenyzoJf7nSzZsxksESkc/zeLETC/DF7QuQdSNnHLPtl9QE NWeuiK9aGQRaItxuOQXpxBE4Pzqii/BcF8wIBQy/Z/78Eq0eHw4XmdUmcN9Z6ckFpiHt DjIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783578806; x=1784183606; h=content-transfer-encoding:content-type:mime-version:message-id:date :user-agent:references:in-reply-to:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=IcB44Zr2CZEq1CoVSNwAy/mSQBTooC4LAipKDPG2DGU=; b=ohpAwGc1Uk4G+Y/OyYF+U+5qWeWjTKj1SqJvz9+JR3iqmeKZKYr1dRanAaEp9TDRnU rz1+Va/aHbY367DyLEMZfCrF/m/OeE7kEtJdn+qKDtzuVCe+6x73ofRCMX3YT8WAulC5 2nTq8KmvZA4t7jFxIAY3ZIgT4julQ43t/6GKrIpOKunS+VlDM82nq5SgHYEdyQrg5Bcp 9K68N4/7twucQbI9n1jtM4Qr8G5TzloATA9wZrTFgpWAVZgRo7NQx5W50wUSjm91n5VV Usag5vYeO8X5qmSXB2kpK0cp5UmGGezra7ZUzvtZMVR1RJEzj0LvYQeOV8oqsfjp1A/G Ow4Q== X-Gm-Message-State: AOJu0YwOwGbhUUENeKxhnARauc2Wy+NbIwdywdSUmToUQagLqtKUH8Ib 8lkxFY3Cae84gsv6qt2eQb/BOt6dFkNqn+3JMyvvdBdvhLwc/3OjigL0OYuX8VAgB/s= X-Gm-Gg: AfdE7ckX8GFrKgYX5Knk/phv4xnDYxViKQXOcr2cZOc3fTOXJW3MzL3QmyxImM41WQT UZD4ztVpyXGfv673Ui2azaQWr6e641nkWZbsDY2qjmYNhPYBmj9vutwreSd6FkTqYwP/ANkhY5A hMNmgPa2hIdrn57J4y/NUJT/lygsuzpVE/FnMiE/E9ZbrDWvG+ixAU4uvUrghsIqTUONObpbv+F frQ2Yg4QvzAgCNkn7HCeW4QDz3aAL9Tls0jLraa8k/JJu3pFIPop6nABfz5fhW37u1PqROWkwQu wX9osYNlbHu5kwBUxtmnSRv0+wo4BWNzSH8j9bN748dk2M3KRFzcMubeUGKgnvfiuzxRSZkUnR/ jOOLGHO9N3sjL5llbdA4DUUpBT3NHWvtZzhBGhimH6/goPnsuAZqe/WV0QJNus8qdSD0liWH6lc iPYvonY8RTXsYt6E65poIaYW0= X-Received: by 2002:a05:690c:e05:b0:80c:85e5:872f with SMTP id 00721157ae682-81dc0ff16a5mr48809967b3.61.1783578806093; Wed, 08 Jul 2026 23:33:26 -0700 (PDT) Received: from localhost ([2804:14d:7e39:8083:f04c:42e3:5943:38f6]) by smtp.gmail.com with ESMTPSA id 00721157ae682-81e6c1a44e1sm10543747b3.29.2026.07.08.23.33.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jul 2026 23:33:25 -0700 (PDT) From: Thiago Jung Bauermann To: Matthieu Longo Cc: , Luis Machado , Luis Machado , Andrew Burgess , "Yury Khrustalev" , Pedro Alves , "Tom Tromey" Subject: Re: [PATCH v1 05/10] gdb: introduce helper class file_reader_t In-Reply-To: <20260707154900.94542-6-matthieu.longo@arm.com> (Matthieu Longo's message of "Tue, 7 Jul 2026 16:48:55 +0100") References: <20260707154900.94542-1-matthieu.longo@arm.com> <20260707154900.94542-6-matthieu.longo@arm.com> User-Agent: mu4e 1.14.2; emacs 30.2 Date: Thu, 09 Jul 2026 06:33:23 +0000 Message-ID: <87bjcg1xl8.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 Matthieu Longo writes: > Wrap all the boilerplate code required to read a file in a new helper > class: file_reader_t. The class owns the file contents together with > the file path, and provides convenient accessors for the data, size and > typed views. It supports both null-terminated text files and binary files. > > This helper eliminates repeated calls to target_fileio_read_stralloc > and target_fileio_read_alloc, remove explicit memory management with > gdb::unique_xmalloc_ptr, and simplifies the casting logic when working > with binary data. > > The patch converts some of the existing Linux, AMD64, and SPARC code that > reads files from /proc to use file_reader_t. > --- > gdb/amd64-linux-tdep.c | 12 ++--- > gdb/linux-tdep.c | 102 +++++++++++++++++------------------------ > gdb/sparc64-tdep.c | 12 ++--- > gdb/target.h | 65 ++++++++++++++++++++++++++ > 4 files changed, 117 insertions(+), 74 deletions(-) With the changes I suggested below: Reviewed-by: Thiago Jung Bauermann > @@ -2285,23 +2280,24 @@ linux_fill_prpsinfo (struct elf_internal_linux_pr= psinfo *p) >=20=20 > /* Obtaining PID and filename. */ > pid =3D inferior_ptid.pid (); > - xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid); > - /* The full name of the program which generated the corefile. */ > - gdb_byte *buf =3D nullptr; > - LONGEST buf_len =3D target_fileio_read_alloc (nullptr, filename, &buf); > - gdb::unique_xmalloc_ptr fname ((char *)buf); > + file_reader_t cmdline_freader > + (string_printf ("/proc/%d/cmdline", pid)); I was scratching my head for a bit trying to understand why not use file_reader_t here and avoid having to use cast_view below. I think it's worth a comment mentioning that it's because cmdline has '\0' separating the arguments. > + if (!cmdline_freader) > + return false; >=20=20 > - if (buf_len < 1 || fname.get () =3D=3D nullptr || fname.get ()[0] =3D= =3D '\0') > + /* The full name of the program which generated the corefile. */ > + gdb::array_view cmdline =3D cmdline_freader.cast_view (); > + if (cmdline.size () < 1 || cmdline[0] =3D=3D '\0') > { > /* No program name was read, so we won't be able to retrieve more > information about the process. */ > return false; > } > - if (fname.get ()[buf_len - 1] !=3D '\0') > + if (cmdline[cmdline.size () - 1] !=3D '\0') > { > warning (_("target file %s " > "does not contain a trailing null character"), > - filename); > + cmdline_freader.c_filepath ()); > return false; > } >=20=20 =E2=8B=AE > diff --git a/gdb/sparc64-tdep.c b/gdb/sparc64-tdep.c > index 93db3417a2a..811b76f27d8 100644 > --- a/gdb/sparc64-tdep.c > +++ b/gdb/sparc64-tdep.c > @@ -305,14 +305,13 @@ adi_is_addr_mapped (CORE_ADDR vaddr, size_t cnt) > size_t i =3D 0; >=20=20 > pid_t pid =3D inferior_ptid.pid (); > - snprintf (filename, sizeof filename, "/proc/%ld/adi/maps", (long) pid); > - gdb::unique_xmalloc_ptr data > - =3D target_fileio_read_stralloc (NULL, filename); This change leaves filename unused and I get a build error when using "configure --enable-targets=3Dall": CXX sparc64-tdep.o /home/bauermann/src/binutils-gdb-wt-2/gdb/sparc64-tdep.c: In function 'bool= adi_is_addr_mapped(CORE_ADDR, size_t)': /home/bauermann/src/binutils-gdb-wt-2/gdb/sparc64-tdep.c:304:8: error: unus= ed variable 'filename' [-Werror=3Dunused-variable] 304 | char filename[MAX_PROC_NAME_SIZE]; | ^~~~~~~~ cc1plus: all warnings being treated as errors make[2]: *** [Makefile:2100: sparc64-tdep.o] Error 1 > - if (data) > + file_reader_t adi_maps_freader > + (string_printf ("/proc/%d/adi/maps", pid)); I'll just note that this patch changes the printf pattern from "%ld" and "(long) pid" to "%d" and "pid". I think the change is for the better and I can't think of why it was done the other way before. > + if (adi_maps_freader) > { > adi_stat_t adi_stat =3D get_adi_info (pid); > char *saveptr; > - for (char *line =3D strtok_r (data.get (), "\n", &saveptr); > + for (char *line =3D strtok_r (adi_maps_freader.data (), "\n", &sav= eptr); > line; > line =3D strtok_r (NULL, "\n", &saveptr)) > { --=20 Thiago (he/him)