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