* Re: [PATCH obv] Remove some superfluous code in corelow.c [not found] <m3r3056l9g.fsf@oc1027705133.ibm.com> @ 2017-05-04 10:19 ` Pedro Alves 2017-05-04 11:58 ` Andreas Arnez 0 siblings, 1 reply; 3+ messages in thread From: Pedro Alves @ 2017-05-04 10:19 UTC (permalink / raw) To: Andreas Arnez, gdb-patches On 05/04/2017 10:09 AM, Andreas Arnez wrote: > Pushed this as obvious. > > > In corelow.c I stumbled upon an extra semicolon and an xfree of a NULL > pointer. Remove them. > @@ -129,7 +129,7 @@ sniff_core_bfd (bfd *abfd) > { > struct core_fns *cf; > struct core_fns *yummy = NULL; > - int matches = 0;; > + int matches = 0; > > /* Don't sniff if we have support for register sets in > CORE_GDBARCH. */ > @@ -511,7 +511,7 @@ get_core_register_section (struct regcache *regcache, > const char *human_name, > int required) > { > - static char *section_name = NULL; > + static char *section_name; 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 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? From 7c91ec5530e157c33d6e5edc3dc040d9fba1818b Mon Sep 17 00:00:00 2001 From: Pedro Alves <palves@redhat.com> 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; -- 2.5.5 ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH obv] Remove some superfluous code in corelow.c 2017-05-04 10:19 ` [PATCH obv] Remove some superfluous code in corelow.c Pedro Alves @ 2017-05-04 11:58 ` Andreas Arnez 2017-05-04 14:17 ` Pedro Alves 0 siblings, 1 reply; 3+ messages in thread From: Andreas Arnez @ 2017-05-04 11:58 UTC (permalink / raw) To: Pedro Alves; +Cc: gdb-patches 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 <palves@redhat.com> > 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; ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH obv] Remove some superfluous code in corelow.c 2017-05-04 11:58 ` Andreas Arnez @ 2017-05-04 14:17 ` Pedro Alves 0 siblings, 0 replies; 3+ messages in thread From: Pedro Alves @ 2017-05-04 14:17 UTC (permalink / raw) To: Andreas Arnez; +Cc: gdb-patches On 05/04/2017 12:58 PM, Andreas Arnez wrote: > Oh my, you're right, of course. Sorry for that. No worries. >> 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. Tested on x86_64 GNU/Linux and pushed now. From 3c3ae77e68a9032ef9f174bf6b1cc992b6a4cb35 Mon Sep 17 00:00:00 2001 From: Pedro Alves <palves@redhat.com> Date: Thu, 4 May 2017 15:14:37 +0100 Subject: [PATCH] Fix get_core_register_section leak, introduce thread_section_name This plugs a leak introduced in the previous change to get_core_register_section, which removed an xfree call that is actually necessary because the 'section_name' local is static. From [1] it looks like the reason the variable was made static to begin with, 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 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 put the section name computation in a wrapper class with state. gdb/ChangeLog: 2017-05-04 Pedro Alves <palves@redhat.com> * corelow.c (thread_section_name): New class. (get_core_register_section, get_core_siginfo): Use it. --- gdb/ChangeLog | 5 ++++ gdb/corelow.c | 76 +++++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 23 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index de4d90f..aeb83dd 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2017-05-04 Pedro Alves <palves@redhat.com> + + * corelow.c (thread_section_name): New class. + (get_core_register_section, get_core_siginfo): Use it. + 2017-05-04 Andreas Arnez <arnez@linux.vnet.ibm.com> * corelow.c (sniff_core_bfd): Remove extra semicolon. 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; -- 2.5.5 ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-05-04 14:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <m3r3056l9g.fsf@oc1027705133.ibm.com>
2017-05-04 10:19 ` [PATCH obv] Remove some superfluous code in corelow.c Pedro Alves
2017-05-04 11:58 ` Andreas Arnez
2017-05-04 14:17 ` Pedro Alves
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox