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
|
// build/run probes
// Copyright (C) 2005-2007 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include "config.h"
#include "buildrun.h"
#include "session.h"
#include "util.h"
#include <cstdlib>
#include <fstream>
#include <sstream>
extern "C" {
#include "signal.h"
#include <sys/wait.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
}
using namespace std;
/* Adjust and run make_cmd to build a kernel module. */
static int
run_make_cmd(systemtap_session& s, string& make_cmd)
{
// Before running make, fix up the environment a bit. PATH should
// already be overridden. Clean out a few variables that
// /lib/modules/${KVER}/build/Makefile uses.
int rc = unsetenv("ARCH") || unsetenv("KBUILD_EXTMOD")
|| unsetenv("CROSS_COMPILE") || unsetenv("KBUILD_IMAGE")
|| unsetenv("KCONFIG_CONFIG") || unsetenv("INSTALL_PATH");
if (rc)
{
const char* e = strerror (errno);
cerr << "unsetenv failed: " << e << endl;
}
if (s.verbose > 1)
make_cmd += " V=1";
else
make_cmd += " -s >/dev/null 2>&1";
if (s.verbose > 1) clog << "Running " << make_cmd << endl;
rc = system (make_cmd.c_str());
return rc;
}
int
compile_pass (systemtap_session& s)
{
// fill in a quick Makefile
string makefile_nm = s.tmpdir + "/Makefile";
ofstream o (makefile_nm.c_str());
int rc = 0;
// Create makefile
// Clever hacks copied from vmware modules
o << "stap_check_gcc = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo \"$(1)\"; else echo \"$(2)\"; fi)" << endl;
o << "stap_check_build = $(shell " << "set -x; " << " if $(CC) $(KBUILD_CPPFLAGS) $(CPPFLAGS) $(KBUILD_CFLAGS) $(CFLAGS_KERNEL) $(EXTRA_CFLAGS) $(CFLAGS) -DKBUILD_BASENAME=\\\"" << s.module_name << "\\\" -Werror -S -o /dev/null -xc $(1) > /dev/null ; then echo \"$(2)\"; else echo \"$(3)\"; fi)" << endl;
o << "SYSTEMTAP_RUNTIME = \"" << s.runtime_path << "\"" << endl;
// "autoconf" options go here
// enum hrtimer_mode renaming near 2.6.21; see tapsets.cxx hrtimer_derived_probe_group::emit_module_decls
string module_cflags = "CFLAGS_" + s.module_name + ".o";
o << module_cflags << " :=" << endl;
o << module_cflags << " += $(call stap_check_build, $(SYSTEMTAP_RUNTIME)/autoconf-hrtimer-rel.c, -DSTAPCONF_HRTIMER_REL,)" << endl;
o << module_cflags << " += $(call stap_check_build, $(SYSTEMTAP_RUNTIME)/autoconf-inode-private.c, -DSTAPCONF_INODE_PRIVATE,)" << endl;
o << module_cflags << " += $(call stap_check_build, $(SYSTEMTAP_RUNTIME)/autoconf-constant-tsc.c, -DSTAPCONF_CONSTANT_TSC,)" << endl;
o << module_cflags << " += $(call stap_check_build, $(SYSTEMTAP_RUNTIME)/autoconf-tsc-khz.c, -DSTAPCONF_TSC_KHZ,)" << endl;
o << module_cflags << " += $(call stap_check_build, $(SYSTEMTAP_RUNTIME)/autoconf-ktime-get-real.c, -DSTAPCONF_KTIME_GET_REAL,)" << endl;
for (unsigned i=0; i<s.macros.size(); i++)
o << "EXTRA_CFLAGS += -D " << lex_cast_qstring(s.macros[i]) << endl;
if (s.verbose > 2)
o << "EXTRA_CFLAGS += -ftime-report -Q" << endl;
// XXX: unfortunately, -save-temps can't work since linux kbuild cwd
// is not writeable.
//
// if (s.keep_tmpdir)
// o << "CFLAGS += -fverbose-asm -save-temps" << endl;
o << "EXTRA_CFLAGS += -freorder-blocks" << endl; // improve on -Os
// o << "CFLAGS += -fno-unit-at-a-time" << endl;
// Assumes linux 2.6 kbuild
o << "EXTRA_CFLAGS += -Wno-unused -Werror" << endl;
o << "EXTRA_CFLAGS += -I\"" << s.runtime_path << "\"" << endl;
// XXX: this may help ppc toc overflow
// o << "CFLAGS := $(subst -Os,-O2,$(CFLAGS)) -fminimal-toc" << endl;
o << "obj-m := " << s.module_name << ".o" << endl;
o.close ();
// Generate module directory pathname and make sure it exists.
string module_dir = string("/lib/modules/")
+ s.kernel_release + "/build";
struct stat st;
rc = stat(module_dir.c_str(), &st);
if (rc != 0)
{
clog << "Module directory " << module_dir << " check failed: "
<< strerror(errno) << endl
<< "Make sure kernel devel is installed." << endl;
return rc;
}
// Run make
string make_cmd = string("make")
+ string (" -C \"") + module_dir + string("\"");
make_cmd += string(" M=\"") + s.tmpdir + string("\" modules");
rc = run_make_cmd(s, make_cmd);
return rc;
}
bool
uprobes_enabled (void)
{
int rc = system ("/bin/grep -q unregister_uprobe /proc/kallsyms");
return (rc == 0);
}
int
make_uprobes (systemtap_session& s)
{
string uprobes_home = string(PKGDATADIR "/runtime/uprobes");
// Quietly skip the build if the Makefile has been removed.
string makefile = uprobes_home + string("/Makefile");
struct stat buf;
if (stat(makefile.c_str(), &buf) != 0)
return 2; // make's exit value for No such file or directory.
if (s.verbose)
clog << "Pass 4, overtime: "
<< "(re)building SystemTap's version of uprobes."
<< endl;
string make_cmd = string("make -C ") + uprobes_home;
int rc = run_make_cmd(s, make_cmd);
if (rc && s.verbose)
clog << "Uprobes build failed. "
<< "Hope uprobes is available at run time."
<< endl;
return rc;
}
int
run_pass (systemtap_session& s)
{
int rc = 0;
struct passwd *pw = getpwuid(getuid());
string username = string(pw->pw_name);
// for now, just spawn staprun
string staprun_cmd = string(BINDIR) + "/staprun "
+ (s.verbose>1 ? "-v " : "")
+ (s.verbose>2 ? "-v " : "")
+ (s.output_file.empty() ? "" : "-o " + s.output_file + " ");
staprun_cmd += "-d " + stringify(getpid()) + " ";
if (s.cmd != "")
staprun_cmd += "-c " + cmdstr_quoted(s.cmd) + " ";
if (s.target_pid)
staprun_cmd += "-t " + stringify(s.target_pid) + " ";
if (s.buffer_size)
staprun_cmd += "-b " + stringify(s.buffer_size) + " ";
if (s.need_uprobes)
staprun_cmd += "-u ";
staprun_cmd += s.tmpdir + "/" + s.module_name + ".ko";
if (s.verbose>1) clog << "Running " << staprun_cmd << endl;
signal (SIGHUP, SIG_IGN);
signal (SIGINT, SIG_IGN);
rc = system (staprun_cmd.c_str ());
return rc;
}
|