From patchwork Sat Nov 16 00:03:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Simon Marchi (Code Review)" X-Patchwork-Id: 35966 Received: (qmail 118655 invoked by alias); 16 Nov 2019 00:04:00 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 118572 invoked by uid 89); 16 Nov 2019 00:03:59 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy= X-HELO: mx1.osci.io Received: from polly.osci.io (HELO mx1.osci.io) (8.43.85.229) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 16 Nov 2019 00:03:57 +0000 Received: by mx1.osci.io (Postfix, from userid 994) id AB61120393; Fri, 15 Nov 2019 19:03:55 -0500 (EST) Received: from gnutoolchain-gerrit.osci.io (gnutoolchain-gerrit.osci.io [8.43.85.239]) by mx1.osci.io (Postfix) with ESMTP id B186B203AC for ; Fri, 15 Nov 2019 19:03:53 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by gnutoolchain-gerrit.osci.io (Postfix) with ESMTP id 5F99228171 for ; Fri, 15 Nov 2019 19:03:53 -0500 (EST) X-Gerrit-PatchSet: 1 Date: Fri, 15 Nov 2019 19:03:53 -0500 From: "Tom Tromey (Code Review)" To: gdb-patches@sourceware.org Message-ID: Auto-Submitted: auto-generated X-Gerrit-MessageType: newchange Subject: [review] Add add_internal_function overload X-Gerrit-Change-Id: I3f6df925bc6b3e1bccbad9eeebc487b908bb5a2a X-Gerrit-Change-Number: 662 X-Gerrit-ChangeURL: X-Gerrit-Commit: e2d15c41ee8f9906a33ec8a238db9c12df500011 References: Reply-To: tromey@sourceware.org, gdb-patches@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-76-gf8b6da0ab5 Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/662 ...................................................................... Add add_internal_function overload add_internal_function sets a command destroyer that frees the doc string. However, many callers do not pass in an allocated doc string. This adds a new overload to clearly differentiate the two cases, fixing the latent bug. gdb/ChangeLog 2019-11-15 Tom Tromey * value.h (add_internal_function): Add new overload. Move documentation from value.h. * value.c (do_add_internal_function): New function. (add_internal_function): Use it. Add new overload. (function_destroyer): Don't free doc. * python/py-function.c (fnpy_init): Update. Change-Id: I3f6df925bc6b3e1bccbad9eeebc487b908bb5a2a --- M gdb/ChangeLog M gdb/python/py-function.c M gdb/value.c M gdb/value.h 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index bbef23a..1745e8b 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,14 @@ 2019-11-15 Tom Tromey + * value.h (add_internal_function): Add new overload. Move + documentation from value.h. + * value.c (do_add_internal_function): New function. + (add_internal_function): Use it. Add new overload. + (function_destroyer): Don't free doc. + * python/py-function.c (fnpy_init): Update. + +2019-11-15 Tom Tromey + * python/py-cmd.c (cmdpy_destroyer): Don't free "doc". (cmdpy_init): Set "doc_allocated". diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c index 46a66cf..1c45887 100644 --- a/gdb/python/py-function.c +++ b/gdb/python/py-function.c @@ -127,7 +127,7 @@ if (! docstring) docstring.reset (xstrdup (_("This function is not documented."))); - add_internal_function (name, docstring.release (), fnpy_call, + add_internal_function (name, std::move (docstring), fnpy_call, self_ref.release ()); return 0; } diff --git a/gdb/value.c b/gdb/value.c index 47e647a..b2714eb 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -2426,17 +2426,13 @@ function_destroyer (struct cmd_list_element *self, void *ignore) { xfree ((char *) self->name); - xfree ((char *) self->doc); } -/* Add a new internal function. NAME is the name of the function; DOC - is a documentation string describing the function. HANDLER is - called when the function is invoked. COOKIE is an arbitrary - pointer which is passed to HANDLER and is intended for "user - data". */ -void -add_internal_function (const char *name, const char *doc, - internal_function_fn handler, void *cookie) +/* Helper function that does the work for add_internal_function. */ + +static struct cmd_list_element * +do_add_internal_function (const char *name, const char *doc, + internal_function_fn handler, void *cookie) { struct cmd_list_element *cmd; struct internal_function *ifn; @@ -2448,6 +2444,29 @@ cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc, &functionlist); cmd->destroyer = function_destroyer; + + return cmd; +} + +/* See value.h. */ + +void +add_internal_function (const char *name, const char *doc, + internal_function_fn handler, void *cookie) +{ + do_add_internal_function (name, doc, handler, cookie); +} + +/* See value.h. */ + +void +add_internal_function (const char *name, gdb::unique_xmalloc_ptr &&doc, + internal_function_fn handler, void *cookie) +{ + struct cmd_list_element *cmd + = do_add_internal_function (name, doc.get (), handler, cookie); + doc.release (); + cmd->doc_allocated = 1; } /* Update VALUE before discarding OBJFILE. COPIED_TYPES is used to diff --git a/gdb/value.h b/gdb/value.h index 2b5d784..fdef835 100644 --- a/gdb/value.h +++ b/gdb/value.h @@ -1165,9 +1165,22 @@ int argc, struct value **argv); -void add_internal_function (const char *name, const char *doc, - internal_function_fn handler, - void *cookie); +/* Add a new internal function. NAME is the name of the function; DOC + is a documentation string describing the function. HANDLER is + called when the function is invoked. COOKIE is an arbitrary + pointer which is passed to HANDLER and is intended for "user + data". */ + +extern void add_internal_function (const char *name, const char *doc, + internal_function_fn handler, + void *cookie); + +/* This overload takes an allocated documentation string. */ + +extern void add_internal_function (const char *name, + gdb::unique_xmalloc_ptr &&doc, + internal_function_fn handler, + void *cookie); struct value *call_internal_function (struct gdbarch *gdbarch, const struct language_defn *language,