[ltt-dev] Make lttctl use new interface

Zhaolei zhaolei at cn.fujitsu.com
Fri Nov 28 02:35:36 EST 2008


Hi, Mathieu

I modified lttctl for support debugfs-based ltt-trace-control.

This patch only modified interior method of control ltt trace,
so command args are not changed.

We can use this code without modify our user-manual, and lttv need not change
too.

But personaly, I'd like to design a set of new command arguments for lttctl in
next step, for support each-channel buffer and overwrite control.
And old cmd-args as -X, -V, -B, -z, -x, -v may be dropped(or change for easy
to remember)
For example:
-c Create a trace
-C Create a trace and start
-d Destroy trace channels
-D Stop and destroy trace channels
...
or
-c  Create a trace
-cs Create a trace and start
-d  Destroy trace channels
-td Terminate and destroy trace channels
...
But it we do, our unlucky users must read our new manual again : (
What is your opinion?

Signed-off-by: Zhao Lei <zhaolei at cn.fujitsu.com>
---
diff -Nur ltt-control-0.58-23112008.org/liblttctl/liblttctl.c ltt-control-0.58-23112008/liblttctl/liblttctl.c
--- ltt-control-0.58-23112008.org/liblttctl/liblttctl.c	2008-11-28 15:08:35.000000000 +0800
+++ ltt-control-0.58-23112008/liblttctl/liblttctl.c	2008-11-28 15:08:35.000000000 +0800
@@ -2,10 +2,7 @@
  *
  * Linux Trace Toolkit Netlink Control Library
  *
- * Controls the ltt-control kernel module through a netlink socket.
- *
- * Heavily inspired from libipq.c (iptables) made by 
- * James Morris <jmorris at intercode.com.au>
+ * Controls the ltt-control kernel module through debugfs.
  *
  * Copyright 2005 -
  * 	Mathieu Desnoyers <mathieu.desnoyers at polymtl.ca>
@@ -20,7 +17,7 @@
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
  * GNU General Public License for more details.
- * 	
+ *
  */
 
 #ifdef HAVE_CONFIG_H
@@ -30,462 +27,557 @@
 #include <liblttctl/lttctl.h>
 #include <errno.h>
 #include <stdio.h>
-#include <error.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
 #include <string.h>
+#include <dirent.h>
+#include <limits.h>
+#include <fcntl.h>
+
+struct chan_info_struct {
+	const char *name;
+	int buf_type; /* 0: low 1: med 2: high */
+} chan_infos[] = {
+	{"cpu", 2},
+	{"processes", 1},
+	{"interrupts", 0},
+	{"network", 0},
+	{"modules", 0},
+	{"metadata", 0},
+};
 
+static char debugfsmntdir[PATH_MAX];
+static int initdebugfsmntdir(void)
+{
+	char mnt_dir[PATH_MAX];
+	char mnt_type[PATH_MAX];
 
+	FILE *fp = fopen("/proc/mounts", "r");
+	if (!fp) {
+		fprintf(stderr, "%s: Can't open /proc/mounts\n", __func__);
+		return 1;
+	}
+
+	while (1) {
+		if (fscanf(fp, "%*s %s %s %*s %*s %*s", mnt_dir, mnt_type)
+			<= 0) {
+			fprintf(stderr, "%s: debugfs mountpoint not found\n",
+				__func__);
+			return 1;
+		}
+		if (!strcmp(mnt_type, "debugfs")) {
+			strcpy(debugfsmntdir, mnt_dir);
+			return 0;
+		}
+	}
+}
 
-/* Private interface */
+int lttctl_init(void)
+{
+	int ret;
+	DIR *dir;
+	char controldirname[PATH_MAX];
 
-enum {
-	LTTCTL_ERR_NONE = 0,
-	LTTCTL_ERR_IMPL,
-	LTTCTL_ERR_HANDLE,
-	LTTCTL_ERR_SOCKET,
-	LTTCTL_ERR_BIND,
-	LTTCTL_ERR_BUFFER,
-	LTTCTL_ERR_RECV,
-	LTTCTL_ERR_NLEOF,
-	LTTCTL_ERR_ADDRLEN,
-	LTTCTL_ERR_STRUNC,
-	LTTCTL_ERR_RTRUNC,
-	LTTCTL_ERR_NLRECV,
-	LTTCTL_ERR_SEND,
-	LTTCTL_ERR_SUPP,
-	LTTCTL_ERR_RECVBUF,
-	LTTCTL_ERR_TIMEOUT,
-	LTTCTL_ERR_PROTOCOL,
-};
-#define LTTCTL_MAXERR LTTCTL_ERR_PROTOCOL
+	ret = initdebugfsmntdir();
+	if (ret) {
+		fprintf(stderr, "Debugfs mount point not found\n");
+		return 1;
+	}
 
+	/* check ltt control's debugfs dir */
+	sprintf(controldirname, "%s/ltt/control/", debugfsmntdir);
 
-struct lttctl_errmap_t {
-	int errcode;
-	char *message;
-} lttctl_errmap[] = {
-	{ LTTCTL_ERR_NONE, "Unknown error" },
-	{ LTTCTL_ERR_IMPL, "Implementation error" },
-	{ LTTCTL_ERR_HANDLE, "Unable to create netlink handle" },
-	{ LTTCTL_ERR_SOCKET, "Unable to create netlink socket" },
-	{ LTTCTL_ERR_BIND, "Unable to bind netlink socket" },
-	{ LTTCTL_ERR_BUFFER, "Unable to allocate buffer" },
-	{ LTTCTL_ERR_RECV, "Failed to receive netlink message" },
-	{ LTTCTL_ERR_NLEOF, "Received EOF on netlink socket" },
-	{ LTTCTL_ERR_ADDRLEN, "Invalid peer address length" },
-	{ LTTCTL_ERR_STRUNC, "Sent message truncated" },
-	{ LTTCTL_ERR_RTRUNC, "Received message truncated" },
-	{ LTTCTL_ERR_NLRECV, "Received error from netlink" },
-	{ LTTCTL_ERR_SEND, "Failed to send netlink message" },
-	{ LTTCTL_ERR_SUPP, "Operation not supported" },
-	{ LTTCTL_ERR_RECVBUF, "Receive buffer size invalid" },
-	{ LTTCTL_ERR_TIMEOUT, "Timeout"},
-	{ LTTCTL_ERR_PROTOCOL, "Invalid protocol specified" }
-};
+	dir = opendir(controldirname);
+	if (!dir) {
+		fprintf(stderr, "ltt-trace-control's debugfs dir not found\n");
+		closedir(dir);
+		return -errno;
+	}
 
-static int lttctl_errno = LTTCTL_ERR_NONE;
+	closedir(dir);
 
+	return 0;
+}
 
-static ssize_t lttctl_netlink_sendto(const struct lttctl_handle *h,
-																	const void *msg, size_t len);
+int lttctl_destroy(void)
+{
+	return 0;
+}
 
-static ssize_t lttctl_netlink_recvfrom(const struct lttctl_handle *h,
-																		unsigned char *buf, size_t len,
-																		int timeout);
+static int lttctl_sendop(const char *fname, const char *op)
+{
+	int fd;
 
-static ssize_t lttctl_netlink_sendmsg(const struct lttctl_handle *h,
-																	 const struct msghdr *msg,
-																	 unsigned int flags);
+	if (!fname) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		return 1;
+	}
 
-static char *lttctl_strerror(int errcode);
+	fd = open(fname, O_WRONLY);
+	if (fd == -1) {
+		fprintf(stderr, "%s: open %s failed: %s\n", __func__, fname,
+			strerror(errno));
+		return errno;
+	}
 
-void lttctl_perror(const char *s);
+	if (write(fd, op, strlen(op)) == -1) {
+		fprintf(stderr, "%s: write %s to %s failed: %s\n", __func__, op,
+			fname, strerror(errno));
+		close(fd);
+		return 1;
+	}
 
-static ssize_t lttctl_netlink_sendto(const struct lttctl_handle *h,
-																	const void *msg, size_t len)
-{
-	int status = sendto(h->fd, msg, len, 0,
-			(struct sockaddr *)&h->peer, sizeof(h->peer));
-	if (status < 0)
-		lttctl_errno = LTTCTL_ERR_SEND;
-	
-	return status;
-}
+	close(fd);
 
-static ssize_t lttctl_netlink_sendmsg(const struct lttctl_handle *h,
-																	 const struct msghdr *msg,
-																	 unsigned int flags)
-{
-	int status = sendmsg(h->fd, msg, flags);
-	if (status < 0)
-		lttctl_errno = LTTCTL_ERR_SEND;
-	return status;
+	return 0;
 }
 
-static ssize_t lttctl_netlink_recvfrom(const struct lttctl_handle *h,
-																		unsigned char *buf, size_t len,
-																		int timeout)
+/*
+ * check is trace exist(check debugfsmntdir too)
+ * expect:
+ *   0: expect that trace not exist
+ *   !0: expect that trace exist
+ *
+ * ret:
+ *   0: check pass
+ *   1: check failed
+ *   -ERRNO: error happened (no check)
+ */
+static int lttctl_check_trace(const char *name, int expect)
 {
-	int addrlen, status;
-	struct nlmsghdr *nlh;
+	char tracedirname[PATH_MAX];
+	DIR *dir;
+	int exist;
 
-	if (len < sizeof(struct nlmsghdr)) {
-		lttctl_errno = LTTCTL_ERR_RECVBUF;
-		lttctl_perror("Netlink recvfrom");
-		return -1;
+	if (!name) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		return -EINVAL;
 	}
-	addrlen = sizeof(h->peer);
 
-	if (timeout != 0) {
-		int ret;
-		struct timeval tv;
-		fd_set read_fds;
-		
-		if (timeout < 0) {
-			/* non-block non-timeout */
-			tv.tv_sec = 0;
-			tv.tv_usec = 0;
-		} else {
-			tv.tv_sec = timeout / 1000000;
-			tv.tv_usec = timeout % 1000000;
-		}
+	if (!debugfsmntdir[0]) {
+		fprintf(stderr, "%s: debugfsmntdir not valid\n", __func__);
+		return -EINVAL;
+	}
 
-		FD_ZERO(&read_fds);
-		FD_SET(h->fd, &read_fds);
-		ret = select(h->fd+1, &read_fds, NULL, NULL, &tv);
-		if (ret < 0) {
-			if (errno == EINTR) {
-				printf("eintr\n");
-				return 0;
-			} else {
-				lttctl_errno = LTTCTL_ERR_RECV;
-				lttctl_perror("Netlink recvfrom");
-				return -1;
-			}
-		}
-		if (!FD_ISSET(h->fd, &read_fds)) {
-			lttctl_errno = LTTCTL_ERR_TIMEOUT;
-			printf("timeout\n");
-			return 0;
+	sprintf(tracedirname, "%s/ltt/control/%s", debugfsmntdir, name);
+
+	dir = opendir(tracedirname);
+	if (dir) {
+		exist = 1;
+	} else {
+		if (errno != ENOENT) {
+			fprintf(stderr, "%s: %s\n", __func__, strerror(errno));
+			return -EINVAL;
 		}
+		exist = 0;
 	}
-	status = recvfrom(h->fd, buf, len, 0,
-			(struct sockaddr *)&h->peer, &addrlen);
-	
-	if (status < 0) {
-		lttctl_errno = LTTCTL_ERR_RECV;
-		lttctl_perror("Netlink recvfrom");
-		return status;
+
+	closedir(dir);
+
+	if (!expect != !exist) {
+		if (exist)
+			fprintf(stderr, "Trace %s already exist\n", name);
+		else
+			fprintf(stderr, "Trace %s not exist\n", name);
+		return 1;
 	}
-	if (addrlen != sizeof(h->peer)) {
-		lttctl_errno = LTTCTL_ERR_RECV;
-		lttctl_perror("Netlink recvfrom");
-		return -1;
+
+	return 0;
+}
+
+int lttctl_setup_trace(const char *name)
+{
+	int ret;
+	char ctlfname[PATH_MAX];
+
+	if (!name) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
 	}
-	if (h->peer.nl_pid != 0) {
-		lttctl_errno = LTTCTL_ERR_RECV;
-		lttctl_perror("Netlink recvfrom");
-		return -1;
+
+	ret = lttctl_check_trace(name, 0);
+	if (ret)
+		goto arg_error;
+
+	sprintf(ctlfname, "%s/ltt/setup_trace", debugfsmntdir);
+
+	ret = lttctl_sendop(ctlfname, name);
+	if (ret) {
+		fprintf(stderr, "Setup trace failed\n");
+		goto op_err;
 	}
-	if (status == 0) {
-		lttctl_errno = LTTCTL_ERR_NLEOF;
-		lttctl_perror("Netlink recvfrom");
-		return -1;
+
+	return 0;
+
+op_err:
+arg_error:
+	return ret;
+}
+
+int lttctl_destroy_trace(const char *name)
+{
+	int ret;
+	char ctlfname[PATH_MAX];
+
+	if (!name) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
 	}
-	nlh = (struct nlmsghdr *)buf;
-	if (nlh->nlmsg_flags & MSG_TRUNC || nlh->nlmsg_len > status) {
-		lttctl_errno = LTTCTL_ERR_RTRUNC;
-		lttctl_perror("Netlink recvfrom");
-		return -1;
+
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
+
+	sprintf(ctlfname, "%s/ltt/destroy_trace", debugfsmntdir);
+
+	ret = lttctl_sendop(ctlfname, name);
+	if (ret) {
+		fprintf(stderr, "Destroy trace failed\n");
+		goto op_err;
 	}
-	
 
-	return status;
-}
+	return 0;
 
+op_err:
+arg_error:
+	return ret;
+}
 
-static char *lttctl_strerror(int errcode)
+int lttctl_alloc_trace(const char *name)
 {
-	if (errcode < 0 || errcode > LTTCTL_MAXERR)
-		errcode = LTTCTL_ERR_IMPL;
-	return lttctl_errmap[errcode].message;
-}
+	int ret;
+	char ctlfname[PATH_MAX];
+
+	if (!name) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
+	}
 
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
 
-char *lttctl_errstr(void)
-{
-	return lttctl_strerror(lttctl_errno);
+	sprintf(ctlfname, "%s/ltt/control/%s/alloc", debugfsmntdir, name);
+
+	ret = lttctl_sendop(ctlfname, "1");
+	if (ret) {
+		fprintf(stderr, "Allocate trace failed\n");
+		goto op_err;
+	}
+
+	return 0;
+
+op_err:
+arg_error:
+	return ret;
 }
 
-void lttctl_perror(const char *s)
+int lttctl_start(const char *name)
 {
-	if (s)
-		fputs(s, stderr);
-	else
-		fputs("ERROR", stderr);
-	if (lttctl_errno)
-		fprintf(stderr, ": %s", lttctl_errstr());
-	if (errno)
-		fprintf(stderr, ": %s", strerror(-errno));
-	fputc('\n', stderr);
-}
+	int ret;
+	char ctlfname[PATH_MAX];
 
-/* public interface */
+	if (!name) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
+	}
 
-/*
- * Create and initialise an lttctl handle.
- */
-struct lttctl_handle *lttctl_create_handle(void)
-{
-	int status;
-	struct lttctl_handle *h;
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
+
+	sprintf(ctlfname, "%s/ltt/control/%s/enabled", debugfsmntdir, name);
 
-	h = (struct lttctl_handle *)malloc(sizeof(struct lttctl_handle));
-	if (h == NULL) {
-		lttctl_errno = LTTCTL_ERR_HANDLE;
-		lttctl_perror("Create handle");
-		goto alloc_error;
-	}
-	
-	memset(h, 0, sizeof(struct lttctl_handle));
-	
-	h->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_LTT);
-				
-	if (h->fd == -1) {
-		lttctl_errno = LTTCTL_ERR_SOCKET;
-		lttctl_perror("Create handle");
-		goto socket_error;
-	}
-	memset(&h->local, 0, sizeof(struct sockaddr_nl));
-	h->local.nl_family = AF_NETLINK;
-	h->local.nl_pid = getpid();
-	h->local.nl_groups = 0;
-	status = bind(h->fd, (struct sockaddr *)&h->local, sizeof(h->local));
-	if (status == -1) {
-		lttctl_errno = LTTCTL_ERR_BIND;
-		lttctl_perror("Create handle");
-		goto bind_error;
-	}
-	memset(&h->peer, 0, sizeof(struct sockaddr_nl));
-	h->peer.nl_family = AF_NETLINK;
-	h->peer.nl_pid = 0;
-	h->peer.nl_groups = 0;
-	return h;
-	
-	/* Error condition */
-bind_error:
-socket_error:
-		close(h->fd);
-alloc_error:
-		free(h);
-	return NULL;
+	ret = lttctl_sendop(ctlfname, "1");
+	if (ret) {
+		fprintf(stderr, "Start trace failed\n");
+		goto op_err;
+	}
+
+	return 0;
+
+op_err:
+arg_error:
+	return ret;
 }
 
-/*
- * No error condition is checked here at this stage, but it may happen
- * if/when reliable messaging is implemented.
- */
-int lttctl_destroy_handle(struct lttctl_handle *h)
+int lttctl_stop(const char *name)
 {
-	if (h) {
-		close(h->fd);
-		free(h);
+	int ret;
+	char ctlfname[PATH_MAX];
+
+	if (!name) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
 	}
+
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
+
+	sprintf(ctlfname, "%s/ltt/control/%s/enabled", debugfsmntdir, name);
+
+	ret = lttctl_sendop(ctlfname, "0");
+	if (ret) {
+		fprintf(stderr, "Stop trace failed\n");
+		goto op_err;
+	}
+
 	return 0;
-}
 
+op_err:
+arg_error:
+	return ret;
+}
 
-int lttctl_create_trace(const struct lttctl_handle *h,
-		char *name, enum trace_mode mode, char *trace_type,
-		unsigned subbuf_size_low, unsigned n_subbufs_low,
-		unsigned subbuf_size_med, unsigned n_subbufs_med,
-		unsigned subbuf_size_high, unsigned n_subbufs_high)
+int lttctl_set_trans(const char *name, const char *trans)
 {
-	int err;
-	
-	struct {
-		struct nlmsghdr	nlh;
-		lttctl_peer_msg_t	msg;
-	} req;
-	struct {
-		struct nlmsghdr	nlh;
-		struct nlmsgerr	nlerr;
-		lttctl_peer_msg_t	msg;
-	} ack;
+	int ret;
+	char ctlfname[PATH_MAX];
 
-	memset(&req, 0, sizeof(req));
-	req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
-	req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
-	req.nlh.nlmsg_type = LTTCTLM_CONTROL;
-	req.nlh.nlmsg_pid = h->local.nl_pid;
-	req.nlh.nlmsg_seq = 0;
-
-	strncpy(req.msg.trace_name, name, NAME_MAX);
-	strncpy(req.msg.trace_type, trace_type, NAME_MAX);
-	req.msg.op = OP_CREATE;
-	req.msg.args.new_trace.mode = mode;
-	req.msg.args.new_trace.subbuf_size_low = subbuf_size_low;
-	req.msg.args.new_trace.n_subbufs_low = n_subbufs_low;
-	req.msg.args.new_trace.subbuf_size_med = subbuf_size_med;
-	req.msg.args.new_trace.n_subbufs_med = n_subbufs_med;
-	req.msg.args.new_trace.subbuf_size_high = subbuf_size_high;
-	req.msg.args.new_trace.n_subbufs_high = n_subbufs_high;
+	if (!name) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
+	}
 
-	err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
-	if(err < 0) goto senderr;
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
 
-	err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
-	if(err < 0) goto senderr;
+	sprintf(ctlfname, "%s/ltt/control/%s/trans", debugfsmntdir, name);
 
-	err = ack.nlerr.error;
-	if(err != 0) {
-		errno = err;
-		lttctl_perror("Create Trace Error");
-		return err;
+	ret = lttctl_sendop(ctlfname, trans);
+	if (ret) {
+		fprintf(stderr, "Set transport failed\n");
+		goto op_err;
 	}
 
 	return 0;
 
-senderr:
-	lttctl_perror("Create Trace Error");
-	err = EPERM;
-	return err;
+op_err:
+arg_error:
+	return ret;
 }
 
-int lttctl_destroy_trace(const struct lttctl_handle *h,
-		char *name)
+int lttctl_set_channel_overwrite(const char *name, const char *channel,
+		int overwrite)
 {
-	struct {
-		struct nlmsghdr	nlh;
-		lttctl_peer_msg_t	msg;
-	} req;
-	struct {
-		struct nlmsghdr	nlh;
-		struct nlmsgerr	nlerr;
-		lttctl_peer_msg_t	msg;
-	} ack;
-	int err;
+	int ret;
+	char ctlfname[PATH_MAX];
 
-	memset(&req, 0, sizeof(req));
-	req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
-	req.nlh.nlmsg_flags = NLM_F_REQUEST;
-	req.nlh.nlmsg_type = LTTCTLM_CONTROL;
-	req.nlh.nlmsg_pid = h->local.nl_pid;
-
-	strncpy(req.msg.trace_name, name, NAME_MAX);
-	req.msg.op = OP_DESTROY;
+	if (!name || !channel) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
+	}
 
-	err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
-	if(err < 0) goto senderr;
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
 
-	err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
-	if(err < 0) goto senderr;
+	sprintf(ctlfname, "%s/ltt/control/%s/channel/%s/subbuf_num",
+		debugfsmntdir, name, channel);
 
-	err = ack.nlerr.error;
-	if(err != 0) {
-		errno = err;
-		lttctl_perror("Destroy Trace Channels Error");
-		return err;
+	ret = lttctl_sendop(ctlfname, overwrite?"1":"0");
+	if (ret) {
+		fprintf(stderr, "Set channel's overwrite mode failed\n");
+		goto op_err;
 	}
 
 	return 0;
 
-senderr:
-	lttctl_perror("Destroy Trace Channels Error");
-	err = EPERM;
-	return err;
-
+op_err:
+arg_error:
+	return ret;
 }
 
-int lttctl_start(const struct lttctl_handle *h,
-		char *name)
+int lttctl_set_channel_subbuf_num(const char *name, const char *channel,
+		unsigned subbuf_num)
 {
-	struct {
-		struct nlmsghdr	nlh;
-		lttctl_peer_msg_t	msg;
-	} req;
-	struct {
-		struct nlmsghdr	nlh;
-		struct nlmsgerr	nlerr;
-		lttctl_peer_msg_t	msg;
-	} ack;
-
-	int err;
+	int ret;
+	char ctlfname[PATH_MAX];
+	char opstr[32];
 
-	memset(&req, 0, sizeof(req));
-	req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
-	req.nlh.nlmsg_flags = NLM_F_REQUEST;
-	req.nlh.nlmsg_type = LTTCTLM_CONTROL;
-	req.nlh.nlmsg_pid = h->local.nl_pid;
+	if (!name || !channel) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
+	}
 
-	strncpy(req.msg.trace_name, name, NAME_MAX);
-	req.msg.op = OP_START;
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
 
-	err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
-	if(err < 0) goto senderr;
+	sprintf(ctlfname, "%s/ltt/control/%s/channel/%s/subbuf_num",
+		debugfsmntdir, name, channel);
 
-	err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
-	if(err < 0) goto senderr;
+	sprintf(opstr, "%u", subbuf_num);
 
-	err = ack.nlerr.error;
-	if(err != 0) {
-		errno = err;
-		lttctl_perror("Start Trace Error");
-		return err;
+	ret = lttctl_sendop(ctlfname, opstr);
+	if (ret) {
+		fprintf(stderr, "Set channel's subbuf number failed\n");
+		goto op_err;
 	}
 
 	return 0;
 
-senderr:
-	err = EPERM;
-	lttctl_perror("Start Trace Error");
-	return err;
-
+op_err:
+arg_error:
+	return ret;
 }
 
-int lttctl_stop(const struct lttctl_handle *h,
-		char *name)
+int lttctl_set_channel_subbuf_size(const char *name, const char *channel,
+		unsigned subbuf_size)
 {
-	struct {
-		struct nlmsghdr	nlh;
-		lttctl_peer_msg_t msg;
-	} req;
-	struct {
-		struct nlmsghdr	nlh;
-		struct nlmsgerr	nlerr;
-		lttctl_peer_msg_t msg;
-	} ack;
-	int err;
+	int ret;
+	char ctlfname[PATH_MAX];
+	char opstr[32];
 
-	memset(&req, 0, sizeof(req));
-	req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(lttctl_peer_msg_t));
-	req.nlh.nlmsg_flags = NLM_F_REQUEST;
-	req.nlh.nlmsg_type = LTTCTLM_CONTROL;
-	req.nlh.nlmsg_pid = h->local.nl_pid;
+	if (!name || !channel) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
+	}
 
-	strncpy(req.msg.trace_name, name, NAME_MAX);
-	req.msg.op = OP_STOP;
+	ret = lttctl_check_trace(name, 1);
+	if (ret)
+		goto arg_error;
 
-	err = lttctl_netlink_sendto(h, (void *)&req, req.nlh.nlmsg_len);
-	if(err < 0) goto senderr;
+	sprintf(ctlfname, "%s/ltt/control/%s/channel/%s/subbuf_size",
+		debugfsmntdir, name, channel);
 
-	err = lttctl_netlink_recvfrom(h, (void*)&ack, sizeof(ack), 0);
-	if(err < 0) goto senderr;
+	sprintf(opstr, "%u", subbuf_size);
 
-	err = ack.nlerr.error;
-	if(err != 0) {
-		errno = err;
-		lttctl_perror("Stop Trace Error");
-		return err;
+	ret = lttctl_sendop(ctlfname, opstr);
+	if (ret) {
+		fprintf(stderr, "Set channel's subbuf size failed\n");
+		goto op_err;
 	}
 
 	return 0;
 
-senderr:
-	err = EPERM;
-	lttctl_perror("Stop Trace Error");
-	return err;
+op_err:
+arg_error:
+	return ret;
 }
 
+int lttctl_create_trace(const char *name, enum trace_mode mode,
+		const char *trace_type,
+		unsigned subbuf_size_low, unsigned n_subbufs_low,
+		unsigned subbuf_size_med, unsigned n_subbufs_med,
+		unsigned subbuf_size_high, unsigned n_subbufs_high)
+{
+	int ret;
+	int i;
+
+	if (!name || !trace_type) {
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto arg_error;
+	}
+
+	ret = lttctl_setup_trace(name);
+	if (ret)
+		goto setup_trace_fail;
+
+	switch (mode) {
+	case LTT_TRACE_NORMAL:
+		/* each channel's default is non-overwrite */
+		break;
+	case LTT_TRACE_FLIGHT:
+		for (i = 0; i < sizeof(chan_infos) / sizeof(chan_infos[0]);
+			i++) {
+			if (strcmp(chan_infos[i].name, "metadata") == 0)
+				continue;
+
+			ret = lttctl_set_channel_overwrite(name,
+				chan_infos[i].name, 1);
+			if (ret)
+				goto set_overwrite_fail;
+		}
+		break;
+	case LTT_TRACE_HYBRID:
+		ret = lttctl_set_channel_overwrite(name, "cpu", 1);
+		if (ret)
+			goto set_overwrite_fail;
+		break;
+	default:
+		fprintf(stderr, "%s: args invalid\n", __func__);
+		ret = -EINVAL;
+		goto set_overwrite_fail;
+	}
+
+	ret = lttctl_set_trans(name, trace_type);
+	if (ret)
+		goto set_trans_fail;
+
+	for (i = 0; i < sizeof(chan_infos) / sizeof(chan_infos[0]); i++) {
+		switch (chan_infos[i].buf_type) {
+		case 0:
+			if (subbuf_size_low) {
+				ret = lttctl_set_channel_subbuf_size(name,
+					chan_infos[i].name, subbuf_size_low);
+				if (ret)
+					goto set_channelbuf_fail;
+			}
+			if (n_subbufs_low) {
+				ret = lttctl_set_channel_subbuf_num(name,
+					chan_infos[i].name, n_subbufs_low);
+				if (ret)
+					goto set_channelbuf_fail;
+			}
+			break;
+		case 1:
+			if (subbuf_size_med) {
+				ret = lttctl_set_channel_subbuf_size(name,
+					chan_infos[i].name, subbuf_size_med);
+				if (ret)
+					goto set_channelbuf_fail;
+			}
+			if (n_subbufs_med) {
+				ret = lttctl_set_channel_subbuf_num(name,
+					chan_infos[i].name, n_subbufs_med);
+				if (ret)
+					goto set_channelbuf_fail;
+			}
+			break;
+		case 2:
+			if (subbuf_size_high) {
+				ret = lttctl_set_channel_subbuf_size(name,
+					chan_infos[i].name, subbuf_size_high);
+				if (ret)
+					goto set_channelbuf_fail;
+			}
+			if (n_subbufs_high) {
+				ret = lttctl_set_channel_subbuf_num(name,
+					chan_infos[i].name, n_subbufs_high);
+				if (ret)
+					goto set_channelbuf_fail;
+			}
+			break;
+		default:
+			fprintf(stderr, "%s: unknown error\n", __func__);
+			ret = -EINVAL;
+			goto set_channelbuf_fail;
+		}
+	}
+
+	ret = lttctl_alloc_trace(name);
+	if (ret)
+		goto alloc_trace_fail;
+
+	return 0;
+
+alloc_trace_fail:
+set_channelbuf_fail:
+set_trans_fail:
+set_overwrite_fail:
+	lttctl_destroy_trace(name);
+setup_trace_fail:
+arg_error:
+	return ret;
+}
diff -Nur ltt-control-0.58-23112008.org/liblttctl/lttctl.h ltt-control-0.58-23112008/liblttctl/lttctl.h
--- ltt-control-0.58-23112008.org/liblttctl/lttctl.h	2008-11-28 15:08:35.000000000 +0800
+++ ltt-control-0.58-23112008/liblttctl/lttctl.h	2008-11-28 15:08:35.000000000 +0800
@@ -14,86 +14,27 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
  * GNU General Public License for more details.
  *
- *
- * Inspired from iptables, by James Morris <jmorris at intercode.com.au>.
- * 
  */
 
 #ifndef _LIBLTT_H
 #define _LIBLTT_H
 
-#include <linux/limits.h>
-#include <asm/types.h>
-#include <sys/socket.h>
-#include <linux/netlink.h>
-
-#ifndef NETLINK_LTT
-#define NETLINK_LTT 31
-#endif
-
-
-enum trace_op {
-	OP_CREATE,
-	OP_DESTROY,
-	OP_START,
-	OP_STOP,
-	OP_NONE
-};
-
 enum trace_mode {
 	LTT_TRACE_NORMAL,
 	LTT_TRACE_FLIGHT,
-	LTT_TRACE_HYBRID
+	LTT_TRACE_HYBRID,
 };
 
-typedef struct lttctl_peer_msg {
-	char trace_name[NAME_MAX];
-	char trace_type[NAME_MAX];
-	enum trace_op op;
-	union {
-		struct {
-			enum trace_mode mode;
-			unsigned subbuf_size_low;
-			unsigned n_subbufs_low;
-			unsigned subbuf_size_med;
-			unsigned n_subbufs_med;
-			unsigned subbuf_size_high;
-			unsigned n_subbufs_high;
-		} new_trace;
-	} args;
-} lttctl_peer_msg_t;
-
-
-struct lttctl_handle
-{
-	int fd;
-	//u_int8_t blocking;
-	struct sockaddr_nl local;
-	struct sockaddr_nl peer;
-};
+int lttctl_init(void);
+int lttctl_destroy(void);
 
-typedef struct lttctl_resp_msg {
-	int err;
-} lttctl_resp_msg_t;
-
-struct lttctl_handle *lttctl_create_handle(void);
-
-int lttctl_destroy_handle(struct lttctl_handle *h);
-
-
-int lttctl_create_trace(const struct lttctl_handle *h,
-		char *name, enum trace_mode mode, char *trace_type,
+int lttctl_create_trace(const char *name, enum trace_mode mode,
+		const char *trace_type,
 		unsigned subbuf_size_low, unsigned n_subbufs_low,
 		unsigned subbuf_size_med, unsigned n_subbufs_med,
 		unsigned subbuf_size_high, unsigned n_subbufs_high);
+int lttctl_destroy_trace(const char *name);
+int lttctl_start(const char *name);
+int lttctl_stop(const char *name);
 
-int lttctl_destroy_trace(const struct lttctl_handle *handle, char *name);
-
-int lttctl_start(const struct lttctl_handle *handle, char *name);
-
-int lttctl_stop(const struct lttctl_handle *handle, char *name);
-
-#define LTTCTLM_BASE	0x10
-#define LTTCTLM_CONTROL	(LTTCTLM_BASE + 1)	/* LTT control message */
-
-#endif //_LIBLTT_H
+#endif /*_LIBLTT_H */
diff -Nur ltt-control-0.58-23112008.org/lttctl/lttctl.c ltt-control-0.58-23112008/lttctl/lttctl.c
--- ltt-control-0.58-23112008.org/lttctl/lttctl.c	2008-11-28 15:08:35.000000000 +0800
+++ ltt-control-0.58-23112008/lttctl/lttctl.c	2008-11-28 15:08:35.000000000 +0800
@@ -16,14 +16,10 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <signal.h>
-#include <dirent.h>
 #include <string.h>
 #include <limits.h>
-#include <sys/stat.h>
 
 /* Buffer for file copy : 4k seems optimal. */
 #define BUF_SIZE 4096
@@ -40,9 +36,9 @@
 	CTL_OP_NONE
 };
 
-static char *trace_name = NULL;
-static char *trace_type = "relay";
-static char *mode_name = NULL;
+static const char *trace_name = NULL;
+static const char *trace_type = "relay";
+static const char *mode_name = NULL;
 static unsigned subbuf_size_low = 0;
 static unsigned n_subbufs_low = 0;
 static unsigned subbuf_size_med = 0;
@@ -52,10 +48,10 @@
 static unsigned append_trace = 0;
 static enum trace_mode mode = LTT_TRACE_NORMAL;
 static enum trace_ctl_op op = CTL_OP_NONE;
-static char *channel_root = NULL;
+static const char *channel_root = NULL;
 static char channel_root_default[PATH_MAX];
-static char *trace_root = NULL;
-static char *num_threads = "1";
+static const char *trace_root = NULL;
+static const char *num_threads = "1";
 
 /* Args :
  *
@@ -352,22 +348,22 @@
 	}
 }
 
-int lttctl_daemon(struct lttctl_handle *handle, char *trace_name)
+int lttctl_daemon(const char *trace_name)
 {
 	char channel_path[PATH_MAX] = "";
 	pid_t pid;
 	int ret;
-	char *lttd_path = getenv("LTT_DAEMON");
+	const char *lttd_path = getenv("LTT_DAEMON");
 
-	if(lttd_path == NULL) lttd_path = 
-		PACKAGE_BIN_DIR "/lttd";
+	if(lttd_path == NULL)
+		lttd_path = PACKAGE_BIN_DIR "/lttd";
 	
 	strcat(channel_path, channel_root);
 	strcat(channel_path, "/");
 	strcat(channel_path, trace_name);
 
 	
-	ret = lttctl_create_trace(handle, trace_name, mode, trace_type,
+	ret = lttctl_create_trace(trace_name, mode, trace_type,
 		subbuf_size_low, n_subbufs_low,
 		subbuf_size_med, n_subbufs_med,
 		subbuf_size_high, n_subbufs_high);
@@ -419,7 +415,7 @@
 		perror("Error in forking for lttd daemon");
 	}
 
-	ret = lttctl_start(handle, trace_name);
+	ret = lttctl_start(trace_name);
 	if(ret != 0) goto start_error;
 
 	return 0;
@@ -427,7 +423,7 @@
 	/* error handling */
 start_error:
 	printf("Trace start error\n");
-	ret |= lttctl_destroy_trace(handle, trace_name);
+	ret |= lttctl_destroy_trace(trace_name);
 create_error:
 	return ret;
 }
@@ -435,22 +431,22 @@
 
 
 
-int lttctl_daemon_hybrid_finish(struct lttctl_handle *handle, char *trace_name)
+int lttctl_daemon_hybrid_finish(const char *trace_name)
 {
 	char channel_path[PATH_MAX] = "";
 	pid_t pid;
 	int ret;
-	char *lttd_path = getenv("LTT_DAEMON");
+	const char *lttd_path = getenv("LTT_DAEMON");
 
-	if(lttd_path == NULL) lttd_path = 
-		PACKAGE_BIN_DIR "/lttd";
+	if(lttd_path == NULL)
+		lttd_path = PACKAGE_BIN_DIR "/lttd";
 	
 	strcat(channel_path, channel_root);
 	strcat(channel_path, "/");
 	strcat(channel_path, trace_name);
 
 	
-	ret = lttctl_stop(handle, trace_name);
+	ret = lttctl_stop(trace_name);
 	if(ret != 0) goto stop_error;
 
 	pid = fork();
@@ -490,7 +486,7 @@
 		perror("Error in forking for lttd daemon");
 	}
 
-	ret = lttctl_destroy_trace(handle, trace_name);
+	ret = lttctl_destroy_trace(trace_name);
 	if(ret != 0) goto destroy_error;
 
 	return 0;
@@ -507,8 +503,7 @@
 int main(int argc, char ** argv)
 {
 	int ret;
-	struct lttctl_handle *handle;
-	
+
 	ret = parse_arguments(argc, argv);
 
 	if(ret != 0) show_arguments();
@@ -516,51 +511,51 @@
 	if(ret == -1) return 0;
 
 	show_info();
-	
-	handle = lttctl_create_handle();
-	
-	if(handle == NULL) return -1;
+
+	ret = lttctl_init();
+	if (ret != 0)
+		return ret;
 	
 	switch(op) {
 		case CTL_OP_CREATE_START:
-			ret = lttctl_create_trace(handle, trace_name, mode, trace_type,
+			ret = lttctl_create_trace(trace_name, mode, trace_type,
 			subbuf_size_low, n_subbufs_low,
 			subbuf_size_med, n_subbufs_med,
 			subbuf_size_high, n_subbufs_high);
 			if(!ret)
-				ret = lttctl_start(handle, trace_name);
+				ret = lttctl_start(trace_name);
 			break;
 		case CTL_OP_CREATE:
-			ret = lttctl_create_trace(handle, trace_name, mode, trace_type,
+			ret = lttctl_create_trace(trace_name, mode, trace_type,
 			subbuf_size_low, n_subbufs_low,
 			subbuf_size_med, n_subbufs_med,
 			subbuf_size_high, n_subbufs_high);
 			break;
 		case CTL_OP_DESTROY:
-			ret = lttctl_destroy_trace(handle, trace_name);
+			ret = lttctl_destroy_trace(trace_name);
 			break;
 		case CTL_OP_STOP_DESTROY:
-			ret = lttctl_stop(handle, trace_name);
+			ret = lttctl_stop(trace_name);
 			if(!ret)
-				ret = lttctl_destroy_trace(handle, trace_name);
+				ret = lttctl_destroy_trace(trace_name);
 			break;
 		case CTL_OP_START:
-			ret = lttctl_start(handle, trace_name);
+			ret = lttctl_start(trace_name);
 			break;
 		case CTL_OP_STOP:
-			ret = lttctl_stop(handle, trace_name);
+			ret = lttctl_stop(trace_name);
 			break;
 		case CTL_OP_DAEMON:
-			ret = lttctl_daemon(handle, trace_name);
+			ret = lttctl_daemon(trace_name);
 			break;
 		case CTL_OP_DAEMON_HYBRID_FINISH:
-			ret = lttctl_daemon_hybrid_finish(handle, trace_name);
+			ret = lttctl_daemon_hybrid_finish(trace_name);
 			break;
 		case CTL_OP_NONE:
 			break;
 	}
 
-	ret |= lttctl_destroy_handle(handle);
-	
+	lttctl_destroy();
+
 	return ret;
 }




More information about the lttng-dev mailing list