3 * Linux Trace Toolkit Netlink Control Library
5 * Controls the ltt-control kernel module through debugfs.
8 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
27 #include <liblttctl/lttctl.h>
36 #define MAX_CHANNEL (256)
38 static char debugfsmntdir
[PATH_MAX
];
40 static int initdebugfsmntdir(void)
42 char mnt_dir
[PATH_MAX
];
43 char mnt_type
[PATH_MAX
];
45 FILE *fp
= fopen("/proc/mounts", "r");
47 fprintf(stderr
, "%s: Can't open /proc/mounts\n", __func__
);
52 if (fscanf(fp
, "%*s %s %s %*s %*s %*s", mnt_dir
, mnt_type
)
54 fprintf(stderr
, "%s: debugfs mountpoint not found\n",
58 if (!strcmp(mnt_type
, "debugfs")) {
59 strcpy(debugfsmntdir
, mnt_dir
);
69 char controldirname
[PATH_MAX
];
71 ret
= initdebugfsmntdir();
73 fprintf(stderr
, "Debugfs mount point not found\n");
77 /* check ltt control's debugfs dir */
78 sprintf(controldirname
, "%s/ltt/control/", debugfsmntdir
);
80 dir
= opendir(controldirname
);
82 fprintf(stderr
, "ltt-trace-control's debugfs dir not found\n");
92 int lttctl_destroy(void)
97 static int lttctl_sendop(const char *fname
, const char *op
)
102 fprintf(stderr
, "%s: args invalid\n", __func__
);
106 fd
= open(fname
, O_WRONLY
);
108 fprintf(stderr
, "%s: open %s failed: %s\n", __func__
, fname
,
113 if (write(fd
, op
, strlen(op
)) == -1) {
114 fprintf(stderr
, "%s: write %s to %s failed: %s\n", __func__
, op
,
115 fname
, strerror(errno
));
126 * check is trace exist(check debugfsmntdir too)
128 * 0: expect that trace not exist
129 * !0: expect that trace exist
134 * -ERRNO: error happened (no check)
136 static int lttctl_check_trace(const char *name
, int expect
)
138 char tracedirname
[PATH_MAX
];
143 fprintf(stderr
, "%s: args invalid\n", __func__
);
147 if (!debugfsmntdir
[0]) {
148 fprintf(stderr
, "%s: debugfsmntdir not valid\n", __func__
);
152 sprintf(tracedirname
, "%s/ltt/control/%s", debugfsmntdir
, name
);
154 dir
= opendir(tracedirname
);
158 if (errno
!= ENOENT
) {
159 fprintf(stderr
, "%s: %s\n", __func__
, strerror(errno
));
167 if (!expect
!= !exist
) {
169 fprintf(stderr
, "Trace %s already exist\n", name
);
171 fprintf(stderr
, "Trace %s not exist\n", name
);
179 * get channel list of a trace
180 * don't include metadata channel when metadata is 0
182 * return number of channel on success
183 * return negative number on fail
184 * Caller must free channellist.
186 static int lttctl_get_channellist(const char *tracename
,
187 char ***channellist
, int metadata
)
189 char tracedirname
[PATH_MAX
];
190 struct dirent
*dirent
;
192 char **list
= NULL
, **old_list
;
195 sprintf(tracedirname
, "%s/ltt/control/%s/channel", debugfsmntdir
,
198 dir
= opendir(tracedirname
);
205 dirent
= readdir(dir
);
208 if (!strcmp(dirent
->d_name
, ".")
209 || !strcmp(dirent
->d_name
, ".."))
211 if (!metadata
&& !strcmp(dirent
->d_name
, "metadata"))
214 list
= malloc(sizeof(char *) * ++nr_chan
);
215 memcpy(list
, old_list
, sizeof(*list
) * (nr_chan
- 1));
217 list
[nr_chan
- 1] = strdup(dirent
->d_name
);
230 int lttctl_setup_trace(const char *name
)
233 char ctlfname
[PATH_MAX
];
236 fprintf(stderr
, "%s: args invalid\n", __func__
);
241 ret
= lttctl_check_trace(name
, 0);
245 sprintf(ctlfname
, "%s/ltt/setup_trace", debugfsmntdir
);
247 ret
= lttctl_sendop(ctlfname
, name
);
249 fprintf(stderr
, "Setup trace failed\n");
260 int lttctl_destroy_trace(const char *name
)
263 char ctlfname
[PATH_MAX
];
266 fprintf(stderr
, "%s: args invalid\n", __func__
);
271 ret
= lttctl_check_trace(name
, 1);
275 sprintf(ctlfname
, "%s/ltt/destroy_trace", debugfsmntdir
);
277 ret
= lttctl_sendop(ctlfname
, name
);
279 fprintf(stderr
, "Destroy trace failed\n");
290 int lttctl_alloc_trace(const char *name
)
293 char ctlfname
[PATH_MAX
];
296 fprintf(stderr
, "%s: args invalid\n", __func__
);
301 ret
= lttctl_check_trace(name
, 1);
305 sprintf(ctlfname
, "%s/ltt/control/%s/alloc", debugfsmntdir
, name
);
307 ret
= lttctl_sendop(ctlfname
, "1");
309 fprintf(stderr
, "Allocate trace failed\n");
320 int lttctl_start(const char *name
)
323 char ctlfname
[PATH_MAX
];
326 fprintf(stderr
, "%s: args invalid\n", __func__
);
331 ret
= lttctl_check_trace(name
, 1);
335 sprintf(ctlfname
, "%s/ltt/control/%s/enabled", debugfsmntdir
, name
);
337 ret
= lttctl_sendop(ctlfname
, "1");
339 fprintf(stderr
, "Start trace failed\n");
350 int lttctl_pause(const char *name
)
353 char ctlfname
[PATH_MAX
];
356 fprintf(stderr
, "%s: args invalid\n", __func__
);
361 ret
= lttctl_check_trace(name
, 1);
365 sprintf(ctlfname
, "%s/ltt/control/%s/enabled", debugfsmntdir
, name
);
367 ret
= lttctl_sendop(ctlfname
, "0");
369 fprintf(stderr
, "Pause trace failed\n");
380 int lttctl_set_trans(const char *name
, const char *trans
)
383 char ctlfname
[PATH_MAX
];
386 fprintf(stderr
, "%s: args invalid\n", __func__
);
391 ret
= lttctl_check_trace(name
, 1);
395 sprintf(ctlfname
, "%s/ltt/control/%s/trans", debugfsmntdir
, name
);
397 ret
= lttctl_sendop(ctlfname
, trans
);
399 fprintf(stderr
, "Set transport failed\n");
410 static int __lttctl_set_channel_enable(const char *name
, const char *channel
,
414 char ctlfname
[PATH_MAX
];
416 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/enable", debugfsmntdir
,
419 ret
= lttctl_sendop(ctlfname
, enable
? "1" : "0");
421 fprintf(stderr
, "Set channel's enable mode failed\n");
425 int lttctl_set_channel_enable(const char *name
, const char *channel
,
430 if (!name
|| !channel
) {
431 fprintf(stderr
, "%s: args invalid\n", __func__
);
436 ret
= lttctl_check_trace(name
, 1);
440 if (strcmp(channel
, "all")) {
441 ret
= __lttctl_set_channel_enable(name
, channel
, enable
);
448 /* Don't allow set enable state for metadata channel */
449 n_channel
= lttctl_get_channellist(name
, &channellist
, 0);
451 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
457 for (; n_channel
> 0; n_channel
--) {
458 ret
= __lttctl_set_channel_enable(name
,
459 channellist
[n_channel
- 1], enable
);
473 static int __lttctl_set_channel_overwrite(const char *name
, const char *channel
,
477 char ctlfname
[PATH_MAX
];
479 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/overwrite",
480 debugfsmntdir
, name
, channel
);
482 ret
= lttctl_sendop(ctlfname
, overwrite
? "1" : "0");
484 fprintf(stderr
, "Set channel's overwrite mode failed\n");
488 int lttctl_set_channel_overwrite(const char *name
, const char *channel
,
493 if (!name
|| !channel
) {
494 fprintf(stderr
, "%s: args invalid\n", __func__
);
499 ret
= lttctl_check_trace(name
, 1);
503 if (strcmp(channel
, "all")) {
504 ret
= __lttctl_set_channel_overwrite(name
, channel
, overwrite
);
511 /* Don't allow set overwrite for metadata channel */
512 n_channel
= lttctl_get_channellist(name
, &channellist
, 0);
514 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
520 for (; n_channel
> 0; n_channel
--) {
521 ret
= __lttctl_set_channel_overwrite(name
,
522 channellist
[n_channel
- 1], overwrite
);
536 static int __lttctl_set_channel_subbuf_num(const char *name
,
537 const char *channel
, unsigned subbuf_num
)
540 char ctlfname
[PATH_MAX
];
543 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/subbuf_num",
544 debugfsmntdir
, name
, channel
);
546 sprintf(opstr
, "%u", subbuf_num
);
548 ret
= lttctl_sendop(ctlfname
, opstr
);
550 fprintf(stderr
, "Set channel's subbuf number failed\n");
554 int lttctl_set_channel_subbuf_num(const char *name
, const char *channel
,
559 if (!name
|| !channel
) {
560 fprintf(stderr
, "%s: args invalid\n", __func__
);
565 ret
= lttctl_check_trace(name
, 1);
569 if (strcmp(channel
, "all")) {
570 ret
= __lttctl_set_channel_subbuf_num(name
, channel
,
578 /* allow set subbuf_num for metadata channel */
579 n_channel
= lttctl_get_channellist(name
, &channellist
, 1);
581 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
587 for (; n_channel
> 0; n_channel
--) {
588 ret
= __lttctl_set_channel_subbuf_num(name
,
589 channellist
[n_channel
- 1], subbuf_num
);
603 static int __lttctl_set_channel_subbuf_size(const char *name
,
604 const char *channel
, unsigned subbuf_size
)
607 char ctlfname
[PATH_MAX
];
610 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/subbuf_size",
611 debugfsmntdir
, name
, channel
);
613 sprintf(opstr
, "%u", subbuf_size
);
615 ret
= lttctl_sendop(ctlfname
, opstr
);
617 fprintf(stderr
, "Set channel's subbuf size failed\n");
619 int lttctl_set_channel_subbuf_size(const char *name
, const char *channel
,
620 unsigned subbuf_size
)
624 if (!name
|| !channel
) {
625 fprintf(stderr
, "%s: args invalid\n", __func__
);
630 ret
= lttctl_check_trace(name
, 1);
634 if (strcmp(channel
, "all")) {
635 ret
= __lttctl_set_channel_subbuf_size(name
, channel
,
643 /* allow set subbuf_size for metadata channel */
644 n_channel
= lttctl_get_channellist(name
, &channellist
, 1);
646 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
652 for (; n_channel
> 0; n_channel
--) {
653 ret
= __lttctl_set_channel_subbuf_size(name
,
654 channellist
[n_channel
- 1], subbuf_size
);
This page took 0.07413 seconds and 4 git commands to generate.