Bladeren bron

AK: Add a new method to propagate errno while printing errors in Kernel

This new method is meant to be used in both userspace and kernel code.
The idea is to allow printing of a verbose message and then returning an
errno code which is the proper mechanism for kernel code because we
should almost always assume that such error will be propagated back to
userspace in some way, so the userspace code could reasonably decode it.

For userspace code however, this new method is meant to be a simple
wrapper for Error::from_string_view, because for most invocations, it's
much more useful to have a verbose & literal error than a errno code, so
we simply ignore that errno code completely in such context.
Liav A 2 jaren geleden
bovenliggende
commit
ab27fce86f
4 gewijzigde bestanden met toevoegingen van 34 en 0 verwijderingen
  1. 1 0
      AK/CMakeLists.txt
  2. 25 0
      AK/Error.cpp
  3. 6 0
      AK/Error.h
  4. 2 0
      Kernel/CMakeLists.txt

+ 1 - 0
AK/CMakeLists.txt

@@ -4,6 +4,7 @@ set(AK_SOURCES
     CircularBuffer.cpp
     DeprecatedFlyString.cpp
     DeprecatedString.cpp
+    Error.cpp
     FloatingPointStringConversions.cpp
     FlyString.cpp
     Format.cpp

+ 25 - 0
AK/Error.cpp

@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Error.h>
+
+#ifdef KERNEL
+#    include <AK/Format.h>
+#endif
+
+namespace AK {
+
+Error Error::from_string_view_or_print_error_and_return_errno(StringView string_literal, [[maybe_unused]] int code)
+{
+#ifdef KERNEL
+    dmesgln("{}", string_literal);
+    return Error::from_errno(code);
+#else
+    return Error::from_string_view(string_literal);
+#endif
+}
+
+}

+ 6 - 0
AK/Error.h

@@ -25,6 +25,12 @@ public:
     ALWAYS_INLINE Error& operator=(Error&&) = default;
 
     [[nodiscard]] static Error from_errno(int code) { return Error(code); }
+
+    // NOTE: For calling this method from within kernel code, we will simply print
+    // the error message and return the errno code.
+    // For calling this method from userspace programs, we will simply return from
+    // the Error::from_string_view method!
+    [[nodiscard]] static Error from_string_view_or_print_error_and_return_errno(StringView string_literal, int code);
     [[nodiscard]] static Error from_syscall(StringView syscall_name, int rc) { return Error(syscall_name, rc); }
     [[nodiscard]] static Error from_string_view(StringView string_literal) { return Error(string_literal); }
 

+ 2 - 0
Kernel/CMakeLists.txt

@@ -489,6 +489,7 @@ set(AK_SOURCES
     ../AK/StringUtils.cpp
     ../AK/StringView.cpp
     ../AK/Time.cpp
+    ../AK/Error.cpp
     ../AK/Format.cpp
     ../AK/UUID.cpp
 )
@@ -626,6 +627,7 @@ if (ENABLE_KERNEL_COVERAGE_COLLECTION)
     set(KCOV_EXCLUDED_SOURCES
         # Make sure we don't instrument any code called from __sanitizer_cov_trace_pc
         # otherwise we'll end up with recursive calls to that function.
+        ../AK/Error.cpp
         ../AK/Format.cpp
         ../AK/StringBuilder.cpp
         ../Kernel/Arch/x86_64/Processor.cpp