public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Yao Qi <qiyaoltc@gmail.com>
To: Pedro Alves <palves@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH v2 28/31] Use ui_file_as_string throughout more
Date: Thu, 23 Feb 2017 10:23:00 -0000	[thread overview]
Message-ID: <86r32pb40h.fsf@gmail.com> (raw)
In-Reply-To: <1476968896-13600-1-git-send-email-palves@redhat.com> (Pedro	Alves's message of "Thu, 20 Oct 2016 14:08:16 +0100")

Pedro Alves <palves@redhat.com> writes:

Hi Pedro,

> std::string as member of a struct, I needed to adjust
> allocation/destruction of said struct to use new/delete instead of
> xmalloc/xfree.
>

> diff --git a/gdb/varobj-iter.h b/gdb/varobj-iter.h
> index bdbf661..1e7889b 100644
> --- a/gdb/varobj-iter.h
> +++ b/gdb/varobj-iter.h
> @@ -19,7 +19,7 @@
>  typedef struct varobj_item
>  {
>    /* Name of this item.  */
> -  char *name;
> +  std::string name;
>  
>    /* Value of this item.  */
>    struct value *value;
> @@ -67,6 +67,6 @@ struct varobj_iter_ops
>        if ((ITER) != NULL)		       \
>  	{				       \
>  	  (ITER)->ops->dtor (ITER);	       \
> -	  xfree (ITER);		       \
> +	  delete (ITER);		       \

We don't need to use "delete" for varobj_iter, because you didn't add
std::string member in varobj_iter.  This change causes an ASAN error,

-var-create container @ c^M
=================================================================^M
^[[1m^[[31m==12975==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x603000085e40^M
^[[1m^[[0m    #0 0x2b8ce74d15d7 in operator delete(void*) (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x555d7)^M
    #1 0xc13d3c in update_dynamic_varobj_children /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:786^M
    #2 0xc1425f in varobj_get_num_children(varobj*) /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:846^M
    #3 0x59ffeb in print_varobj /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-cmd-var.c:61^M
    #4 0x5a0689 in mi_cmd_var_create(char*, char**, int) /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-cmd-var.c:145^M
    #5 0x5b5ac9 in mi_cmd_execute /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-main.c:2295^M
    #6 0x5b3de0 in captured_mi_execute_command /home/yao/SourceCode/gnu/gdb/git/gdb/mi/mi-main.c:1997^M
.....
^M
^[[1m^[[32m0x603000085e40 is located 0 bytes inside of 32-byte region [0x603000085e40,0x603000085e60)^M
^[[1m^[[0m^[[1m^[[35mallocated by thread T0 here:^[[1m^[[0m^M
    #0 0x2b8ce74d0862 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.1+0x54862)^M
    #1 0x80df7f in xmalloc /home/yao/SourceCode/gnu/gdb/git/gdb/common/common-utils.c:43^M
    #2 0x6364ec in py_varobj_iter_new /home/yao/SourceCode/gnu/gdb/git/gdb/python/py-varobj.c:154^M
    #3 0x636710 in py_varobj_get_iterator(varobj*, _object*) /home/yao/SourceCode/gnu/gdb/git/gdb/python/py-varobj.c:188^M
    #4 0xc13580 in varobj_get_iterator /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:712^M
    #5 0xc138fa in update_dynamic_varobj_children /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:750^M
    #6 0xc1425f in varobj_get_num_children(varobj*) /home/yao/SourceCode/gnu/gdb/git/gdb/varobj.c:846^M

reverting the change above fixes the error.  We can also C++-fy
varobj_iter, but I'd like to do in another patch.  How is the patch below?

>  	}				       \
>      } while (0)

> diff --git a/gdb/varobj.h b/gdb/varobj.h
> index 6b9a71f..7f4aad2 100644
> --- a/gdb/varobj.h
> +++ b/gdb/varobj.h
> @@ -86,22 +86,22 @@ struct varobj_dynamic;
>  
>  /* Every variable in the system has a structure of this type defined
>     for it.  This structure holds all information necessary to manipulate
> -   a particular object variable.  Members which must be freed are noted.  */
> +   a particular object variable.  */
>  struct varobj
>  {
> -  /* Alloc'd name of the variable for this object.  If this variable is a
> +  /* Name of the variable for this object.  If this variable is a
>       child, then this name will be the child's source name.
>       (bar, not foo.bar).  */
>    /* NOTE: This is the "expression".  */
> -  char *name;
> +  std::string name;
>  
> -  /* Alloc'd expression for this child.  Can be used to create a
> -     root variable corresponding to this child.  */
> -  char *path_expr;
> +  /* Expression for this child.  Can be used to create a root variable
> +     corresponding to this child.  */
> +  std::string path_expr;
>  
> -  /* The alloc'd name for this variable's object.  This is here for
> +  /* The name for this variable's object.  This is here for
>       convenience when constructing this object's children.  */
> -  char *obj_name;
> +  std::string obj_name;
>  
>    /* Index of this variable in its parent or -1.  */
>    int index;
> @@ -137,7 +137,7 @@ struct varobj
>    int updated;
>  
>    /* Last print value.  */
> -  char *print_value;
> +  std::string print_value;
>  
>    /* Is this variable frozen.  Frozen variables are never implicitly
>       updated by -var-update * 
> @@ -170,18 +170,15 @@ struct lang_varobj_ops
>    /* The number of children of PARENT.  */
>    int (*number_of_children) (const struct varobj *parent);
>  
> -  /* The name (expression) of a root varobj.  The returned value must be freed
> -     by the caller.  */
> -  char *(*name_of_variable) (const struct varobj *parent);
> +  /* The name (expression) of a root varobj.  */
> +  std::string (*name_of_variable) (const struct varobj *parent);
>  
> -  /* The name of the INDEX'th child of PARENT.  The returned value must be
> -     freed by the caller.  */
> -  char *(*name_of_child) (const struct varobj *parent, int index);
> +  /* The name of the INDEX'th child of PARENT.  */
> +  std::string (*name_of_child) (const struct varobj *parent, int index);
>  
>    /* Returns the rooted expression of CHILD, which is a variable
> -     obtain that has some parent.  The returned value must be freed by the
> -     caller.  */
> -  char *(*path_expr_of_child) (const struct varobj *child);
> +     obtain that has some parent.  */
> +  std::string (*path_expr_of_child) (const struct varobj *child);
>  
>    /* The ``struct value *'' of the INDEX'th child of PARENT.  */
>    struct value *(*value_of_child) (const struct varobj *parent, int index);
> @@ -189,10 +186,9 @@ struct lang_varobj_ops
>    /* The type of the INDEX'th child of PARENT.  */
>    struct type *(*type_of_child) (const struct varobj *parent, int index);
>  
> -  /* The current value of VAR.  The returned value must be freed by the
> -     caller.  */
> -  char *(*value_of_variable) (const struct varobj *var,
> -			      enum varobj_display_formats format);
> +  /* The current value of VAR.  */
> +  std::string (*value_of_variable) (const struct varobj *var,
> +				    enum varobj_display_formats format);
>  
>    /* Return non-zero if changes in value of VAR must be detected and
>       reported by -var-update.  Return zero if -var-update should never

-- 
Yao (齐尧)
From 71dc64a061cfb1ec57b753e495c02665827c7a01 Mon Sep 17 00:00:00 2001
From: Yao Qi <yao.qi@linaro.org>
Date: Thu, 23 Feb 2017 09:29:48 +0000
Subject: [PATCH] Use xfree rather than delete for varobj_iter

ASAN reports an error,

-var-create container @ c^M
=================================================================^M
^[[1m^[[31m==21639==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x6030000805c0^M
^[[1m^[[0m    #0 0x7f2449b01b2a in operator delete(void*) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99b2a)^M
    #1 0xbb601d in update_dynamic_varobj_children ../../binutils-gdb/gdb/varobj.c:794^M
    #2 0xbb6556 in varobj_get_num_children(varobj*) ../../binutils-gdb/gdb/varobj.c:854^M
    #3 0x580cb4 in print_varobj ../../binutils-gdb/gdb/mi/mi-cmd-var.c:61^M
    #4 0x58138b in mi_cmd_var_create(char*, char**, int) ../../binutils-gdb/gdb/mi/mi-cmd-var.c:145^M
    #5 0x5967ce in mi_cmd_execute ../../binutils-gdb/gdb/mi/mi-main.c:2301^M
    #6 0x594b05 in captured_mi_execute_command ../../binutils-gdb/gdb/mi/mi-main.c:2001
....
^M
^[[1m^[[32m0x6030000805c0 is located 0 bytes inside of 32-byte region [0x6030000805c0,0x6030000805e0)^M
^[[1m^[[0m^[[1m^[[35mallocated by thread T0 here:^[[1m^[[0m^M
    #0 0x7f2449b00602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)^M
    #1 0x7d1596 in xmalloc ../../binutils-gdb/gdb/common/common-utils.c:43^M
    #2 0x604176 in py_varobj_iter_new ../../binutils-gdb/gdb/python/py-varobj.c:159^M
    #3 0x6042da in py_varobj_get_iterator(varobj*, _object*) ../../binutils-gdb/gdb/python/py-varobj.c:198^M
    #4 0xbb5806 in varobj_get_iterator ../../binutils-gdb/gdb/varobj.c:720^M
    #5 0xbb5b9b in update_dynamic_varobj_children ../../binutils-gdb/gdb/varobj.c:758^M

gdb:

2017-02-23  Yao Qi  <yao.qi@linaro.org>

	* varobj-iter.h (varobj_iter_delete): Call xfree instead of
	delete.

diff --git a/gdb/varobj-iter.h b/gdb/varobj-iter.h
index 34182e0..7eed4f1 100644
--- a/gdb/varobj-iter.h
+++ b/gdb/varobj-iter.h
@@ -67,6 +67,6 @@ struct varobj_iter_ops
       if ((ITER) != NULL)		       \
 	{				       \
 	  (ITER)->ops->dtor (ITER);	       \
-	  delete (ITER);		       \
+	  xfree (ITER);		       \
 	}				       \
     } while (0)

  reply	other threads:[~2017-02-23 10:23 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-19  1:13 [PATCH v2 00/31] More cleanup elimination & unlimited args to user-defined funcs Pedro Alves
2016-10-19  1:12 ` [PATCH v2 22/31] Use ui_file_as_string in gdb/c-exp.y Pedro Alves
2016-10-19  1:12 ` [PATCH v2 21/31] Use ui_file_as_string in gdb/compile/ Pedro Alves
2016-10-19 23:08   ` Simon Marchi
2016-10-19 23:48     ` Pedro Alves
2016-10-20  3:17       ` Simon Marchi
2016-10-20 12:13         ` Pedro Alves
2016-10-19  1:12 ` [PATCH v2 19/31] Use ui_file_as_string in gdb/remote.c Pedro Alves
2016-10-19  1:12 ` [PATCH v2 07/31] Clean up tracepoint.h/c:collection_list Pedro Alves
2016-10-19  1:12 ` [PATCH v2 24/31] Use ui_file_as_string in gdb/ada-lang.c Pedro Alves
2016-10-19  1:12 ` [PATCH v2 06/31] Introduce ui_file_as_string Pedro Alves
2016-10-19  1:12 ` [PATCH v2 01/31] Introduce string_printf Pedro Alves
2016-10-19 13:43   ` Trevor Saunders
2016-10-19 14:41     ` Pedro Alves
2016-10-19 17:18   ` Simon Marchi
2016-10-19 21:02     ` Pedro Alves
2016-11-08 15:35       ` Pedro Alves
2016-10-19  1:12 ` [PATCH v2 20/31] Use ui_file_as_string in gdb/cli/cli-setshow.c Pedro Alves
2016-10-19  1:12 ` [PATCH v2 12/31] Use ui_file_as_string in gdb/utils.c Pedro Alves
2016-10-19  1:13 ` [PATCH v2 23/31] Use ui_file_as_string in gdbarch.sh/gdbarch.c Pedro Alves
2016-10-19  1:13 ` [PATCH v2 26/31] Use ui_file_as_string in gdb/rust-lang.c Pedro Alves
2016-10-19  1:13 ` [PATCH v2 03/31] breakpoint.c:commands_command_1 constification and cleanup Pedro Alves
2016-10-19  1:13 ` [PATCH v2 14/31] Use ui_file_as_string in gdb/guile/ Pedro Alves
2016-10-19  1:13 ` [PATCH v2 29/31] 'struct agent_expr *' -> unique_ptr<agent_expr> Pedro Alves
2016-10-19 23:19   ` Simon Marchi
2016-10-19 23:58     ` Pedro Alves
2016-10-19  1:13 ` [PATCH v2 27/31] Use ui_file_as_string in gdb/language.c Pedro Alves
2016-10-19  1:13 ` [PATCH v2 15/31] Use ui_file_as_string in execute_command_to_string Pedro Alves
2016-10-19  1:13 ` [PATCH v2 08/31] Use ui_file_as_string in dwarf2_compute_name Pedro Alves
2016-10-19  1:13 ` [PATCH v2 02/31] cli/cli-script.c: Remove some dead NULL checks Pedro Alves
2016-10-19 17:24   ` Simon Marchi
2016-10-19 21:18     ` Pedro Alves
2016-10-19  1:13 ` [PATCH v2 31/31] Support an "unlimited" number of user-defined arguments Pedro Alves
2016-10-19  6:29   ` Eli Zaretskii
2016-10-19 11:33   ` Philipp Rudo
2016-10-19 12:47     ` Pedro Alves
2016-10-19 17:40       ` Philipp Rudo
2016-10-19 17:45         ` Pedro Alves
2016-11-08 15:41   ` Pedro Alves
2016-10-19  1:17 ` [PATCH v2 10/31] Use ui_file_as_string in gdb/ada-valprint.c Pedro Alves
2016-10-19  1:17 ` [PATCH v2 11/31] Use ui_file_as_string in gdb/ui-out.c Pedro Alves
2016-10-19  1:17 ` [PATCH v2 25/31] Use ui_file_as_string in gdb/infrun.c Pedro Alves
2016-10-19  1:17 ` [PATCH v2 09/31] Use ui_file_as_string in gdb/xtensa-tdep.c Pedro Alves
2016-10-19  1:18 ` [PATCH v2 05/31] 'struct expression *' -> gdb::unique_xmalloc_ptr<expression> Pedro Alves
2016-10-19 18:45   ` Simon Marchi
2016-10-19 21:50     ` Pedro Alves
2016-10-19 22:25       ` Simon Marchi
2016-10-19 22:36         ` Pedro Alves
2016-10-19  1:18 ` [PATCH v2 30/31] Eliminate agent_expr_p; VEC -> std::vector in struct bp_target_info Pedro Alves
2016-10-19  1:19 ` [PATCH v2 04/31] cli-script.c: Simplify using std::string, eliminate cleanups Pedro Alves
2016-10-19 18:25   ` Simon Marchi
2016-10-19 21:45     ` Pedro Alves
2016-10-19  1:21 ` [PATCH v2 13/31] Use ui_file_as_string in gdb/arm-tdep.c Pedro Alves
2016-10-19 22:54   ` Simon Marchi
2016-10-19  1:21 ` [PATCH v2 18/31] Use ui_file_as_string in gdb/python/ Pedro Alves
2016-10-19  1:21 ` [PATCH v2 16/31] Use ui_file_as_string in gdb/top.c Pedro Alves
2016-10-19  1:21 ` [PATCH v2 17/31] Use ui_file_as_string in gdb/printcmd.c Pedro Alves
2016-10-20 13:08 ` [PATCH v2 28/31] Use ui_file_as_string throughout more Pedro Alves
2017-02-23 10:23   ` Yao Qi [this message]
2017-02-23 10:53     ` Pedro Alves
2017-02-23 10:35   ` Yao Qi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86r32pb40h.fsf@gmail.com \
    --to=qiyaoltc@gmail.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).