* printfs output in wrong order in MI
@ 2006-04-12 16:54 Vladimir Prus
2006-04-13 8:02 ` Vladimir Prus
0 siblings, 1 reply; 6+ messages in thread
From: Vladimir Prus @ 2006-04-12 16:54 UTC (permalink / raw)
To: gdb
Hi,
I'm seing strange behaviour of the "printf" command if used in MI:
(gdb)
print &i
&"print &i\n"
~"$1 = (int *) 0xbfc5f484\n"
^done
(gdb)
printf "%x", &i
&"printf \"%x\", &i\n"
^done
(gdb)
~"bfc5f484"
-interpreter-exec console "printf \"%x\", &i"
^done
(gdb)
~"bfc5f484"
The output of 'print' appears before "^done", but the output of "printf"
appears *after* both "^done", and the prompt, which makes it impossible to
reliably catch the output.
I'm trying to use 'printf' in order to obtain addresses of all local
variables in one command, without round-trips for every local variable.
This behaviour of 'printfs' seems to prevent my trick from working.
Any ideas why "printf" is so special?
- Volodya
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: printfs output in wrong order in MI
2006-04-12 16:54 printfs output in wrong order in MI Vladimir Prus
@ 2006-04-13 8:02 ` Vladimir Prus
2006-04-13 8:10 ` Eli Zaretskii
0 siblings, 1 reply; 6+ messages in thread
From: Vladimir Prus @ 2006-04-13 8:02 UTC (permalink / raw)
To: gdb
Vladimir Prus wrote:
>
> Hi,
> I'm seing strange behaviour of the "printf" command if used in MI:
>
> (gdb)
> print &i
> &"print &i\n"
> ~"$1 = (int *) 0xbfc5f484\n"
> ^done
> (gdb)
> printf "%x", &i
> &"printf \"%x\", &i\n"
> ^done
> (gdb)
> ~"bfc5f484"
> -interpreter-exec console "printf \"%x\", &i"
> ^done
> (gdb)
> ~"bfc5f484"
>
> The output of 'print' appears before "^done", but the output of "printf"
> appears *after* both "^done", and the prompt, which makes it impossible to
> reliably catch the output.
....
> Any ideas why "printf" is so special?
I turns out that using
printf "%x\n", &i
(that is, adding "\n"), fixes the problem. Still looks like a bug to me,
though.
- Volodya
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: printfs output in wrong order in MI
2006-04-13 8:02 ` Vladimir Prus
@ 2006-04-13 8:10 ` Eli Zaretskii
2006-04-13 8:20 ` Vladimir Prus
0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2006-04-13 8:10 UTC (permalink / raw)
To: Vladimir Prus; +Cc: gdb
> From: Vladimir Prus <ghost@cs.msu.su>
> Date: Thu, 13 Apr 2006 11:02:23 +0400
>
> Vladimir Prus wrote:
>
> >
> > Hi,
> > I'm seing strange behaviour of the "printf" command if used in MI:
> >
> > (gdb)
> > print &i
> > &"print &i\n"
> > ~"$1 = (int *) 0xbfc5f484\n"
> > ^done
> > (gdb)
> > printf "%x", &i
> > &"printf \"%x\", &i\n"
> > ^done
> > (gdb)
> > ~"bfc5f484"
> > -interpreter-exec console "printf \"%x\", &i"
> > ^done
> > (gdb)
> > ~"bfc5f484"
> >
> > The output of 'print' appears before "^done", but the output of "printf"
> > appears *after* both "^done", and the prompt, which makes it impossible to
> > reliably catch the output.
> ....
> > Any ideas why "printf" is so special?
>
> I turns out that using
>
> printf "%x\n", &i
>
> (that is, adding "\n"), fixes the problem. Still looks like a bug to me,
> though.
I think it's expected behavior: printf uses buffered output, so if you
don't finish the line with a newline, the buffer is not flushed.
Similar ``bugs'' can be seen in any C program that displays characters
via printf without \n or fflush.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: printfs output in wrong order in MI
2006-04-13 8:10 ` Eli Zaretskii
@ 2006-04-13 8:20 ` Vladimir Prus
2006-04-13 16:04 ` Eli Zaretskii
0 siblings, 1 reply; 6+ messages in thread
From: Vladimir Prus @ 2006-04-13 8:20 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: gdb
On Thursday 13 April 2006 11:55, Eli Zaretskii wrote:
> > > Any ideas why "printf" is so special?
> >
> > I turns out that using
> >
> > printf "%x\n", &i
> >
> > (that is, adding "\n"), fixes the problem. Still looks like a bug to me,
> > though.
>
> I think it's expected behavior: printf uses buffered output,
Where in documentation is it stated that 'printf' uses buffered output?
> so if you
> don't finish the line with a newline, the buffer is not flushed.
> Similar ``bugs'' can be seen in any C program that displays characters
> via printf without \n or fflush.
Isn't it possible to auto-flush the buffer at the end of "-interpreter-exec
console" execution it's possible to flush the buffers? I think it's
reasonable to expect that *all* output of command wrapped in
"-interpreter-exec console" would appears before the "^done" marker.
- Volodya
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: printfs output in wrong order in MI
2006-04-13 8:20 ` Vladimir Prus
@ 2006-04-13 16:04 ` Eli Zaretskii
2006-04-14 13:38 ` Daniel Jacobowitz
0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2006-04-13 16:04 UTC (permalink / raw)
To: Vladimir Prus; +Cc: gdb
> From: Vladimir Prus <ghost@cs.msu.su>
> Date: Thu, 13 Apr 2006 12:01:57 +0400
> Cc: gdb@sources.redhat.com
>
> > > (that is, adding "\n"), fixes the problem. Still looks like a bug to me,
> > > though.
> >
> > I think it's expected behavior: printf uses buffered output,
>
> Where in documentation is it stated that 'printf' uses buffered output?
I'll welcome patches to document that, TIA.
> Isn't it possible to auto-flush the buffer at the end of "-interpreter-exec
> console" execution it's possible to flush the buffers?
It is usually a performance tradeoff: GDB has no idea whether a single
printf command is all you will want to print, or a part of a block of
commands that your front end feeds one after the other. In the latter
case, flushing the buffers will be a performance hit.
The current code assumes that \n marks the end of the values you want
to see output, so line buffering generally does The Right Thing
(because, at least in interactive CLI usage, no one in their right
mind will want to print strings unterminated by a newline: it would
cause the next GDB prompt to not be the first thing on the line).
> I think it's reasonable to expect that *all* output of command
> wrapped in "-interpreter-exec console" would appears before the
> "^done" marker.
Is it? I wouldn't know; I'll let other front-end gurus to comment on
that.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: printfs output in wrong order in MI
2006-04-13 16:04 ` Eli Zaretskii
@ 2006-04-14 13:38 ` Daniel Jacobowitz
0 siblings, 0 replies; 6+ messages in thread
From: Daniel Jacobowitz @ 2006-04-14 13:38 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Vladimir Prus, gdb
On Thu, Apr 13, 2006 at 12:25:45PM +0300, Eli Zaretskii wrote:
> > I think it's reasonable to expect that *all* output of command
> > wrapped in "-interpreter-exec console" would appears before the
> > "^done" marker.
>
> Is it? I wouldn't know; I'll let other front-end gurus to comment on
> that.
I agree.
Suppose you define a user defined command with multiple printfs; even
from MI, we don't need to (or want to) flush between them. But when
-interpreter-exec return ^done, it ought to be ^done. Flushing the
output streams explicitly before then seems like a good idea to me.
--
Daniel Jacobowitz
CodeSourcery
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2006-04-14 13:17 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-12 16:54 printfs output in wrong order in MI Vladimir Prus
2006-04-13 8:02 ` Vladimir Prus
2006-04-13 8:10 ` Eli Zaretskii
2006-04-13 8:20 ` Vladimir Prus
2006-04-13 16:04 ` Eli Zaretskii
2006-04-14 13:38 ` Daniel Jacobowitz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox