[lttng-dev] [PATCH] add_context.c : Document return values, fix session_name memory leak, enforce exactly one domain flag, adjust usage() output accordingly

Thibault, Daniel Daniel.Thibault at drdc-rddc.gc.ca
Thu Feb 2 10:58:08 EST 2012


>From 93687b3bac52519ce834bc36f2e2265f71a6e8ee Thu, 2 Feb 2012 10:53:02 -0500
From: Daniel U. Thibault <daniel.thibault at drdc-rddc.gc.ca>
Date: Thu, 2 Feb 2012 10:52:38 -0500
Subject: [PATCH] add_context.c : Document return values, fix session_name memory leak, enforce exactly one domain flag, adjust usage() output accordingly

Signed-off-by: Daniel U. Thibault <daniel.thibault at drdc-rddc.gc.ca>

diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c
index ed4f87f..91795a4 100644
--- a/src/bin/lttng/commands/add_context.c
+++ b/src/bin/lttng/commands/add_context.c
@@ -309,6 +309,7 @@
 	fprintf(ofp, "will be added to all events and all channels.\n");
 	fprintf(ofp, "Otherwise the context will be added only to the channel (-c)\n");
 	fprintf(ofp, "and/or event (-e) indicated.\n");
+	fprintf(ofp, "Exactly one domain (-k/--kernel or -u/--userspace) must be specified.\n");
 	fprintf(ofp, "\n");
 	fprintf(ofp, "Options:\n");
 	fprintf(ofp, "  -h, --help               Show this help\n");
@@ -325,9 +326,8 @@
 #else
 	fprintf(ofp, "  -u, --userspace          Apply to the user-space tracer\n");
 #endif
-	fprintf(ofp, "  -t, --type TYPE          Context type. You can repeat that option on\n");
-	fprintf(ofp, "                           the command line to specify multiple contexts at once.\n");
-	fprintf(ofp, "                           (--kernel preempts --userspace)\n");
+	fprintf(ofp, "  -t, --type TYPE          Context type. You can repeat that option on the\n");
+	fprintf(ofp, "                           command line to specify multiple contexts at once.\n");
 	fprintf(ofp, "                           TYPE can be one of the strings below:\n");
 	print_ctx_type(ofp);
 	fprintf(ofp, "\n");
@@ -340,7 +340,8 @@
 }
 
 /*
- * Find context numerical value from string.
+ * Find context numerical value from string (opt must not be NULL).
+ * Returns the index on success, -1 on failure.
  */
 static int find_ctx_type_idx(const char *opt)
 {
@@ -360,6 +361,7 @@
 
 /*
  * Add context to channel or event.
+ * Returns a CMD_* result value.
  */
 static int add_context(char *session_name)
 {
@@ -369,14 +371,12 @@
 	struct ctx_type *type;
 	char *ptr;
 
+	/* cmd_add_context() enforces the existence of exactly one domain flag */
 	if (opt_kernel) {
 		dom.type = LTTNG_DOMAIN_KERNEL;
-	} else if (opt_userspace) {
+	} else { /* if (opt_userspace) { */
 		dom.type = LTTNG_DOMAIN_UST;
-	} else {
-		ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
-		ret = CMD_ERROR;
-		goto error;
+	/* TODO As LTTNG_DOMAIN_* values are added, capture them here */
 	}
 
 	handle = lttng_create_handle(session_name, &dom);
@@ -445,6 +445,7 @@
 
 /*
  * Add context to channel or event.
+ * Returns a CMD_* result value.
  */
 int cmd_add_context(int argc, const char **argv)
 {
@@ -512,6 +513,18 @@
 		}
 	}
 
+	/* Enforce requirement for exactly one domain */
+	/* Five LTTNG_DOMAIN_* values are currently planned */
+	if (!opt_userspace && !opt_kernel) {
+		ERR("No domain specified: Use one of -k/--kernel or -u/--userspace");
+		ret = CMD_ERROR;
+		goto end;
+	} else if (opt_userspace && opt_kernel) {
+		ERR("More than one domain specified");
+		ret = CMD_ERROR;
+		goto end;
+	}
+
 	if (!opt_type) {
 		ERR("Missing mandatory -t TYPE");
 		usage(stderr);
@@ -519,18 +532,18 @@
 		goto end;
 	}
 
-	if (!opt_session_name) {
-		session_name = get_session_name();
-		if (session_name == NULL) {
-			ret = CMD_ERROR;
-			goto end;
-		}
-	} else {
-		session_name = opt_session_name;
+	session_name = opt_session_name ? opt_session_name : get_session_name();
+	if (session_name == NULL) {
+		ret = CMD_ERROR;
+		goto end;
 	}
 
 	ret = add_context(session_name);
 
+	if (!opt_session_name) {
+		free (session_name);
+	}
+
 end:
 	/* Cleanup allocated memory */
 	cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
------------------------------

Daniel U. Thibault
R & D pour la défense Canada - Valcartier (RDDC Valcartier) / Defence R&D Canada - Valcartier (DRDC Valcartier)
Système de systèmes (SdS) / System of Systems (SoS)
Solutions informatiques et expérimentations (SIE) / Computing Solutions and Experimentations (CSE)
2459 Boul. Pie XI Nord
Québec, QC  G3J 1X5
CANADA
Vox : (418) 844-4000 x4245
Fax : (418) 844-4538
NAC: 918V QSDJ
Gouvernement du Canada / Government of Canada
<http://www.valcartier.drdc-rddc.gc.ca/>



More information about the lttng-dev mailing list