* gdb printing of dynamically allocated matrix
@ 2007-07-18 17:59 Peter Toft
2007-07-18 19:32 ` Daniel Jacobowitz
0 siblings, 1 reply; 10+ messages in thread
From: Peter Toft @ 2007-07-18 17:59 UTC (permalink / raw)
To: gdb
Focus of this mail is GDB on 1D and 2D arrays.
I have pasted a small C-code program at
http://pastebin.com/f63009dd8
(download from http://pastebin.com/pastebin.php?dl=f63009dd8)
which I like your opinion on.
I compile it with "gcc -g"
Throw a breakpoint on line 59 and run under gdb.
* I can see the contents of the "a"-vector in by
(gdb) print *a @ 3
Very cool!
* I can see the matrix (2D array) contents of the "c"
matrix by
(gdb) print c
Elegant - very cool!
* However how can I use GDB to see the contents of "b"
similar to the contents of "c" and "a"?
The best I can do is
(gdb) print *b[0] @3
(gdb) print *b[1] @3
Obviously I would like to do the display in one command rather
than several, especially if I changed the b-matrix to be b[7][8]
I think I will manually have to supply the length of the vectors,
but it seems that I cannot do a compound GDB-operation like
(gdb) print **b @3 @2
and get
{{2, -2, 22},{12 -12 -12}}
instead I get
{{2, -2, 22},{17# 12# -12}}
where I have inserted #-marks where I find wrong values.
How come that the next vector contains wrong values?
Or rather - how can I see the b content in one command?
Best
--
Peter Toft <pto@linuxbog.dk>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-18 17:59 gdb printing of dynamically allocated matrix Peter Toft
@ 2007-07-18 19:32 ` Daniel Jacobowitz
2007-07-18 20:32 ` Peter Toft
0 siblings, 1 reply; 10+ messages in thread
From: Daniel Jacobowitz @ 2007-07-18 19:32 UTC (permalink / raw)
To: Peter Toft; +Cc: gdb
On Wed, Jul 18, 2007 at 01:22:33PM +0200, Peter Toft wrote:
> * However how can I use GDB to see the contents of "b"
> similar to the contents of "c" and "a"?
> The best I can do is
> (gdb) print *b[0] @3
> (gdb) print *b[1] @3
> Obviously I would like to do the display in one command rather
> than several, especially if I changed the b-matrix to be b[7][8]
I don't believe this is possible without writing a user-defined
command for it - see the manual.
> How come that the next vector contains wrong values?
It's printing six consecutive elements from memory.
--
Daniel Jacobowitz
CodeSourcery
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-18 19:32 ` Daniel Jacobowitz
@ 2007-07-18 20:32 ` Peter Toft
2007-07-18 21:56 ` Peter Toft
0 siblings, 1 reply; 10+ messages in thread
From: Peter Toft @ 2007-07-18 20:32 UTC (permalink / raw)
To: gdb
[-- Attachment #1: Type: TEXT/PLAIN, Size: 1077 bytes --]
On Wed, 18 Jul 2007, Daniel Jacobowitz wrote:
> On Wed, Jul 18, 2007 at 01:22:33PM +0200, Peter Toft wrote:
> > * However how can I use GDB to see the contents of "b"
> > similar to the contents of "c" and "a"?
> > The best I can do is
> > (gdb) print *b[0] @3
> > (gdb) print *b[1] @3
> > Obviously I would like to do the display in one command rather
> > than several, especially if I changed the b-matrix to be b[7][8]
>
> I don't believe this is possible without writing a user-defined
> command for it - see the manual.
Something like
define matprint
set $i = 0
while $i< $arg1
echo $arg0[$i++][0] @ $arg2
end
end
so I then can do
(gdb) matprint c 2 3
close but I quess it can be made better[1]
>
> > How come that the next vector contains wrong values?
>
> It's printing six consecutive elements from memory.
Yeah - seems to be the case
Thanx Daniel
Best regards
Peter
[1] after GDB-101 manual crash reading :)
--
Peter Toft, Ph.D. [pto@linuxbog.dk] http://petertoft.dk
Følg min Linux-blog på http://www.version2.dk/blogs/petertoft
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-18 20:32 ` Peter Toft
@ 2007-07-18 21:56 ` Peter Toft
2007-07-19 3:10 ` Daniel Jacobowitz
2007-07-20 19:08 ` Jim Blandy
0 siblings, 2 replies; 10+ messages in thread
From: Peter Toft @ 2007-07-18 21:56 UTC (permalink / raw)
To: gdb
[-- Attachment #1: Type: TEXT/PLAIN, Size: 1954 bytes --]
On Wed, 18 Jul 2007, Peter Toft wrote:
> On Wed, 18 Jul 2007, Daniel Jacobowitz wrote:
>
> > On Wed, Jul 18, 2007 at 01:22:33PM +0200, Peter Toft wrote:
> > > * However how can I use GDB to see the contents of "b"
> > > similar to the contents of "c" and "a"?
> > > The best I can do is
> > > (gdb) print *b[0] @3
> > > (gdb) print *b[1] @3
> > > Obviously I would like to do the display in one command rather
> > > than several, especially if I changed the b-matrix to be b[7][8]
> >
> > I don't believe this is possible without writing a user-defined
> > command for it - see the manual.
>
> Something like
>
> define matprint
> set $i = 0
> while $i< $arg1
> echo $arg0[$i++][0] @ $arg2
> end
> end
>
> so I then can do
> (gdb) matprint c 2 3
>
> close but I quess it can be made better[1]
or ...
define matprint2
set $i = 0
print
printf "{{"
while $i< $arg1
set $j = 0
printf "%d",$arg0[$i][$j++]
while $j< $arg2
printf ", %d",$arg0[$i][$j++]
end
printf "}"
set $i = 1+$i
if $i < $arg1
printf ", {"
end
end
printf "}\n"
end
(gdb) matprint c 2 3
{{2, -2, 22}, {12, -12, 212}}
From the manual I don't understand how the macro is further hacked to also
give the exact output format as
(gdb) print c
$1 = {{2, -2, 22}, {12, -12, -12}}
so my matprint does not insert into a dollar variable - can someone help
with this last part?
/peter
>
>
> >
> > > How come that the next vector contains wrong values?
> >
> > It's printing six consecutive elements from memory.
>
> Yeah - seems to be the case
>
> Thanx Daniel
>
> Best regards
>
> Peter
>
> [1] after GDB-101 manual crash reading :)
>
> --
> Peter Toft, Ph.D. [pto@linuxbog.dk] http://petertoft.dk
> Følg min Linux-blog på http://www.version2.dk/blogs/petertoft
>
--
Peter Toft, Ph.D. [pto@linuxbog.dk] http://petertoft.dk
Følg min Linux-blog på http://www.version2.dk/blogs/petertoft
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-18 21:56 ` Peter Toft
@ 2007-07-19 3:10 ` Daniel Jacobowitz
2007-07-20 18:48 ` Peter Toft
2007-07-20 19:08 ` Jim Blandy
1 sibling, 1 reply; 10+ messages in thread
From: Daniel Jacobowitz @ 2007-07-19 3:10 UTC (permalink / raw)
To: Peter Toft; +Cc: gdb
On Wed, Jul 18, 2007 at 10:32:19PM +0200, Peter Toft wrote:
> From the manual I don't understand how the macro is further hacked to also
> give the exact output format as
> (gdb) print c
> $1 = {{2, -2, 22}, {12, -12, -12}}
>
> so my matprint does not insert into a dollar variable - can someone help
> with this last part?
I don't think you're going to be able to do that - our type system
does not deal well with structures that don't live exactly in target
memory. Yet.
--
Daniel Jacobowitz
CodeSourcery
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-19 3:10 ` Daniel Jacobowitz
@ 2007-07-20 18:48 ` Peter Toft
0 siblings, 0 replies; 10+ messages in thread
From: Peter Toft @ 2007-07-20 18:48 UTC (permalink / raw)
To: Daniel Jacobowitz; +Cc: gdb
[-- Attachment #1: Type: TEXT/PLAIN, Size: 885 bytes --]
On Wed, 18 Jul 2007, Daniel Jacobowitz wrote:
> On Wed, Jul 18, 2007 at 10:32:19PM +0200, Peter Toft wrote:
> > From the manual I don't understand how the macro is further hacked to also
> > give the exact output format as
> > (gdb) print c
> > $1 = {{2, -2, 22}, {12, -12, -12}}
> >
> > so my matprint does not insert into a dollar variable - can someone help
> > with this last part?
>
> I don't think you're going to be able to do that - our type system
> does not deal well with structures that don't live exactly in target
> memory. Yet.
ok - on your "Yet" statement -> such an improvement would be very
interesting for most designers working with numerical analysis,
signal&image processing, and similar fields of work.
Thank you again
Peter
--
Peter Toft, Ph.D. [pto@linuxbog.dk] http://petertoft.dk
Følg min Linux-blog på http://www.version2.dk/blogs/petertoft
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-18 21:56 ` Peter Toft
2007-07-19 3:10 ` Daniel Jacobowitz
@ 2007-07-20 19:08 ` Jim Blandy
2007-07-21 14:16 ` Peter Toft
1 sibling, 1 reply; 10+ messages in thread
From: Jim Blandy @ 2007-07-20 19:08 UTC (permalink / raw)
To: Peter Toft; +Cc: gdb
Peter Toft <pto@linuxbog.dk> writes:
>> (gdb) matprint c 2 3
Could you use sizeof to compute the dimensions automatically? Or at
least one of them?
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-20 19:08 ` Jim Blandy
@ 2007-07-21 14:16 ` Peter Toft
2007-07-23 17:05 ` Jim Blandy
0 siblings, 1 reply; 10+ messages in thread
From: Peter Toft @ 2007-07-21 14:16 UTC (permalink / raw)
To: Jim Blandy; +Cc: gdb
[-- Attachment #1: Type: TEXT/PLAIN, Size: 672 bytes --]
On Fri, 20 Jul 2007, Jim Blandy wrote:
>
> Peter Toft <pto@linuxbog.dk> writes:
> >> (gdb) matprint c 2 3
>
> Could you use sizeof to compute the dimensions automatically? Or at
> least one of them?
Dear Jim
I think I understand which direction you are point me to, but
"sizeof" in GDB does not ring a bell here. In C yes, but in GDB no.
Can you give me an example e.g. for 1D arrays? We might target this
snippet befor we move to 2D matrices;
int *a;
a=(int *)malloc(3*sizeof(int));
a[0] = 1;
a[1] = 6;
a[2] = 8;
Best
Peter
--
Peter Toft, Ph.D. [pto@linuxbog.dk] http://petertoft.dk
Følg min Linux-blog på http://www.version2.dk/blogs/petertoft
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-21 14:16 ` Peter Toft
@ 2007-07-23 17:05 ` Jim Blandy
2007-07-23 21:38 ` Peter Toft
0 siblings, 1 reply; 10+ messages in thread
From: Jim Blandy @ 2007-07-23 17:05 UTC (permalink / raw)
To: Peter Toft; +Cc: gdb
Peter Toft <pto@linuxbog.dk> writes:
> On Fri, 20 Jul 2007, Jim Blandy wrote:
>
>>
>> Peter Toft <pto@linuxbog.dk> writes:
>> >> (gdb) matprint c 2 3
>>
>> Could you use sizeof to compute the dimensions automatically? Or at
>> least one of them?
>
> Dear Jim
>
> I think I understand which direction you are point me to, but
> "sizeof" in GDB does not ring a bell here. In C yes, but in GDB no.
>
> Can you give me an example e.g. for 1D arrays? We might target this
> snippet befor we move to 2D matrices;
>
> int *a;
> a=(int *)malloc(3*sizeof(int));
> a[0] = 1;
> a[1] = 6;
> a[2] = 8;
Ah, if you're allocating dynamically, then there's no way to do it
using sizeof.
GDB supports the C 'sizeof' operator; in your example above, 'print
sizeof (a)' will give you '4' or '8' or whatever is appropriate for a
pointer on the program being debugged. There are no facilities (in C
or in GDB) for finding the size of a malloc block; since the library
may round up the size of the block, it might be impossible to do
without changing the heap data structure.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: gdb printing of dynamically allocated matrix
2007-07-23 17:05 ` Jim Blandy
@ 2007-07-23 21:38 ` Peter Toft
0 siblings, 0 replies; 10+ messages in thread
From: Peter Toft @ 2007-07-23 21:38 UTC (permalink / raw)
To: Jim Blandy; +Cc: gdb
[-- Attachment #1: Type: TEXT/PLAIN, Size: 1440 bytes --]
On Mon, 23 Jul 2007, Jim Blandy wrote:
>
> Peter Toft <pto@linuxbog.dk> writes:
> > On Fri, 20 Jul 2007, Jim Blandy wrote:
> >
> >>
> >> Peter Toft <pto@linuxbog.dk> writes:
> >> >> (gdb) matprint c 2 3
> >>
> >> Could you use sizeof to compute the dimensions automatically? Or at
> >> least one of them?
> >
> > Dear Jim
> >
> > I think I understand which direction you are point me to, but
> > "sizeof" in GDB does not ring a bell here. In C yes, but in GDB no.
> >
> > Can you give me an example e.g. for 1D arrays? We might target this
> > snippet befor we move to 2D matrices;
> >
> > int *a;
> > a=(int *)malloc(3*sizeof(int));
> > a[0] = 1;
> > a[1] = 6;
> > a[2] = 8;
>
> Ah, if you're allocating dynamically, then there's no way to do it
> using sizeof.
Clearly my focus is dynamic memory allocation.
> GDB supports the C 'sizeof' operator; in your example above, 'print
> sizeof (a)' will give you '4' or '8' or whatever is appropriate for a
> pointer on the program being debugged. There are no facilities (in C
> or in GDB) for finding the size of a malloc block; since the library
> may round up the size of the block, it might be impossible to do
> without changing the heap data structure.
Ok - we are now on the same (memory) page :-)
I was just a bit confused by your answer.
--
Peter Toft, Ph.D. [pto@linuxbog.dk] http://petertoft.dk
Følg min Linux-blog på http://www.version2.dk/blogs/petertoft
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2007-07-23 19:22 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-18 17:59 gdb printing of dynamically allocated matrix Peter Toft
2007-07-18 19:32 ` Daniel Jacobowitz
2007-07-18 20:32 ` Peter Toft
2007-07-18 21:56 ` Peter Toft
2007-07-19 3:10 ` Daniel Jacobowitz
2007-07-20 18:48 ` Peter Toft
2007-07-20 19:08 ` Jim Blandy
2007-07-21 14:16 ` Peter Toft
2007-07-23 17:05 ` Jim Blandy
2007-07-23 21:38 ` Peter Toft
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox