1032f15660019608b5682e4e41180591db31a5d2
4 * Linux Trace Toolkit Control Library
6 * Controls the ltt-control kernel module through debugfs.
8 * Copyright (c) 2005-2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <liblttctl/lttctl.h>
38 #define MAX_CHANNEL (256)
40 static char debugfsmntdir
[PATH_MAX
];
42 static int initdebugfsmntdir(void)
44 return getdebugfsmntdir(debugfsmntdir
);
48 * This function must called posterior to initdebugfsmntdir(),
49 * because it need to use debugfsmntdir[] which is inited in initdebugfsmntdir()
51 static int initmodule(void)
53 char controldirname
[PATH_MAX
];
57 sprintf(controldirname
, "%s/ltt/control/", debugfsmntdir
);
61 * Check ltt control's debugfs dir
63 * We don't check is ltt-trace-control module exist, because it maybe
64 * compiled into kernel.
66 dir
= opendir(controldirname
);
73 system("modprobe ltt-trace-control");
86 ret
= initdebugfsmntdir();
88 fprintf(stderr
, "Get debugfs mount point failed\n");
94 fprintf(stderr
, "Control module seems not work\n");
101 int lttctl_destroy(void)
106 static int lttctl_sendop(const char *fname
, const char *op
)
111 fprintf(stderr
, "%s: args invalid\n", __func__
);
115 fd
= open(fname
, O_WRONLY
);
117 fprintf(stderr
, "%s: open %s failed: %s\n", __func__
, fname
,
122 if (write(fd
, op
, strlen(op
)) == -1) {
124 fprintf(stderr
, "%s: write %s to %s failed: %s\n", __func__
, op
,
125 fname
, strerror(errno
));
136 * check is trace exist(check debugfsmntdir too)
138 * 0: expect that trace not exist
139 * !0: expect that trace exist
143 * -(EEXIST | ENOENT): check failed
144 * -ERRNO: error happened (no check)
146 static int lttctl_check_trace(const char *name
, int expect
)
148 char tracedirname
[PATH_MAX
];
153 fprintf(stderr
, "%s: args invalid\n", __func__
);
157 if (!debugfsmntdir
[0]) {
158 fprintf(stderr
, "%s: debugfsmntdir not valid\n", __func__
);
162 sprintf(tracedirname
, "%s/ltt/control/%s", debugfsmntdir
, name
);
164 dir
= opendir(tracedirname
);
169 if (errno
!= ENOENT
) {
170 fprintf(stderr
, "%s: %s\n", __func__
, strerror(errno
));
176 if (!expect
!= !exist
) {
179 fprintf(stderr
, "Trace %s already exist\n", name
);
184 fprintf(stderr
, "Trace %s not exist\n", name
);
194 * get channel list of a trace
195 * don't include metadata channel when metadata is 0
197 * return number of channel on success
198 * return negative number on fail
199 * Caller must free channellist.
201 static int lttctl_get_channellist(const char *tracename
,
202 char ***channellist
, int metadata
)
204 char tracedirname
[PATH_MAX
];
205 struct dirent
*dirent
;
207 char **list
= NULL
, **old_list
;
210 sprintf(tracedirname
, "%s/ltt/control/%s/channel", debugfsmntdir
,
213 dir
= opendir(tracedirname
);
220 dirent
= readdir(dir
);
223 if (!strcmp(dirent
->d_name
, ".")
224 || !strcmp(dirent
->d_name
, ".."))
226 if (!metadata
&& !strcmp(dirent
->d_name
, "metadata"))
229 list
= malloc(sizeof(char *) * ++nr_chan
);
230 memcpy(list
, old_list
, sizeof(*list
) * (nr_chan
- 1));
232 list
[nr_chan
- 1] = strdup(dirent
->d_name
);
245 static void lttctl_free_channellist(char **channellist
, int n_channel
)
248 for(; i
< n_channel
; ++i
)
249 free(channellist
[i
]);
253 int lttctl_setup_trace(const char *name
)
256 char ctlfname
[PATH_MAX
];
259 fprintf(stderr
, "%s: args invalid\n", __func__
);
264 ret
= lttctl_check_trace(name
, 0);
268 sprintf(ctlfname
, "%s/ltt/setup_trace", debugfsmntdir
);
270 ret
= lttctl_sendop(ctlfname
, name
);
272 fprintf(stderr
, "Setup trace failed\n");
283 int lttctl_destroy_trace(const char *name
)
286 char ctlfname
[PATH_MAX
];
289 fprintf(stderr
, "%s: args invalid\n", __func__
);
294 ret
= lttctl_check_trace(name
, 1);
298 sprintf(ctlfname
, "%s/ltt/destroy_trace", debugfsmntdir
);
300 ret
= lttctl_sendop(ctlfname
, name
);
302 fprintf(stderr
, "Destroy trace failed\n");
313 int lttctl_alloc_trace(const char *name
)
316 char ctlfname
[PATH_MAX
];
319 fprintf(stderr
, "%s: args invalid\n", __func__
);
324 ret
= lttctl_check_trace(name
, 1);
328 sprintf(ctlfname
, "%s/ltt/control/%s/alloc", debugfsmntdir
, name
);
330 ret
= lttctl_sendop(ctlfname
, "1");
332 fprintf(stderr
, "Allocate trace failed\n");
343 int lttctl_start(const char *name
)
346 char ctlfname
[PATH_MAX
];
349 fprintf(stderr
, "%s: args invalid\n", __func__
);
354 ret
= lttctl_check_trace(name
, 1);
358 sprintf(ctlfname
, "%s/ltt/control/%s/enabled", debugfsmntdir
, name
);
360 ret
= lttctl_sendop(ctlfname
, "1");
362 fprintf(stderr
, "Start trace failed\n");
373 int lttctl_pause(const char *name
)
376 char ctlfname
[PATH_MAX
];
379 fprintf(stderr
, "%s: args invalid\n", __func__
);
384 ret
= lttctl_check_trace(name
, 1);
388 sprintf(ctlfname
, "%s/ltt/control/%s/enabled", debugfsmntdir
, name
);
390 ret
= lttctl_sendop(ctlfname
, "0");
392 fprintf(stderr
, "Pause trace failed\n");
403 int lttctl_set_trans(const char *name
, const char *trans
)
406 char ctlfname
[PATH_MAX
];
409 fprintf(stderr
, "%s: args invalid\n", __func__
);
414 ret
= lttctl_check_trace(name
, 1);
418 sprintf(ctlfname
, "%s/ltt/control/%s/trans", debugfsmntdir
, name
);
420 ret
= lttctl_sendop(ctlfname
, trans
);
422 fprintf(stderr
, "Set transport failed\n");
433 static int __lttctl_set_channel_enable(const char *name
, const char *channel
,
437 char ctlfname
[PATH_MAX
];
439 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/enable", debugfsmntdir
,
442 ret
= lttctl_sendop(ctlfname
, enable
? "1" : "0");
444 fprintf(stderr
, "Set channel's enable mode failed\n");
449 int lttctl_set_channel_enable(const char *name
, const char *channel
,
456 if (!name
|| !channel
) {
457 fprintf(stderr
, "%s: args invalid\n", __func__
);
462 ret
= lttctl_check_trace(name
, 1);
466 if (strcmp(channel
, "all")) {
467 ret
= __lttctl_set_channel_enable(name
, channel
, enable
);
471 /* Don't allow set enable state for metadata channel */
472 n_channel
= lttctl_get_channellist(name
, &channellist
, 0);
474 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
480 for (; n_channel
> 0; n_channel
--) {
481 ret
= __lttctl_set_channel_enable(name
,
482 channellist
[n_channel
- 1], enable
);
486 lttctl_free_channellist(channellist
, n_channel
);
492 lttctl_free_channellist(channellist
, n_channel
);
498 static int __lttctl_set_channel_overwrite(const char *name
, const char *channel
,
502 char ctlfname
[PATH_MAX
];
504 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/overwrite",
505 debugfsmntdir
, name
, channel
);
507 ret
= lttctl_sendop(ctlfname
, overwrite
? "1" : "0");
509 fprintf(stderr
, "Set channel's overwrite mode failed\n");
513 int lttctl_set_channel_overwrite(const char *name
, const char *channel
,
520 if (!name
|| !channel
) {
521 fprintf(stderr
, "%s: args invalid\n", __func__
);
526 ret
= lttctl_check_trace(name
, 1);
530 if (strcmp(channel
, "all")) {
531 ret
= __lttctl_set_channel_overwrite(name
, channel
, overwrite
);
535 /* Don't allow set overwrite for metadata channel */
536 n_channel
= lttctl_get_channellist(name
, &channellist
, 0);
538 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
544 for (; n_channel
> 0; n_channel
--) {
545 ret
= __lttctl_set_channel_overwrite(name
,
546 channellist
[n_channel
- 1], overwrite
);
550 lttctl_free_channellist(channellist
, n_channel
);
556 lttctl_free_channellist(channellist
, n_channel
);
562 static int __lttctl_set_channel_subbuf_num(const char *name
,
563 const char *channel
, unsigned subbuf_num
)
566 char ctlfname
[PATH_MAX
];
569 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/subbuf_num",
570 debugfsmntdir
, name
, channel
);
572 sprintf(opstr
, "%u", subbuf_num
);
574 ret
= lttctl_sendop(ctlfname
, opstr
);
576 fprintf(stderr
, "Set channel's subbuf number failed\n");
580 int lttctl_set_channel_subbuf_num(const char *name
, const char *channel
,
587 if (!name
|| !channel
) {
588 fprintf(stderr
, "%s: args invalid\n", __func__
);
593 ret
= lttctl_check_trace(name
, 1);
597 if (strcmp(channel
, "all")) {
598 ret
= __lttctl_set_channel_subbuf_num(name
, channel
,
603 /* allow set subbuf_num for metadata channel */
604 n_channel
= lttctl_get_channellist(name
, &channellist
, 1);
606 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
612 for (; n_channel
> 0; n_channel
--) {
613 ret
= __lttctl_set_channel_subbuf_num(name
,
614 channellist
[n_channel
- 1], subbuf_num
);
618 lttctl_free_channellist(channellist
, n_channel
);
624 lttctl_free_channellist(channellist
, n_channel
);
630 static int __lttctl_set_channel_subbuf_size(const char *name
,
631 const char *channel
, unsigned subbuf_size
)
634 char ctlfname
[PATH_MAX
];
637 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/subbuf_size",
638 debugfsmntdir
, name
, channel
);
640 sprintf(opstr
, "%u", subbuf_size
);
642 ret
= lttctl_sendop(ctlfname
, opstr
);
644 fprintf(stderr
, "Set channel's subbuf size failed\n");
648 int lttctl_set_channel_subbuf_size(const char *name
, const char *channel
,
649 unsigned subbuf_size
)
655 if (!name
|| !channel
) {
656 fprintf(stderr
, "%s: args invalid\n", __func__
);
661 ret
= lttctl_check_trace(name
, 1);
665 if (strcmp(channel
, "all")) {
666 ret
= __lttctl_set_channel_subbuf_size(name
, channel
,
671 /* allow set subbuf_size for metadata channel */
672 n_channel
= lttctl_get_channellist(name
, &channellist
, 1);
674 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
680 for (; n_channel
> 0; n_channel
--) {
681 ret
= __lttctl_set_channel_subbuf_size(name
,
682 channellist
[n_channel
- 1], subbuf_size
);
686 lttctl_free_channellist(channellist
, n_channel
);
692 lttctl_free_channellist(channellist
, n_channel
);
698 static int __lttctl_set_channel_switch_timer(const char *name
,
699 const char *channel
, unsigned switch_timer
)
702 char ctlfname
[PATH_MAX
];
705 sprintf(ctlfname
, "%s/ltt/control/%s/channel/%s/switch_timer",
706 debugfsmntdir
, name
, channel
);
708 sprintf(opstr
, "%u", switch_timer
);
710 ret
= lttctl_sendop(ctlfname
, opstr
);
712 fprintf(stderr
, "Set channel's switch timer failed\n");
716 int lttctl_set_channel_switch_timer(const char *name
, const char *channel
,
717 unsigned switch_timer
)
723 if (!name
|| !channel
) {
724 fprintf(stderr
, "%s: args invalid\n", __func__
);
729 ret
= lttctl_check_trace(name
, 1);
733 if (strcmp(channel
, "all")) {
734 ret
= __lttctl_set_channel_switch_timer(name
, channel
,
739 /* allow set subbuf_size for metadata channel */
740 n_channel
= lttctl_get_channellist(name
, &channellist
, 1);
742 fprintf(stderr
, "%s: lttctl_get_channellist failed\n",
748 for (; n_channel
> 0; n_channel
--) {
749 ret
= __lttctl_set_channel_switch_timer(name
,
750 channellist
[n_channel
- 1], switch_timer
);
754 lttctl_free_channellist(channellist
, n_channel
);
760 lttctl_free_channellist(channellist
, n_channel
);
766 int getdebugfsmntdir(char *mntdir
)
768 char mnt_dir
[PATH_MAX
];
769 char mnt_type
[PATH_MAX
];
770 int trymount_done
= 0;
772 FILE *fp
= fopen("/proc/mounts", "r");
778 if (fscanf(fp
, "%*s %s %s %*s %*s %*s", mnt_dir
, mnt_type
) <= 0)
781 if (!strcmp(mnt_type
, "debugfs")) {
782 strcpy(mntdir
, mnt_dir
);
787 if (!trymount_done
) {
788 mount("debugfs", "/sys/kernel/debug/", "debugfs", 0, NULL
);
This page took 0.046224 seconds and 5 git commands to generate.