Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* sizeof evaluates expression to compute size?
@ 2015-09-16 14:41 Senthil Kumar Selvaraj
  2015-09-16 14:56 ` Joel Brobecker
  0 siblings, 1 reply; 4+ messages in thread
From: Senthil Kumar Selvaraj @ 2015-09-16 14:41 UTC (permalink / raw)
  To: gdb

Hi,

  Is it true that gdb evaluates the expression in a "print sizeof(expression)" 
  to figure out the size?
  
  For one expression on the ARM target 

     print sizeof((char*)(s->m)

  where s is a struct and m is a fixed length char array member)), gdb even 
  tries to allocate memory in the inferior (through an injected malloc call) to
  compute the size.

  Am I missing something? Is there a reason why gdb does this? Surely it
  should be able to compute the type (and hence the size) statically?

Regards
Senthil


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

* Re: sizeof evaluates expression to compute size?
  2015-09-16 14:41 sizeof evaluates expression to compute size? Senthil Kumar Selvaraj
@ 2015-09-16 14:56 ` Joel Brobecker
  2015-09-17 14:10   ` Senthil Kumar Selvaraj
  0 siblings, 1 reply; 4+ messages in thread
From: Joel Brobecker @ 2015-09-16 14:56 UTC (permalink / raw)
  To: Senthil Kumar Selvaraj; +Cc: gdb

>   Is it true that gdb evaluates the expression in a "print
>   sizeof(expression)" to figure out the size?

Yes; it actually evaluates the entire expression (including the use
of the sizeof operator).

>   For one expression on the ARM target 
> 
>      print sizeof((char*)(s->m)

The expression is incomplete; but with the expression you gave and
assuming we're just missing a closing parenthesis, I don't see why
it would need to allocate memory in the inferior. Perhaps you've found
a bug.

-- 
Joel


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

* Re: sizeof evaluates expression to compute size?
  2015-09-16 14:56 ` Joel Brobecker
@ 2015-09-17 14:10   ` Senthil Kumar Selvaraj
  2015-09-17 14:19     ` Joel Brobecker
  0 siblings, 1 reply; 4+ messages in thread
From: Senthil Kumar Selvaraj @ 2015-09-17 14:10 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb

On Wed, Sep 16, 2015 at 07:56:32AM -0700, Joel Brobecker wrote:
> >   Is it true that gdb evaluates the expression in a "print
> >   sizeof(expression)" to figure out the size?
> 
> Yes; it actually evaluates the entire expression (including the use
> of the sizeof operator).
> 
> >   For one expression on the ARM target 
> > 
> >      print sizeof((char*)(s->m)
> 
> The expression is incomplete; but with the expression you gave and
> assuming we're just missing a closing parenthesis, I don't see why
> it would need to allocate memory in the inferior. Perhaps you've found
> a bug.

Here's a more complete testcase.

typedef struct tagFoo
{
    int x;
    char arr[10];
} Foo;

Foo *pFoo;
int main()
{
  return 1;
}

Compiling with
$ gcc test.c -O0 -g3

and then starting gdb (native x86_64, v7.10) gives
$ gdb a.out
>>> print sizeof((char*)pFoo->arr) 
evaluation of this expression requires the target program to be active

Starting the program, putting a breakpoint on malloc, and then executing
the above command  causes the breakpoint to be hit.

Evaluation works fine without the cast to char*
>>> print sizeof(pFoo->arr)
$2 = 10

Should I file a bug?

Regards
Senthil


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

* Re: sizeof evaluates expression to compute size?
  2015-09-17 14:10   ` Senthil Kumar Selvaraj
@ 2015-09-17 14:19     ` Joel Brobecker
  0 siblings, 0 replies; 4+ messages in thread
From: Joel Brobecker @ 2015-09-17 14:19 UTC (permalink / raw)
  To: Senthil Kumar Selvaraj; +Cc: gdb

> Evaluation works fine without the cast to char*
> >>> print sizeof(pFoo->arr)
> $2 = 10

Ah, it's the cast that's causing it! I don't remember exactly
what the limitations where in terms of trying to limit those
inferior memory allocations. Perhaps it is a bug indeed. Perhaps
a limitation we'll have to live with.

Your example is not very meaningful, at the moment. You could
just do "print sizeof (char *)" for instance. That will give you
an answer that's different from what the actual size of your
component is (4 or 8 vs 10).

-- 
Joel


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

end of thread, other threads:[~2015-09-17 14:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-16 14:41 sizeof evaluates expression to compute size? Senthil Kumar Selvaraj
2015-09-16 14:56 ` Joel Brobecker
2015-09-17 14:10   ` Senthil Kumar Selvaraj
2015-09-17 14:19     ` Joel Brobecker

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