-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_wrappers.pl
executable file
·160 lines (117 loc) · 3.55 KB
/
gen_wrappers.pl
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
#!/usr/bin/perl -w
use YAML;
use Data::Dumper;
my $outfile = "wrap_funcs.c";
$#ARGV==0 or die "gen_wrappers.pl\n";
my $infile = "funcs.yaml";
my $yaml = YAML::LoadFile($infile) or die "Could not open YAML file $infile $!\n";
my @funcs = @{$yaml->{functions}};
open (WRAPPER, ">", $outfile);
print WRAPPER <<ENDF;
/***** THIS FILE IS AUTOGENERATED. DO NOT EDIT! *****/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <libelf/libelf.h>
#include "hrtrt.h"
#include "wrap.h"
extern Elf * elf_handle;
int hrt_mode_enabled = 0;
int
hrt_mode_switch (void)
{
hrt_mode_enabled = 1;
return 0;
}
ENDF
foreach my $func (@funcs) {
# we special case these two functions
if ($func->{name} eq "pthread_create" or $func->{name} eq "pthread_join") {
next;
}
if ($func->{override} eq "true") {
print_func_hdr(WRAPPER, $func->{name}, "__real_", $func->{ak_ret}, $func->{args});
print WRAPPER ";\n";
print_func_hdr(WRAPPER, $func->{name}, "__wrap_", $func->{ak_ret}, $func->{args});
print WRAPPER ";\n";
print_func_hdr(WRAPPER, $func->{name}, "__wrap_", $func->{ak_ret}, $func->{args});
} else {
print_func_hdr(WRAPPER, $func->{name}, "", $func->{ak_ret}, $func->{args});
print WRAPPER ";\n";
print_func_hdr(WRAPPER, $func->{name}, "", $func->{ak_ret}, $func->{args});
}
print WRAPPER <<ENDF;
{
char * ak_func;
int rc;
if (!hrt_mode_enabled) {
if (hrt_mode_switch() != 0) {
fprintf(stderr, "Could not switch into HRT mode, defaulting to original function\\n");
ENDF
if ($func->{override} eq "true") {
print WRAPPER " return __real_$func->{name}(";
for (my $i = 0; $i < $func->{num_args}; $i = $i + 1) {
print WRAPPER "a$i";
print WRAPPER "," unless $i == $func->{num_args}-1;
}
print WRAPPER ");\n";
} else {
print WRAPPER "exit(EXIT_FAILURE);\n";
}
print WRAPPER <<ENDF;
}
}
ENDF
if ($func->{override} eq "true") {
print WRAPPER "ak_func = \"$func->{ak_name}\";\n";
} else {
print WRAPPER "ak_func = (char*)__func__;\n";
}
print WRAPPER <<ENDF;
if (!ak_func) {
fprintf(stderr, "Could not find corresponding AeroKernel func name for (%s)\\n", __func__);
exit(EXIT_FAILURE);
}
void * call_addr = find_aerokernel_addr(elf_handle, ak_func);
if (!call_addr) {
fprintf(stderr, "Could not find target for (%s)\\n", ak_func);
exit(EXIT_FAILURE);
}
ENDF
print WRAPPER "rc=call_aerokernel_func((void (*)(void))call_addr,\n";
for (my $i = 0; $i < 8; $i = $i + 1) {
print WRAPPER "\t\t";
if ($i >= $func->{ak_args}) {
print WRAPPER "0,\n";
} else {
my $map = ${$func->{arg_mapping}}[$i];
if ($map != -1) {
print WRAPPER "(uint64_t)a$map,\n";
} else {
my $def = ${$func->{arg_defaults}}[$i];
print WRAPPER "(uint64_t)$def,\n";
}
}
}
print WRAPPER "ak_func);\n";
if ($func->{ak_ret} ne "void") {
print WRAPPER "return rc;\n";
}
print WRAPPER "}\n";
}
sub print_func_hdr {
my $file = shift;
my $name = shift;
my $prefix = shift;
my $ret = shift;
my $aref = shift;
my @args = @{$aref};
print $file "$ret $prefix" . "$name(";
foreach my $arg (@args) {
print $file $arg;
my $argnum = $#args;
print $file "," unless $arg eq $args[$argnum];
}
print $file ")";
}