Commit | Line | Data |
---|---|---|
ba6459ba PMF |
1 | /* |
2 | * Copyright (C) 2007 Mathieu Desnoyers | |
3 | * | |
34e4b7db PMF |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; either | |
7 | * version 2.1 of the License, or (at your option) any later version. | |
ba6459ba | 8 | * |
34e4b7db | 9 | * This library is distributed in the hope that it will be useful, |
ba6459ba | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
34e4b7db PMF |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. | |
ba6459ba | 13 | * |
34e4b7db PMF |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
ba6459ba PMF |
17 | * |
18 | * LTT marker control module over /proc | |
19 | */ | |
20 | ||
772030fe | 21 | #include <ctype.h> |
909bc43f | 22 | #include <stdlib.h> |
772030fe | 23 | |
c93858f1 | 24 | #include "tracer.h" |
6af64c43 | 25 | #include "usterr.h" |
ba6459ba PMF |
26 | |
27 | #define DEFAULT_CHANNEL "cpu" | |
28 | #define DEFAULT_PROBE "default" | |
29 | ||
30 | LIST_HEAD(probes_list); | |
31 | ||
32 | /* | |
33 | * Mutex protecting the probe slab cache. | |
34 | * Nests inside the traces mutex. | |
35 | */ | |
36 | DEFINE_MUTEX(probes_mutex); | |
37 | ||
38 | struct ltt_available_probe default_probe = { | |
39 | .name = "default", | |
40 | .format = NULL, | |
41 | .probe_func = ltt_vtrace, | |
42 | .callbacks[0] = ltt_serialize_data, | |
43 | }; | |
44 | ||
45 | //ust//static struct kmem_cache *markers_loaded_cachep; | |
46 | static LIST_HEAD(markers_loaded_list); | |
47 | /* | |
48 | * List sorted by name strcmp order. | |
49 | */ | |
50 | static LIST_HEAD(probes_registered_list); | |
51 | ||
52 | //ust// static struct proc_dir_entry *pentry; | |
53 | ||
54 | //ust// static struct file_operations ltt_fops; | |
55 | ||
56 | static struct ltt_available_probe *get_probe_from_name(const char *pname) | |
57 | { | |
58 | struct ltt_available_probe *iter; | |
59 | int comparison, found = 0; | |
60 | ||
61 | if (!pname) | |
62 | pname = DEFAULT_PROBE; | |
63 | list_for_each_entry(iter, &probes_registered_list, node) { | |
64 | comparison = strcmp(pname, iter->name); | |
65 | if (!comparison) | |
66 | found = 1; | |
67 | if (comparison <= 0) | |
68 | break; | |
69 | } | |
70 | if (found) | |
71 | return iter; | |
72 | else | |
73 | return NULL; | |
74 | } | |
75 | ||
772030fe | 76 | /* (unused) |
ba6459ba PMF |
77 | static char *skip_spaces(char *buf) |
78 | { | |
79 | while (*buf != '\0' && isspace(*buf)) | |
80 | buf++; | |
81 | return buf; | |
82 | } | |
83 | ||
84 | static char *skip_nonspaces(char *buf) | |
85 | { | |
86 | while (*buf != '\0' && !isspace(*buf)) | |
87 | buf++; | |
88 | return buf; | |
89 | } | |
90 | ||
91 | static void get_marker_string(char *buf, char **start, | |
92 | char **end) | |
93 | { | |
94 | *start = skip_spaces(buf); | |
95 | *end = skip_nonspaces(*start); | |
96 | **end = '\0'; | |
97 | } | |
772030fe | 98 | */ |
ba6459ba PMF |
99 | |
100 | int ltt_probe_register(struct ltt_available_probe *pdata) | |
101 | { | |
102 | int ret = 0; | |
103 | int comparison; | |
104 | struct ltt_available_probe *iter; | |
105 | ||
106 | mutex_lock(&probes_mutex); | |
107 | list_for_each_entry_reverse(iter, &probes_registered_list, node) { | |
108 | comparison = strcmp(pdata->name, iter->name); | |
109 | if (!comparison) { | |
110 | ret = -EBUSY; | |
111 | goto end; | |
112 | } else if (comparison > 0) { | |
113 | /* We belong to the location right after iter. */ | |
114 | list_add(&pdata->node, &iter->node); | |
115 | goto end; | |
116 | } | |
117 | } | |
118 | /* Should be added at the head of the list */ | |
119 | list_add(&pdata->node, &probes_registered_list); | |
120 | end: | |
121 | mutex_unlock(&probes_mutex); | |
122 | return ret; | |
123 | } | |
ba6459ba PMF |
124 | |
125 | /* | |
126 | * Called when a probe does not want to be called anymore. | |
127 | */ | |
128 | int ltt_probe_unregister(struct ltt_available_probe *pdata) | |
129 | { | |
130 | int ret = 0; | |
131 | struct ltt_active_marker *amark, *tmp; | |
132 | ||
133 | mutex_lock(&probes_mutex); | |
134 | list_for_each_entry_safe(amark, tmp, &markers_loaded_list, node) { | |
135 | if (amark->probe == pdata) { | |
136 | ret = marker_probe_unregister_private_data( | |
137 | pdata->probe_func, amark); | |
138 | if (ret) | |
139 | goto end; | |
140 | list_del(&amark->node); | |
141 | free(amark); | |
142 | } | |
143 | } | |
144 | list_del(&pdata->node); | |
145 | end: | |
146 | mutex_unlock(&probes_mutex); | |
147 | return ret; | |
148 | } | |
ba6459ba PMF |
149 | |
150 | /* | |
151 | * Connect marker "mname" to probe "pname". | |
152 | * Only allow _only_ probe instance to be connected to a marker. | |
153 | */ | |
154 | int ltt_marker_connect(const char *channel, const char *mname, | |
155 | const char *pname) | |
156 | ||
157 | { | |
158 | int ret; | |
159 | struct ltt_active_marker *pdata; | |
160 | struct ltt_available_probe *probe; | |
161 | ||
162 | ltt_lock_traces(); | |
163 | mutex_lock(&probes_mutex); | |
164 | probe = get_probe_from_name(pname); | |
165 | if (!probe) { | |
166 | ret = -ENOENT; | |
167 | goto end; | |
168 | } | |
169 | pdata = marker_get_private_data(channel, mname, probe->probe_func, 0); | |
170 | if (pdata && !IS_ERR(pdata)) { | |
171 | ret = -EEXIST; | |
172 | goto end; | |
173 | } | |
174 | pdata = zmalloc(sizeof(struct ltt_active_marker)); | |
175 | if (!pdata) { | |
176 | ret = -ENOMEM; | |
177 | goto end; | |
178 | } | |
179 | pdata->probe = probe; | |
180 | /* | |
181 | * ID has priority over channel in case of conflict. | |
182 | */ | |
183 | ret = marker_probe_register(channel, mname, NULL, | |
184 | probe->probe_func, pdata); | |
185 | if (ret) | |
186 | free(pdata); | |
187 | else | |
188 | list_add(&pdata->node, &markers_loaded_list); | |
189 | end: | |
190 | mutex_unlock(&probes_mutex); | |
191 | ltt_unlock_traces(); | |
192 | return ret; | |
193 | } | |
ba6459ba PMF |
194 | |
195 | /* | |
196 | * Disconnect marker "mname", probe "pname". | |
197 | */ | |
198 | int ltt_marker_disconnect(const char *channel, const char *mname, | |
199 | const char *pname) | |
200 | { | |
201 | struct ltt_active_marker *pdata; | |
202 | struct ltt_available_probe *probe; | |
203 | int ret = 0; | |
204 | ||
205 | mutex_lock(&probes_mutex); | |
206 | probe = get_probe_from_name(pname); | |
207 | if (!probe) { | |
208 | ret = -ENOENT; | |
209 | goto end; | |
210 | } | |
211 | pdata = marker_get_private_data(channel, mname, probe->probe_func, 0); | |
212 | if (IS_ERR(pdata)) { | |
213 | ret = PTR_ERR(pdata); | |
214 | goto end; | |
215 | } else if (!pdata) { | |
216 | /* | |
217 | * Not registered by us. | |
218 | */ | |
219 | ret = -EPERM; | |
220 | goto end; | |
221 | } | |
222 | ret = marker_probe_unregister(channel, mname, probe->probe_func, pdata); | |
223 | if (ret) | |
224 | goto end; | |
225 | else { | |
226 | list_del(&pdata->node); | |
227 | free(pdata); | |
228 | } | |
229 | end: | |
230 | mutex_unlock(&probes_mutex); | |
231 | return ret; | |
232 | } | |
ba6459ba PMF |
233 | |
234 | /* | |
235 | * function handling proc entry write. | |
236 | * | |
237 | * connect <channel name> <marker name> [<probe name>]] | |
238 | * disconnect <channel name> <marker name> [<probe name>] | |
239 | */ | |
240 | //ust// static ssize_t ltt_write(struct file *file, const char __user *buffer, | |
241 | //ust// size_t count, loff_t *offset) | |
242 | //ust// { | |
243 | //ust// char *kbuf; | |
244 | //ust// char *iter, *marker_action, *arg[4]; | |
245 | //ust// ssize_t ret; | |
246 | //ust// int i; | |
247 | //ust// | |
248 | //ust// if (!count) | |
249 | //ust// return -EINVAL; | |
250 | //ust// | |
251 | //ust// kbuf = vmalloc(count + 1); | |
252 | //ust// kbuf[count] = '\0'; /* Transform into a string */ | |
253 | //ust// ret = copy_from_user(kbuf, buffer, count); | |
254 | //ust// if (ret) { | |
255 | //ust// ret = -EINVAL; | |
256 | //ust// goto end; | |
257 | //ust// } | |
258 | //ust// get_marker_string(kbuf, &marker_action, &iter); | |
259 | //ust// if (!marker_action || marker_action == iter) { | |
260 | //ust// ret = -EINVAL; | |
261 | //ust// goto end; | |
262 | //ust// } | |
263 | //ust// for (i = 0; i < 4; i++) { | |
264 | //ust// arg[i] = NULL; | |
265 | //ust// if (iter < kbuf + count) { | |
266 | //ust// iter++; /* skip the added '\0' */ | |
267 | //ust// get_marker_string(iter, &arg[i], &iter); | |
268 | //ust// if (arg[i] == iter) | |
269 | //ust// arg[i] = NULL; | |
270 | //ust// } | |
271 | //ust// } | |
272 | //ust// | |
273 | //ust// if (!arg[0] || !arg[1]) { | |
274 | //ust// ret = -EINVAL; | |
275 | //ust// goto end; | |
276 | //ust// } | |
277 | //ust// | |
278 | //ust// if (!strcmp(marker_action, "connect")) { | |
279 | //ust// ret = ltt_marker_connect(arg[0], arg[1], arg[2]); | |
280 | //ust// if (ret) | |
281 | //ust// goto end; | |
282 | //ust// } else if (!strcmp(marker_action, "disconnect")) { | |
283 | //ust// ret = ltt_marker_disconnect(arg[0], arg[1], arg[2]); | |
284 | //ust// if (ret) | |
285 | //ust// goto end; | |
286 | //ust// } | |
287 | //ust// ret = count; | |
288 | //ust// end: | |
289 | //ust// vfree(kbuf); | |
290 | //ust// return ret; | |
291 | //ust// } | |
292 | //ust// | |
293 | //ust// static void *s_next(struct seq_file *m, void *p, loff_t *pos) | |
294 | //ust// { | |
295 | //ust// struct marker_iter *iter = m->private; | |
296 | //ust// | |
297 | //ust// marker_iter_next(iter); | |
298 | //ust// if (!iter->marker) { | |
299 | //ust// /* | |
300 | //ust// * Setting the iter module to -1UL will make sure | |
301 | //ust// * that no module can possibly hold the current marker. | |
302 | //ust// */ | |
303 | //ust// iter->module = (void *)-1UL; | |
304 | //ust// return NULL; | |
305 | //ust// } | |
306 | //ust// return iter->marker; | |
307 | //ust// } | |
308 | //ust// | |
309 | //ust// static void *s_start(struct seq_file *m, loff_t *pos) | |
310 | //ust// { | |
311 | //ust// struct marker_iter *iter = m->private; | |
312 | //ust// | |
313 | //ust// if (!*pos) | |
314 | //ust// marker_iter_reset(iter); | |
315 | //ust// marker_iter_start(iter); | |
316 | //ust// if (!iter->marker) { | |
317 | //ust// /* | |
318 | //ust// * Setting the iter module to -1UL will make sure | |
319 | //ust// * that no module can possibly hold the current marker. | |
320 | //ust// */ | |
321 | //ust// iter->module = (void *)-1UL; | |
322 | //ust// return NULL; | |
323 | //ust// } | |
324 | //ust// return iter->marker; | |
325 | //ust// } | |
326 | //ust// | |
327 | //ust// static void s_stop(struct seq_file *m, void *p) | |
328 | //ust// { | |
329 | //ust// marker_iter_stop(m->private); | |
330 | //ust// } | |
331 | //ust// | |
332 | //ust// static int s_show(struct seq_file *m, void *p) | |
333 | //ust// { | |
334 | //ust// struct marker_iter *iter = m->private; | |
335 | //ust// | |
336 | //ust// seq_printf(m, "channel: %s marker: %s format: \"%s\" state: %d " | |
337 | //ust// "event_id: %hu call: 0x%p probe %s : 0x%p\n", | |
338 | //ust// iter->marker->channel, | |
339 | //ust// iter->marker->name, iter->marker->format, | |
340 | //ust// _imv_read(iter->marker->state), | |
341 | //ust// iter->marker->event_id, | |
342 | //ust// iter->marker->call, | |
343 | //ust// iter->marker->ptype ? "multi" : "single", | |
344 | //ust// iter->marker->ptype ? | |
345 | //ust// (void*)iter->marker->multi : (void*)iter->marker->single.func); | |
346 | //ust// return 0; | |
347 | //ust// } | |
348 | //ust// | |
349 | //ust// static const struct seq_operations ltt_seq_op = { | |
350 | //ust// .start = s_start, | |
351 | //ust// .next = s_next, | |
352 | //ust// .stop = s_stop, | |
353 | //ust// .show = s_show, | |
354 | //ust// }; | |
355 | //ust// | |
356 | //ust// static int ltt_open(struct inode *inode, struct file *file) | |
357 | //ust// { | |
358 | //ust// /* | |
359 | //ust// * Iterator kept in m->private. | |
360 | //ust// * Restart iteration on all modules between reads because we do not lock | |
361 | //ust// * the module mutex between those. | |
362 | //ust// */ | |
363 | //ust// int ret; | |
364 | //ust// struct marker_iter *iter; | |
365 | //ust// | |
366 | //ust// iter = kzalloc(sizeof(*iter), GFP_KERNEL); | |
367 | //ust// if (!iter) | |
368 | //ust// return -ENOMEM; | |
369 | //ust// | |
370 | //ust// ret = seq_open(file, <t_seq_op); | |
371 | //ust// if (ret == 0) | |
372 | //ust// ((struct seq_file *)file->private_data)->private = iter; | |
373 | //ust// else | |
374 | //ust// kfree(iter); | |
375 | //ust// return ret; | |
376 | //ust// } | |
377 | //ust// | |
378 | //ust// static struct file_operations ltt_fops = { | |
379 | //ust// .write = ltt_write, | |
380 | //ust// .open = ltt_open, | |
381 | //ust// .read = seq_read, | |
382 | //ust// .llseek = seq_lseek, | |
383 | //ust// .release = seq_release_private, | |
384 | //ust// }; | |
385 | ||
386 | static void disconnect_all_markers(void) | |
387 | { | |
388 | struct ltt_active_marker *pdata, *tmp; | |
389 | ||
390 | list_for_each_entry_safe(pdata, tmp, &markers_loaded_list, node) { | |
391 | marker_probe_unregister_private_data(pdata->probe->probe_func, | |
392 | pdata); | |
393 | list_del(&pdata->node); | |
394 | free(pdata); | |
395 | } | |
396 | } | |
397 | ||
4db647c5 PMF |
398 | static char initialized = 0; |
399 | ||
400 | void __attribute__((constructor)) init_marker_control(void) | |
ba6459ba | 401 | { |
4db647c5 PMF |
402 | if(!initialized) { |
403 | int ret; | |
ba6459ba PMF |
404 | |
405 | //ust// pentry = create_proc_entry("ltt", S_IRUSR|S_IWUSR, NULL); | |
406 | //ust// if (!pentry) | |
407 | //ust// return -EBUSY; | |
408 | //ust// markers_loaded_cachep = KMEM_CACHE(ltt_active_marker, 0); | |
409 | ||
4db647c5 PMF |
410 | ret = ltt_probe_register(&default_probe); |
411 | BUG_ON(ret); | |
412 | ret = ltt_marker_connect("metadata", "core_marker_format", | |
413 | DEFAULT_PROBE); | |
414 | BUG_ON(ret); | |
415 | ret = ltt_marker_connect("metadata", "core_marker_id", DEFAULT_PROBE); | |
416 | BUG_ON(ret); | |
ba6459ba PMF |
417 | //ust// pentry->proc_fops = <t_fops; |
418 | ||
4db647c5 PMF |
419 | initialized = 1; |
420 | } | |
ba6459ba PMF |
421 | } |
422 | //ust// module_init(marker_control_init); | |
423 | ||
772030fe | 424 | static void __attribute__((destructor)) marker_control_exit(void) |
ba6459ba PMF |
425 | { |
426 | int ret; | |
427 | ||
428 | //ust// remove_proc_entry("ltt", NULL); | |
429 | ret = ltt_marker_disconnect("metadata", "core_marker_format", | |
430 | DEFAULT_PROBE); | |
431 | BUG_ON(ret); | |
432 | ret = ltt_marker_disconnect("metadata", "core_marker_id", | |
433 | DEFAULT_PROBE); | |
434 | BUG_ON(ret); | |
435 | ret = ltt_probe_unregister(&default_probe); | |
436 | BUG_ON(ret); | |
437 | disconnect_all_markers(); | |
438 | //ust// kmem_cache_destroy(markers_loaded_cachep); | |
439 | //ust// marker_synchronize_unregister(); | |
440 | } | |
441 | //ust// module_exit(marker_control_exit); | |
442 | ||
443 | //ust// MODULE_LICENSE("GPL"); | |
444 | //ust// MODULE_AUTHOR("Mathieu Desnoyers"); | |
445 | //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Marker Control"); |