printf() problem with ported NewLib

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

printf() problem with ported NewLib

Post by IanSeyler »

I'm wondering if anyone else has seen this behavior.

Example:

Code: Select all

printf("NewLib Test Application\n=======================\n");
printf("%s %d\n", "Output:", 1234);
Result:

Code: Select all

NewLib Test Application
Output: 1234
The NewLib write() function was only called to print 24 characters on the first call to printf().
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: printf() problem with ported NewLib

Post by Kazinsal »

I would wager it's because of the newline and not an arbitrary 24-character limit. Try removing the newline?
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Re: printf() problem with ported NewLib

Post by IanSeyler »

Code:

Code: Select all

printf("NewLib Test Application=======================\n");
printf("%s %d\n", "Output:", 1234);
Results:

Code: Select all

NewLib Test Application=======================
Output: 1234
Looks like it stops printing after the first newline. That first call to printf() should result with one call to write(), correct?
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: printf() problem with ported NewLib

Post by Kazinsal »

My crystal ball tells me you haven't implemented isatty() and/or fstat(), so newlib is confused as to what buffering to use.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: printf() problem with ported NewLib

Post by Antti »

Does your write() correctly handle the newline?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: printf() problem with ported NewLib

Post by bluemoon »

ReturnInfinity wrote:Looks like it stops printing after the first newline. That first call to printf() should result with one call to write(), correct?
In my implementation, newlib make 2 calls to write with if there is line feed in middle of string:

Code: Select all

printf ("  PID[%d]: Hello again! counter=%d, foo=%d\n   line2\n", pid, i, g_foo.foo++);


Result:
SYSCALL : fstat(1, 7FBFF7D0)
SYSCALL : sbrk(00000430)
SYSCALL : sbrk(00000BD0)
SYSCALL : write(1, 01000010, 20)
FOO::FOO(0x10d008);
SYSCALL : write(1, 01000010, 21)
FOO::FOO(0x1000420);
SYSCALL : getpid() -> FFFFFFFF:801210D0: 1
SYSCALL : write(1, 01000010, 42)
  PID[1]: Hello again! counter=0, foo=123
SYSCALL : write(1, 01000010, 9)
   line2
SYSCALL : usleep(000F4240)
My fstat:

Code: Select all

long kservice_fstat(int fd, struct stat* st) {
    kprintf ( "SYSCALL : fstat(%d, %p)\n", fd, st);
    if ( st == NULL ) return -1;
    st->st_mode = S_IFCHR; // | S_IWUSR;
                           //    st->st_blksize = 1;
    return 0;
}                   
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Re: printf() problem with ported NewLib

Post by IanSeyler »

Blacklight, isatty() looks fine.. it was just returning 1 be default. I changed it to return 1 for STDOUT and STDERR. fstat was just set to minimal implementation of returning 0. I'll take a further look at fstat().

Antti, write() handles the newlines correctly. write(1, "test1\ntest2\n", 12) works as expected.
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Re: printf() problem with ported NewLib

Post by IanSeyler »

I think my issue is with sbrk(). A really simple program crashes whereas a more complicated one doesn't.

I get a page-fault with this:

Code: Select all

#include <stdio.h>

int main()
{
    printf("Hello1\nHello2\nHello3\n");
    return 0;
}
I'll do some digging and report back.
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
IanSeyler
Member
Member
Posts: 326
Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:

Re: printf() problem with ported NewLib

Post by IanSeyler »

My sbrk() is borked (I think)! I'm also thinking it could be due to me not zeroing BSS.

Should I be implementing a crt0.c file to run before calling main?

Should NewLib work correctly with flat binaries?

My linker script:

Code: Select all

OUTPUT_FORMAT("binary")
OUTPUT_ARCH("i386:x86-64")
ENTRY(main)
SECTIONS
{
        . = 0x0000000000200000;
        .text : { *(.text) }
        .data : { *(.data .rodata) QUAD(ADDR(.bss)+SIZEOF(.bss)) }
        .bss : { *(.bss) }
        end = .; _end = .; __end = .;
}
How I compile:

Code: Select all

gcc -I ../../newlib-2.0.0/newlib/libc/include/ -c helloc.c -o helloc.o
ld -T app.ld -o helloc.app helloc.o libc.a
More debugging to do!
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: printf() problem with ported NewLib

Post by Owen »

ReturnInfinity wrote:I'm also thinking it could be due to me not zeroing BSS.
That is part of the contract for BSS...
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: printf() problem with ported NewLib

Post by bluemoon »

Owen wrote:
ReturnInfinity wrote:I'm also thinking it could be due to me not zeroing BSS.
That is part of the contract for BSS...
LOL, I saw at least 5 people on this forum, including myself, made this mistake :mrgreen:
I know, when you drag newlib into your OS you just can't wait to make some test application, and missed this (and other) critical parts.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: printf() problem with ported NewLib

Post by sortie »

If you don't already, you ought to be using a real cross-compiler to build your kernel and user-space. Please do that.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: printf() problem with ported NewLib

Post by dozniak »

bluemoon wrote:
Owen wrote:
ReturnInfinity wrote:I'm also thinking it could be due to me not zeroing BSS.
That is part of the contract for BSS...
LOL, I saw at least 5 people on this forum, including myself, made this mistake :mrgreen:
I know, when you drag newlib into your OS you just can't wait to make some test application, and missed this (and other) critical parts.
If you're using multiboot bootloader, it clears out the bss for you. In any other case - it's up to you.
Learn to read.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: printf() problem with ported NewLib

Post by bluemoon »

dozniak wrote:If you're using multiboot bootloader, it clears out the bss for you. In any other case - it's up to you.
NewLib Test Application=======================
I presume we are dealing with loading application, not about loading the kernel from boot loader.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: printf() problem with ported NewLib

Post by dozniak »

bluemoon wrote:
dozniak wrote:If you're using multiboot bootloader, it clears out the bss for you. In any other case - it's up to you.
NewLib Test Application=======================
I presume we are dealing with loading application, not about loading the kernel from boot loader.
Oh, indeed. In this case the crt should deal with that.
Learn to read.
Post Reply