-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.h
74 lines (62 loc) · 2.33 KB
/
utils.h
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
#pragma once
#include <elf.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>
class nullstream : public std::ostream {
public:
nullstream() : std::ostream(nullptr) {}
};
inline nullstream null;
#define CHECK(x) \
if (!(x)) { \
std::cerr << "CHECK failed: " << #x << std::endl; \
exit(1); \
}
#define CHECK_GT(x, y) CHECK((x) > (y))
#define CHECK_LT(x, y) CHECK((x) < (y))
#define CHECK_EQ(x, y) CHECK((x) == (y))
#define CHECK_NE(x, y) CHECK((x) != (y))
#define CHECK_GE(x, y) CHECK((x) >= (y))
#define CHECK_LE(x, y) CHECK((x) <= (y))
/* Replace null with std::cout to enable logging */
#define LOG(loglevel) \
null << #loglevel << " " << __FILE__ << ":" << __LINE__ << " " \
<< " "
#define LOG_KEY_VALUE(key, value) " " << key << "=" << value
#define LOG_KEY(key) LOG_KEY_VALUE(#key, key)
#define LOG_64BITS(key) LOG_KEY_VALUE(#key, HexString(key, 16))
#define LOG_32BITS(key) LOG_KEY_VALUE(#key, HexString(key, 8))
#define LOG_16BITS(key) LOG_KEY_VALUE(#key, HexString(key, 4))
#define LOG_8BITS(key) LOG_KEY_VALUE(#key, HexString(key, 2))
#define LOG_BITS(key) LOG_KEY_VALUE(#key, HexString(key))
#define LOG_DWEHPE(type) LOG_KEY_VALUE(#type, ShowDW_EH_PE(type))
template <class T>
std::string HexString(T* num, int length = -1) {
uint64_t n = reinterpret_cast<uint64_t>(num);
if (length == -1) {
length = 16;
}
std::stringstream ss;
ss << "0x" << std::uppercase << std::setfill('0') << std::setw(length) << std::hex << n;
return ss.str();
}
template <class T>
std::string HexString(const T* num, int length = -1) {
return HexString(const_cast<T*>(num), length);
}
template <class T>
std::string HexString(T num, int length = -1) {
if (length == -1) {
length = sizeof(T) / 2;
}
std::stringstream ss;
ss << "0x" << std::uppercase << std::setfill('0') << std::setw(length) << std::hex << +num;
return ss.str();
}
std::vector<std::string> SplitWith(std::string str, const std::string& delimiter);
std::string ShowRela(const Elf64_Rela& r);
std::string ShowSym(const Elf64_Sym& s, const char* strtab);
std::string ShowRelocationType(int type);