Skip to content

Commit cce171b

Browse files
authored
Merge pull request #2309 from mazunki/feat-debugging
fix several printf-related warnings
2 parents bed943d + ed25751 commit cce171b

File tree

22 files changed

+242
-106
lines changed

22 files changed

+242
-106
lines changed

api/arch/x86/paging.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class Page_table {
317317
auto* sub = page_dir(&ent);
318318
Expects(sub != nullptr);
319319
if (print) {
320-
printf("%.*s-+<%s> 0x%zx\n", print_lvl * 2, pad,
320+
printf("%.*s-+<%s> %p\n", print_lvl * 2, pad,
321321
util::Byte_r(page_size).to_string().c_str(), (void*)sub->start_addr());
322322
}
323323
sum += sub->summary(print, print_lvl + 1);

api/expects

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,43 @@
3232
#endif
3333

3434
#include <os.hpp>
35-
inline void __expect_fail(const char *expr, const char *file, int line, const char *func){
35+
inline void __expect_emit_failure(std::string_view msg, std::string_view panic_text) {
3636
#ifndef UNITTESTS
3737
#ifdef INCLUDEOS_SMP_ENABLE
3838
SMP::global_lock();
3939
#endif
40-
fprintf(stderr, "%s:%i:%s %s \n",file, line, func, expr);
40+
std::fprintf(stderr, "%.*s\n", int(msg.size()), msg.data());
4141
fflush(NULL);
4242
#ifdef INCLUDEOS_SMP_ENABLE
4343
SMP::global_unlock();
4444
#endif
45-
os::panic(expr);
45+
os::panic(std::string(panic_text).c_str());
4646
#else // TEST
47+
(void) panic_text;
4748
// throw here to allow tests to capture the error
4849
#include <stdexcept>
49-
#include <format>
50-
auto msg = std::format("{}:{}:{} {}",file, line, func, expr);
51-
throw std::runtime_error(msg);
50+
throw std::runtime_error(std::string(msg));
5251
#endif
5352
}
5453

55-
#define Expects(x) ((void)((x) || (__expect_fail("Expects failed: "#x, __FILE__, __LINE__, __func__),0)))
56-
#define Ensures(x) ((void)((x) || (__expect_fail("Ensures failed: "#x, __FILE__, __LINE__, __func__),0)))
54+
template <class... Args>
55+
inline void __expect_failf(const char *err_prefix, const char * /*cond*/, const char *file, int line, const char *func, std::format_string<Args...> fmt, Args&&... args){
56+
auto reason_msg = std::format(fmt, std::forward<Args>(args)...);
57+
auto error_msg = std::format("{}:{}:{}: {}: {}", file, line, func, err_prefix, reason_msg);
58+
__expect_emit_failure(error_msg, reason_msg);
59+
}
60+
61+
inline void __expect_failf(const char *err_prefix, const char *cond, const char *file, int line, const char *func){
62+
auto reason_msg = std::format("{}: {}", err_prefix, cond);
63+
auto error_msg = std::format("{}:{}:{}: {}", file, line, func, err_prefix);
64+
__expect_emit_failure(error_msg, reason_msg);
65+
}
66+
67+
#define Expects(cond) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__),0)))
68+
#define Ensures(cond) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__),0)))
69+
70+
#define Expectsf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Expects failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0)))
71+
#define Ensuresf(cond, fmt, ...) ((void)((cond) || (__expect_failf("Ensures failed", #cond, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__),0)))
5772

5873
namespace os {
5974
// parameter for noexcept specifier when bypassing noexcept for testing

api/util/elf_binary.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ const typename Elf_binary<Arch>::Section_header& Elf_binary<Arch>::section_heade
149149

150150
template <typename Arch>
151151
const typename Elf_binary<Arch>::Span Elf_binary<Arch>::section_data(const Section_header& sh) const {
152-
return {data_.data() + sh.sh_offset, static_cast<std::ptrdiff_t>(sh.sh_size)};
152+
return {data_.data() + static_cast<std::size_t>(sh.sh_offset), static_cast<std::size_t>(sh.sh_size)};
153153
};
154154

155155
template <typename Arch>

cmake/os.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ function(os_add_executable TARGET NAME)
194194
endif()
195195
endforeach()
196196

197+
find_package(fmt CONFIG REQUIRED)
198+
target_link_libraries(${ELF_TARGET} fmt::fmt)
199+
197200
# TODO: if not debug strip
198201
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
199202
set(STRIP_LV )

deps/libfmt/default.nix

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# note that there is also `<nixpkgs>.fmt`
2+
# so this entire file could just be `{ pkgs }: pkgs.fmt`
3+
{
4+
pkgs,
5+
stdenv ? pkgs.stdenv,
6+
cmake ? pkgs.cmake
7+
}:
8+
let
9+
libfmt = stdenv.mkDerivation rec {
10+
pname = "fmt";
11+
version = "12.0.0";
12+
13+
src = pkgs.fetchFromGitHub {
14+
owner = "fmtlib";
15+
repo = "fmt";
16+
rev = "12.0.0";
17+
hash = "sha256-AZDmIeU1HbadC+K0TIAGogvVnxt0oE9U6ocpawIgl6g=";
18+
};
19+
20+
nativeBuildInputs = [ cmake ];
21+
22+
cmakeFlags = [
23+
"-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY"
24+
"-DBUILD_SHARED_LIBS=OFF"
25+
26+
"-DFMT_TEST=OFF"
27+
"-DFMT_DOC=OFF"
28+
"-DFMT_INSTALL=ON"
29+
];
30+
};
31+
in
32+
libfmt // {
33+
include = "${libfmt}/include";
34+
lib = "${libfmt}/lib";
35+
}

develop.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ includeos.pkgs.mkShell.override { inherit (includeos) stdenv; } rec {
8383
jq \
8484
--arg libcxx "${includeos.libraries.libcxx.include}" \
8585
--arg libc "${includeos.libraries.libc}" \
86-
--arg localsrc "${toString ./.}" \
86+
--arg libfmt "${includeos.passthru.libfmt.include}" \
87+
--arg localsrc "${toString ./.}" \
8788
'
8889
map(.command |= ( .
8990
+ " -isystem \($libcxx)"
9091
+ " -isystem \($libc)/include"
92+
+ " -I \($libfmt)"
9193
| gsub("(?<a>-I)(?<b>/lib/LiveUpdate/include)"; .a + $localsrc + .b)
9294
))
9395
' "$CCDB" > "$tmp" && mv "$tmp" "$CCDB"

overlay.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ final: prev: {
8787
inherit suppressTargetWarningHook;
8888

8989
# Deps
90-
uzlib = self.callPackage ./deps/uzlib/default.nix { };
9190
botan2 = self.callPackage ./deps/botan/default.nix { };
92-
s2n-tls = self.callPackage ./deps/s2n/default.nix { };
91+
libfmt = self.callPackage ./deps/libfmt/default.nix { };
9392
http-parser = self.callPackage ./deps/http-parser/default.nix { };
93+
s2n-tls = self.callPackage ./deps/s2n/default.nix { };
94+
uzlib = self.callPackage ./deps/uzlib/default.nix { };
95+
9496
vmbuild = self.callPackage ./vmbuild.nix { };
9597

9698
ccacheWrapper = prev.ccacheWrapper.override {
@@ -157,6 +159,7 @@ final: prev: {
157159
++ prev.lib.optionals withCcache [self.ccacheWrapper ccacheNoticeHook];
158160

159161
buildInputs = [
162+
self.libfmt
160163
self.botan2
161164
self.http-parser
162165
prev.pkgsStatic.openssl
@@ -205,6 +208,7 @@ final: prev: {
205208
inherit (self) uzlib;
206209
inherit (self) http-parser;
207210
inherit (self) botan2;
211+
inherit (self) libfmt;
208212
#inherit (self) s2n-tls;
209213
inherit (self) cmake;
210214
inherit (self) vmbuild;

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ include_directories(
1515
include
1616
)
1717

18+
find_package(fmt CONFIG REQUIRED)
19+
get_target_property(libfmt_include_path fmt::fmt INTERFACE_INCLUDE_DIRECTORIES)
20+
include_directories(${libfmt_include_path})
21+
1822
#TODO move to util and check if needed / can be changed ?..
1923
include_directories(${INCLUDEOS_ROOT}/lib/LiveUpdate/include)
2024

@@ -66,6 +70,7 @@ FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/version.h
6670
include_directories(${CMAKE_CURRENT_BINARY_DIR})
6771

6872
add_library(os STATIC ${SRCS} ${OBJECTS} ${CMAKE_CURRENT_BINARY_DIR}/version.h)
73+
target_link_libraries(os PRIVATE fmt::fmt)
6974

7075
#TODO check if this is almost correct for platform userspace
7176
if (NOT CMAKE_TESTING_ENABLED)

src/chainload/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
44
project(chainloader C CXX)
55

66
find_package(IncludeOS REQUIRED)
7+
find_package(fmt CONFIG REQUIRED)
78

89
set(ARCH i686)
910
set(PLATFORM nano)

src/include/kernel.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace kernel {
2626
using namespace util;
2727
constexpr size_t default_max_mem = 2_GiB;
2828
constexpr uintptr_t page_shift = 12;
29+
constexpr size_t kprintf_max_size = 8192;
2930

3031
struct State {
3132
bool running = true;

0 commit comments

Comments
 (0)