2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Support for exceptions libsupc++
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Sebastian Sumpf
|
|
|
|
* \date 2006-07-21
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2006-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
2015-06-15 14:53:15 +02:00
|
|
|
/* libsupc++ includes */
|
|
|
|
#include <cxxabi.h>
|
2019-05-07 13:21:23 +02:00
|
|
|
#include <exception>
|
2019-09-16 13:06:23 +02:00
|
|
|
#include <typeinfo>
|
2015-06-15 14:53:15 +02:00
|
|
|
|
|
|
|
/* Genode includes */
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
#include <base/log.h>
|
|
|
|
#include <util/string.h>
|
2015-06-15 14:53:15 +02:00
|
|
|
|
2016-10-30 15:17:24 +01:00
|
|
|
/* base-internal includes */
|
|
|
|
#include <base/internal/globals.h>
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
extern "C" char __eh_frame_start__[]; /* from linker script */
|
|
|
|
extern "C" void __register_frame (const void *begin); /* from libgcc_eh */
|
2016-05-18 14:47:36 +02:00
|
|
|
extern "C" char *__cxa_demangle(const char *mangled_name,
|
|
|
|
char *output_buffer,
|
|
|
|
size_t *length,
|
|
|
|
int *status);
|
|
|
|
extern "C" void free(void *ptr);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This symbol is overwritten by Genode's dynamic linker (ld.lib.so).
|
|
|
|
* After setup, the symbol will point to the actual implementation of
|
|
|
|
* 'dl_iterate_phdr', which is located within the linker. 'dl_iterate_phdr'
|
|
|
|
* iterates through all (linker loaded) binaries and shared libraries. This
|
|
|
|
* function has to be implemented in order to support C++ exceptions within
|
|
|
|
* shared libraries.
|
|
|
|
* Return values of dl_iterate_phdr (gcc 4.2.4):
|
|
|
|
* < 0 = error
|
|
|
|
* 0 = continue program header iteration
|
|
|
|
* > 0 = stop iteration (no errors occured)
|
|
|
|
*
|
|
|
|
* See also: man dl_iterate_phdr
|
|
|
|
*/
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
extern "C" int dl_iterate_phdr(int (*callback) (void *info, unsigned long size,
|
|
|
|
void *data), void *data) __attribute__((weak));
|
|
|
|
extern "C" int dl_iterate_phdr(int (*) (void *, unsigned long, void *), void *) {
|
2011-12-22 16:19:25 +01:00
|
|
|
return -1; }
|
|
|
|
|
|
|
|
|
2015-06-15 14:53:15 +02:00
|
|
|
/*
|
|
|
|
* Terminate handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
void terminate_handler()
|
|
|
|
{
|
|
|
|
std::type_info *t = __cxxabiv1::__cxa_current_exception_type();
|
|
|
|
|
2016-05-18 14:47:36 +02:00
|
|
|
if (!t)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char *demangled_name = __cxa_demangle(t->name(), nullptr, nullptr, nullptr);
|
|
|
|
if (demangled_name) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::error("Uncaught exception of type "
|
|
|
|
"'", Genode::Cstring(demangled_name), "'");
|
2016-05-18 14:47:36 +02:00
|
|
|
free(demangled_name);
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
} else {
|
|
|
|
Genode::error("Uncaught exception of type '", t->name(), "' "
|
|
|
|
"(use 'c++filt -t' to demangle)");
|
|
|
|
}
|
2015-06-15 14:53:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-30 15:17:24 +01:00
|
|
|
/**
|
|
|
|
* Init program headers of the dynamic linker
|
|
|
|
*
|
|
|
|
* The weak function is used for statically linked binaries. The
|
|
|
|
* dynamic linker provides an implementation that loads the program
|
|
|
|
* headers of the linker. This must be done before the first exception
|
|
|
|
* is thrown.
|
|
|
|
*/
|
|
|
|
void Genode::init_ldso_phdr(Env &) __attribute__((weak));
|
|
|
|
void Genode::init_ldso_phdr(Env &) { }
|
|
|
|
|
|
|
|
|
2015-06-15 14:53:15 +02:00
|
|
|
/*
|
|
|
|
* Initialization
|
|
|
|
*/
|
2016-10-30 15:17:24 +01:00
|
|
|
void Genode::init_exception_handling(Env &env)
|
2015-06-15 14:53:15 +02:00
|
|
|
{
|
2016-10-30 15:17:24 +01:00
|
|
|
init_ldso_phdr(env);
|
|
|
|
init_cxx_heap(env);
|
|
|
|
|
2015-06-15 14:53:15 +02:00
|
|
|
__register_frame(__eh_frame_start__);
|
|
|
|
|
|
|
|
std::set_terminate(terminate_handler);
|
2016-10-30 15:17:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Trigger first exception. This step has two purposes.
|
|
|
|
* First, it enables us to detect problems related to exception handling as
|
|
|
|
* early as possible. If there are problems with the C++ support library,
|
|
|
|
* it is much easier to debug them at this early stage. Otherwise problems
|
|
|
|
* with half-working exception handling cause subtle failures that are hard
|
|
|
|
* to interpret.
|
|
|
|
*
|
|
|
|
* Second, the C++ support library allocates data structures lazily on the
|
|
|
|
* first occurrence of an exception. In some corner cases, this allocation
|
|
|
|
* consumes several KB of stack. This is usually not a problem when the
|
|
|
|
* first exception is triggered from the main thread but it becomes an
|
|
|
|
* issue when the first exception is thrown from the stack of a thread with
|
|
|
|
* a specially tailored (and otherwise sufficient) stack size. By throwing
|
|
|
|
* an exception here, we mitigate this issue by eagerly performing those
|
|
|
|
* allocations.
|
|
|
|
*/
|
|
|
|
try { throw 1; } catch (...) { }
|
2015-06-15 14:53:15 +02:00
|
|
|
}
|