From 5f20bd02e07d7ea497bbce6cd562d7bec8c4959e Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Tue, 11 Feb 2014 18:22:51 +0100 Subject: [PATCH] Improve error reporting between kernel<->user --- kernel/src/scheduler.cpp | 7 ++++--- programs/tsh/src/main.cpp | 11 ++--------- tstl/include/errors.hpp | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 tstl/include/errors.hpp diff --git a/kernel/src/scheduler.cpp b/kernel/src/scheduler.cpp index 74169697..8375c26a 100644 --- a/kernel/src/scheduler.cpp +++ b/kernel/src/scheduler.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "scheduler.hpp" #include "paging.hpp" @@ -532,7 +533,7 @@ int64_t scheduler::exec(const std::string& file){ k_print_line("Not a file"); } - return -1; + return -std::ERROR_NOT_EXISTS; } if(!elf::is_valid(content)){ @@ -540,7 +541,7 @@ int64_t scheduler::exec(const std::string& file){ k_print_line("Not a valid file"); } - return -2; + return -std::ERROR_NOT_EXECUTABLE; } auto buffer = content.c_str(); @@ -552,7 +553,7 @@ int64_t scheduler::exec(const std::string& file){ k_print_line("Impossible to create paging"); } - return -3; + return -std::ERROR_FAILED_EXECUTION; } process.brk_start = program_break; diff --git a/programs/tsh/src/main.cpp b/programs/tsh/src/main.cpp index 29c2b29c..f0ec9bdd 100644 --- a/programs/tsh/src/main.cpp +++ b/programs/tsh/src/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace { @@ -99,15 +100,7 @@ int main(){ if(!result.valid()){ print("error: "); - - auto err = result.error(); - if(err == 1){ - print_line("The file does not exist"); - } else if(err == 2){ - print_line("The file is not an executable"); - } else if(err == 3){ - print_line("Failed to execute the file"); - } + print_line(std::error_message(result.error())); } } } diff --git a/tstl/include/errors.hpp b/tstl/include/errors.hpp new file mode 100644 index 00000000..b6279a1c --- /dev/null +++ b/tstl/include/errors.hpp @@ -0,0 +1,32 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#ifndef ERRORS_H +#define ERRORS_H + +namespace std { + +constexpr const size_t ERROR_NOT_EXISTS = 1; +constexpr const size_t ERROR_NOT_EXECUTABLE = 2; +constexpr const size_t ERROR_FAILED_EXECUTION = 3; + +inline const char* error_message(size_t error){ + switch(error){ + case ERROR_NOT_EXISTS: + return "The file does not exist"; + case ERROR_NOT_EXECUTABLE: + return "The file is not an executable"; + case ERROR_FAILED_EXECUTION: + return "Execution failed"; + default: + return "Unknonwn error"; + } +} + +} + +#endif