Commit | Line | Data |
---|---|---|
d65106b1 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
d65106b1 DG |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
90b9a268 | 20 | #include <ctype.h> |
d65106b1 DG |
21 | #include <popt.h> |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
27 | #include <unistd.h> | |
28 | ||
3301e740 DG |
29 | #include <urcu/list.h> |
30 | ||
c399183f | 31 | #include "../command.h" |
d65106b1 | 32 | |
b13d56d7 MD |
33 | #define PRINT_LINE_LEN 80 |
34 | ||
d65106b1 DG |
35 | static char *opt_event_name; |
36 | static char *opt_channel_name; | |
5440dc42 | 37 | static char *opt_session_name; |
55cc08a6 | 38 | static int opt_kernel; |
d65106b1 | 39 | static int opt_userspace; |
eeac7d46 | 40 | static char *opt_cmd_name; |
d65106b1 | 41 | static pid_t opt_pid; |
6caa2bcc | 42 | static char *opt_type; |
d65106b1 DG |
43 | |
44 | enum { | |
45 | OPT_HELP = 1, | |
46 | OPT_TYPE, | |
eeac7d46 | 47 | OPT_USERSPACE, |
d65106b1 DG |
48 | }; |
49 | ||
cd80958d DG |
50 | static struct lttng_handle *handle; |
51 | ||
90b9a268 DG |
52 | /* |
53 | * Taken from the LTTng ABI | |
54 | */ | |
55 | enum context_type { | |
56 | CONTEXT_PID = 0, | |
57 | CONTEXT_PERF_COUNTER = 1, | |
95da1297 | 58 | CONTEXT_PROCNAME = 2, |
90b9a268 DG |
59 | CONTEXT_PRIO = 3, |
60 | CONTEXT_NICE = 4, | |
61 | CONTEXT_VPID = 5, | |
62 | CONTEXT_TID = 6, | |
63 | CONTEXT_VTID = 7, | |
64 | CONTEXT_PPID = 8, | |
65 | CONTEXT_VPPID = 9, | |
3301e740 DG |
66 | }; |
67 | ||
90b9a268 DG |
68 | /* |
69 | * Taken from the Perf ABI (all enum perf_*) | |
70 | */ | |
71 | enum perf_type { | |
72 | PERF_TYPE_HARDWARE = 0, | |
73 | PERF_TYPE_SOFTWARE = 1, | |
b13d56d7 | 74 | PERF_TYPE_HW_CACHE = 3, |
3301e740 DG |
75 | }; |
76 | ||
90b9a268 | 77 | enum perf_count_hard { |
b13d56d7 MD |
78 | PERF_COUNT_HW_CPU_CYCLES = 0, |
79 | PERF_COUNT_HW_INSTRUCTIONS = 1, | |
80 | PERF_COUNT_HW_CACHE_REFERENCES = 2, | |
81 | PERF_COUNT_HW_CACHE_MISSES = 3, | |
82 | PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4, | |
83 | PERF_COUNT_HW_BRANCH_MISSES = 5, | |
84 | PERF_COUNT_HW_BUS_CYCLES = 6, | |
85 | PERF_COUNT_HW_STALLED_CYCLES_FRONTEND = 7, | |
86 | PERF_COUNT_HW_STALLED_CYCLES_BACKEND = 8, | |
90b9a268 DG |
87 | }; |
88 | ||
89 | enum perf_count_soft { | |
90 | PERF_COUNT_SW_CPU_CLOCK = 0, | |
91 | PERF_COUNT_SW_TASK_CLOCK = 1, | |
92 | PERF_COUNT_SW_PAGE_FAULTS = 2, | |
93 | PERF_COUNT_SW_CONTEXT_SWITCHES = 3, | |
94 | PERF_COUNT_SW_CPU_MIGRATIONS = 4, | |
95 | PERF_COUNT_SW_PAGE_FAULTS_MIN = 5, | |
96 | PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6, | |
97 | PERF_COUNT_SW_ALIGNMENT_FAULTS = 7, | |
98 | PERF_COUNT_SW_EMULATION_FAULTS = 8, | |
3301e740 DG |
99 | }; |
100 | ||
b13d56d7 MD |
101 | /* |
102 | * Generalized hardware cache events: | |
103 | * | |
104 | * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x | |
105 | * { read, write, prefetch } x | |
106 | * { accesses, misses } | |
107 | */ | |
108 | enum perf_hw_cache_id { | |
109 | PERF_COUNT_HW_CACHE_L1D = 0, | |
110 | PERF_COUNT_HW_CACHE_L1I = 1, | |
111 | PERF_COUNT_HW_CACHE_LL = 2, | |
112 | PERF_COUNT_HW_CACHE_DTLB = 3, | |
113 | PERF_COUNT_HW_CACHE_ITLB = 4, | |
114 | PERF_COUNT_HW_CACHE_BPU = 5, | |
115 | ||
116 | PERF_COUNT_HW_CACHE_MAX, /* non-ABI */ | |
117 | }; | |
118 | ||
119 | enum perf_hw_cache_op_id { | |
120 | PERF_COUNT_HW_CACHE_OP_READ = 0, | |
121 | PERF_COUNT_HW_CACHE_OP_WRITE = 1, | |
122 | PERF_COUNT_HW_CACHE_OP_PREFETCH = 2, | |
123 | ||
124 | PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */ | |
125 | }; | |
126 | ||
127 | enum perf_hw_cache_op_result_id { | |
128 | PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0, | |
129 | PERF_COUNT_HW_CACHE_RESULT_MISS = 1, | |
130 | ||
131 | PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */ | |
132 | }; | |
133 | ||
d65106b1 DG |
134 | static struct poptOption long_options[] = { |
135 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
136 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
5440dc42 | 137 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
d65106b1 DG |
138 | {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, |
139 | {"event", 'e', POPT_ARG_STRING, &opt_event_name, 0, 0, 0}, | |
140 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, | |
55cc08a6 | 141 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
d65106b1 | 142 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
6caa2bcc | 143 | {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0}, |
d65106b1 DG |
144 | {0, 0, 0, 0, 0, 0, 0} |
145 | }; | |
146 | ||
90b9a268 | 147 | /* |
b13d56d7 | 148 | * Context options |
90b9a268 | 149 | */ |
b13d56d7 MD |
150 | #define PERF_HW(opt, name) \ |
151 | { \ | |
152 | "perf:" #opt, CONTEXT_PERF_COUNTER, \ | |
153 | .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\ | |
154 | } | |
90b9a268 | 155 | |
b13d56d7 MD |
156 | #define PERF_SW(opt, name) \ |
157 | { \ | |
158 | "perf:" #opt, CONTEXT_PERF_COUNTER, \ | |
159 | .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\ | |
160 | } | |
90b9a268 | 161 | |
b13d56d7 MD |
162 | #define _PERF_HW_CACHE(optstr, name, op, result) \ |
163 | { \ | |
164 | "perf:" optstr, CONTEXT_PERF_COUNTER, \ | |
165 | .u.perf = { \ | |
166 | PERF_TYPE_HW_CACHE, \ | |
167 | (uint64_t) PERF_COUNT_HW_CACHE_##name \ | |
18829107 MD |
168 | | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \ |
169 | | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \ | |
b13d56d7 MD |
170 | }, \ |
171 | } | |
172 | ||
173 | #define PERF_HW_CACHE(opt, name) \ | |
174 | _PERF_HW_CACHE(#opt "-loads", name, READ, ACCESS), \ | |
175 | _PERF_HW_CACHE(#opt "-load-misses", name, READ, MISS), \ | |
176 | _PERF_HW_CACHE(#opt "-stores", name, WRITE, ACCESS), \ | |
177 | _PERF_HW_CACHE(#opt "-store-misses", name, WRITE, MISS), \ | |
178 | _PERF_HW_CACHE(#opt "-prefetches", name, PREFETCH, ACCESS), \ | |
179 | _PERF_HW_CACHE(#opt "-prefetch-misses", name, PREFETCH, MISS) \ | |
180 | ||
181 | static | |
182 | const struct ctx_opts { | |
90b9a268 | 183 | char *symbol; |
b13d56d7 MD |
184 | enum context_type ctx_type; |
185 | union { | |
186 | struct { | |
187 | uint32_t type; | |
188 | uint64_t config; | |
189 | } perf; | |
190 | } u; | |
191 | } ctx_opts[] = { | |
192 | { "pid", CONTEXT_PID }, | |
95da1297 | 193 | { "procname", CONTEXT_PROCNAME }, |
b13d56d7 MD |
194 | { "prio", CONTEXT_PRIO }, |
195 | { "nice", CONTEXT_NICE }, | |
196 | { "vpid", CONTEXT_VPID }, | |
197 | { "tid", CONTEXT_TID }, | |
198 | { "vtid", CONTEXT_VTID }, | |
199 | { "ppid", CONTEXT_PPID }, | |
200 | { "vppid", CONTEXT_VPPID }, | |
201 | /* Perf options */ | |
202 | PERF_HW(cpu-cycles, CPU_CYCLES), | |
203 | PERF_HW(cycles, CPU_CYCLES), | |
204 | PERF_HW(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND), | |
205 | PERF_HW(idle-cycles-frontend, STALLED_CYCLES_FRONTEND), | |
206 | PERF_HW(stalled-cycles-backend, STALLED_CYCLES_BACKEND), | |
207 | PERF_HW(idle-cycles-backend, STALLED_CYCLES_BACKEND), | |
208 | PERF_HW(instructions, INSTRUCTIONS), | |
209 | PERF_HW(cache-references, CACHE_REFERENCES), | |
210 | PERF_HW(cache-misses, CACHE_MISSES), | |
211 | PERF_HW(branch-instructions, BRANCH_INSTRUCTIONS), | |
212 | PERF_HW(branches, BRANCH_INSTRUCTIONS), | |
213 | PERF_HW(branch-misses, BRANCH_MISSES), | |
214 | PERF_HW(bus-cycles, BUS_CYCLES), | |
215 | ||
216 | PERF_HW_CACHE(L1-dcache, L1D), | |
217 | PERF_HW_CACHE(L1-icache, L1I), | |
218 | PERF_HW_CACHE(LLC, LL), | |
219 | PERF_HW_CACHE(dTLB, DTLB), | |
220 | _PERF_HW_CACHE("iTLB-loads", ITLB, READ, ACCESS), | |
221 | _PERF_HW_CACHE("iTLB-load-misses", ITLB, READ, MISS), | |
222 | _PERF_HW_CACHE("branch-loads", BPU, READ, ACCESS), | |
223 | _PERF_HW_CACHE("branch-load-misses", BPU, READ, MISS), | |
224 | ||
225 | ||
226 | PERF_SW(cpu-clock, CPU_CLOCK), | |
227 | PERF_SW(task-clock, TASK_CLOCK), | |
228 | PERF_SW(page-fault, PAGE_FAULTS), | |
229 | PERF_SW(faults, PAGE_FAULTS), | |
230 | PERF_SW(major-faults, PAGE_FAULTS_MAJ), | |
231 | PERF_SW(minor-faults, PAGE_FAULTS_MIN), | |
232 | PERF_SW(context-switches, CONTEXT_SWITCHES), | |
233 | PERF_SW(cs, CONTEXT_SWITCHES), | |
234 | PERF_SW(cpu-migrations, CPU_MIGRATIONS), | |
235 | PERF_SW(migrations, CPU_MIGRATIONS), | |
236 | PERF_SW(alignment-faults, ALIGNMENT_FAULTS), | |
237 | PERF_SW(emulation-faults, EMULATION_FAULTS), | |
238 | { NULL, -1 }, /* Closure */ | |
90b9a268 DG |
239 | }; |
240 | ||
b13d56d7 MD |
241 | #undef PERF_SW |
242 | #undef PERF_HW | |
243 | ||
90b9a268 | 244 | /* |
b13d56d7 | 245 | * Context type for command line option parsing. |
90b9a268 | 246 | */ |
b13d56d7 MD |
247 | struct ctx_type { |
248 | const struct ctx_opts *opt; | |
249 | struct cds_list_head list; | |
90b9a268 DG |
250 | }; |
251 | ||
252 | /* | |
253 | * List of context type. Use to enable multiple context on a single command | |
254 | * line entry. | |
255 | */ | |
256 | struct ctx_type_list { | |
257 | struct cds_list_head head; | |
258 | } ctx_type_list = { | |
259 | .head = CDS_LIST_HEAD_INIT(ctx_type_list.head), | |
260 | }; | |
261 | ||
90b9a268 DG |
262 | /* |
263 | * Pretty print context type. | |
264 | */ | |
265 | static void print_ctx_type(FILE *ofp) | |
266 | { | |
b13d56d7 MD |
267 | const char *indent = " "; |
268 | int indent_len = strlen(indent); | |
269 | int len, i = 0; | |
90b9a268 | 270 | |
76e3c5dd | 271 | fprintf(ofp, "%s", indent); |
b13d56d7 | 272 | len = indent_len; |
90b9a268 | 273 | while (ctx_opts[i].symbol != NULL) { |
b13d56d7 MD |
274 | if (len > indent_len) { |
275 | if (len + strlen(ctx_opts[i].symbol) + 2 | |
276 | >= PRINT_LINE_LEN) { | |
277 | fprintf(ofp, ",\n"); | |
76e3c5dd | 278 | fprintf(ofp, "%s", indent); |
b13d56d7 MD |
279 | len = indent_len; |
280 | } else { | |
281 | len += fprintf(ofp, ", "); | |
90b9a268 DG |
282 | } |
283 | } | |
b13d56d7 | 284 | len += fprintf(ofp, "%s", ctx_opts[i].symbol); |
90b9a268 DG |
285 | i++; |
286 | } | |
90b9a268 DG |
287 | } |
288 | ||
d65106b1 DG |
289 | /* |
290 | * usage | |
291 | */ | |
292 | static void usage(FILE *ofp) | |
293 | { | |
b13d56d7 | 294 | fprintf(ofp, "usage: lttng add-context -t TYPE\n"); |
d65106b1 | 295 | fprintf(ofp, "\n"); |
55cc08a6 | 296 | fprintf(ofp, "If no event name is given (-e), the context will be added to the channel\n"); |
b13d56d7 | 297 | fprintf(ofp, "If no channel and no event is given (-c/-e), the context\n"); |
55cc08a6 | 298 | fprintf(ofp, "will be added to all events and all channels.\n"); |
3301e740 | 299 | fprintf(ofp, "\n"); |
d65106b1 DG |
300 | fprintf(ofp, "Options:\n"); |
301 | fprintf(ofp, " -h, --help Show this help\n"); | |
5440dc42 | 302 | fprintf(ofp, " -s, --session Apply on session name\n"); |
d65106b1 DG |
303 | fprintf(ofp, " -c, --channel NAME Apply on channel\n"); |
304 | fprintf(ofp, " -e, --event NAME Apply on event\n"); | |
305 | fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); | |
55cc08a6 | 306 | fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n"); |
e14f64a8 DG |
307 | fprintf(ofp, " If no CMD, the domain used is UST global\n"); |
308 | fprintf(ofp, " or else the domain is UST EXEC_NAME\n"); | |
309 | fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n"); | |
b13d56d7 MD |
310 | fprintf(ofp, " -t, --type TYPE Context type. You can repeat that option on\n"); |
311 | fprintf(ofp, " the command line.\n"); | |
312 | fprintf(ofp, " TYPE can be one of the strings below:\n"); | |
90b9a268 | 313 | print_ctx_type(ofp); |
d65106b1 | 314 | fprintf(ofp, "\n"); |
90b9a268 | 315 | fprintf(ofp, "Example:\n"); |
b13d56d7 | 316 | fprintf(ofp, "This command will add the context information 'prio' and two perf\n" |
e0899ffa | 317 | "counters: hardware branch misses and cache misses, to all events\n" |
b13d56d7 MD |
318 | "in the trace data output:\n"); |
319 | fprintf(ofp, "# lttng add-context -k -t prio -t perf:branch-misses -t perf:cache-misses\n"); | |
d65106b1 DG |
320 | fprintf(ofp, "\n"); |
321 | } | |
322 | ||
90b9a268 DG |
323 | /* |
324 | * Find context numerical value from string. | |
325 | */ | |
326 | static int find_ctx_type_idx(const char *opt) | |
327 | { | |
328 | int ret = -1, i = 0; | |
329 | ||
330 | while (ctx_opts[i].symbol != NULL) { | |
331 | if (strcmp(opt, ctx_opts[i].symbol) == 0) { | |
332 | ret = i; | |
333 | goto end; | |
334 | } | |
335 | i++; | |
336 | } | |
337 | ||
338 | end: | |
339 | return ret; | |
340 | } | |
341 | ||
90b9a268 DG |
342 | /* |
343 | * Add context to channel or event. | |
d65106b1 | 344 | */ |
cd80958d | 345 | static int add_context(char *session_name) |
d65106b1 | 346 | { |
b13d56d7 | 347 | int ret = CMD_SUCCESS; |
7d29a247 DG |
348 | struct lttng_event_context context; |
349 | struct lttng_domain dom; | |
3301e740 | 350 | struct ctx_type *type; |
b13d56d7 | 351 | char *ptr; |
d65106b1 | 352 | |
cd80958d DG |
353 | if (opt_kernel) { |
354 | dom.type = LTTNG_DOMAIN_KERNEL; | |
55cc08a6 DG |
355 | } else if (opt_pid != 0) { |
356 | dom.type = LTTNG_DOMAIN_UST_PID; | |
357 | dom.attr.pid = opt_pid; | |
358 | DBG("PID %d set to lttng handle", opt_pid); | |
359 | } else if (opt_userspace && opt_cmd_name == NULL) { | |
360 | dom.type = LTTNG_DOMAIN_UST; | |
361 | } else if (opt_userspace && opt_cmd_name != NULL) { | |
362 | dom.type = LTTNG_DOMAIN_UST_EXEC_NAME; | |
363 | strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX); | |
364 | } else { | |
e14f64a8 | 365 | ERR("Please specify a tracer (-k/--kernel or -u/--userspace)"); |
55cc08a6 DG |
366 | ret = CMD_NOT_IMPLEMENTED; |
367 | goto error; | |
cd80958d DG |
368 | } |
369 | ||
370 | handle = lttng_create_handle(session_name, &dom); | |
371 | if (handle == NULL) { | |
372 | ret = -1; | |
373 | goto error; | |
374 | } | |
375 | ||
3301e740 DG |
376 | /* Iterate over all context type given */ |
377 | cds_list_for_each_entry(type, &ctx_type_list.head, list) { | |
b13d56d7 MD |
378 | context.ctx = type->opt->ctx_type; |
379 | if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) { | |
380 | context.u.perf_counter.type = type->opt->u.perf.type; | |
381 | context.u.perf_counter.config = type->opt->u.perf.config; | |
d7b91b3c | 382 | strcpy(context.u.perf_counter.name, type->opt->symbol); |
b13d56d7 MD |
383 | /* Replace : and - by _ */ |
384 | while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) { | |
385 | *ptr = '_'; | |
3301e740 | 386 | } |
b13d56d7 MD |
387 | while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) { |
388 | *ptr = '_'; | |
3301e740 | 389 | } |
3301e740 | 390 | } |
55cc08a6 DG |
391 | DBG("Adding context..."); |
392 | ||
393 | ret = lttng_add_context(handle, &context, opt_event_name, | |
394 | opt_channel_name); | |
395 | if (ret < 0) { | |
396 | fprintf(stderr, "%s: ", type->opt->symbol); | |
397 | continue; | |
d65106b1 | 398 | } else { |
55cc08a6 DG |
399 | MSG("%s context %s added to %s event in %s", |
400 | opt_kernel ? "kernel" : "UST", type->opt->symbol, | |
401 | opt_event_name ? opt_event_name : "all", | |
402 | opt_channel_name ? opt_channel_name : "all channels"); | |
d65106b1 | 403 | } |
d65106b1 DG |
404 | } |
405 | ||
406 | error: | |
cd80958d DG |
407 | lttng_destroy_handle(handle); |
408 | ||
d65106b1 DG |
409 | return ret; |
410 | } | |
411 | ||
412 | /* | |
90b9a268 | 413 | * Add context on channel or event. |
d65106b1 DG |
414 | */ |
415 | int cmd_add_context(int argc, const char **argv) | |
416 | { | |
90b9a268 | 417 | int index, opt, ret = CMD_SUCCESS; |
d65106b1 | 418 | static poptContext pc; |
b13d56d7 | 419 | struct ctx_type *type, *tmptype; |
cd80958d | 420 | char *session_name = NULL; |
d65106b1 | 421 | |
636167b6 DG |
422 | if (argc < 2) { |
423 | usage(stderr); | |
424 | goto end; | |
425 | } | |
426 | ||
d65106b1 DG |
427 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
428 | poptReadDefaultConfig(pc, 0); | |
429 | ||
430 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
431 | switch (opt) { | |
432 | case OPT_HELP: | |
433 | usage(stderr); | |
434 | ret = CMD_SUCCESS; | |
435 | goto end; | |
436 | case OPT_TYPE: | |
3301e740 DG |
437 | type = malloc(sizeof(struct ctx_type)); |
438 | if (type == NULL) { | |
439 | perror("malloc ctx_type"); | |
440 | ret = -1; | |
636167b6 DG |
441 | goto end; |
442 | } | |
6caa2bcc | 443 | index = find_ctx_type_idx(opt_type); |
b13d56d7 | 444 | if (index < 0) { |
6caa2bcc | 445 | ERR("Unknown context type %s", opt_type); |
b13d56d7 | 446 | goto end; |
90b9a268 | 447 | } |
b13d56d7 MD |
448 | type->opt = &ctx_opts[index]; |
449 | if (type->opt->ctx_type == -1) { | |
6caa2bcc | 450 | ERR("Unknown context type %s", opt_type); |
90b9a268 DG |
451 | } else { |
452 | cds_list_add(&type->list, &ctx_type_list.head); | |
453 | } | |
d65106b1 | 454 | break; |
eeac7d46 MD |
455 | case OPT_USERSPACE: |
456 | opt_userspace = 1; | |
457 | opt_cmd_name = poptGetOptArg(pc); | |
458 | break; | |
d65106b1 DG |
459 | default: |
460 | usage(stderr); | |
461 | ret = CMD_UNDEFINED; | |
462 | goto end; | |
463 | } | |
464 | } | |
465 | ||
cd80958d DG |
466 | if (!opt_session_name) { |
467 | session_name = get_session_name(); | |
468 | if (session_name == NULL) { | |
469 | ret = -1; | |
470 | goto end; | |
471 | } | |
472 | } else { | |
473 | session_name = opt_session_name; | |
474 | } | |
475 | ||
476 | ret = add_context(session_name); | |
3301e740 DG |
477 | |
478 | /* Cleanup allocated memory */ | |
b13d56d7 | 479 | cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) { |
3301e740 DG |
480 | free(type); |
481 | } | |
482 | ||
d65106b1 DG |
483 | end: |
484 | return ret; | |
485 | } |