diff options
author | dzwdz | 2022-10-19 15:17:36 +0200 |
---|---|---|
committer | dzwdz | 2022-10-19 15:17:36 +0200 |
commit | e4118bb67dee6b4064d196cbe6e1c83cb7d1d586 (patch) | |
tree | c19eae356e2437d779af176c855a4a2a0587ea26 /src | |
parent | d98c6dd0954bd753a9150d39364f3866579cf2f0 (diff) |
shared/printf: implement %p
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/printf.c | 28 | ||||
-rw-r--r-- | src/user/app/init/init.c | 2 | ||||
-rw-r--r-- | src/user/app/netstack/netstack.c | 1 | ||||
-rw-r--r-- | src/user/app/testelf/main.c | 6 | ||||
-rw-r--r-- | src/user/app/tests/tests.c | 2 | ||||
-rw-r--r-- | src/user/lib/_start2.c | 2 | ||||
-rw-r--r-- | src/user/lib/elfload.c | 1 |
7 files changed, 25 insertions, 17 deletions
diff --git a/src/shared/printf.c b/src/shared/printf.c index a9cd355..5134048 100644 --- a/src/shared/printf.c +++ b/src/shared/printf.c @@ -2,6 +2,7 @@ #include <shared/printf.h> #include <stdarg.h> #include <stdbool.h> +#include <stdint.h> enum lenmod { LM_int, @@ -82,6 +83,18 @@ static void output_uint(struct out_state *os, struct mods *m, unsigned long long output(os, buf + pos, len); } +static void output_uint16(struct out_state *os, struct mods *m, unsigned long long n) { + size_t len = 1; + while (n >> (len * 4) && (len * 4) < (sizeof(n) * 8)) + len++; + padnum(os, m, len, '\0'); + while (len-- > 0) { + char h = '0' + ((n >> (len * 4)) & 0xf); + if (h > '9') h += 'a' - '9' - 1; + output_c(os, h, 1); + } +} + int __printf_internal(const char *fmt, va_list argp, void (*back)(void*, const char*, size_t), void *backarg) @@ -176,19 +189,16 @@ int __printf_internal(const char *fmt, va_list argp, output(&os, s, len); break; + case 'p': + output(&os, "0x", 2); + output_uint16(&os, &m, (uintptr_t)va_arg(argp, void*)); + break; + case 'x': if (lm == LM_int) n = va_arg(argp, unsigned int); else if (lm == LM_long) n = va_arg(argp, unsigned long); else if (lm == LM_longlong) n = va_arg(argp, unsigned long long); - len = 1; - while (n >> (len * 4) && (len * 4) < (sizeof(n) * 8)) - len++; - padnum(&os, &m, len, '\0'); - while (len-- > 0) { - char h = '0' + ((n >> (len * 4)) & 0xf); - if (h > '9') h += 'a' - '9' - 1; - output_c(&os, h, 1); - } + output_uint16(&os, &m, n); break; case 'u': diff --git a/src/user/app/init/init.c b/src/user/app/init/init.c index 7e6ddab..5f275ee 100644 --- a/src/user/app/init/init.c +++ b/src/user/app/init/init.c @@ -37,7 +37,7 @@ int main(void) { freopen("/kdev/com1", "a+", stdout); freopen("/kdev/com1", "a+", stderr); - printf("in init (stage 2), main at 0x%x\n", &main); + printf("in init (stage 2), main at %p\n", &main); MOUNT_AT("/keyboard") { ps2_drv(); } MOUNT_AT("/bin/") { diff --git a/src/user/app/netstack/netstack.c b/src/user/app/netstack/netstack.c index 852cd37..405be09 100644 --- a/src/user/app/netstack/netstack.c +++ b/src/user/app/netstack/netstack.c @@ -29,7 +29,6 @@ void fs_thread(void *arg); __attribute__((visibility("hidden"))) extern char _image_base[]; int main(int argc, char **argv) { - eprintf("0x%x", _image_base); if (argc < 4) { eprintf("usage: netstack iface ip gateway"); return 1; diff --git a/src/user/app/testelf/main.c b/src/user/app/testelf/main.c index ea97b4a..5608cc7 100644 --- a/src/user/app/testelf/main.c +++ b/src/user/app/testelf/main.c @@ -5,11 +5,11 @@ const char *str = "Hello!", *str2 = "World."; int main(int argc, char **argv) { - printf("elftest's &main == 0x%x\n", &main); + printf("elftest's &main == %p\n", &main); printf("%s %s\n", str, str2); printf("argc == %u\n", argc); for (int i = 0; i < argc; i++) - printf("argv[%u] == 0x%x == \"%s\"\n", i, argv[i], argv[i]); + printf("argv[%u] == %p == \"%s\"\n", i, argv[i], argv[i]); if (strcmp(argv[1], "stackexec") == 0) { /* exec something with arguments on the stack */ const char s_d[] = "I am a pretty long string on the stack. Oh my. " \ @@ -17,7 +17,7 @@ int main(int argc, char **argv) { char s[sizeof(s_d)]; memcpy(s, s_d, sizeof(s_d)); const char *argv2[] = {argv[0], s, s, "hello", s, s, s, "lol", NULL}; - printf("argv2 == 0x%x, s == 0x%x\n== exec ==\n", argv2, s); + printf("argv2 == %p, s == %p\n== exec ==\n", argv2, s); execv(argv[0], (void*)argv2); puts("stackexec failed"); } diff --git a/src/user/app/tests/tests.c b/src/user/app/tests/tests.c index e4b4d37..90a4978 100644 --- a/src/user/app/tests/tests.c +++ b/src/user/app/tests/tests.c @@ -14,7 +14,7 @@ void run_test(void (*fn)()) { } else { /* successful tests must return 0 */ if (_syscall_await() != 0) { - test_failf("0x%x, base 0x%x", (void*)fn - (void*)_image_base, _image_base); + test_failf("%p, base %p", (void*)fn - (void*)_image_base, _image_base); } } } diff --git a/src/user/lib/_start2.c b/src/user/lib/_start2.c index 0582dde..ba3b6b0 100644 --- a/src/user/lib/_start2.c +++ b/src/user/lib/_start2.c @@ -24,7 +24,7 @@ _Noreturn void _start2(struct execdata *ed) { progname = shortname(ed->argv[0]); setprogname(progname); - _klogf("_start2 %s 0x%x", progname, _image_base); + _klogf("_start2 %s %p", progname, _image_base); exit(main(ed->argc, ed->argv, ed->envp)); } diff --git a/src/user/lib/elfload.c b/src/user/lib/elfload.c index 99633d1..6dab8ae 100644 --- a/src/user/lib/elfload.c +++ b/src/user/lib/elfload.c @@ -153,7 +153,6 @@ void elf_exec(void *base, char **argv, char **envp) { void *exebase = elf_loadmem(ehdr); if (!exebase) return; - _klogf("exebase 0x%x", exebase); void *newstack = _syscall_memflag((void*)0x1000, 0x1000, MEMFLAG_FINDFREE | MEMFLAG_PRESENT); if (!newstack) return; |