Add kernel context support
[lttng-tools.git] / lttng / commands / add_context.c
CommitLineData
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
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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
20#include <popt.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include "cmd.h"
29#include "conf.h"
30#include "utils.h"
31
32static char *opt_event_name;
33static char *opt_channel_name;
34static char *opt_perf_name;
35static int *opt_kernel;
36static int opt_pid_all;
37static int opt_userspace;
38static int opt_ctx_type;
39static int opt_perf_type;
40static int opt_perf_id;
41static pid_t opt_pid;
42
43enum {
44 OPT_HELP = 1,
45 OPT_TYPE,
46};
47
48static struct poptOption long_options[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
51 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
52 {"event", 'e', POPT_ARG_STRING, &opt_event_name, 0, 0, 0},
53 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
54 {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0},
55 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
56 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
57 {"type", 't', POPT_ARG_INT, 0, OPT_TYPE, 0, 0},
58 {"perf-name", 0, POPT_ARG_STRING, &opt_perf_name, 0, 0, 0},
59 {"perf-type", 0, POPT_ARG_INT, &opt_perf_type, 0, 0, 0},
60 {"perf-id", 0, POPT_ARG_INT, &opt_perf_id, 0, 0, 0},
61 {0, 0, 0, 0, 0, 0, 0}
62};
63
64/*
65 * usage
66 */
67static void usage(FILE *ofp)
68{
69 fprintf(ofp, "usage: lttng add-context [options] [context_options]\n");
70 fprintf(ofp, "\n");
71 fprintf(ofp, "Options:\n");
72 fprintf(ofp, " -h, --help Show this help\n");
73 fprintf(ofp, " -c, --channel NAME Apply on channel\n");
74 fprintf(ofp, " -e, --event NAME Apply on event\n");
75 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
76 fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n");
77 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
78 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
79 fprintf(ofp, " -t, --type TYPE Context type. TYPE must be a numerical value:\n");
80 fprintf(ofp, " KERNEL_CONTEXT_PID = 0\n");
81 fprintf(ofp, " KERNEL_CONTEXT_PERF_COUNTER = 1\n");
82 fprintf(ofp, " KERNEL_CONTEXT_COMM = 2\n");
83 fprintf(ofp, " KERNEL_CONTEXT_PRIO = 3\n");
84 fprintf(ofp, " KERNEL_CONTEXT_NICE = 4\n");
85 fprintf(ofp, " KERNEL_CONTEXT_VPID = 5\n");
86 fprintf(ofp, " KERNEL_CONTEXT_TID = 6\n");
87 fprintf(ofp, " KERNEL_CONTEXT_VTID = 7\n");
88 fprintf(ofp, " KERNEL_CONTEXT_PPID = 8\n");
89 fprintf(ofp, " KERNEL_CONTEXT_VPPID = 9\n");
90 fprintf(ofp, "\n");
91 fprintf(ofp, "Context options:\n");
92 fprintf(ofp, " --perf-name NAME Perf event name\n");
93 fprintf(ofp, " --perf-type TYPE Perf event type. TYPE must be a numeric value:\n");
94 fprintf(ofp, " PERF_TYPE_HARDWARE = 0\n");
95 fprintf(ofp, " PERF_TYPE_SOFTWARE = 1\n");
96 fprintf(ofp, " --perf-id ID Perf event id. ID must be a numeric value:\n");
97 fprintf(ofp, " Hardware IDs (0):\n");
98 fprintf(ofp, " PERF_COUNT_HW_CPU_CYCLES = 0\n");
99 fprintf(ofp, " PERF_COUNT_HW_INSTRUCTIONS = 1\n");
100 fprintf(ofp, " PERF_COUNT_HW_CACHE_REFERENCES = 2\n");
101 fprintf(ofp, " PERF_COUNT_HW_CACHE_MISSES = 3\n");
102 fprintf(ofp, " PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4\n");
103 fprintf(ofp, " PERF_COUNT_HW_BRANCH_MISSES = 5\n");
104 fprintf(ofp, " PERF_COUNT_HW_BUS_CYCLES = 6\n");
105 fprintf(ofp, " Software IDs (1):\n");
106 fprintf(ofp, " PERF_COUNT_SW_CPU_CLOCK = 0\n");
107 fprintf(ofp, " PERF_COUNT_SW_TASK_CLOCK = 1\n");
108 fprintf(ofp, " PERF_COUNT_SW_PAGE_FAULTS = 2\n");
109 fprintf(ofp, " PERF_COUNT_SW_CONTEXT_SWITCHES = 3\n");
110 fprintf(ofp, " PERF_COUNT_SW_CPU_MIGRATIONS = 4\n");
111 fprintf(ofp, " PERF_COUNT_SW_PAGE_FAULTS_MIN = 5\n");
112 fprintf(ofp, " PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6\n");
113 fprintf(ofp, "\n");
114}
115
116/*
117 * add_context
118 *
119 * Add context to channel or event.
120 */
121static int add_context(void)
122{
123 int ret = CMD_SUCCESS;
124 struct lttng_kernel_context context;
125
126 if (set_session_name() < 0) {
127 ret = CMD_ERROR;
128 goto error;
129 }
130
131 context.ctx = opt_ctx_type;
132 if (opt_ctx_type == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
133 context.u.perf_counter.type = opt_perf_type;
134 context.u.perf_counter.config = opt_perf_id;
135 strncpy(context.u.perf_counter.name, opt_perf_name,
136 LTTNG_SYMBOL_NAME_LEN);
137 }
138
139 if (opt_kernel) {
140 DBG("Adding kernel context\n");
141 ret = lttng_kernel_add_context(&context, opt_event_name, opt_channel_name);
142 if (ret < 0) {
143 goto error;
144 } else {
145 MSG("Kernel context added");
146 }
147 } else if (opt_userspace) { /* User-space tracer action */
148 /*
149 * TODO: Waiting on lttng UST 2.0
150 */
151 if (opt_pid_all) {
152 } else if (opt_pid != 0) {
153 }
154 ret = CMD_NOT_IMPLEMENTED;
155 goto error;
156 } else {
157 ERR("Please specify a tracer (kernel or user-space)");
158 goto error;
159 }
160
161error:
162 return ret;
163}
164
165/*
166 * cmd_add_context
167 *
168 * Add context on channel or event.
169 */
170int cmd_add_context(int argc, const char **argv)
171{
172 int opt, ret;
173 char *tmp;
174 static poptContext pc;
175
176 pc = poptGetContext(NULL, argc, argv, long_options, 0);
177 poptReadDefaultConfig(pc, 0);
178
179 while ((opt = poptGetNextOpt(pc)) != -1) {
180 switch (opt) {
181 case OPT_HELP:
182 usage(stderr);
183 ret = CMD_SUCCESS;
184 goto end;
185 case OPT_TYPE:
186 /* Mandatory field */
187 tmp = poptGetOptArg(pc);
188 if (tmp == NULL) {
189 usage(stderr);
190 ret = CMD_ERROR;
191 goto end;
192 }
193 opt_ctx_type = atoi(tmp);
194 break;
195 default:
196 usage(stderr);
197 ret = CMD_UNDEFINED;
198 goto end;
199 }
200 }
201
202 ret = add_context();
203
204end:
205 return ret;
206}
This page took 0.031033 seconds and 5 git commands to generate.