Merge branch 'master' of ssh://git.dorsal.polymtl.ca/home/git/ust
[ust.git] / ustctl / ustctl.c
CommitLineData
c39c72ee 1/* Copyright (C) 2009 Pierre-Marc Fournier
0c89df6c 2 * Copyright (C) 2011 Ericsson AB, Nils Carlson <nils.carlson@ericsson.com>
c39c72ee
PMF
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
872037bb
PMF
19#define _GNU_SOURCE
20#include <stdio.h>
fbd8191b
PMF
21#include <unistd.h>
22#include <getopt.h>
23#include <stdlib.h>
fbd8191b 24#include <fcntl.h>
fbd8191b 25
2298f329 26#include "ust/ustctl.h"
2028e7fd 27#include "usterr.h"
0c89df6c
NC
28#include "cli.h"
29#include "scanning_functions.h"
ae937656 30
0c89df6c 31void usage(const char *process_name)
fd2fb4f9 32{
0c89df6c
NC
33 fprintf(stderr, "Usage: %s COMMAND [ARGS]...\n", process_name);
34 fprintf(stderr,
35 "Control tracing within a process that supports UST,\n"
36 " the Userspace Tracing libary\n"
37 "Options:\n"
38 " -h[<cmd>], --help[=<cmd>] "
39 "help, for a command if provided\n"
40 " -l, --list "
41 "short list of commands\n"
42 " -e, --extended-list "
43 "extented list of commands with help\n"
44 "Commands:\n");
45 list_cli_cmds(CLI_DESCRIPTIVE_LIST);
fd2fb4f9
PMF
46}
47
0c89df6c 48struct option options[] =
fbd8191b 49{
0c89df6c
NC
50 {"help", 2, NULL, 'h'},
51 {"list", 0, NULL, 'l'},
52 {"extended-list", 0, NULL, 'e'},
53 {NULL, 0, NULL, 0},
54};
52c51a47 55
0c89df6c
NC
56int main(int argc, char *argv[])
57{
58 struct cli_cmd *cli_cmd;
59 int opt;
52c51a47 60
0c89df6c
NC
61 if(argc <= 1) {
62 fprintf(stderr, "No operation specified.\n");
63 usage(argv[0]);
64 exit(EXIT_FAILURE);
65 }
52c51a47 66
0c89df6c
NC
67 while ((opt = getopt_long(argc, argv, "+h::le",
68 options, NULL)) != -1) {
69 switch (opt) {
70 case 'h':
71 if (!optarg) {
72 usage(argv[0]);
73 } else {
74 if (cli_print_help(optarg)) {
75 fprintf(stderr, "No such command %s\n",
76 optarg);
77 }
78 }
79 exit(EXIT_FAILURE);
52c51a47 80 break;
0c89df6c
NC
81 case 'l':
82 list_cli_cmds(CLI_SIMPLE_LIST);
83 exit(EXIT_FAILURE);
52c51a47 84 break;
0c89df6c
NC
85 case 'e':
86 list_cli_cmds(CLI_EXTENDED_LIST);
87 exit(EXIT_FAILURE);
88 default:
89 fprintf(stderr, "Unknown option\n");
ae937656 90 break;
fbd8191b
PMF
91 }
92 }
93
0c89df6c
NC
94 cli_cmd = find_cli_cmd(argv[optind]);
95 if (!cli_cmd) {
96 fprintf(stderr, "No such command %s\n",
97 argv[optind]);
98 exit(EXIT_FAILURE);
fbd8191b
PMF
99 }
100
0c89df6c
NC
101 cli_dispatch_cmd(cli_cmd, argc - optind, &argv[optind]);
102
52c51a47
PMF
103 return 0;
104}
fbd8191b 105
0c89df6c 106static int list_trace_events(int argc, char *argv[])
72098143 107{
0c89df6c
NC
108 struct trace_event_status *tes = NULL;
109 int i;
110 pid_t pid;
72098143 111
0c89df6c 112 pid = parse_pid(argv[1]);
72098143 113
2298f329 114 if (ustctl_get_tes(&tes, pid)) {
0c89df6c
NC
115 ERR("error while trying to list "
116 "trace_events for PID %u\n",
117 pid);
72098143 118 return -1;
72098143 119 }
0c89df6c
NC
120 i = 0;
121 for (i = 0; tes[i].name; i++) {
122 printf("{PID: %u, trace_event: %s}\n",
123 pid,
124 tes[i].name);
125 }
2298f329 126 ustctl_free_tes(tes);
0c89df6c
NC
127
128 return 0;
72098143
NC
129}
130
0c89df6c 131static int set_sock_path(int argc, char *argv[])
72098143 132{
0c89df6c 133 pid_t pid;
72098143 134
0c89df6c 135 pid = parse_pid(argv[1]);
72098143 136
2298f329 137 if (ustctl_set_sock_path(argv[2], pid)) {
0c89df6c 138 ERR("error while trying to set sock path for PID %u\n", pid);
72098143
NC
139 return -1;
140 }
72098143 141
0c89df6c
NC
142 return 0;
143}
d89b8191 144
0c89df6c 145static int get_sock_path(int argc, char *argv[])
fbd8191b 146{
0c89df6c
NC
147 pid_t pid;
148 char *sock_path;
fbd8191b 149
0c89df6c 150 pid = parse_pid(argv[1]);
fbd8191b 151
2298f329 152 if (ustctl_get_sock_path(&sock_path, pid)) {
0c89df6c
NC
153 ERR("error while trying to get sock path for PID %u\n", pid);
154 return -1;
ae937656 155 }
0c89df6c
NC
156 printf("The socket path is %s\n", sock_path);
157 free(sock_path);
fbd8191b 158
0c89df6c 159 return 0;
fbd8191b 160}
ae937656 161
0c89df6c
NC
162struct cli_cmd __cli_cmds general_cmds[] = {
163 {
164 .name = "list-trace-events",
165 .description = "List trace-events for a given pid",
166 .help_text = "list-trace-events <pid>\n"
167 "List the trace-events in a process\n",
168 .function = list_trace_events,
169 .desired_args = 1,
170 .desired_args_op = CLI_EQ,
171 },
172 {
173 .name = "set-sock-path",
174 .description = "Set the path to the consumer daemon socket",
175 .help_text = "set-sock-path <pid> <sock-path>\n"
176 "Set the path to the consumer daemon socket\n",
177 .function = set_sock_path,
178 .desired_args = 2,
179 .desired_args_op = CLI_EQ,
180 },
181 {
182 .name = "get-sock-path",
183 .description = "Get the path to the consumer daemon socket",
184 .help_text = "get-sock-path <pid>\n"
185 "Get the path to the consumer daemon socket\n",
186 .function = get_sock_path,
187 .desired_args = 1,
188 .desired_args_op = CLI_EQ,
189 },
190};
This page took 0.050606 seconds and 4 git commands to generate.