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.
IanSeyler
Member
Posts: 326 Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:
Post
by IanSeyler » Thu Apr 25, 2013 2:49 pm
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().
Kazinsal
Member
Posts: 559 Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:
Post
by Kazinsal » Thu Apr 25, 2013 3:05 pm
I would wager it's because of the newline and not an arbitrary 24-character limit. Try removing the newline?
IanSeyler
Member
Posts: 326 Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:
Post
by IanSeyler » Fri Apr 26, 2013 8:56 am
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?
Kazinsal
Member
Posts: 559 Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:
Post
by Kazinsal » Fri Apr 26, 2013 9:59 am
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
Posts: 923 Joined: Thu Jul 05, 2012 5:12 am
Location: Finland
Post
by Antti » Fri Apr 26, 2013 10:01 am
Does your write() correctly handle the newline?
bluemoon
Member
Posts: 1761 Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong
Post
by bluemoon » Fri Apr 26, 2013 12:13 pm
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;
}
IanSeyler
Member
Posts: 326 Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:
Post
by IanSeyler » Fri Apr 26, 2013 12:40 pm
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.
IanSeyler
Member
Posts: 326 Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:
Post
by IanSeyler » Fri Apr 26, 2013 1:35 pm
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.
IanSeyler
Member
Posts: 326 Joined: Mon Jul 28, 2008 9:46 am
Location: Ontario, Canada
Contact:
Post
by IanSeyler » Mon Apr 29, 2013 2:46 pm
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!
Owen
Member
Posts: 1700 Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:
Post
by Owen » Mon Apr 29, 2013 4:25 pm
ReturnInfinity wrote: I'm also thinking it could be due to me not zeroing BSS.
That is part of the contract for BSS...
bluemoon
Member
Posts: 1761 Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong
Post
by bluemoon » Mon Apr 29, 2013 11:34 pm
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
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.
sortie
Member
Posts: 931 Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie
Post
by sortie » Tue Apr 30, 2013 12:15 am
If you don't already, you ought to be using a real cross-compiler to build your kernel and user-space. Please do that.
dozniak
Member
Posts: 723 Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia
Post
by dozniak » Tue Apr 30, 2013 2:40 am
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
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.
bluemoon
Member
Posts: 1761 Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong
Post
by bluemoon » Tue Apr 30, 2013 3:05 am
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.
dozniak
Member
Posts: 723 Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia
Post
by dozniak » Tue Apr 30, 2013 5:24 am
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.