-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy patharch.cpp
More file actions
106 lines (90 loc) · 2.21 KB
/
arch.cpp
File metadata and controls
106 lines (90 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <arch.hpp>
#include <cstdint>
#include <ctime>
#include <os.hpp>
# define weak_alias(name, aliasname) \
extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
typedef void (*ctor_t) ();
extern "C" __attribute__(( visibility("hidden") )) void default_ctor() {}
ctor_t __plugin_ctors_start = default_ctor;
weak_alias(__plugin_ctors_start, __plugin_ctors_end);
ctor_t __service_ctors_start = default_ctor;
weak_alias(__service_ctors_start, __service_ctors_end);
char _ELF_START_;
char _ELF_END_;
void __arch_subscribe_irq(uint8_t) {}
uint64_t __arch_system_time() noexcept
{
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv.tv_sec*(uint64_t)1000000000ull+tv.tv_nsec;
}
timespec __arch_wall_clock() noexcept
{
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return tv;
}
#include <random>
uint32_t __arch_rand32()
{
static std::random_device rd;
static std::mt19937_64 gen(rd());
static std::uniform_int_distribution<uint32_t> dis;
return dis(gen);
}
void __arch_reboot()
{
exit(0);
}
void __arch_system_deactivate()
{
// nada
}
#ifdef __linux__
#include <execinfo.h>
#endif
void os::print_backtrace() noexcept
{
static const int NUM_ADDRS = 64;
void* addresses[NUM_ADDRS];
#ifdef __linux__
int nptrs = backtrace(addresses, NUM_ADDRS);
printf("backtrace() returned %d addresses\n", nptrs);
/* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
would produce similar output to the following: */
char** strings = backtrace_symbols(addresses, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
for (int j = 0; j < nptrs; j++)
printf("#%02d: %8p %s\n", j, addresses[j], strings[j]);
free(strings);
#endif
}
// context buffer
static std::array<char, 512> context_buffer;
size_t get_crash_context_length()
{
return context_buffer.size();
}
char* get_crash_context_buffer()
{
return context_buffer.data();
}
#include <signal.h>
namespace os
{
void panic(const char* why) noexcept
{
printf("!! PANIC !!\nReason: %s\n", why);
print_backtrace();
raise(SIGINT);
exit(1);
}
}
extern "C"
void __os_store_soft_reset(const void*, size_t) {
// don't need to on this platform
}