4 #define RESET_COLOR "\033[m" 5 #define BLUE "\033[1;34m" 6 #define YELLOW "\033[1;33m" 7 #define GREEN "\033[1;32m" 8 #define RED "\033[1;31m" 9 #define BLACK "\033[1;30m" 10 #define MAGENTA "\033[1;35m" 11 #define CYAN "\033[1;36m" 12 #define WHITE "\033[1;37m" 14 #define DBLUE "\033[0;34m" 15 #define DYELLOW "\033[0;33m" 16 #define DGREEN "\033[0;32m" 17 #define DRED "\033[0;31m" 18 #define DBLACK "\033[0;30m" 19 #define DMAGENTA "\033[0;35m" 20 #define DCYAN "\033[0;36m" 21 #define DWHITE "\033[0;37m" 23 #define BG_WHITE "\033[47m" 24 #define BG_RED "\033[41m" 25 #define BG_GREEN "\033[42m" 26 #define BG_YELLOW "\033[43m" 27 #define BG_BLUE "\033[44m" 28 #define BG_MAGENTA "\033[45m" 29 #define BG_CYAN "\033[46m" 31 #define HIDE_CURSOR "\033[?25l" 32 #define SHOW_CURSOR "\033[?25h" 34 #define ALERTTEXT "\033[47m\033[0;31m" 36 #define NUM_SIS_CHAN 8 38 #define MAXSAMPLESIZE 8192 40 #define FRAGMENTBUFFERSIZE 1000 42 #define BUILDINGTIMECONDITION 2 43 #define BUILDINGTRIGGERCONDITION 999 48 typedef char __signed;
55 #include <_types/_uint16_t.h> 56 #include <_types/_uint32_t.h> 57 #include <_types/_uint64_t.h> 58 #include <sys/_types/_int16_t.h> 78 #include <sys/prctl.h> 99 void Append(
std::stringstream& stream, const T& tail) {
105 template <
typename T,
typename... U>
106 void Append(std::stringstream& stream,
const T& head,
const U&... tail) {
115 template <
typename T,
typename... U>
116 std::string
Stringify(
const T& head,
const U&... tail) {
118 std::stringstream stream;
133 template <
typename T>
134 inline std::string
hex(T val,
int width = -1)
136 std::ostringstream str;
139 str<<std::setfill(
'0')<<std::setw(width);
143 str<<std::setfill(
' ');
150 ssize_t count = readlink(
"/proc/self/exe", result,
sizeof(result)-1);
151 return std::string(result, (count > 0) ? count : 0);
154 static inline std::string
sh(std::string cmd) {
155 std::array<char, 128> buffer;
157 std::shared_ptr<FILE> pipe(popen(cmd.c_str(),
"r"), pclose);
158 if(!pipe)
throw std::runtime_error(
"popen() failed!");
159 while(!feof(pipe.get())) {
160 if(fgets(buffer.data(), 128, pipe.get()) !=
nullptr) {
161 result += buffer.data();
168 static inline void PrintStacktrace(std::ostream& out = std::cout,
unsigned int maxFrames = 63)
170 std::stringstream str;
171 str<<
"stack trace:"<<std::endl;
174 void** addrlist =
new void*[maxFrames+1];
177 int addrlen = backtrace(addrlist, maxFrames+1);
180 str<<
" <empty, possibly corrupt>"<<std::endl;
187 char** symbollist = backtrace_symbols(addrlist, addrlen);
190 size_t funcnamesize = 256;
191 char* funcname =
new char[funcnamesize];
195 for(
int i = 2; i < addrlen; i++) {
196 char* begin_name =
nullptr;
197 char* begin_offset =
nullptr;
198 char* end_offset =
nullptr;
202 for(
char* p = symbollist[i]; *p; ++p) {
205 }
else if (*p ==
'+') {
207 }
else if(*p ==
')' && begin_offset) {
223 if(begin_name && begin_offset && end_offset && begin_name < begin_offset) {
224 *begin_name++ =
'\0';
225 *begin_offset++ =
'\0';
233 char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
236 str<<
" "<<symbollist[i]<<
": "<<funcname<<
"+"<<begin_offset<<std::endl;
240 str<<
" "<<symbollist[i]<<
": "<<begin_name<<
"()+"<<begin_offset<<std::endl;
244 str<<
" "<<symbollist[i]<<std::endl;
256 sprintf(pid_buf,
"%d", getpid());
258 name_buf[readlink(
"/proc/self/exe", name_buf, 511)] = 0;
259 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0);
260 int child_pid = fork();
263 execl(
"/usr/bin/gdb",
"gdb",
"--batch",
"-n",
"-ex",
"thread",
"-ex",
"bt", name_buf, pid_buf, NULL);
266 waitpid(child_pid,NULL,0);
void Append(std::stringstream &stream, const T &tail)
static std::string sh(std::string cmd)
std::string hex(T val, int width=-1)
static std::string getexepath()
static void PrintStacktrace(std::ostream &out=std::cout, unsigned int maxFrames=63)
static void PrintGdbStacktrace()
std::string Stringify(const T &head, const U &... tail)
~exit_exception() override=default
exit_exception(int c, const char *msg="")
const std::string & ProgramName()