Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
@ 2011-07-20 10:37 Hui Zhu
  2011-07-21 20:43 ` Tom Tromey
  0 siblings, 1 reply; 14+ messages in thread
From: Hui Zhu @ 2011-07-20 10:37 UTC (permalink / raw)
  To: gdb-patches ml

Hi,

printf and eval commands call ui_printf to get the format string.
But I found that:
gdb
(gdb) p $a="pwd"
$1 = "pwd"
(gdb) p $a
$2 = "pwd"
(gdb) printf "%s\n", $a
evaluation of this expression requires the target program to be active
(gdb) eval "%s", $a
evaluation of this expression requires the target program to be active

This error is because we can alloc memory from inferior now.  But
ui_printf will call allocate_space_in_inferior to alloc inferior
memory.  This issue can reproduce in the inferior that cannot alloc
memory such as KGTP.

So I make a patch to handle the error from value_as_address.  If
value_as_address get error and this is a internalvar, call
value_contents to get the address of the val.

After this patch, gdb can always success with this commands.

Please help review the patch, it for the trunk and 7.3.

Thanks,
Hui

2011-07-20  Hui Zhu  <teawater@gmail.com>

	* printcmd.c (ui_printf): Add error handler for value_as_address.
---
 printcmd.c |   47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)

--- a/printcmd.c
+++ b/printcmd.c
@@ -2330,27 +2330,42 @@ ui_printf (char *arg, struct ui_file *st
 	      gdb_byte *str;
 	      CORE_ADDR tem;
 	      int j;
+	      volatile struct gdb_exception ex;

-	      tem = value_as_address (val_args[i]);
-
-	      /* This is a %s argument.  Find the length of the string.  */
-	      for (j = 0;; j++)
+	      TRY_CATCH (ex, RETURN_MASK_ERROR)
 		{
-		  gdb_byte c;
-
-		  QUIT;
-		  read_memory (tem + j, &c, 1);
-		  if (c == 0)
-		    break;
+		  tem = value_as_address (val_args[i]);
 		}
+	      if (ex.reason >= 0)
+		{
+		  /* This is a %s argument.  Find the length of the string.  */
+		  for (j = 0;; j++)
+		    {
+		      gdb_byte c;

-	      /* Copy the string contents into a string inside GDB.  */
-	      str = (gdb_byte *) alloca (j + 1);
-	      if (j != 0)
-		read_memory (tem, str, j);
-	      str[j] = 0;
+		      QUIT;
+		      read_memory (tem + j, &c, 1);
+		      if (c == 0)
+			break;
+		    }

-              fprintf_filtered (stream, current_substring, (char *) str);
+		  /* Copy the string contents into a string inside GDB.  */
+		  str = (gdb_byte *) alloca (j + 1);
+		  if (j != 0)
+		    read_memory (tem, str, j);
+		  str[j] = 0;
+
+		  fprintf_filtered (stream, current_substring, (char *) str);
+		}
+	      else
+		{
+		  if (VALUE_LVAL (val_args[i]) == lval_internalvar)
+		    fprintf_filtered (stream,
+				      current_substring,
+				      (char *) value_contents (val_args[i]));
+		  else
+		    error (_("%s"), ex.message);
+		}
 	    }
 	    break;
 	  case wide_string_arg:


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-07-20 10:37 [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory Hui Zhu
@ 2011-07-21 20:43 ` Tom Tromey
  2011-08-14 15:10   ` Hui Zhu
  0 siblings, 1 reply; 14+ messages in thread
From: Tom Tromey @ 2011-07-21 20:43 UTC (permalink / raw)
  To: Hui Zhu; +Cc: gdb-patches ml

>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:

>> So I make a patch to handle the error from value_as_address.  If
>> value_as_address get error and this is a internalvar, call
>> value_contents to get the address of the val.

I don't think this is the best approach to solve this problem.

It seems to me that if the value is already an array, the data might
already exist in gdb, and then you don't have to even try to coerce it
to memory.  (However, for a pointer or integer value, using
value_as_address is still the best thing.)

I would suggest looking to see how valprint handles this situation, then
do the same thing here.

Tom


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-07-21 20:43 ` Tom Tromey
@ 2011-08-14 15:10   ` Hui Zhu
  2011-08-15 19:06     ` Tom Tromey
  0 siblings, 1 reply; 14+ messages in thread
