-
Notifications
You must be signed in to change notification settings - Fork 19
/
debug_jit.cpp
94 lines (77 loc) · 1.71 KB
/
debug_jit.cpp
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
#include "debug_jit.hpp"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
namespace JIT
{
struct DebugBlock::Impl
{
Impl() = default;
Impl(Impl &&) = delete;
void operator=(Impl &&) = delete;
~Impl();
void *dylib = nullptr;
Func block = nullptr;
string name, soname;
bool compile(uint64_t hash, const std::string &source);
};
DebugBlock::DebugBlock(const unordered_map<string, uint64_t> &)
{
}
DebugBlock::~DebugBlock()
{
}
DebugBlock::Impl::~Impl()
{
if (dylib)
dlclose(dylib);
remove(soname.c_str());
remove(name.c_str());
}
bool DebugBlock::compile(uint64_t hash, const std::string &source)
{
impl = unique_ptr<Impl>(new Impl);
bool ret = impl->compile(hash, source);
if (ret)
block = impl->block;
return ret;
}
bool DebugBlock::Impl::compile(uint64_t hash, const std::string &source)
{
name = "/tmp/";
name += to_string(hash);
soname = name;
name += ".c";
soname += ".so";
FILE *file = fopen(name.c_str(), "w");
if (!file)
return false;
fputs(source.c_str(), file);
fclose(file);
char command[256];
if (sizeof(size_t) == 8)
{
sprintf(command, "gcc -o %s %s -shared -fpic -O0 -g -std=c99 -Wl,--unresolved-symbols=ignore-all",
soname.c_str(),
name.c_str());
}
else if (sizeof(size_t) == 4)
{
sprintf(command, "gcc -m32 -o %s %s -shared -fpic -O0 -g -std=c99 -Wl,--unresolved-symbols=ignore-all",
soname.c_str(),
name.c_str());
}
int ret = system(command);
if (ret != 0)
return false;
dylib = dlopen(soname.c_str(), RTLD_LOCAL | RTLD_LAZY);
if (!dylib)
return false;
block = reinterpret_cast<Func>(dlsym(dylib, "block_entry"));
if (!dylib)
return false;
return true;
}
} // namespace JIT