Commit | Line | Data |
---|---|---|
68c1021b PMF |
1 | #include <stdio.h> |
2 | #include <unistd.h> | |
b6bf28ec | 3 | #include <sys/mman.h> |
68c1021b | 4 | |
59b161cd | 5 | #include "../libmarkers/marker.h" |
5f54827b PMF |
6 | #include "usterr.h" |
7 | #include "tracer.h" | |
59b161cd PMF |
8 | |
9 | void probe(const struct marker *mdata, | |
10 | void *probe_private, void *call_private, | |
11 | const char *fmt, va_list *args) | |
12 | { | |
13 | printf("In probe\n"); | |
14 | } | |
15 | ||
5f54827b PMF |
16 | //ust// void try_map() |
17 | //ust// { | |
18 | //ust// char *m; | |
19 | //ust// | |
20 | //ust// /* maybe add MAP_LOCKED */ | |
21 | //ust// m = mmap(NULL, 4096, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE , -1, 0); | |
22 | //ust// if(m == (char*)-1) { | |
23 | //ust// perror("mmap"); | |
24 | //ust// return; | |
25 | //ust// } | |
26 | //ust// | |
27 | //ust// printf("The mapping is at %p.\n", m); | |
28 | //ust// strcpy(m, "Hello, Mapping!"); | |
29 | //ust// } | |
30 | ||
8d938dbd PMF |
31 | //sig_atomic_t must_quit; |
32 | ||
33 | void inthandler(int sig) | |
34 | { | |
35 | printf("in handler\n"); | |
36 | exit(0); | |
37 | } | |
38 | ||
39 | int init_int_handler(void) | |
40 | { | |
41 | int result; | |
42 | struct sigaction act; | |
43 | ||
44 | result = sigemptyset(&act.sa_mask); | |
45 | if(result == -1) { | |
46 | PERROR("sigemptyset"); | |
47 | return -1; | |
48 | } | |
49 | ||
50 | act.sa_handler = inthandler; | |
51 | act.sa_flags = SA_RESTART; | |
52 | ||
53 | /* Only defer ourselves. Also, try to restart interrupted | |
54 | * syscalls to disturb the traced program as little as possible. | |
55 | */ | |
56 | result = sigaction(SIGINT, &act, NULL); | |
57 | if(result == -1) { | |
58 | PERROR("sigaction"); | |
59 | return -1; | |
60 | } | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
65 | DEFINE_MUTEX(probes_mutex); | |
66 | ||
67 | static LIST_HEAD(probes_registered_list); | |
68 | ||
69 | int ltt_marker_connect(const char *channel, const char *mname, | |
70 | const char *pname) | |
71 | ||
72 | { | |
73 | int ret; | |
74 | struct ltt_active_marker *pdata; | |
75 | struct ltt_available_probe *probe; | |
76 | ||
77 | ltt_lock_traces(); | |
78 | mutex_lock(&probes_mutex); | |
79 | probe = get_probe_from_name(pname); | |
80 | if (!probe) { | |
81 | ret = -ENOENT; | |
82 | goto end; | |
83 | } | |
84 | pdata = marker_get_private_data(channel, mname, probe->probe_func, 0); | |
85 | if (pdata && !IS_ERR(pdata)) { | |
86 | ret = -EEXIST; | |
87 | goto end; | |
88 | } | |
89 | pdata = kmem_cache_zalloc(markers_loaded_cachep, GFP_KERNEL); | |
90 | if (!pdata) { | |
91 | ret = -ENOMEM; | |
92 | goto end; | |
93 | } | |
94 | pdata->probe = probe; | |
95 | /* | |
96 | * ID has priority over channel in case of conflict. | |
97 | */ | |
98 | ret = marker_probe_register(channel, mname, NULL, | |
99 | probe->probe_func, pdata); | |
100 | if (ret) | |
101 | kmem_cache_free(markers_loaded_cachep, pdata); | |
102 | else | |
103 | list_add(&pdata->node, &markers_loaded_list); | |
104 | end: | |
105 | mutex_unlock(&probes_mutex); | |
106 | ltt_unlock_traces(); | |
107 | return ret; | |
108 | } | |
109 | ||
110 | ||
111 | int ltt_probe_register(struct ltt_available_probe *pdata) | |
112 | { | |
113 | int ret = 0; | |
114 | int comparison; | |
115 | struct ltt_available_probe *iter; | |
116 | ||
117 | mutex_lock(&probes_mutex); | |
118 | list_for_each_entry_reverse(iter, &probes_registered_list, node) { | |
119 | comparison = strcmp(pdata->name, iter->name); | |
120 | if (!comparison) { | |
121 | ret = -EBUSY; | |
122 | goto end; | |
123 | } else if (comparison > 0) { | |
124 | /* We belong to the location right after iter. */ | |
125 | list_add(&pdata->node, &iter->node); | |
126 | goto end; | |
127 | } | |
128 | } | |
129 | /* Should be added at the head of the list */ | |
130 | list_add(&pdata->node, &probes_registered_list); | |
131 | end: | |
132 | mutex_unlock(&probes_mutex); | |
133 | return ret; | |
134 | } | |
135 | ||
136 | ||
137 | struct ltt_available_probe default_probe = { | |
138 | .name = "default", | |
139 | .format = NULL, | |
140 | .probe_func = ltt_vtrace, | |
141 | .callbacks[0] = ltt_serialize_data, | |
142 | }; | |
143 | ||
5f54827b | 144 | int main() |
b6bf28ec | 145 | { |
5f54827b PMF |
146 | int result; |
147 | ||
8d938dbd PMF |
148 | init_int_handler(); |
149 | ||
5f54827b PMF |
150 | init_ustrelay_transport(); |
151 | ||
8d938dbd PMF |
152 | printf("page size is %d\n", sysconf(_SC_PAGE_SIZE)); |
153 | ||
5f54827b | 154 | char trace_name[] = "theusttrace"; |
8d938dbd | 155 | char trace_type[] = "ustrelay"; |
5f54827b PMF |
156 | |
157 | marker_probe_register("abc", "testmark", "", probe, NULL); | |
158 | marker_probe_register("metadata", "core_marker_id", "channel %s name %s event_id %hu int #1u%zu long #1u%zu pointer #1u%zu size_t #1u%zu alignment #1u%u", probe, NULL); | |
8d938dbd PMF |
159 | result = ltt_probe_register(&default_probe); |
160 | if(result) | |
161 | ERR("ltt_probe_register"); | |
162 | ||
163 | result = ltt_marker_connect("abc", "testmark2", "default"); | |
164 | if(result) | |
165 | ERR("ltt_marker_connect"); | |
166 | ||
b6bf28ec | 167 | |
5f54827b PMF |
168 | result = ltt_trace_setup(trace_name); |
169 | if(result < 0) { | |
170 | ERR("ltt_trace_setup failed"); | |
171 | return 1; | |
b6bf28ec PMF |
172 | } |
173 | ||
8d938dbd PMF |
174 | result = ltt_trace_set_type(trace_name, trace_type); |
175 | if(result < 0) { | |
176 | ERR("ltt_trace_set_type failed"); | |
177 | return 1; | |
178 | } | |
b6bf28ec | 179 | |
5f54827b PMF |
180 | result = ltt_trace_alloc(trace_name); |
181 | if(result < 0) { | |
182 | ERR("ltt_trace_alloc failed"); | |
183 | return 1; | |
184 | } | |
b6bf28ec | 185 | |
8d938dbd PMF |
186 | result = ltt_trace_start(trace_name); |
187 | if(result < 0) { | |
188 | ERR("ltt_trace_start failed"); | |
189 | return 1; | |
190 | } | |
191 | ||
b6bf28ec | 192 | |
68c1021b | 193 | printf("Hello, World!\n"); |
59b161cd | 194 | |
8d938dbd PMF |
195 | for(;;) { |
196 | trace_mark(abc, testmark, "", MARK_NOARGS); | |
197 | trace_mark(abc, testmark2, "", MARK_NOARGS); | |
198 | sleep(1); | |
199 | } | |
59b161cd | 200 | |
68c1021b PMF |
201 | scanf("%*s"); |
202 | ||
203 | return 0; | |
204 | } |