Page 1 of 1

64bit printf

Posted: Sat Jun 07, 2008 10:55 pm
by blound
I am having a problem with my 64 bit kernel and printf. I have tested my puts and putch routines to death, and have put in my own basic printhex/printdecimal wrapper functions around them and they work. the problem is that when I do simple printfs I get random values from the stack.
In my boot assembly file I declare my stack as:

Code: Select all

kernel_stack:
		times 32768 db 0xcc
to catch errors. Now in my kmain I have:

Code: Select all

int r = 0x33413442;
	
puts("\n-----\n");
puts((char *)&r);
puts("\n-----\n");
	
printf("r is %x\n",r);
(printf ideals with the va_list etc then sends the final string to puts)

and the puts routine prints 'r' properly but printf prints 0xcccccccc which is somewhere random on the stack. I have tried many different combinations of print modifiers and none of them work. I have also tried moving where the variable im printing is located (bss/data) etc and same results.

I don't think its stack corruption on my side because puts is working ifne otherwise.

I was also wondering what should be the proper type for rounding inside of the va_ functions. 32 bit systems use size of (4 bytes), but shouldnt 64 bit systems use sizeof(long) (8 bytes)? I tried both and neither fixed my problem.

Does someone have a working 64bit printf so I can test to make sure i am not completely off or has anyone else had problems with this? I have tried 3 different printf implementations and all have the same problem.

Posted: Sun Jun 08, 2008 2:23 am
by xyzzy
I'm using the vsnprintf function from Linux, which works fine on both my IA32 and x86_64 ports.

The vsnprintf function is here and the kprintf function is here

Posted: Sun Jun 08, 2008 3:33 am
by Korona
The va_arg macros on x86-64 don't work like the va_arg macros in 32bit mode. Try to use the va_arg macros from a working system. (e.g. copy them from linux)

Posted: Sun Jun 08, 2008 5:56 am
by xyzzy
Korona wrote:The va_arg macros on x86-64 don't work like the va_arg macros in 32bit mode. Try to use the va_arg macros from a working system. (e.g. copy them from linux)
I personally find it easier to just use the GCC builtins for it (__builtin_va_arg, __builtin_va_start, __builtin_va_end).

Posted: Sun Jun 08, 2008 6:19 am
by Korona
IIRC I also used them when I was coding in C, but they don't appear in the latest gcc manuals so I thought that they were deprecated.