Skip to content

Commit a95c1ab

Browse files
committed
fix printf-type specifiers
1 parent 08b0a5b commit a95c1ab

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

src/kernel/multiboot.cpp

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,38 @@
2222
#include <boot/multiboot.h>
2323
#include <kernel/memory.hpp>
2424

25+
#include <format>
26+
template<class... Args>
27+
static inline void debug_(const char* /*from*/, std::format_string<Args...> fmt, Args&&... args) {
28+
auto s = std::format(fmt, std::forward<Args>(args)...);
29+
kprintf("%s", s.c_str());
30+
}
31+
32+
template<class... Args>
33+
static inline void myinfo_(const char* from, std::format_string<Args...> fmt, Args&&... args) {
34+
auto s = std::format("<Multiboot>{}\n", std::format(fmt, std::forward<Args>(args)...));
35+
kprintf("< %13s > %s", from, s.c_str());
36+
}
37+
38+
template<class... Args>
39+
static inline void info2_(const char* /*from*/, std::format_string<Args...> fmt, Args&&... args) {
40+
auto s = std::format(fmt, std::forward<Args>(args)...);
41+
kprintf("\t%s\n", s.c_str());
42+
}
43+
2544
#define DEBUG_MULTIBOOT
2645
#if defined(DEBUG_MULTIBOOT)
2746
#undef debug
28-
#define debug(X,...) kprintf(X,##__VA_ARGS__);
29-
#define MYINFO(X,...) kprintf("<Multiboot>" X "\n", ##__VA_ARGS__)
47+
#define debug(fmt, ...) debug_("multiboot", fmt, ##__VA_ARGS__)
48+
#undef MYINFO
49+
#define MYINFO(fmt, ...) myinfo_("multiboot", fmt, ##__VA_ARGS__)
3050
#undef INFO2
31-
#define INFO2(X,...) kprintf("\t" X "\n", ##__VA_ARGS__)
51+
#define INFO2(fmt, ...) info2_("multiboot", fmt, ##__VA_ARGS__)
3252
#else
33-
#define debug(X,...)
34-
#define MYINFO(X,...) INFO("Kernel", X, ##__VA_ARGS__)
53+
#define debug(...)
54+
#define MYINFO(fmt, ,...) INFO("Kernel", X, ##__VA_ARGS__)
3555
#endif
3656

37-
3857
extern uintptr_t _end;
3958

4059

@@ -73,11 +92,11 @@ uintptr_t _multiboot_free_begin(uintptr_t boot_addr)
7392
auto* info = bootinfo(boot_addr);
7493
uintptr_t multi_end = reinterpret_cast<uintptr_t>(&_end);
7594

76-
debug("* Multiboot begin: 0x%lx \n", (uintptr_t)info);
95+
debug("* Multiboot begin: {:#x}\n", (uintptr_t)info);
7796
if (info->flags & MULTIBOOT_INFO_CMDLINE
7897
and info->cmdline > multi_end)
7998
{
80-
debug("* Multiboot cmdline @ 0x%x: %s \n", info->cmdline, reinterpret_cast<char*>(info->cmdline));
99+
debug("* Multiboot cmdline @ 0x{:08x}: {}\n", info->cmdline, reinterpret_cast<char*>(info->cmdline));
81100
// We can't use a cmdline that's either insde our ELF or pre-ELF area
82101
Expects(info->cmdline > multi_end
83102
or info->cmdline < 0x100000);
@@ -89,20 +108,20 @@ uintptr_t _multiboot_free_begin(uintptr_t boot_addr)
89108
}
90109
}
91110

92-
debug("* Multiboot end: 0x%lx \n", multi_end);
111+
debug("* Multiboot end: {:#x}\n", multi_end);
93112
if (info->mods_count == 0)
94113
return multi_end;
95114

96115
auto* mods_list = (multiboot_module_t*) (uintptr_t) info->mods_addr;
97-
debug("* Module list @ %p \n",mods_list);
116+
debug("* Module list @ {}\n", static_cast<const void*>(mods_list));
98117

