-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.cpp
362 lines (306 loc) · 9.37 KB
/
plugin.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
*
*/
#include <iostream>
#include <stdio.h>
#include <gcc-plugin.h>
#include "tree.h"
#include "plugin-version.h"
//#include <iostream>
#include <vector>
#include <gcc-plugin.h>
#include <coretypes.h>
#include <tree.h>
#include "record_context.hpp"
#include <string.h>
#include "introspection.hpp"
#include "callback.hpp"
#include "callbacks.hpp"
#include "tree-pass.h"
#include "cp/name-lookup.h"
#include "owl/librdfinterface.hpp"
#include <google/coredumper.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <criu/criu.h>
static void error_msg(int code){
switch (code) {
case -EBADE:
cerr << "RPC has returned fail" << endl;
break;
case -ECONNREFUSED:
cerr << "Unable to connect to CRIU" << endl;
break;
case -ECOMM:
cerr << "Unable to send/recv msg to/from CRIU" << endl;
break;
case -EINVAL:
cerr << "CRIU doesn't support this type of request."
"You should probably update CRIU" << endl;
break;
case -EBADMSG:
cerr << "Unexpected response from CRIU." << endl;
break;
default:
cerr << "Unknown error type code." << endl;
}
}
//static int ccount = 0;
static void write_core(const char * tag) {
//char fname [256];
//sprintf(fname,"core.%s.%d",tag,ccount++);
//printf("going to write core %s\n",fname);
//WriteCoreDump(fname);
//raise (SIGABRT);
cout << "Dump start\n";
criu_init_opts();
/*
start the daemon in the bg like this in the same dir
/usr/sbin/criu service -v4
*/
//const char socket = "./criu_service.socket";
char socket [300];
//strcpy(socket, "/var/run/criu_service.socket");
strcpy(socket, "/usr/local/sbin/criu");
criu_set_service_address(socket);
criu_set_shell_job(true);
criu_set_leave_running(true);
//cribu_set_service_binary("/usr/local/sbin/criu");
//criu_set_pid(pid);
//criu_set_log_file("dump.log");
criu_set_log_level(4);
int fd = open("./criu/", O_DIRECTORY);
criu_set_images_dir_fd(fd);
//criu_set_service_comm(CRIU_COMM_SK);
int ret = criu_check();
if (ret < 0) {
cout << "err" << ret;
cout << "check failed\n";
error_msg(ret);
}
else {
cout << "check succeeded\n";
}
ret = criu_dump();
//sleep(4);
if (ret < 0) {
//what_err_ret_mean(ret);
cout << "err" << ret;
cout << "Dump failed\n";
error_msg(ret);
}
else {
cout << "Dump succeeded\n";
}
}
//#include "symtab.h"
//#include "cgraph.h"
using namespace std;
union tree_node;
typedef union tree_node *tree;
// static void
// generic_callback_PLUGIN_ALL_IPA_PASSES_START ()
// {
// }
static void
generic_callback_PLUGIN_FINISH_TYPE (tree t, void *i)
{
//cpp_callback_PLUGIN_FINISH_TYPE (t, i);
cerr << "generic_callback_PLUGIN_FINISH_TYPE(";
call_type_ret < CallBack, int >(t, CallBack::finish_type_callback);
cerr << ")" << endl;
}
static void
generic_callback_PLUGIN_FINISH_DECL (tree t, void *i)
{
//cpp_callback_PLUGIN_FINISH_DECL (t, i);
/*cerr << "finish decl" << endl;
int x= call_type_ret<CallBack,int>(t,
CallBack::finish_decl_callback
); */
}
static void generic_callback_PLUGIN_FINISH_UNIT (void *a, void *b);
static void
generic_callback_PLUGIN_START_UNIT (void *a, void *b)
{
cerr << "start unit" << a << " : " << b << " main_input_filename:" <<
main_input_filename << endl;
rdf_world::get_context ().start_unit (main_input_filename);
//generic_callback_PLUGIN_FINISH_UNIT(a,b);
}
/* Obtain a backtrace and print it to stdout.
https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
*/
void
print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 20);
strings = backtrace_symbols (array, size);
cerr << "Obtained %zd stack frames." << size << "\n";
for (i = 0; i < size; i++){
cerr << strings[i] << endl;
}
free (strings);
}
// forward
int dump_memory_main();
static void
generic_callback_PLUGIN_FINISH_UNIT (void *a, void *b)
{
cerr << "finish unit"<< a << " : " << b << endl;
int i;
tree t;
FOR_EACH_VEC_ELT ( (*all_translation_units), i, t )
{
cerr << "each unit"<< i << " : " << t << endl;
// call_type_ret<CallBack,int>(t,
// CallBack::finish_unit_callback);
}
// call_type_ret<CallBack,int>(global_namespace,
// CallBack::finish_unit_callback);
//cerr << "end unit"<< a << " : " << b << endl;
//cerr << "end unit" << endl;
rdf_world::get_context ().finish_unit ();
print_trace ();
dump_memory_main();
//cout << "going to call criu\n";
//write_core("finish_unit");
cout << "going to kill the process now\n";
kill(getpid(), SIGQUIT); // now just kill the process and produce a normal core
}
// static void
// generic_callback_PLUGIN_FINISH (tree t, void *_)
// {
// cerr << "finished_plugin" << endl;
// }
// static void
// generic_callback_PLUGIN_ATTRIBUTES ()
// {
// }
// /*
// handling of new passes
// */
static void
generic_callback_PLUGIN_NEW_PASS (opt_pass * new_pass, void *b)
{
cerr << "NEW PASS DEFINED_" << new_pass << " name: "<< new_pass->name << endl;
}
// static void
// generic_callback_PLUGIN_EVENT_FIRST_DYNAMIC (opt_pass * new_pass, void *b)
// {
// cerr << "PLUGIN_EVENT_FIRST_DYNAMIC: " << new_pass;
// cerr << endl;
// //cerr << " name: "<< new_pass->name << endl;
// }
static void
generic_callback_PLUGIN_ALL_PASSES_START (opt_pass * new_pass, void *b)
{
cerr << "PLUGIN_ALL_PASSES_START: " << new_pass;
cerr << endl;
}
//static int callcount = 0;
static void
generic_callback_PLUGIN_PASS_EXECUTION (opt_pass * new_pass, void *b)
{
cerr << "PLUGIN_EVENT_PASS_EXECUTION: " << new_pass->name;
cerr << "\tNumber: " << new_pass->static_pass_number;
cerr << "\tType: " << new_pass->type;
cerr << endl;
// tree t;
// int i;
// FOR_EACH_VEC_ELT ( (*all_translation_units), i, t ){
// cerr << "insite each unit"<< i << " : " << t << endl;
// call_type_ret<CallBack,int>(t,
// CallBack::finish_unit_callback);
// }
// if (callcount++ == 0) {
// write_core("first_pass");
// }
}
#define DEFEVENTEMPTY(X) \
static void generic_callback_ ##X (void *a, void * b) { \
cerr << "PLUGIN_" #X << a << " : " << b << endl; \
}
//DEFEVENTEMPTY (PLUGIN_PASS_MANAGER_SETUP);
//DEFEVENTEMPTY (PLUGIN_PRE_GENERICIZE);
DEFEVENTEMPTY (PLUGIN_FINISH);
//DEFEVENTEMPTY (PLUGIN_INFO);
//DEFEVENTEMPTY (PLUGIN_GGC_START);
//DEFEVENTEMPTY (PLUGIN_GGC_MARKING);
//DEFEVENTEMPTY (PLUGIN_GGC_END);
//DEFEVENTEMPTY (PLUGIN_REGISTER_GGC_ROOTS);
//DEFEVENTEMPTY (PLUGIN_REGISTER_GGC_CACHES);
//DEFEVENTEMPTY (PLUGIN_ATTRIBUTES);
//DEFEVENTEMPTY (PLUGIN_PRAGMAS);
//DEFEVENTEMPTY (PLUGIN_ALL_PASSES_START);
DEFEVENTEMPTY (PLUGIN_ALL_PASSES_END);
//DEFEVENTEMPTY (PLUGIN_ALL_IPA_PASSES_START);
//DEFEVENTEMPTY (PLUGIN_ALL_IPA_PASSES_END);
//DEFEVENTEMPTY (PLUGIN_OVERRIDE_GATE);
//DEFEVENTEMPTY (PLUGIN_EARLY_GIMPLE_PASSES_START);
//DEFEVENTEMPTY (PLUGIN_EARLY_GIMPLE_PASSES_END);
//DEFEVENTEMPTY (PLUGIN_NEW_PASS);
//DEFEVENTEMPTY (PLUGIN_INCLUDE_FILE);
DEFEVENTEMPTY ( PLUGIN_EVENT_FIRST_DYNAMIC);
/*
register a simple handler for all plugins,
the commented out ones just crash
*/
#define DEFEVENTSPECIAL(X) \
cerr << "going to register callback" << X << endl; \
register_callback ( \
plugin_info->base_name, \
X, \
(plugin_callback_func)&generic_callback_ ##X, \
0 \
);
//enum plugin_event
void
register_special_plugins (struct plugin_name_args *plugin_info)
{
/* Called at start of GCC Garbage Collection. */
/* Called before GCC exits. */
//DEFEVENTSPECIAL(PLUGIN_ALL_IPA_PASSES_END);
//DEFEVENTSPECIAL(PLUGIN_ALL_IPA_PASSES_START);
//DEFEVENTSPECIAL(PLUGIN_ALL_IPA_PASSES_START); // close the output files
DEFEVENTSPECIAL(PLUGIN_ALL_PASSES_END);
DEFEVENTSPECIAL (PLUGIN_ALL_PASSES_START);
//DEFEVENTSPECIAL(PLUGIN_ATTRIBUTES);
//DEFEVENTSPECIAL(PLUGIN_EARLY_GIMPLE_PASSES_END);
//DEFEVENTSPECIAL(PLUGIN_EARLY_GIMPLE_PASSES_START);
//DEFEVENTSPECIAL(PLUGIN_EVENT_FIRST_DYNAMIC);
DEFEVENTSPECIAL(PLUGIN_FINISH );
//DEFEVENTSPECIAL(PLUGIN_GGC_END);
//DEFEVENTSPECIAL(PLUGIN_GGC_MARKING);
//DEFEVENTSPECIAL(PLUGIN_GGC_START);
//DEFEVENTSPECIAL(PLUGIN_INFO ); crash
//DEFEVENTSPECIAL(PLUGIN_OVERRIDE_GATE);
//DEFEVENTSPECIAL(PLUGIN_PASS_MANAGER_SETUP ); crash
//DEFEVENTSPECIAL(PLUGIN_PRAGMAS);
//DEFEVENTSPECIAL(PLUGIN_REGISTER_GGC_CACHES);
//DEFEVENTSPECIAL(PLUGIN_REGISTER_GGC_ROOTS);
DEFEVENTSPECIAL (PLUGIN_FINISH);
DEFEVENTSPECIAL (PLUGIN_FINISH_DECL);
DEFEVENTSPECIAL (PLUGIN_FINISH_TYPE);
DEFEVENTSPECIAL (PLUGIN_FINISH_UNIT);
// DEFEVENTSPECIAL(PLUGIN_INCLUDE_FILE);
//DEFEVENTSPECIAL(PLUGIN_NEW_PASS);
DEFEVENTSPECIAL (PLUGIN_PASS_EXECUTION);
DEFEVENTSPECIAL (PLUGIN_START_UNIT); // open the output files
//DEFEVENTSPECIAL(PLUGIN_PRE_GENERICIZE );
}
int plugin_is_GPL_compatible; //
int
plugin_init (struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version)
{
// register_all_plugins(plugin_info);
register_special_plugins (plugin_info);
return 0;
}