From: Hui Zhu @ 2011-08-14 15:10 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches ml

On Fri, Jul 22, 2011 at 04:38, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:
>
>>> So I make a patch to handle the error from value_as_address.  If
>>> value_as_address get error and this is a internalvar, call
>>> value_contents to get the address of the val.
>
> I don't think this is the best approach to solve this problem.
>
> It seems to me that if the value is already an array, the data might
> already exist in gdb, and then you don't have to even try to coerce it
> to memory.  (However, for a pointer or integer value, using
> value_as_address is still the best thing.)
>
> I would suggest looking to see how valprint handles this situation, then
> do the same thing here.
>
> Tom
>

Hi Tom,

Thanks for your help.
I make a new patch according to your mail.

Best,
Hui

2011-08-14  Hui Zhu  <teawater@gmail.com>

       * printcmd.c (ui_printf): Add a handler for internalvar and
TYPE_CODE_ARRAY.
---
 printcmd.c |   48 +++++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 21 deletions(-)

--- a/printcmd.c
+++ b/printcmd.c
@@ -2343,32 +2343,38 @@ ui_printf (char *arg, struct ui_file *st
 	switch (argclass[i])
 	  {
 	  case string_arg:
-	    {
-	      gdb_byte *str;
-	      CORE_ADDR tem;
-	      int j;
+            if (VALUE_LVAL (val_args[i]) == lval_internalvar
+		&& TYPE_CODE (check_typedef (value_type (val_args[i])))
+		     == TYPE_CODE_ARRAY)
+	      fprintf_filtered (stream, current_substring,
+				(char *) value_contents (val_args[i]));
+	    else
+	      {
+	        gdb_byte *str;
+	        CORE_ADDR tem;
+	        int j;

-	      tem = value_as_address (val_args[i]);
+	        tem = value_as_address (val_args[i]);

-	      /* This is a %s argument.  Find the length of the string.  */
-	      for (j = 0;; j++)
-		{
-		  gdb_byte c;
+	        /* This is a %s argument.  Find the length of the string.  */
+	        for (j = 0;; j++)
+		  {
+		    gdb_byte c;

-		  QUIT;
-		  read_memory (tem + j, &c, 1);
-		  if (c == 0)
-		    break;
-		}
+		    QUIT;
+		    read_memory (tem + j, &c, 1);
+		    if (c == 0)
+		      break;
+		  }

-	      /* Copy the string contents into a string inside GDB.  */
-	      str = (gdb_byte *) alloca (j + 1);
-	      if (j != 0)
-		read_memory (tem, str, j);
-	      str[j] = 0;
+	        /* Copy the string contents into a string inside GDB.  */
+	        str = (gdb_byte *) alloca (j + 1);
+	        if (j != 0)
+		  read_memory (tem, str, j);
+	        str[j] = 0;

-              fprintf_filtered (stream, current_substring, (char *) str);
-	    }
+                fprintf_filtered (stream, current_substring, (char *) str);
+	      }
 	    break;
 	  case wide_string_arg:
 	    {


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-08-14 15:10   ` Hui Zhu
@ 2011-08-15 19:06     ` Tom Tromey
  2011-08-16  4:58       ` Hui Zhu
  0 siblings, 1 reply; 14+ messages in thread
From: Tom Tromey @ 2011-08-15 19:06 UTC (permalink / raw)
  To: Hui Zhu; +Cc: gdb-patches ml

>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:

>> Thanks for your help.
>> I make a new patch according to your mail.

>> 2011-08-14  Hui Zhu  <teawater@gmail.com>
>>        * printcmd.c (ui_printf): Add a handler for internalvar and
>> TYPE_CODE_ARRAY.

I still don't think this is correct.

This special-cases lval_internalvar, but IIUC this will still fail for
something like:

    printf "hi %s\n", "bob"

In this code I don't think you need to call value_as_address for an
array; likewise reading from memory.

The patch also doesn't address the wide-string case, which it should.

Tom


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-08-15 19:06     ` Tom Tromey
@ 2011-08-16  4:58       ` Hui Zhu
  2011-08-17 14:38         ` Tom Tromey
  0 siblings, 1 reply; 14+ messages in thread
From: Hui Zhu @ 2011-08-16  4:58 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches ml

Hi Tom,

Thanks for your help.

On Tue, Aug 16, 2011 at 03:06, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:
>
>>> Thanks for your help.
>>> I make a new patch according to your mail.
>
>>> 2011-08-14  Hui Zhu  <teawater@gmail.com>
>>>        * printcmd.c (ui_printf): Add a handler for internalvar and
>>> TYPE_CODE_ARRAY.
>
> I still don't think this is correct.
>
> This special-cases lval_internalvar, but IIUC this will still fail for
> something like:
>
>    printf "hi %s\n", "bob"
>
> In this code I don't think you need to call value_as_address for an
> array; likewise reading from memory.

I make a new patch according to it.  Please help me review it.

>
> The patch also doesn't address the wide-string case, which it should.

I don't know howto input a  wide-string to GDB command line.
Could you help me with that and let me write a separate patch special
for wide-string case?

>
> Tom
>

Best,
Hui

2011-08-16  Hui Zhu  <teawater@gmail.com>

	* printcmd.c (ui_printf): Add a handler for internalvar or not_lval
	and TYPE_CODE_ARRAY.
---
 printcmd.c |   49 ++++++++++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 21 deletions(-)

--- a/printcmd.c
+++ b/printcmd.c
@@ -2343,32 +2343,39 @@ ui_printf (char *arg, struct ui_file *st
 	switch (argclass[i])
 	  {
 	  case string_arg:
-	    {
-	      gdb_byte *str;
-	      CORE_ADDR tem;
-	      int j;
+            if ((VALUE_LVAL (val_args[i]) == lval_internalvar
+		 || VALUE_LVAL (val_args[i]) == not_lval)
+		&& TYPE_CODE (check_typedef (value_type (val_args[i])))
+		       == TYPE_CODE_ARRAY)
+	      fprintf_filtered (stream, current_substring,
+				(char *) value_contents (val_args[i]));
+	    else
+	      {
+	        gdb_byte *str;
+	        CORE_ADDR tem;
+	        int j;

-	      tem = value_as_address (val_args[i]);
+	        tem = value_as_address (val_args[i]);

-	      /* This is a %s argument.  Find the length of the string.  */
-	      for (j = 0;; j++)
-		{
-		  gdb_byte c;
+	        /* This is a %s argument.  Find the length of the string.  */
+	        for (j = 0;; j++)
+		  {
+		    gdb_byte c;

-		  QUIT;
-		  read_memory (tem + j, &c, 1);
-		  if (c == 0)
-		    break;
-		}
+		    QUIT;
+		    read_memory (tem + j, &c, 1);
+		    if (c == 0)
+		      break;
+		  }

-	      /* Copy the string contents into a string inside GDB.  */
-	      str = (gdb_byte *) alloca (j + 1);
-	      if (j != 0)
-		read_memory (tem, str, j);
-	      str[j] = 0;
+	        /* Copy the string contents into a string inside GDB.  */
+	        str = (gdb_byte *) alloca (j + 1);
+	        if (j != 0)
+		  read_memory (tem, str, j);
+	        str[j] = 0;

-              fprintf_filtered (stream, current_substring, (char *) str);
-	    }
+                fprintf_filtered (stream, current_substring, (char *) str);
+	      }
 	    break;
 	  case wide_string_arg:
 	    {


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-08-16  4:58       ` Hui Zhu
@ 2011-08-17 14:38         ` Tom Tromey
  2011-08-18  2:54           ` Hui Zhu
  0 siblings, 1 reply; 14+ messages in thread
From: Tom Tromey @ 2011-08-17 14:38 UTC (permalink / raw)
  To: Hui Zhu; +Cc: gdb-patches ml

>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:

>> I don't know howto input a  wide-string to GDB command line.
>> Could you help me with that and let me write a separate patch special
>> for wide-string case?

I think you can't make one directly without an inferior, but you can
make an array of integers using the {...} syntax.  Then, I think, you
can print this using "%ls".

>> +            if ((VALUE_LVAL (val_args[i]) == lval_internalvar
>> +		 || VALUE_LVAL (val_args[i]) == not_lval)

This test seems odd to me.  Why does the lvalue-ness of the value
matter?

Tom


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-08-17 14:38         ` Tom Tromey
@ 2011-08-18  2:54           ` Hui Zhu
  2011-08-19 14:15             ` Tom Tromey
  0 siblings, 1 reply; 14+ messages in thread
From: Hui Zhu @ 2011-08-18  2:54 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches ml

On Wed, Aug 17, 2011 at 22:38, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:
>
>>> I don't know howto input a  wide-string to GDB command line.
>>> Could you help me with that and let me write a separate patch special
>>> for wide-string case?
>
> I think you can't make one directly without an inferior, but you can
> make an array of integers using the {...} syntax.  Then, I think, you
> can print this using "%ls".
>
>>> +            if ((VALUE_LVAL (val_args[i]) == lval_internalvar
>>> +             || VALUE_LVAL (val_args[i]) == not_lval)
>
> This test seems odd to me.  Why does the lvalue-ness of the value
> matter?

I don't understand your means, could you do some explain?

Thanks,
Hui

>
> Tom
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-08-18  2:54           ` Hui Zhu
@ 2011-08-19 14:15             ` Tom Tromey
  2011-09-06  8:50               ` Hui Zhu
  0 siblings, 1 reply; 14+ messages in thread
From: Tom Tromey @ 2011-08-19 14:15 UTC (permalink / raw)
  To: Hui Zhu; +Cc: gdb-patches ml

>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:

Tom> This test seems odd to me.  Why does the lvalue-ness of the value
Tom> matter?

>> I don't understand your means, could you do some explain?

I just don't understand why a check of the VALUE_LVAL is necessary.
Does something go wrong in the other cases?  It doesn't seem to me that
it should.

Tom


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-08-19 14:15             ` Tom Tromey
@ 2011-09-06  8:50               ` Hui Zhu
  2011-09-06 13:44                 ` Jan Kratochvil
  0 siblings, 1 reply; 14+ messages in thread
From: Hui Zhu @ 2011-09-06  8:50 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches ml

[-- Attachment #1: Type: text/plain, Size: 788 bytes --]

On Fri, Aug 19, 2011 at 22:14, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:
>
> Tom> This test seems odd to me.  Why does the lvalue-ness of the value
> Tom> matter?
>
>>> I don't understand your means, could you do some explain?
>
> I just don't understand why a check of the VALUE_LVAL is necessary.
> Does something go wrong in the other cases?  It doesn't seem to me that
> it should.
>
> Tom
>

Got it.

I make a new patch remove the line:
>> +            if ((VALUE_LVAL (val_args[i]) == lval_internalvar
>> +             || VALUE_LVAL (val_args[i]) == not_lval)

Please help me review it.

Thanks,
Hui

2011-09-06  Hui Zhu  <teawater@gmail.com>

	* printcmd.c (ui_printf): Add a handler for TYPE_CODE_ARRAY.

[-- Attachment #2: fix-v-string.txt --]
[-- Type: text/plain, Size: 1731 bytes --]

---
 printcmd.c |   47 ++++++++++++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 21 deletions(-)

--- a/printcmd.c
+++ b/printcmd.c
@@ -2343,32 +2343,37 @@ ui_printf (char *arg, struct ui_file *st
 	switch (argclass[i])
 	  {
 	  case string_arg:
-	    {
-	      gdb_byte *str;
-	      CORE_ADDR tem;
-	      int j;
+            if (TYPE_CODE (check_typedef (value_type (val_args[i])))
+		    == TYPE_CODE_ARRAY)
+	      fprintf_filtered (stream, current_substring,
+				(char *) value_contents (val_args[i]));
+	    else
+	      {
+	        gdb_byte *str;
+	        CORE_ADDR tem;
+	        int j;
 
-	      tem = value_as_address (val_args[i]);
+	        tem = value_as_address (val_args[i]);
 
-	      /* This is a %s argument.  Find the length of the string.  */
-	      for (j = 0;; j++)
-		{
-		  gdb_byte c;
+	        /* This is a %s argument.  Find the length of the string.  */
+	        for (j = 0;; j++)
+		  {
+		    gdb_byte c;
 
-		  QUIT;
-		  read_memory (tem + j, &c, 1);
-		  if (c == 0)
-		    break;
-		}
+		    QUIT;
+		    read_memory (tem + j, &c, 1);
+		    if (c == 0)
+		      break;
+		  }
 
-	      /* Copy the string contents into a string inside GDB.  */
-	      str = (gdb_byte *) alloca (j + 1);
-	      if (j != 0)
-		read_memory (tem, str, j);
-	      str[j] = 0;
+	        /* Copy the string contents into a string inside GDB.  */
+	        str = (gdb_byte *) alloca (j + 1);
+	        if (j != 0)
+		  read_memory (tem, str, j);
+	        str[j] = 0;
 
-              fprintf_filtered (stream, current_substring, (char *) str);
-	    }
+                fprintf_filtered (stream, current_substring, (char *) str);
+	      }
 	    break;
 	  case wide_string_arg:
 	    {

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-09-06  8:50               ` Hui Zhu
@ 2011-09-06 13:44                 ` Jan Kratochvil
  2011-09-07  9:27                   ` Hui Zhu
  0 siblings, 1 reply; 14+ messages in thread
From: Jan Kratochvil @ 2011-09-06 13:44 UTC (permalink / raw)
  To: Hui Zhu; +Cc: Tom Tromey, gdb-patches ml

On Tue, 06 Sep 2011 10:21:53 +0200, Hui Zhu wrote:
> +            if (TYPE_CODE (check_typedef (value_type (val_args[i])))
> +		    == TYPE_CODE_ARRAY)
> +	      fprintf_filtered (stream, current_substring,
> +				(char *) value_contents (val_args[i]));

(gdb) set $a={'a','b'}
(gdb) printf "%s\n",$a
==23392== Invalid read of size 1
==23392==    at: vfprintf (vfprintf.c:1568)
==23392==    by: vasprintf (vasprintf.c:64)
==23392==    by: xstrvprintf (common-utils.c:131)
==23392==    by: vfprintf_maybe_filtered (utils.c:2379)
==23392==    by: vfprintf_filtered (utils.c:2389)
==23392==    by: fprintf_filtered (utils.c:2441)
==23392==    by: ui_printf (printcmd.c:2348)
[...]
==23392==  Address 0xd24bb82 is 0 bytes after a block of size 2 alloc'd
==23392==    at: calloc (vg_replace_malloc.c:467)
==23392==    by: xcalloc (common-utils.c:92)
==23392==    by: xzalloc (common-utils.c:102)
==23392==    by: allocate_value_contents (value.c:690)
==23392==    by: allocate_value (value.c:700)
==23392==    by: value_copy (value.c:1299)
==23392==    by: value_of_internalvar (value.c:1725)
==23392==    by: evaluate_subexp_standard (eval.c:903)
==23392==    by: evaluate_subexp_c (c-lang.c:720)
==23392==    by: evaluate_subexp (eval.c:76)
==23392==    by: evaluate_expression (eval.c:151)
==23392==    by: parse_to_comma_and_eval (eval.c:136)
==23392==    by: ui_printf (printcmd.c:2328)
[...]

The original code was not completely correct in such cases but GDB could not
crash, now it can, I find it as a regression.

I would welcome a testcase.


Thanks,
Jan


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-09-06 13:44                 ` Jan Kratochvil
@ 2011-09-07  9:27                   ` Hui Zhu
  2011-09-07 10:02                     ` Jan Kratochvil
  0 siblings, 1 reply; 14+ messages in thread
From: Hui Zhu @ 2011-09-07  9:27 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: Tom Tromey, gdb-patches ml

[-- Attachment #1: Type: text/plain, Size: 2042 bytes --]

On Tue, Sep 6, 2011 at 16:50, Jan Kratochvil <jan.kratochvil@redhat.com> wrote:
> On Tue, 06 Sep 2011 10:21:53 +0200, Hui Zhu wrote:
>> +            if (TYPE_CODE (check_typedef (value_type (val_args[i])))
>> +                 == TYPE_CODE_ARRAY)
>> +           fprintf_filtered (stream, current_substring,
>> +                             (char *) value_contents (val_args[i]));
>
> (gdb) set $a={'a','b'}
> (gdb) printf "%s\n",$a
> ==23392== Invalid read of size 1
> ==23392==    at: vfprintf (vfprintf.c:1568)
> ==23392==    by: vasprintf (vasprintf.c:64)
> ==23392==    by: xstrvprintf (common-utils.c:131)
> ==23392==    by: vfprintf_maybe_filtered (utils.c:2379)
> ==23392==    by: vfprintf_filtered (utils.c:2389)
> ==23392==    by: fprintf_filtered (utils.c:2441)
> ==23392==    by: ui_printf (printcmd.c:2348)
> [...]
> ==23392==  Address 0xd24bb82 is 0 bytes after a block of size 2 alloc'd
> ==23392==    at: calloc (vg_replace_malloc.c:467)
> ==23392==    by: xcalloc (common-utils.c:92)
> ==23392==    by: xzalloc (common-utils.c:102)
> ==23392==    by: allocate_value_contents (value.c:690)
> ==23392==    by: allocate_value (value.c:700)
> ==23392==    by: value_copy (value.c:1299)
> ==23392==    by: value_of_internalvar (value.c:1725)
> ==23392==    by: evaluate_subexp_standard (eval.c:903)
> ==23392==    by: evaluate_subexp_c (c-lang.c:720)
> ==23392==    by: evaluate_subexp (eval.c:76)
> ==23392==    by: evaluate_expression (eval.c:151)
> ==23392==    by: parse_to_comma_and_eval (eval.c:136)
> ==23392==    by: ui_printf (printcmd.c:2328)
> [...]
>
> The original code was not completely correct in such cases but GDB could not
> crash, now it can, I find it as a regression.
>
> I would welcome a testcase.
>
>
> Thanks,
> Jan
>

Thanks for remind me about it.
I make a patch add a test for it.

Please help me review it.

Best,
Hui

2011-09-07  Hui Zhu  <teawater@gmail.com>

	* gdb.base/printcmds.exp: Add test for printing internal var
	values with printf.

[-- Attachment #2: test-v-string.txt --]
[-- Type: text/plain, Size: 836 bytes --]

---
 testsuite/gdb.base/printcmds.exp |    8 ++++++++
 1 file changed, 8 insertions(+)

--- a/testsuite/gdb.base/printcmds.exp
+++ b/testsuite/gdb.base/printcmds.exp
@@ -775,6 +775,12 @@ proc test_printf_with_dfp {} {
     gdb_test "printf \"%DDf\\n\",1.2E6144dl" "1.200000000000000000000000000000000E\\+6144"
 }
 
+#Test printing internal var values with printf
+proc test_printf_with_internalvar {} {
+    gdb_test "set \$a={\'a\',\'b\'}" ""
+    gdb_test "printf \"%s\\n\", \$a" "ab"
+}
+
 # Escape a left curly brace to prevent it from being interpreted as 
 # the beginning of a bound
 proc gdb_test_escape_braces { args } {
@@ -816,6 +822,8 @@ if { [test_compiler_info "armcc-*"] } {
 }
 gdb_test "p ctable1\[120\]" "120 'x'" "p ctable1\[120\] #1"
 
+test_printf_with_internalvar
+
 gdb_load ${binfile}
 
 if ![runto_main] then {

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-09-07  9:27                   ` Hui Zhu
@ 2011-09-07 10:02                     ` Jan Kratochvil
  2011-09-09  7:59                       ` Hui Zhu
  0 siblings, 1 reply; 14+ messages in thread
From: Jan Kratochvil @ 2011-09-07 10:02 UTC (permalink / raw)
  To: Hui Zhu; +Cc: Tom Tromey, gdb-patches ml

On Wed, 07 Sep 2011 04:55:32 +0200, Hui Zhu wrote:
> +#Test printing internal var values with printf
> +proc test_printf_with_internalvar {} {
> +    gdb_test "set \$a={\'a\',\'b\'}" ""

Escaping of ' is redundant.

> +    gdb_test "printf \"%s\\n\", \$a" "ab"

I would find easier { and } instead of " and " to prevent so many backslashes
but opinions differ on it, FYI:
    gdb_test_no_output {set $a={'a','b'}}
    gdb_test {printf "%s\n", $a} "ab"


Still this testcase FAILs for me:
printf "%s\n", $a^M
abX^M
(gdb) FAIL: gdb.base/printcmds.exp: printf "%s\n", $a

Where X is some binary unprintable garbage character, not sure which way you
plan to fix it (maybe some `error' call if there is no zero-terminator?).


Thanks,
Jan


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-09-07 10:02                     ` Jan Kratochvil
@ 2011-09-09  7:59                       ` Hui Zhu
  2011-09-09 12:32                         ` Jan Kratochvil
  0 siblings, 1 reply; 14+ messages in thread
From: Hui Zhu @ 2011-09-09  7:59 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: Tom Tromey, gdb-patches ml

[-- Attachment #1: Type: text/plain, Size: 1300 bytes --]

On Wed, Sep 7, 2011 at 17:58, Jan Kratochvil <jan.kratochvil@redhat.com> wrote:
> On Wed, 07 Sep 2011 04:55:32 +0200, Hui Zhu wrote:
>> +#Test printing internal var values with printf
>> +proc test_printf_with_internalvar {} {
>> +    gdb_test "set \$a={\'a\',\'b\'}" ""
>
> Escaping of ' is redundant.
>
>> +    gdb_test "printf \"%s\\n\", \$a" "ab"
>
> I would find easier { and } instead of " and " to prevent so many backslashes
> but opinions differ on it, FYI:
>    gdb_test_no_output {set $a={'a','b'}}
>    gdb_test {printf "%s\n", $a} "ab"
>

Thanks for your help Jan.

I make a new patch according to it.  And I add more
test_printf_with_internalvar in the end of test because I think it can
test this issue after the inferior exec.
>
> Still this testcase FAILs for me:
> printf "%s\n", $a^M
> abX^M
> (gdb) FAIL: gdb.base/printcmds.exp: printf "%s\n", $a
>
> Where X is some binary unprintable garbage character, not sure which way you
> plan to fix it (maybe some `error' call if there is no zero-terminator?).

I didn't got this issue.  Could you post more info about this issue?

>
>
> Thanks,
> Jan
>

Best,
Hui

2011-09-07  Hui Zhu  <teawater@gmail.com>

	* gdb.base/printcmds.exp: Add test for printing internal var
	values with printf.

[-- Attachment #2: test-v-string.txt --]
[-- Type: text/plain, Size: 965 bytes --]

---
 testsuite/gdb.base/printcmds.exp |    9 +++++++++
 1 file changed, 9 insertions(+)

--- a/testsuite/gdb.base/printcmds.exp
+++ b/testsuite/gdb.base/printcmds.exp
@@ -775,6 +775,12 @@ proc test_printf_with_dfp {} {
     gdb_test "printf \"%DDf\\n\",1.2E6144dl" "1.200000000000000000000000000000000E\\+6144"
 }
 
+#Test printing internal var values with printf
+proc test_printf_with_internalvar {} {
+    gdb_test_no_output {set $a={'a','b'}}
+    gdb_test {printf "%s\n", $a} "ab"
+}
+
 # Escape a left curly brace to prevent it from being interpreted as 
 # the beginning of a bound
 proc gdb_test_escape_braces { args } {
@@ -816,6 +822,8 @@ if { [test_compiler_info "armcc-*"] } {
 }
 gdb_test "p ctable1\[120\]" "120 'x'" "p ctable1\[120\] #1"
 
+test_printf_with_internalvar
+
 gdb_load ${binfile}
 
 if ![runto_main] then {
@@ -842,3 +850,4 @@ test_print_array_constants
 test_print_enums
 test_printf
 test_printf_with_dfp
+test_printf_with_internalvar

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory
  2011-09-09  7:59                       ` Hui Zhu
@ 2011-09-09 12:32                         ` Jan Kratochvil
  0 siblings, 0 replies; 14+ messages in thread
From: Jan Kratochvil @ 2011-09-09 12:32 UTC (permalink / raw)
  To: Hui Zhu; +Cc: Tom Tromey, gdb-patches ml

On Fri, 09 Sep 2011 09:27:09 +0200, Hui Zhu wrote:
> >    gdb_test_no_output {set $a={'a','b'}}

(I forgot to state the original gdb_test expect string "" matches any output.)


> > Still this testcase FAILs for me:
> > printf "%s\n", $a^M
> > abX^M
> > (gdb) FAIL: gdb.base/printcmds.exp: printf "%s\n", $a
> >
> > Where X is some binary unprintable garbage character, not sure which way you
> > plan to fix it (maybe some `error' call if there is no zero-terminator?).
> 
> I didn't got this issue.  Could you post more info about this issue?

The primary problem is the regression with valgrind, as shown in:
	http://sourceware.org/ml/gdb-patches/2011-09/msg00084.html
	http://sourceware.org/gdb/wiki/TestingGDB#Running_GDB_under_Valgrind_in_the_testsuite

The valgrind regression is present even with live inferior (but GDB must not
valgrind-complain even on commands which were refused with error before).

Moreover when I link GDB with -lmcheck (which I always do) I get the testcase
FAIL as shown above but that is exactly the same GDB code patch problem as the
valgrind-shown case.

I did not try to but I think with proper size of the $a array GDB will crash.


very minor issue: Now with your recent change it will with -lmcheck:
	FAIL: gdb.base/printcmds.exp: printf "%s\n", $a
	FAIL: gdb.base/printcmds.exp: printf "%s\n", $a
 - the testcase names should be unique.


Thanks,
Jan


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2011-09-09  7:59 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-20 10:37 [PATCH] printcmd.c (ui_printf): make internalvar string can be printf and eval when inferior cannot alloc memory Hui Zhu
2011-07-21 20:43 ` Tom Tromey
2011-08-14 15:10   ` Hui Zhu
2011-08-15 19:06     ` Tom Tromey
2011-08-16  4:58       ` Hui Zhu
2011-08-17 14:38         ` Tom Tromey
2011-08-18  2:54           ` Hui Zhu
2011-08-19 14:15             ` Tom Tromey
2011-09-06  8:50               ` Hui Zhu
2011-09-06 13:44                 ` Jan Kratochvil
2011-09-07  9:27                   ` Hui Zhu
2011-09-07 10:02                     ` Jan Kratochvil
2011-09-09  7:59                       ` Hui Zhu
2011-09-09 12:32                         ` Jan Kratochvil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox