diff options
-rw-r--r-- | 02_process_init/README.md | 36 | ||||
-rw-r--r-- | 02_process_init/entry.c | 36 | ||||
-rw-r--r-- | include/fmt.h | 77 | ||||
-rw-r--r-- | include/io.h | 10 | ||||
-rw-r--r-- | test/checker.cc | 12 |
5 files changed, 88 insertions, 83 deletions
diff --git a/02_process_init/README.md b/02_process_init/README.md index 9dcda8d..fbe9c4a 100644 --- a/02_process_init/README.md +++ b/02_process_init/README.md @@ -222,31 +222,31 @@ Finally the data can be printed as ```c // entry ... - dynld_printf("Got %d arg(s)\n", argc); + pfmt("Got %d arg(s)\n", argc); for (const char** arg = argv; *arg; ++arg) { - dynld_printf("\targ = %s\n", *arg); + pfmt("\targ = %s\n", *arg); } const int max_env = 10; - dynld_printf("Print first %d env var(s)\n", max_env - 1); + pfmt("Print first %d env var(s)\n", max_env - 1); for (const char** env = envv; *env && (env - envv < max_env); ++env) { - dynld_printf("\tenv = %s\n", *env); + pfmt("\tenv = %s\n", *env); } - dynld_printf("Print auxiliary vector\n"); - dynld_printf("\tAT_EXECFD: %ld\n", auxv[AT_EXECFD]); - dynld_printf("\tAT_PHDR : %p\n", auxv[AT_PHDR]); - dynld_printf("\tAT_PHENT : %ld\n", auxv[AT_PHENT]); - dynld_printf("\tAT_PHNUM : %ld\n", auxv[AT_PHNUM]); - dynld_printf("\tAT_PAGESZ: %ld\n", auxv[AT_PAGESZ]); - dynld_printf("\tAT_BASE : %lx\n", auxv[AT_BASE]); - dynld_printf("\tAT_FLAGS : %ld\n", auxv[AT_FLAGS]); - dynld_printf("\tAT_ENTRY : %p\n", auxv[AT_ENTRY]); - dynld_printf("\tAT_NOTELF: %lx\n", auxv[AT_NOTELF]); - dynld_printf("\tAT_UID : %ld\n", auxv[AT_UID]); - dynld_printf("\tAT_EUID : %ld\n", auxv[AT_EUID]); - dynld_printf("\tAT_GID : %ld\n", auxv[AT_GID]); - dynld_printf("\tAT_EGID : %ld\n", auxv[AT_EGID]); + pfmt("Print auxiliary vector\n"); + pfmt("\tAT_EXECFD: %ld\n", auxv[AT_EXECFD]); + pfmt("\tAT_PHDR : %p\n", auxv[AT_PHDR]); + pfmt("\tAT_PHENT : %ld\n", auxv[AT_PHENT]); + pfmt("\tAT_PHNUM : %ld\n", auxv[AT_PHNUM]); + pfmt("\tAT_PAGESZ: %ld\n", auxv[AT_PAGESZ]); + pfmt("\tAT_BASE : %lx\n", auxv[AT_BASE]); + pfmt("\tAT_FLAGS : %ld\n", auxv[AT_FLAGS]); + pfmt("\tAT_ENTRY : %p\n", auxv[AT_ENTRY]); + pfmt("\tAT_NOTELF: %lx\n", auxv[AT_NOTELF]); + pfmt("\tAT_UID : %ld\n", auxv[AT_UID]); + pfmt("\tAT_EUID : %ld\n", auxv[AT_EUID]); + pfmt("\tAT_GID : %ld\n", auxv[AT_GID]); + pfmt("\tAT_EGID : %ld\n", auxv[AT_EGID]); ... ``` The full source code of the `entry` function is available in [entry.c](./entry.c). diff --git a/02_process_init/entry.c b/02_process_init/entry.c index 360d205..37f57c8 100644 --- a/02_process_init/entry.c +++ b/02_process_init/entry.c @@ -35,29 +35,29 @@ void entry(const long* prctx) { // Print for demonstration - dynld_printf("Got %d arg(s)\n", argc); + pfmt("Got %d arg(s)\n", argc); for (const char** arg = argv; *arg; ++arg) { - dynld_printf("\targ = %s\n", *arg); + pfmt("\targ = %s\n", *arg); } const int max_env = 10; - dynld_printf("Print first %d env var(s)\n", max_env - 1); + pfmt("Print first %d env var(s)\n", max_env - 1); for (const char** env = envv; *env && (env - envv < max_env); ++env) { - dynld_printf("\tenv = %s\n", *env); + pfmt("\tenv = %s\n", *env); } - dynld_printf("Print auxiliary vector\n"); - dynld_printf("\tAT_EXECFD: %ld\n", auxv[AT_EXECFD]); - dynld_printf("\tAT_PHDR : %p\n", auxv[AT_PHDR]); - dynld_printf("\tAT_PHENT : %ld\n", auxv[AT_PHENT]); - dynld_printf("\tAT_PHNUM : %ld\n", auxv[AT_PHNUM]); - dynld_printf("\tAT_PAGESZ: %ld\n", auxv[AT_PAGESZ]); - dynld_printf("\tAT_BASE : %lx\n", auxv[AT_BASE]); - dynld_printf("\tAT_FLAGS : %ld\n", auxv[AT_FLAGS]); - dynld_printf("\tAT_ENTRY : %p\n", auxv[AT_ENTRY]); - dynld_printf("\tAT_NOTELF: %lx\n", auxv[AT_NOTELF]); - dynld_printf("\tAT_UID : %ld\n", auxv[AT_UID]); - dynld_printf("\tAT_EUID : %ld\n", auxv[AT_EUID]); - dynld_printf("\tAT_GID : %ld\n", auxv[AT_GID]); - dynld_printf("\tAT_EGID : %ld\n", auxv[AT_EGID]); + pfmt("Print auxiliary vector\n"); + pfmt("\tAT_EXECFD: %ld\n", auxv[AT_EXECFD]); + pfmt("\tAT_PHDR : %p\n", auxv[AT_PHDR]); + pfmt("\tAT_PHENT : %ld\n", auxv[AT_PHENT]); + pfmt("\tAT_PHNUM : %ld\n", auxv[AT_PHNUM]); + pfmt("\tAT_PAGESZ: %ld\n", auxv[AT_PAGESZ]); + pfmt("\tAT_BASE : %lx\n", auxv[AT_BASE]); + pfmt("\tAT_FLAGS : %ld\n", auxv[AT_FLAGS]); + pfmt("\tAT_ENTRY : %p\n", auxv[AT_ENTRY]); + pfmt("\tAT_NOTELF: %lx\n", auxv[AT_NOTELF]); + pfmt("\tAT_UID : %ld\n", auxv[AT_UID]); + pfmt("\tAT_EUID : %ld\n", auxv[AT_EUID]); + pfmt("\tAT_GID : %ld\n", auxv[AT_GID]); + pfmt("\tAT_EGID : %ld\n", auxv[AT_EGID]); } diff --git a/include/fmt.h b/include/fmt.h index c74ac4c..f4be0b1 100644 --- a/include/fmt.h +++ b/include/fmt.h @@ -6,42 +6,11 @@ #define ALLOW_UNUSED __attribute__((unused)) -ALLOW_UNUSED -static const char* num2dec(char* buf, unsigned long len, unsigned long long num) { - char* pbuf = buf + len - 1; - *pbuf = '\0'; - - if (num == 0) { - *(--pbuf) = '0'; - } - - while (num > 0 && pbuf != buf) { - char d = (num % 10) + '0'; - *(--pbuf) = d; - num /= 10; - } - return pbuf; -} - -ALLOW_UNUSED -static const char* num2hex(char* buf, unsigned long len, unsigned long long num) { - char* pbuf = buf + len - 1; - *pbuf = '\0'; - - if (num == 0) { - *(--pbuf) = '0'; - } - - while (num > 0 && pbuf != buf) { - char d = (num & 0xf); - *(--pbuf) = d + (d > 9 ? 'a' - 10 : '0'); - num >>= 4; - } - return pbuf; -} +static const char* num2dec(char* buf, unsigned long len, unsigned long long num); +static const char* num2hex(char* buf, unsigned long len, unsigned long long num); ALLOW_UNUSED -static int dynld_vsnprintf(char* buf, unsigned long len, const char* fmt, va_list ap) { +static int vfmt(char* buf, unsigned long len, const char* fmt, va_list ap) { unsigned i = 0; #define put(c) \ @@ -116,10 +85,46 @@ static int dynld_vsnprintf(char* buf, unsigned long len, const char* fmt, va_lis } ALLOW_UNUSED -static int dynld_snprintf(char* buf, unsigned long len, const char* fmt, ...) { +static int fmt(char* buf, unsigned long len, const char* fmt, ...) { va_list ap; va_start(ap, fmt); - int ret = dynld_vsnprintf(buf, len, fmt, ap); + int ret = vfmt(buf, len, fmt, ap); va_end(ap); return ret; } + +/// Internal Helper + +ALLOW_UNUSED +static const char* num2dec(char* buf, unsigned long len, unsigned long long num) { + char* pbuf = buf + len - 1; + *pbuf = '\0'; + + if (num == 0) { + *(--pbuf) = '0'; + } + + while (num > 0 && pbuf != buf) { + char d = (num % 10) + '0'; + *(--pbuf) = d; + num /= 10; + } + return pbuf; +} + +ALLOW_UNUSED +static const char* num2hex(char* buf, unsigned long len, unsigned long long num) { + char* pbuf = buf + len - 1; + *pbuf = '\0'; + + if (num == 0) { + *(--pbuf) = '0'; + } + + while (num > 0 && pbuf != buf) { + char d = (num & 0xf); + *(--pbuf) = d + (d > 9 ? 'a' - 10 : '0'); + num >>= 4; + } + return pbuf; +} diff --git a/include/io.h b/include/io.h index 4b3ea4b..d9a1ae1 100644 --- a/include/io.h +++ b/include/io.h @@ -7,7 +7,7 @@ #include <asm/unistd.h> -// `dynld_printf` uses fixed-size buffer on the stack for formating the message +// `pfmt` uses fixed-size buffer on the stack for formating the message // (since we don't impl buffered I/O). // // Size can be re-configured by defining `MAX_PRINTF_LEN` before including @@ -16,24 +16,24 @@ // NOTE: This allows to specify an arbitrarily large buffer on the stack, but // for the purpose of this study that's fine, we are cautious. #if !defined(MAX_PRINTF_LEN) -# define MAX_PRINTF_LEN 64 +# define MAX_PRINTF_LEN 128 #endif #define FD_STDOUT 1 #define FD_STDERR 2 -int dynld_printf(const char* fmt, ...) { +static int pfmt(const char* fmt, ...) { char buf[MAX_PRINTF_LEN]; va_list ap; va_start(ap, fmt); - int ret = dynld_vsnprintf(buf, sizeof(buf), fmt, ap); + int ret = vfmt(buf, sizeof(buf), fmt, ap); va_end(ap); if (ret > MAX_PRINTF_LEN - 1) { syscall3(__NR_write, FD_STDERR, buf, MAX_PRINTF_LEN - 1); - static const char warn[] = "\ndynld_printf: Message truncated, max length can be configured by defining MAX_PRINTF_LEN\n"; + static const char warn[] = "\npfmt: Message truncated, max length can be configured by defining MAX_PRINTF_LEN\n"; syscall3(__NR_write, FD_STDOUT, warn, sizeof(warn)); return MAX_PRINTF_LEN - 1; } diff --git a/test/checker.cc b/test/checker.cc index 2b5bc8a..2a06666 100644 --- a/test/checker.cc +++ b/test/checker.cc @@ -7,7 +7,7 @@ void check_dec() { char have[16]; - int len = dynld_snprintf(have, sizeof(have), "%d %d", 12345, -54321); + int len = fmt(have, sizeof(have), "%d %d", 12345, -54321); ASSERT_EQ("12345 -54321", have); ASSERT_EQ(12, len); @@ -16,7 +16,7 @@ void check_dec() { void check_hex() { char have[16]; - int len = dynld_snprintf(have, sizeof(have), "%x %x", 0xdeadbeef, 0xcafe); + int len = fmt(have, sizeof(have), "%x %x", 0xdeadbeef, 0xcafe); ASSERT_EQ("deadbeef cafe", have); ASSERT_EQ(13, len); @@ -25,7 +25,7 @@ void check_hex() { void check_ptr() { char have[16]; - int len = dynld_snprintf(have, sizeof(have), "%p %p", (void*)0xabcd, (void*)0x0); + int len = fmt(have, sizeof(have), "%p %p", (void*)0xabcd, (void*)0x0); ASSERT_EQ("0xabcd 0x0", have); ASSERT_EQ(10, len); @@ -33,14 +33,14 @@ void check_ptr() { } void check_null() { - int len = dynld_snprintf(0, 0, "%s", "abcd1234efgh5678"); + int len = fmt(0, 0, "%s", "abcd1234efgh5678"); ASSERT_EQ(16, len); } void check_exact_len() { char have[8]; - int len = dynld_snprintf(have, sizeof(have), "%s", "12345678"); + int len = fmt(have, sizeof(have), "%s", "12345678"); ASSERT_EQ("1234567", have); ASSERT_EQ(8, len); @@ -49,7 +49,7 @@ void check_exact_len() { void check_exceed_len() { char have[8]; - int len = dynld_snprintf(have, sizeof(have), "%s", "123456789abcedf"); + int len = fmt(have, sizeof(have), "%s", "123456789abcedf"); ASSERT_EQ("1234567", have); ASSERT_EQ(15, len); |