1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* stp.c - stp 'daemon'
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2005 IBM Corporation
* Copyright (C) 2005-2006 Red Hat, Inc.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/statfs.h>
#include <pwd.h>
#include "librelay.h"
extern char *optarg;
extern int optopt;
extern int optind;
int print_only = 0;
int quiet = 0;
int merge = 1;
int verbose = 0;
int enable_relayfs = 1;
int target_pid = 0;
int driver_pid = 0;
unsigned int buffer_size = 0;
char *modname = NULL;
char *modpath = NULL;
#define MAXMODOPTIONS 64
char *modoptions[MAXMODOPTIONS];
char *target_cmd = NULL;
char *outfile_name = NULL;
char *username = NULL;
uid_t cmd_uid;
gid_t cmd_gid;
/* relayfs base file name */
static char stpd_filebase[1024];
/* if no output file name is specified, use this */
#define DEFAULT_OUTFILE_NAME "probe.out"
/* stp_check script */
#ifdef PKGLIBDIR
char *stp_check=PKGLIBDIR "/stp_check";
#else
char *stp_check="stp_check";
#endif
static void usage(char *prog)
{
fprintf(stderr, "\n%s [-m] [-p] [-q] [-r] [-c cmd ] [-t pid]\n"
"\t[-b bufsize] [-o FILE] kmod-name [kmod-options]\n", prog);
fprintf(stderr, "-m Don't merge per-cpu files.\n");
fprintf(stderr, "-p Print only. Don't log to files.\n");
fprintf(stderr, "-q Quiet. Don't display trace to stdout.\n");
fprintf(stderr, "-r Disable running stp_check and loading relayfs module.\n");
fprintf(stderr, "-c cmd. Command \'cmd\' will be run and stpd will exit when it does.\n");
fprintf(stderr, " _stp_target will contain the pid for the command.\n");
fprintf(stderr, "-t pid. Sets _stp_target to pid.\n");
fprintf(stderr, "-d pid. Pass the systemtap driver's pid.\n");
fprintf(stderr, "-o FILE. Send output to FILE.\n");
fprintf(stderr, "-u username. Run commands as username.\n");
fprintf(stderr, "-b buffer size. The systemtap module will specify a buffer size.\n");
fprintf(stderr, " Setting one here will override that value. The value should be\n");
fprintf(stderr, " an integer between 1 and 64 which be assumed to be the\n");
fprintf(stderr, " buffer size in MB. That value will be per-cpu if relayfs is used.\n");
exit(1);
}
int main(int argc, char **argv)
{
int c, status;
pid_t pid;
struct statfs st;
while ((c = getopt(argc, argv, "mpqrb:n:t:d:c:vo:u:")) != EOF)
{
switch (c) {
case 'm':
merge = 0;
break;
case 'p':
print_only = 1;
break;
case 'q':
quiet = 1;
break;
case 'v':
verbose = 1;
break;
case 'r':
enable_relayfs = 0;
break;
case 'b':
{
int size = (unsigned)atoi(optarg);
if (!size)
usage(argv[0]);
if (size > 64) {
fprintf(stderr, "Maximum buffer size is 64 (MB)\n");
exit(1);
}
buffer_size = size;
break;
}
case 't':
target_pid = atoi(optarg);
break;
case 'd':
driver_pid = atoi(optarg);
break;
case 'c':
target_cmd = optarg;
break;
case 'o':
outfile_name = optarg;
break;
case 'u':
username = optarg;
break;
default:
usage(argv[0]);
}
}
if (verbose) {
if (buffer_size)
printf ("Using a buffer of %u bytes.\n", buffer_size);
}
if (optind < argc)
{
/* Collect both full path and just the trailing module name. */
modpath = argv[optind++];
modname = rindex (modpath, '/');
if (modname == NULL)
modname = modpath;
else
modname++; /* skip over / */
}
if (optind < argc)
{
unsigned start_idx = 3; /* reserve three slots in modoptions[] */
while (optind < argc && start_idx+1 < MAXMODOPTIONS)
modoptions[start_idx++] = argv[optind++];
/* Redundantly ensure that there is a NULL pointer at the end
of modoptions[]. */
modoptions[start_idx] = NULL;
}
if (!modname) {
fprintf (stderr, "Cannot invoke daemon without probe module\n");
usage(argv[0]);
}
if (print_only && quiet) {
fprintf (stderr, "Cannot do \"-p\" and \"-q\" both.\n");
usage(argv[0]);
}
if (username) {
struct passwd *pw = getpwnam(username);
if (!pw) {
fprintf(stderr, "Cannot find user \"%s\".\n", username);
exit(1);
}
cmd_uid = pw->pw_uid;
cmd_gid = pw->pw_gid;
} else {
cmd_uid = getuid();
cmd_gid = getgid();
}
if (enable_relayfs) {
/* now run the _stp_check script */
if ((pid = fork()) < 0) {
perror ("fork of stp_check failed.");
exit(-1);
} else if (pid == 0) {
if (execlp(stp_check, stp_check, NULL) < 0)
_exit (-1);
}
if (waitpid(pid, &status, 0) < 0) {
perror("waitpid");
exit(-1);
}
if (WIFEXITED(status) && WEXITSTATUS(status)) {
perror (stp_check);
fprintf(stderr, "Could not execute %s\n", stp_check);
exit(1);
}
if (!outfile_name)
outfile_name = DEFAULT_OUTFILE_NAME;
}
if (statfs("/mnt/relay", &st) < 0)
sprintf(stpd_filebase, "/proc/systemtap/stap_%d/cpu", driver_pid);
else
sprintf(stpd_filebase, "/mnt/relay/%d/cpu", getpid());
if (init_stp(stpd_filebase, !quiet)) {
//fprintf(stderr, "Couldn't initialize stpd. Exiting.\n");
exit(1);
}
if (stp_main_loop()) {
fprintf(stderr,"Couldn't enter main loop. Exiting.\n");
exit(1);
}
return 0;
}
|