[comp.sys.amiga] printf

ejkst@cisunx.UUCP (Eric J. Kennedy) (03/03/88)

I was trying to debug a small program last night, using the world's most
popular debugger:

#ifdef DEBUG
printf("At such-N-such a location\n");
#endif

I was trying to track down a bug that was causing an immediate 'task
held' requester.  The problem appeared to be comming at the very
beginning of the program, so I inserted a printf("Beginning\n"); as the
first line of the program, after the declarations.  I still got a 'task
held' requester _before_ anything printed out.  Well, I figured out that
the bug was in a fscanf(file,"%d",n), (forgot the &n) on about the
fourth line of the program.
 
Now, my question is, why the heck didn't my message print out if it was
before the actual bug?  Is this a side effect of multitasking?  Is it
because my program and the console device are two different tasks, and
my program was crashing before the console device got around to
displaying the message?  If so, how can I get around it?  I sure don't
want to 
 
printf("...");
getchar(...);     /* stop program, wait for console to catch up */

or something silly like that.  

Thanks,

-- 
------------
Eric Kennedy
ejkst@cisunx.UUCP

dillon@CORY.BERKELEY.EDU (Matt Dillon) (03/03/88)

:beginning of the program, so I inserted a printf("Beginning\n"); as the
:first line of the program, after the declarations.  I still got a 'task
:held' requester _before_ anything printed out.  Well, I figured out that
:the bug was in a fscanf(file,"%d",n), (forgot the &n) on about the
:fourth line of the program.

	No, it should have printed it before crashing.  I don't know 
about lattice, but in Aztec C the stdout buffer is flushed whenever a \n
is encountered.  Try sticking a fflush(stdout) after the printf...

						-Matt

dillon@CORY.BERKELEY.EDU (Matt Dillon) (03/03/88)

:beginning of the program, so I inserted a printf("Beginning\n"); as the
:first line of the program, after the declarations.  I still got a 'task
:held' requester _before_ anything printed out.  Well, I figured out that
:the bug was in a fscanf(file,"%d",n), (forgot the &n) on about the
:fourth line of the program.

	Oh, I forgot to mention.  If your stdout is NOT a console 
device (i.e. is a file), it is usually *not* flushed when \n is
encountered.

				-Matt

kenchiu@phoenix.Princeton.EDU (Kenneth Chiu) (03/03/88)

In article <7364@cisunx.UUCP> ejkst@cisunx.UUCP (Eric J. Kennedy) writes:
>I inserted a printf("Beginning\n"); as the
>first line of the program, after the declarations.
 . . . .
>Now, my question is, why the heck didn't my message print out if it was
>before the actual bug?

Are you absolutely sure that you included '\n' in your printf?  Manx (and
probably Lattice, though I'm not sure) will buffer the output until a new
line, file closed, flushed explicitly, or buffer full.

Ken Chiu

cmcmanis%pepper@Sun.COM (Chuck McManis) (03/04/88)

In article <7364@cisunx.UUCP> ejkst@cisunx.UUCP (Eric J. Kennedy) writes:
>Now, my question is, why the heck didn't my message print out if it was
>before the actual bug?  

Well if the lack of the pointer was the true reason for the bug then I 
can't guess why it wouldn't print out the error. If however you had a
statement like :

	int	foo = somefunc(value);
	    -or a favorite-
	UBYTE	data[47];
	int	datum = *(int *)(&data[3]);
		^- Causes odd address GURU because data[3] is on odd word

in your declarations somewhere then you might have a problem. Look at
the assembly source and find out where it really starts executing the
code. And also to see if anything besides the startup file gets executed
before the compiler. Last but not least make sure you are using the correct
startup code for the compiler/version you are running. 

--Chuck McManis
uucp: {anywhere}!sun!cmcmanis   BIX: cmcmanis  ARPAnet: cmcmanis@sun.com
These opinions are my own and no one elses, but you knew that didn't you.

fnf@fishpond.UUCP (Fred Fish) (03/04/88)

In article <7364@cisunx.UUCP> ejkst@cisunx.UUCP (Eric J. Kennedy) writes:
>I was trying to track down a bug that was causing an immediate 'task
>held' requester.  The problem appeared to be comming at the very
>beginning of the program, so I inserted a printf("Beginning\n"); as the
>first line of the program, after the declarations.  I still got a 'task
>held' requester _before_ anything printed out.
> 
>Now, my question is, why the heck didn't my message print out if it was
>before the actual bug?  Is this a side effect of multitasking?  Is it

Most likely you would have gotten your message if you had flushed
your stdio output buffers via an "fflush" call.  A delay after the
fflush might also be necessary.

Both of these facilities are provided automatically by my macro based
debugging package (dbug) distributed on several disks in my library.
I believe the latest version is on disk 102.  You can even specify
the delay interval after each debug output line, in 0.10 second
increments.  And best of all, you don't have to remove the debugging
support to disable it or clean up your code.  Thus when the next
bug crops up, you'll be "armed and ready".

-Fred
 ><>

-- 
# Fred Fish    hao!noao!mcdsun!fishpond!fnf     (602) 921-1113
# Ye Olde Fishpond, 1346 West 10th Place, Tempe, AZ 85281  USA

ejkst@cisunx.UUCP (Eric J. Kennedy) (03/04/88)

In article <8803030730.AA20513@cory.Berkeley.EDU>, dillon@CORY.BERKELEY.EDU (Matt Dillon) writes:
> :beginning of the program, so I inserted a printf("Beginning\n"); as the
> :first line of the program, after the declarations.  I still got a 'task
> :held' requester _before_ anything printed out.  Well, I figured out that
> :the bug was in a fscanf(file,"%d",n), (forgot the &n) on about the
> :fourth line of the program.
 
> 	No, it should have printed it before crashing.  I don't know 
> about lattice, but in Aztec C the stdout buffer is flushed whenever a \n
> is encountered.  Try sticking a fflush(stdout) after the printf...
 
 						-Matt

I'm using Aztec.  I think I might have left out the \n, now that I think
about it.  That certainly would do it, huh?  Also, someone else
suggested using fprintf(stderr,...);  which should also work fine.
 
Thanks

-- 
------------
Eric Kennedy
ejkst@cisunx.UUCP

shimoda@rmi.UUCP (Markus Schmidt) (03/07/88)

Hi!

I may be a little late with my hint, but I may be helpful for someone
although:
On Manx use puts("I am in the midth of desaster()") rather than
printf("...");  
puts flushes the stream automatically while I think that printf()
does *not* always at |\\n.

C u
Markus
(shimoda@rmi.uucp)