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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
use proc_macro::*;
use crate::provider_info::ProviderInfo;
use crate::strings::*;
use crate::tree::Tree;
pub struct ProviderGenerator {
prov_tree: Tree,
tree1: Tree,
tree2: Tree,
}
impl ProviderGenerator {
pub fn new(span: Span) -> Self {
return Self {
prov_tree: Tree::new(span),
tree1: Tree::new(span),
tree2: Tree::new(span),
};
}
pub fn generate(&mut self, provider: ProviderInfo) -> TokenStream {
let provider_sym = provider.symbol.to_string();
// __start__eh_tracepoints_MY_PROVIDER
let provider_section_start =
String::from_iter([TRACEPOINTS_SECTION_START_PREFIX, &provider_sym]);
// __stop__eh_tracepoints_MY_PROVIDER
let provider_section_stop =
String::from_iter([TRACEPOINTS_SECTION_STOP_PREFIX, &provider_sym]);
let options = if provider.group_name.is_empty() {
String::new()
} else {
String::from_iter(["G", &provider.group_name])
};
let prov_tokens = self
.prov_tree
// #[cfg(not(target_os = "linux"))]
.add_cfg_not_linux()
// static MY_PROVIDER: eh::Provider = unsafe { ... };
.add_ident("static")
.add_token(provider.symbol.clone())
.add_punct(":")
.add_path(PROVIDER_PATH)
.add_punct("=")
.add_ident("unsafe")
.add_group_curly(
self.tree1
// eh::_internal::provider_new( ... )
.add_path_call(
PROVIDER_NEW_PATH,
self.tree2
// b"ProviderName",
.add_literal(Literal::byte_string(provider.name.as_bytes()))
.add_punct(",")
// b"Ggroupname",
.add_literal(Literal::byte_string(options.as_bytes()))
.add_punct(",")
// &_start__eh_tracepoints_MY_PROVIDER as *const usize,
.add_path_call(NULL_PATH, [])
.add_punct(",")
// &_stop__eh_tracepoints_MY_PROVIDER as *const usize,
.add_path_call(NULL_PATH, [])
.add_punct(",")
.drain(),
)
.drain(),
)
.add_punct(";")
// #[cfg(target_os = "linux")]
.add_cfg_linux()
// extern { _start; _stop; }
.add_ident("extern")
.add_group_curly(
self.tree1
// #[link_name = "__start__eh_tracepoints_MY_PROVIDER"]
.add_punct("#")
.add_group_square(
self.tree2
.add_ident("link_name")
.add_punct("=")
.add_literal(Literal::string(&provider_section_start))
.drain(),
)
// static mut _start__eh_tracepoints_MY_PROVIDER: ::core::primitive::usize;
.add_ident("static")
.add_ident("mut")
.add_ident(provider_section_start.split_at(1).1)
.add_punct(":")
.add_path(USIZE_PATH)
.add_punct(";")
// #[link_name = "__stop__eh_tracepoints_MY_PROVIDER"] // linker-generated symbol
.add_punct("#")
.add_group_square(
self.tree2
.add_ident("link_name")
.add_punct("=")
.add_literal(Literal::string(&provider_section_stop))
.drain(),
)
// static mut _stop__eh_tracepoints_MY_PROVIDER: ::core::primitive::usize;
.add_ident("static")
.add_ident("mut")
.add_ident(provider_section_stop.split_at(1).1)
.add_punct(":")
.add_path(USIZE_PATH)
.add_punct(";")
.drain(),
)
// #[link_section = "_eh_tracepoints_MY_PROVIDER"]
.add_punct("#")
.add_group_square(
self.tree1
.add_ident("link_section")
.add_punct("=")
.add_literal(Literal::string(&String::from_iter([
TRACEPOINTS_SECTION_PREFIX,
&provider_sym,
])))
.drain(),
)
// #[no_mangle]
.add_punct("#")
.add_group_square(self.tree1.add_ident("no_mangle").drain())
// #[used]
.add_punct("#")
.add_group_square(self.tree1.add_ident("used").drain())
// static _eh_define_provider_MY_PROVIDER: usize = 0;
.add_ident("static")
.add_ident(&String::from_iter([PROVIDER_PTR_VAR_PREFIX, &provider_sym]))
.add_punct(":")
.add_path(USIZE_PATH)
.add_punct("=")
.add_literal(Literal::usize_unsuffixed(0))
.add_punct(";")
// #[cfg(target_os = "linux")]
.add_cfg_linux()
// #[allow(static_mut_refs)]
.add_outer_attribute(
"allow",
self.tree1
.add_ident("static_mut_refs")
.drain(),
)
// static MY_PROVIDER: eh::Provider = unsafe { ... };
.add_ident("static")
.add_token(provider.symbol)
.add_punct(":")
.add_path(PROVIDER_PATH)
.add_punct("=")
.add_ident("unsafe")
.add_group_curly(
self.tree1
// eh::_internal::provider_new( ... )
.add_path_call(
PROVIDER_NEW_PATH,
self.tree2
// b"ProviderName",
.add_literal(Literal::byte_string(provider.name.as_bytes()))
.add_punct(",")
// b"Ggroupname",
.add_literal(Literal::byte_string(options.as_bytes()))
.add_punct(",")
// &_start__eh_tracepoints_MY_PROVIDER as *const usize,
.add_punct("&")
.add_ident(provider_section_start.split_at(1).1)
.add_ident("as")
.add_punct("*")
.add_ident("const")
.add_path(USIZE_PATH)
.add_punct(",")
// &_stop__eh_tracepoints_MY_PROVIDER as *const usize,
.add_punct("&")
.add_ident(provider_section_stop.split_at(1).1)
.add_ident("as")
.add_punct("*")
.add_ident("const")
.add_path(USIZE_PATH)
.add_punct(",")
.drain(),
)
.drain(),
)
.add_punct(";")
.drain()
.collect();
if provider.debug {
println!("{}", prov_tokens);
}
return prov_tokens;
}
}