ust: allow tracing of dynamically linked libraries
[ust.git] / hello / hello.c
CommitLineData
68c1021b
PMF
1#include <stdio.h>
2#include <unistd.h>
b6bf28ec 3#include <sys/mman.h>
9c67dc50
PMF
4#include <stdarg.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
68c1021b 8
59b161cd 9#include "../libmarkers/marker.h"
5f54827b
PMF
10#include "usterr.h"
11#include "tracer.h"
ba6459ba 12#include "marker-control.h"
9c67dc50
PMF
13#include "relay.h"
14
15
16char consumer_stack[10000];
17char trace_name[] = "theusttrace";
18char trace_type[] = "ustrelay";
19
20#define CPRINTF(fmt, args...) safe_printf(fmt "\n", ## args)
21
22int safe_printf(const char *fmt, ...)
23{
24 static char buf[500];
25 va_list ap;
26 int n;
27
28 va_start(ap, fmt);
29
30 n = vsnprintf(buf, sizeof(buf), fmt, ap);
31
32 write(STDOUT_FILENO, buf, n);
33
34 va_end(ap);
35}
36
98963de4
PMF
37struct consumer_channel {
38 int fd;
39 struct ltt_channel_struct *chan;
40};
41
9c67dc50
PMF
42int consumer(void *arg)
43{
44 int result;
9c67dc50 45 int fd;
9c67dc50 46 char str[] = "Hello, this is the consumer.\n";
9c67dc50 47 struct ltt_trace_struct *trace;
98963de4
PMF
48 struct consumer_channel *consumer_channels;
49 int i;
9c67dc50
PMF
50
51 ltt_lock_traces();
52 trace = _ltt_trace_find(trace_name);
53 ltt_unlock_traces();
54
55 if(trace == NULL) {
56 CPRINTF("cannot find trace!");
57 return 1;
58 }
59
98963de4
PMF
60 consumer_channels = (struct consumer_channel *) malloc(trace->nr_channels * sizeof(struct consumer_channel));
61 if(consumer_channels == NULL) {
62 ERR("malloc returned NULL");
63 return 1;
64 }
9c67dc50 65
98963de4
PMF
66 CPRINTF("opening trace files");
67 for(i=0; i<trace->nr_channels; i++) {
68 char tmp[100];
69 struct ltt_channel_struct *chan = &trace->channels[i];
9c67dc50 70
98963de4 71 consumer_channels[i].chan = chan;
9c67dc50 72
98963de4
PMF
73 snprintf(tmp, sizeof(tmp), "trace/%s", chan->channel_name);
74 result = consumer_channels[i].fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 00644);
75 if(result == -1) {
76 perror("open");
77 return -1;
78 }
79 CPRINTF("\topened trace file %s", tmp);
80
9c67dc50 81 }
98963de4 82 CPRINTF("done opening trace files");
9c67dc50
PMF
83
84 for(;;) {
98963de4
PMF
85 /*wait*/
86
87 for(i=0; i<trace->nr_channels; i++) {
88 struct rchan *rchan = consumer_channels[i].chan->trans_channel_data;
89 struct rchan_buf *rbuf = rchan->buf;
90 struct ltt_channel_buf_struct *lttbuf = consumer_channels[i].chan->buf;
91 long consumed_old;
92
93 result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
94 if(result < 0) {
95 CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
96 }
97 else {
98 CPRINTF("success!");
99
100 result = write(consumer_channels[i].fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
101 ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
102 }
9c67dc50 103 }
9c67dc50
PMF
104
105 sleep(1);
106 }
98963de4
PMF
107
108// CPRINTF("consumer: got a trace: %s with %d channels\n", trace_name, trace->nr_channels);
109//
110// struct ltt_channel_struct *chan = &trace->channels[0];
111//
112// CPRINTF("channel 1 (%s) active=%u", chan->channel_name, chan->active & 1);
113
114// struct rchan *rchan = chan->trans_channel_data;
115// struct rchan_buf *rbuf = rchan->buf;
116// struct ltt_channel_buf_struct *lttbuf = chan->buf;
117// long consumed_old;
118//
119// result = fd = open("trace.out", O_WRONLY | O_CREAT | O_TRUNC, 00644);
120// if(result == -1) {
121// perror("open");
122// return -1;
123// }
124
125// for(;;) {
126// write(STDOUT_FILENO, str, sizeof(str));
127//
128// result = ltt_do_get_subbuf(rbuf, lttbuf, &consumed_old);
129// if(result < 0) {
130// CPRINTF("ltt_do_get_subbuf: error: %s", strerror(-result));
131// }
132// else {
133// CPRINTF("success!");
134//
135// result = write(fd, rbuf->buf_data + (consumed_old & (2 * 4096-1)), 4096);
136// ltt_do_put_subbuf(rbuf, lttbuf, consumed_old);
137// }
138//
139// //CPRINTF("There seems to be %ld bytes available", SUBBUF_TRUNC(local_read(&lttbuf->offset), rbuf->chan) - consumed_old);
140// CPRINTF("Commit count %ld", local_read(&lttbuf->commit_count[0]));
141//
142//
143// sleep(1);
144// }
9c67dc50
PMF
145}
146
147void start_consumer(void)
148{
149 int result;
150
151 result = clone(consumer, consumer_stack+sizeof(consumer_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM, NULL);
152 if(result == -1) {
153 perror("clone");
154 }
155}
59b161cd
PMF
156
157void probe(const struct marker *mdata,
158 void *probe_private, void *call_private,
159 const char *fmt, va_list *args)
160{
161 printf("In probe\n");
162}
163
8d938dbd
PMF
164void inthandler(int sig)
165{
166 printf("in handler\n");
167 exit(0);
168}
169
170int init_int_handler(void)
171{
172 int result;
173 struct sigaction act;
174
175 result = sigemptyset(&act.sa_mask);
176 if(result == -1) {
177 PERROR("sigemptyset");
178 return -1;
179 }
180
181 act.sa_handler = inthandler;
182 act.sa_flags = SA_RESTART;
183
184 /* Only defer ourselves. Also, try to restart interrupted
185 * syscalls to disturb the traced program as little as possible.
186 */
187 result = sigaction(SIGINT, &act, NULL);
188 if(result == -1) {
189 PERROR("sigaction");
190 return -1;
191 }
192
193 return 0;
194}
195
5f54827b 196int main()
b6bf28ec 197{
5f54827b 198 int result;
98963de4 199 int i;
5f54827b 200
8d938dbd
PMF
201 init_int_handler();
202
5f54827b
PMF
203 init_ustrelay_transport();
204
8d938dbd
PMF
205 printf("page size is %d\n", sysconf(_SC_PAGE_SIZE));
206
c463904d
PMF
207// extern struct marker __start___markers[] __attribute__((visibility("hidden")));
208// extern struct marker __stop___markers[] __attribute__((visibility("hidden")));
209//
210// printf("the executable's markers start at %lx and end at %lx, the size of a marker is %d\n", __start___markers, __stop___markers, sizeof(struct marker));
211
ba6459ba
PMF
212 marker_control_init();
213
9c67dc50
PMF
214 //marker_probe_register("abc", "testmark", "", probe, NULL);
215//ust// 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);
ba6459ba
PMF
216//ust// result = ltt_probe_register(&default_probe);
217//ust// if(result)
218//ust// ERR("ltt_probe_register");
8d938dbd 219
9c67dc50
PMF
220 //result = ltt_marker_connect("metadata", "testev", "default");
221 //if(result)
222 // ERR("ltt_marker_connect");
98963de4
PMF
223 result = ltt_marker_connect("foo", "bar", "default");
224 if(result)
225 ERR("ltt_marker_connect");
b6bf28ec 226
5f54827b
PMF
227 result = ltt_trace_setup(trace_name);
228 if(result < 0) {
229 ERR("ltt_trace_setup failed");
230 return 1;
b6bf28ec
PMF
231 }
232
8d938dbd
PMF
233 result = ltt_trace_set_type(trace_name, trace_type);
234 if(result < 0) {
235 ERR("ltt_trace_set_type failed");
236 return 1;
237 }
b6bf28ec 238
5f54827b
PMF
239 result = ltt_trace_alloc(trace_name);
240 if(result < 0) {
241 ERR("ltt_trace_alloc failed");
242 return 1;
243 }
b6bf28ec 244
8d938dbd
PMF
245 result = ltt_trace_start(trace_name);
246 if(result < 0) {
247 ERR("ltt_trace_start failed");
248 return 1;
249 }
250
9c67dc50 251 start_consumer();
68c1021b 252 printf("Hello, World!\n");
59b161cd 253
9c67dc50 254 sleep(1);
98963de4 255 for(i=0; i<50; i++) {
9c67dc50
PMF
256 //trace_mark(abc, testmark, "", MARK_NOARGS);
257 //trace_mark(metadata, testev, "", MARK_NOARGS);
98963de4
PMF
258 trace_mark(foo, bar, "%s", "FOOBAZ");
259 //trace_mark(metadata, core_marker_id,
260 // "channel %s name %s event_id %hu "
261 // "int #1u%zu long #1u%zu pointer #1u%zu "
262 // "size_t #1u%zu alignment #1u%u",
263 // "abc", "def", (unsigned short)1000,
264 // sizeof(int), sizeof(long), sizeof(void *),
265 // sizeof(size_t), ltt_get_alignment());
9c67dc50 266 usleep(100000);
8d938dbd 267 }
59b161cd 268
98963de4
PMF
269 ltt_trace_stop(trace_name);
270 ltt_trace_destroy(trace_name);
271
68c1021b
PMF
272 scanf("%*s");
273
274 return 0;
275}
c463904d
PMF
276
277MARKER_LIB
This page took 0.033886 seconds and 4 git commands to generate.