99118
for (multiboot_module_t* mod = mods_list;
100119
mod < mods_list + info->mods_count;
101120
mod ++) {
102121

103-
debug("\t * Module @ %#x \n", mod->mod_start);
104-
debug("\t * Args: %s \n ", (char*) (uintptr_t) mod->cmdline);
105-
debug("\t * End: %#x \n ", mod->mod_end);
122+
debug("\t * Module @ {:#x}\n", mod->mod_start);
123+
debug("\t * Args: {}\n ", (char*) (uintptr_t) mod->cmdline);
124+
debug("\t * End: {:#x}\n", mod->mod_end);
106125

107126
if (mod->mod_end > multi_end)
108127
multi_end = mod->mod_end;
@@ -117,7 +136,7 @@ void kernel::multiboot(uint32_t boot_addr)
117136
{
118137
MYINFO("Booted with multiboot");
119138
auto* info = ::bootinfo(boot_addr);
120-
INFO2("* Boot flags: %#x", info->flags);
139+
INFO2("* Boot flags: {:#x}", info->flags);
121140

122141
if (info->flags & MULTIBOOT_INFO_MEMORY) {
123142
uint32_t mem_low_start = 0;
@@ -128,10 +147,8 @@ void kernel::multiboot(uint32_t boot_addr)
128147
uint32_t mem_high_kb = info->mem_upper;
129148

130149
INFO2("* Valid memory (%i Kib):", mem_low_kb + mem_high_kb);
131-
INFO2(" 0x%08x - 0x%08x (%i Kib)",
132-
mem_low_start, mem_low_end, mem_low_kb);
133-
INFO2(" 0x%08x - 0x%08x (%i Kib)",
134-
mem_high_start, mem_high_end, mem_high_kb);
150+
INFO2(" 0x{:08x} - 0x{:08x} ({} KiB)", mem_low_start, mem_low_end, mem_low_kb);
151+
INFO2(" 0x{:08x} - 0x{:08x} ({} KiB)", mem_high_start, mem_high_end, mem_high_kb);
135152
INFO2("");
136153
}
137154
else {
@@ -140,14 +157,14 @@ void kernel::multiboot(uint32_t boot_addr)
140157

141158
if (info->flags & MULTIBOOT_INFO_CMDLINE) {
142159
const auto* cmdline = (const char*) (uintptr_t) info->cmdline;
143-
INFO2("* Booted with parameters @ %p: %s", cmdline, cmdline);
160+
INFO2("* Booted with parameters @ {}: {}", (const void*)(uintptr_t)info->cmdline, cmdline);
144161
kernel::state().cmdline = std::pmr::string(cmdline).data();
145162
}
146163

147164
if (info->flags & MULTIBOOT_INFO_MEM_MAP) {
148-
INFO2("* Multiboot provided memory map (%zu entries @ %p)",
165+
INFO2("* Multiboot provided memory map ({} entries @ {})",
149166
info->mmap_length / sizeof(multiboot_memory_map_t),
150-
(void*) (uintptr_t) info->mmap_addr);
167+
(const void*)(uintptr_t)info->mmap_addr);
151168
std::span<multiboot_memory_map_t> mmap {
152169
reinterpret_cast<multiboot_memory_map_t*>(info->mmap_addr),
153170
static_cast<size_t>(info->mmap_length / sizeof(multiboot_memory_map_t))
@@ -158,8 +175,7 @@ void kernel::multiboot(uint32_t boot_addr)
158175
const char* str_type = map.type & MULTIBOOT_MEMORY_AVAILABLE ? "FREE" : "RESERVED";
159176
const uintptr_t addr = map.addr;
160177
const uintptr_t size = map.len;
161-
INFO2(" 0x%010zx - 0x%010zx %s (%zu Kb.)",
162-
addr, addr + size - 1, str_type, size / 1024 );
178+
INFO2(" {:#x} - {:#x} {} ({} KiB)", addr, addr + size - 1, str_type, size / 1024);
163179

164180
if (not (map.type & MULTIBOOT_MEMORY_AVAILABLE)) {
165181

@@ -186,7 +202,7 @@ void kernel::multiboot(uint32_t boot_addr)
186202
if (not mods.empty()) {
187203
MYINFO("OS loaded with %zu modules", mods.size());
188204
for (auto mod : mods) {
189-
INFO2("* %s @ 0x%x - 0x%x, size: %ib",
205+
INFO2("* {} @ 0x{:08x} - 0x{:08x}, size: {} B",
190206
reinterpret_cast<char*>(mod.params),
191207
mod.mod_start, mod.mod_end, mod.mod_end - mod.mod_start);
192208
}

0 commit comments

Comments
 (0)