From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 93267 invoked by alias); 4 May 2017 11:58:38 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 93231 invoked by uid 89); 4 May 2017 11:58:36 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.4 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy= X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0a-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.156.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 04 May 2017 11:58:34 +0000 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v44Bs1Nf119665 for ; Thu, 4 May 2017 07:58:35 -0400 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0a-001b2d01.pphosted.com with ESMTP id 2a7ux54h70-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 04 May 2017 07:58:35 -0400 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 4 May 2017 12:58:32 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 4 May 2017 12:58:30 +0100 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v44BwTR935455142; Thu, 4 May 2017 11:58:29 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8E6A54C059; Thu, 4 May 2017 12:57:27 +0100 (BST) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 714674C040; Thu, 4 May 2017 12:57:27 +0100 (BST) Received: from oc1027705133.ibm.com (unknown [9.152.212.109]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Thu, 4 May 2017 12:57:27 +0100 (BST) From: Andreas Arnez To: Pedro Alves Cc: gdb-patches@sourceware.org Subject: Re: [PATCH obv] Remove some superfluous code in corelow.c References: Date: Thu, 04 May 2017 11:58:00 -0000 In-Reply-To: (Pedro Alves's message of "Thu, 4 May 2017 11:19:33 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-TM-AS-GCONF: 00 x-cbid: 17050411-0012-0000-0000-00000520F290 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17050411-0013-0000-0000-0000185367D8 Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-05-04_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1705040194 X-IsSubscribed: yes X-SW-Source: 2017-05/txt/msg00106.txt.bz2 On Thu, May 04 2017, Pedro Alves wrote: [...] But this variable is static.. > >> struct bfd_section *section; >> bfd_size_type size; >> char *contents; >> @@ -519,8 +519,6 @@ get_core_register_section (struct regcache *regcache, >> && regset->flags & REGSET_VARIABLE_SIZE); >> ptid_t ptid = regcache_get_ptid (regcache); >> >> - xfree (section_name); >> - > > ..so this was releasing the memory allocated in a previous invocation of > get_core_register_section, AFAICS. So, this wasn't superfluous, > and we now leak. > > I'm not sure why the variable is static but from [1] I suspect > that the reason was just "laziness" to avoid having to think > about freeing it on every function return path: > > https://sourceware.org/ml/gdb-patches/2005-03/msg00237.html Oh my, you're right, of course. Sorry for that. > The easiest to fix that nowadays is to use a std::string. > > I don't see a need to xstrdup the section name in the single-threaded > case though, and also there's more than one place that computes > a multi-threaded section name in the same way, so how about the > patch below? Yeah, looks OK to me. > > From 7c91ec5530e157c33d6e5edc3dc040d9fba1818b Mon Sep 17 00:00:00 2001 > From: Pedro Alves > Date: Thu, 4 May 2017 10:47:04 +0100 > Subject: [PATCH] fix leak, introduce thread_section_name > > --- > gdb/corelow.c | 76 +++++++++++++++++++++++++++++++++++++++++------------------ > 1 file changed, 53 insertions(+), 23 deletions(-) > > diff --git a/gdb/corelow.c b/gdb/corelow.c > index 2266f24..0aff02d 100644 > --- a/gdb/corelow.c > +++ b/gdb/corelow.c > @@ -485,6 +485,51 @@ core_detach (struct target_ops *ops, const char *args, int from_tty) > printf_filtered (_("No core file now.\n")); > } > > +/* Build either a single-thread or multi-threaded section name for > + PTID. > + > + If ptid's lwp member is zero, we want to do the single-threaded > + thing: look for a section named NAME (as passed to the > + constructor). If ptid's lwp member is non-zero, we'll want do the > + multi-threaded thing: look for a section named "NAME/LWP", where > + LWP is the shortest ASCII decimal representation of ptid's lwp > + member. */ > + > +class thread_section_name > +{ > +public: > + /* NAME is the single-threaded section name. If PTID represents an > + LWP, then the build section name is "NAME/LWP", otherwise it's > + just "NAME" unmodified. */ > + thread_section_name (const char *name, ptid_t ptid) > + { > + if (ptid.lwp_p ()) > + { > + m_storage = string_printf ("%s/%ld", name, ptid.lwp ()); > + m_section_name = m_storage.c_str (); > + } > + else > + m_section_name = name; > + } > + > + /* Return the computed section name. The result is valid as long as > + this thread_section_name object is live. */ > + const char *c_str () const > + { return m_section_name; } > + > + /* Disable copy. */ > + thread_section_name (const thread_section_name &) = delete; > + void operator= (const thread_section_name &) = delete; > + > +private: > + /* Either a pointer into M_STORAGE, or a pointer to the name passed > + as parameter to the constructor. */ > + const char *m_section_name; > + /* If we need to build a new section name, this is where we store > + it. */ > + std::string m_storage; > +}; > + > /* Try to retrieve registers from a section in core_bfd, and supply > them to core_vec->core_read_registers, as the register set numbered > WHICH. > @@ -511,21 +556,15 @@ get_core_register_section (struct regcache *regcache, > const char *human_name, > int required) > { > - static char *section_name; > struct bfd_section *section; > bfd_size_type size; > char *contents; > bool variable_size_section = (regset != NULL > && regset->flags & REGSET_VARIABLE_SIZE); > - ptid_t ptid = regcache_get_ptid (regcache); > > - if (ptid_get_lwp (ptid)) > - section_name = xstrprintf ("%s/%ld", name, > - ptid_get_lwp (ptid)); > - else > - section_name = xstrdup (name); > + thread_section_name section_name (name, regcache->ptid ()); > > - section = bfd_get_section_by_name (core_bfd, section_name); > + section = bfd_get_section_by_name (core_bfd, section_name.c_str ()); > if (! section) > { > if (required) > @@ -537,13 +576,14 @@ get_core_register_section (struct regcache *regcache, > size = bfd_section_size (core_bfd, section); > if (size < min_size) > { > - warning (_("Section `%s' in core file too small."), section_name); > + warning (_("Section `%s' in core file too small."), > + section_name.c_str ()); > return; > } > if (size != min_size && !variable_size_section) > { > warning (_("Unexpected size of section `%s' in core file."), > - section_name); > + section_name.c_str ()); > } > > contents = (char *) alloca (size); > @@ -551,7 +591,7 @@ get_core_register_section (struct regcache *regcache, > (file_ptr) 0, size)) > { > warning (_("Couldn't read %s registers from `%s' section in core file."), > - human_name, name); > + human_name, section_name.c_str ()); > return; > } > > @@ -681,18 +721,8 @@ add_to_spuid_list (bfd *abfd, asection *asect, void *list_p) > static LONGEST > get_core_siginfo (bfd *abfd, gdb_byte *readbuf, ULONGEST offset, ULONGEST len) > { > - asection *section; > - char *section_name; > - const char *name = ".note.linuxcore.siginfo"; > - > - if (ptid_get_lwp (inferior_ptid)) > - section_name = xstrprintf ("%s/%ld", name, > - ptid_get_lwp (inferior_ptid)); > - else > - section_name = xstrdup (name); > - > - section = bfd_get_section_by_name (abfd, section_name); > - xfree (section_name); > + thread_section_name section_name (".note.linuxcore.siginfo", inferior_ptid); > + asection *section = bfd_get_section_by_name (abfd, section_name.c_str ()); > if (section == NULL) > return -1;