Debian Codegraph @

stapdyn/dynprobe.cxx

raw
1 // stapdyn probe functions
2 // Copyright (C) 2012-2014 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9 #include "dynprobe.h"
10 #include "dynutil.h"
11 #include "../staputil.h"
12
13 extern "C" {
14 #include "../runtime/dyninst/stapdyn.h"
15 }
16
17
18 using namespace std;
19
20
21 // Look for probes in the stap module which need Dyninst instrumentation.
22 int
23 find_dynprobes(void* module, vector<dynprobe_target>& targets)
24 {
25 // We query for probes with a function interface, so first we have
26 // to get function pointers from the stap module.
27 decltype(&stp_dyninst_target_count) target_count = NULL;
28 decltype(&stp_dyninst_target_path) target_path = NULL;
29
30 decltype(&stp_dyninst_probe_count) probe_count = NULL;
31 decltype(&stp_dyninst_probe_target) probe_target = NULL;
32 decltype(&stp_dyninst_probe_offset) probe_offset = NULL;
33 decltype(&stp_dyninst_probe_semaphore) probe_semaphore = NULL;
34
35 // If we don't even have this, then there aren't any uprobes in the module.
36 set_dlsym(target_count, module, "stp_dyninst_target_count", false);
37 if (target_count == NULL)
38 return 0;
39
40 try
41 {
42 // If target_count exists, the rest of these should too.
43 set_dlsym(target_path, module, "stp_dyninst_target_path");
44 set_dlsym(probe_count, module, "stp_dyninst_probe_count");
45 set_dlsym(probe_target, module, "stp_dyninst_probe_target");
46 set_dlsym(probe_offset, module, "stp_dyninst_probe_offset");
47 set_dlsym(probe_semaphore, module, "stp_dyninst_probe_semaphore");
48 }
49 catch (runtime_error& e)
50 {
51 staperror() << e.what() << endl;
52 return 1;
53 }
54
55 // This is optional - was only added post-2.0
56 decltype(&stp_dyninst_probe_flags) probe_flags = NULL;
57 set_dlsym(probe_flags, module, "stp_dyninst_probe_flags", false);
58
59 // Construct all the targets in the module.
60 const uint64_t ntargets = target_count();
61 for (uint64_t i = 0; i < ntargets; ++i)
62 {
63 const char* path = target_path(i);
64 if (path == NULL)
65 path = "";
66 dynprobe_target target(path);
67 targets.push_back(target);
68 }
69
70 // Construct all the probes in the module,
71 // and associate each with their target.
72 const uint64_t nprobes = probe_count();
73 for (uint64_t i = 0; i < nprobes; ++i)
74 {
75 uint64_t target_index = probe_target(i);
76 uint64_t offset = probe_offset(i);
77 uint64_t semaphore = probe_semaphore(i);
78 uint64_t flags = probe_flags ? probe_flags(i) : 0;
79 dynprobe_location p(i, offset, semaphore, flags);
80 if (p.validate() && target_index < ntargets)
81 targets[target_index].probes.push_back(p);
82 }
83
84 // For debugging, dump what we found.
85 for (uint64_t i = 0; i < ntargets; ++i)
86 {
87 dynprobe_target& t = targets[i];
88 staplog(3) << "target \"" << t.path << "\" has "
89 << t.probes.size() << " probes" << endl;
90 for (uint64_t j = 0; j < t.probes.size(); ++j)
91 staplog(3) << " offset:" << lex_cast_hex(t.probes[j].offset)
92 << " semaphore:" << lex_cast_hex(t.probes[j].semaphore)
93 << " flags:" << lex_cast_hex(t.probes[j].flags) << endl;
94 }
95
96 return 0;
97 }
98
99
100 dynprobe_location::dynprobe_location(uint64_t index, uint64_t offset,
101 uint64_t semaphore, uint64_t flags):
102 index(index), offset(offset), semaphore(semaphore),
103 flags(flags), return_p(flags & STAPDYN_PROBE_FLAG_RETURN)
104 {
105 }
106
107 bool
108 dynprobe_location::validate()
109 {
110 if (flags & ~STAPDYN_PROBE_ALL_FLAGS)
111 {
112 stapwarn() << "Unknown flags " << lex_cast_hex(flags)
113 << " in probe " << index << endl;
114 return false;
115 }
116
117 return true;
118 }
119
120
121 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */