]> sourceware.org Git - systemtap.git/blob - buildrun.cxx
Merge branch 'dsmith/secure-boot' into master.
[systemtap.git] / buildrun.cxx
1 // build/run probes
2 // Copyright (C) 2005-2014 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9 #include "config.h"
10 #include "buildrun.h"
11 #include "session.h"
12 #include "util.h"
13 #include "hash.h"
14 #include "translate.h"
15
16 #include <cstdlib>
17 #include <fstream>
18 #include <sstream>
19
20 extern "C" {
21 #include <signal.h>
22 #include <sys/wait.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/resource.h>
31 }
32
33
34 using namespace std;
35
36 /* Adjust and run make_cmd to build a kernel module. */
37 static int
38 run_make_cmd(systemtap_session& s, vector<string>& make_cmd,
39 bool null_out=false, bool null_err=false)
40 {
41 assert_no_interrupts();
42
43 // PR14168: we used to unsetenv values here; instead do it via
44 // env(1) in make_any_make_cmd().
45
46 // Disable ccache to avoid saving files that will never be reused.
47 // (ccache is useless to us, because our compiler commands always
48 // include the randomized tmpdir path.)
49 // It's not critical if this fails, so the return is ignored.
50 (void) setenv("CCACHE_DISABLE", "1", 0);
51
52 if (s.verbose > 2)
53 make_cmd.push_back("V=1");
54 else if (s.verbose > 1)
55 make_cmd.push_back("--no-print-directory");
56 else
57 {
58 make_cmd.push_back("-s");
59 make_cmd.push_back("--no-print-directory");
60 }
61
62 // Exploit SMP parallelism, if available.
63 long smp = sysconf(_SC_NPROCESSORS_ONLN);
64 if (smp <= 0) smp = 1;
65 // PR16276: but only if we're not running severely nproc-rlimited
66 struct rlimit rlim;
67 int rlimit_rc = getrlimit(RLIMIT_NPROC, &rlim);
68 const unsigned int severely_limited = smp*30; // WAG at number of gcc+make etc. nested processes
69 bool nproc_limited = (rlimit_rc == 0 && (rlim.rlim_max <= severely_limited ||
70 rlim.rlim_cur <= severely_limited));
71 if (smp >= 1 && !nproc_limited)
72 make_cmd.push_back("-j" + lex_cast(smp+1));
73
74 if (strverscmp (s.kernel_base_release.c_str(), "2.6.29") < 0)
75 {
76 // Older kernels, before linux commit #fd54f502841c1, include
77 // gratuitous "echo"s in their Makefile. We need to suppress
78 // that with this bluntness.
79 null_out = true;
80 }
81
82 int rc = stap_system (s.verbose, "kbuild", make_cmd, null_out, null_err);
83 if (rc != 0)
84 s.set_try_server ();
85 return rc;
86 }
87
88 static vector<string>
89 make_any_make_cmd(systemtap_session& s, const string& dir, const string& target)
90 {
91 vector<string> make_cmd;
92
93 // PR14168: sanitize environment variables for kbuild invocation
94 make_cmd.push_back("env");
95 make_cmd.push_back("-uARCH");
96 make_cmd.push_back("-uKBUILD_EXTMOD");
97 make_cmd.push_back("-uCROSS_COMPILE");
98 make_cmd.push_back("-uKBUILD_IMAGE");
99 make_cmd.push_back("-uKCONFIG_CONFIG");
100 make_cmd.push_back("-uINSTALL_PATH");
101 string newpath = string("PATH=/usr/bin:/bin:") + (getenv("PATH") ?: "");
102 make_cmd.push_back(newpath.c_str());
103
104 make_cmd.push_back("make");
105 make_cmd.push_back("-C");
106 make_cmd.push_back(s.kernel_build_tree);
107 make_cmd.push_back("M=" + dir); // need make-quoting?
108 make_cmd.push_back(target);
109
110 // Add architecture, except for old powerpc (RHBZ669082)
111 if (s.architecture != "powerpc" ||
112 (strverscmp (s.kernel_base_release.c_str(), "2.6.15") >= 0))
113 make_cmd.push_back("ARCH=" + s.architecture); // need make-quoting?
114
115 // PR13847: suppress debuginfo creation by default
116 make_cmd.insert(make_cmd.end(), "CONFIG_DEBUG_INFO=");
117
118 // Add any custom kbuild flags
119 make_cmd.insert(make_cmd.end(), s.kbuildflags.begin(), s.kbuildflags.end());
120
121 return make_cmd;
122 }
123
124 static vector<string>
125 make_make_cmd(systemtap_session& s, const string& dir)
126 {
127 return make_any_make_cmd(s, dir, "modules");
128 }
129
130 static vector<string>
131 make_make_objs_cmd(systemtap_session& s, const string& dir)
132 {
133 // Kbuild uses these rules to build external modules:
134 //
135 // module-dirs := $(addprefix _module_,$(KBUILD_EXTMOD))
136 // modules: $(module-dirs)
137 // @$(kecho) ' Building modules, stage 2.';
138 // $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
139 //
140 // So if we're only interested in the stage 1 objects, we can
141 // cheat and make only the $(module-dirs) part.
142 return make_any_make_cmd(s, dir, "_module_" + dir);
143 }
144
145 static void
146 output_autoconf(systemtap_session& s, ofstream& o, const char *autoconf_c,
147 const char *deftrue, const char *deffalse)
148 {
149 o << "\t";
150 if (s.verbose < 4)
151 o << "@";
152 o << "if $(CHECK_BUILD) $(SYSTEMTAP_RUNTIME)/linux/" << autoconf_c;
153 if (s.verbose < 5)
154 o << " > /dev/null 2>&1";
155 o << "; then ";
156 if (deftrue)
157 o << "echo \"#define " << deftrue << " 1\"";
158 if (deffalse)
159 o << "; else echo \"#define " << deffalse << " 1\"";
160 o << "; fi >> $@" << endl;
161 }
162
163
164 void output_exportconf(systemtap_session& s, ofstream& o, const char *symbol,
165 const char *deftrue)
166 {
167 o << "\t";
168 if (s.verbose < 4)
169 o << "@";
170 if (s.kernel_exports.find(symbol) != s.kernel_exports.end())
171 o << "echo \"#define " << deftrue << " 1\"";
172 o << ">> $@" << endl;
173 }
174
175
176 void output_dual_exportconf(systemtap_session& s, ofstream& o,
177 const char *symbol1, const char *symbol2,
178 const char *deftrue)
179 {
180 o << "\t";
181 if (s.verbose < 4)
182 o << "@";
183 if (s.kernel_exports.find(symbol1) != s.kernel_exports.end()
184 && s.kernel_exports.find(symbol2) != s.kernel_exports.end())
185 o << "echo \"#define " << deftrue << " 1\"";
186 o << ">> $@" << endl;
187 }
188
189
190 void output_either_exportconf(systemtap_session& s, ofstream& o,
191 const char *symbol1, const char *symbol2,
192 const char *deftrue)
193 {
194 o << "\t";
195 if (s.verbose < 4)
196 o << "@";
197 if (s.kernel_exports.find(symbol1) != s.kernel_exports.end()
198 || s.kernel_exports.find(symbol2) != s.kernel_exports.end())
199 o << "echo \"#define " << deftrue << " 1\"";
200 o << ">> $@" << endl;
201 }
202
203
204 static int
205 compile_dyninst (systemtap_session& s)
206 {
207 const string module = s.tmpdir + "/" + s.module_filename();
208
209 vector<string> cmd;
210 cmd.push_back("gcc");
211 cmd.push_back("--std=gnu99");
212 cmd.push_back("-Wall");
213 cmd.push_back("-Werror");
214 cmd.push_back("-Wno-unused");
215 cmd.push_back("-Wno-strict-aliasing");
216
217 // BZ855981/948279. Since dyninst/runtime.h includes __sync_* calls,
218 // the compiler may generate different code for it depending on -march.
219 // For example, if the default is i386, we may get references to auxiliary
220 // functions like __sync_add_and_fetch_4, which appear to be defined
221 // nowhere. We hack around this problem thusly:
222 if (s.architecture == "i386")
223 cmd.push_back("-march=i586");
224
225 cmd.push_back("-fvisibility=hidden");
226 cmd.push_back("-O2");
227 cmd.push_back("-I" + s.runtime_path);
228 cmd.push_back("-D__DYNINST__");
229 for (size_t i = 0; i < s.c_macros.size(); ++i)
230 cmd.push_back("-D" + s.c_macros[i]);
231 cmd.push_back(s.translated_source);
232 cmd.push_back("-pthread");
233 cmd.push_back("-lrt");
234 cmd.push_back("-fPIC");
235 cmd.push_back("-shared");
236 cmd.push_back("-o");
237 cmd.push_back(module);
238 if (s.verbose > 3)
239 {
240 cmd.push_back("-ftime-report");
241 cmd.push_back("-Q");
242 }
243
244 int rc = stap_system (s.verbose, cmd);
245 if (rc)
246 s.set_try_server ();
247 return rc;
248 }
249
250
251 int
252 compile_pass (systemtap_session& s)
253 {
254 if (s.runtime_usermode_p())
255 return compile_dyninst (s);
256
257 int rc = uprobes_pass (s);
258 if (rc)
259 {
260 s.set_try_server ();
261 return rc;
262 }
263
264 // fill in a quick Makefile
265 string makefile_nm = s.tmpdir + "/Makefile";
266 ofstream o (makefile_nm.c_str());
267
268 // Create makefile
269
270 // Clever hacks copied from vmware modules
271 string superverbose;
272 if (s.verbose > 3)
273 superverbose = "set -x;";
274
275 string redirecterrors = "> /dev/null 2>&1";
276 if (s.verbose > 6)
277 redirecterrors = "";
278
279 // Support O= (or KBUILD_OUTPUT) option
280 o << "_KBUILD_CFLAGS := $(call flags,KBUILD_CFLAGS)" << endl;
281
282 o << "stap_check_gcc = $(shell " << superverbose << " if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo \"$(1)\"; else echo \"$(2)\"; fi)" << endl;
283 o << "CHECK_BUILD := $(CC) $(NOSTDINC_FLAGS) $(KBUILD_CPPFLAGS) $(CPPFLAGS) $(LINUXINCLUDE) $(_KBUILD_CFLAGS) $(CFLAGS_KERNEL) $(EXTRA_CFLAGS) $(CFLAGS) -DKBUILD_BASENAME=\\\"" << s.module_name << "\\\"" << (s.omit_werror ? "" : " -Werror") << " -S -o /dev/null -xc " << endl;
284 o << "stap_check_build = $(shell " << superverbose << " if $(CHECK_BUILD) $(1) " << redirecterrors << " ; then echo \"$(2)\"; else echo \"$(3)\"; fi)" << endl;
285
286 o << "SYSTEMTAP_RUNTIME = \"" << s.runtime_path << "\"" << endl;
287
288 // "autoconf" options go here
289
290 // RHBZ 543529: early rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
291 o << "CONFIG_MODULE_SIG := n" << endl;
292
293 string module_cflags = "EXTRA_CFLAGS";
294 o << module_cflags << " :=" << endl;
295
296 // XXX: This gruesome hack is needed on some kernels built with separate O=directory,
297 // where files like 2.6.27 x86's asm/mach-*/mach_mpspec.h are not found on the cpp path.
298 // This could be a bug in arch/x86/Makefile that names
299 // mflags-y += -Iinclude/asm-x86/mach-default
300 // but that path does not exist in an O= build tree.
301 o << module_cflags << " += -Iinclude2/asm/mach-default" << endl;
302 if (s.kernel_source_tree != "")
303 o << module_cflags << " += -I" + s.kernel_source_tree << endl;
304
305 // NB: don't try
306 // o << module_cflags << " += -Iusr/include" << endl;
307 // since such headers are cleansed of _KERNEL_ pieces that we need
308
309 o << "STAPCONF_HEADER := " << s.tmpdir << "/" << s.stapconf_name << endl;
310 o << "$(STAPCONF_HEADER):" << endl;
311 o << "\t@echo -n > $@" << endl;
312 output_autoconf(s, o, "autoconf-hrtimer-rel.c", "STAPCONF_HRTIMER_REL", NULL);
313 output_autoconf(s, o, "autoconf-generated-compile.c", "STAPCONF_GENERATED_COMPILE", NULL);
314 output_autoconf(s, o, "autoconf-hrtimer-getset-expires.c", "STAPCONF_HRTIMER_GETSET_EXPIRES", NULL);
315 output_autoconf(s, o, "autoconf-inode-private.c", "STAPCONF_INODE_PRIVATE", NULL);
316 output_autoconf(s, o, "autoconf-constant-tsc.c", "STAPCONF_CONSTANT_TSC", NULL);
317 output_autoconf(s, o, "autoconf-ktime-get-real.c", "STAPCONF_KTIME_GET_REAL", NULL);
318 output_autoconf(s, o, "autoconf-x86-uniregs.c", "STAPCONF_X86_UNIREGS", NULL);
319 output_autoconf(s, o, "autoconf-nameidata.c", "STAPCONF_NAMEIDATA_CLEANUP", NULL);
320 output_dual_exportconf(s, o, "unregister_kprobes", "unregister_kretprobes", "STAPCONF_UNREGISTER_KPROBES");
321 output_autoconf(s, o, "autoconf-kprobe-symbol-name.c", "STAPCONF_KPROBE_SYMBOL_NAME", NULL);
322 output_autoconf(s, o, "autoconf-real-parent.c", "STAPCONF_REAL_PARENT", NULL);
323 output_autoconf(s, o, "autoconf-uaccess.c", "STAPCONF_LINUX_UACCESS_H", NULL);
324 output_autoconf(s, o, "autoconf-oneachcpu-retry.c", "STAPCONF_ONEACHCPU_RETRY", NULL);
325 output_autoconf(s, o, "autoconf-dpath-path.c", "STAPCONF_DPATH_PATH", NULL);
326 output_exportconf(s, o, "synchronize_sched", "STAPCONF_SYNCHRONIZE_SCHED");
327 output_autoconf(s, o, "autoconf-task-uid.c", "STAPCONF_TASK_UID", NULL);
328 output_exportconf(s, o, "from_kuid_munged", "STAPCONF_FROM_KUID_MUNGED");
329 output_dual_exportconf(s, o, "alloc_vm_area", "free_vm_area", "STAPCONF_VM_AREA");
330 output_autoconf(s, o, "autoconf-procfs-owner.c", "STAPCONF_PROCFS_OWNER", NULL);
331 output_autoconf(s, o, "autoconf-alloc-percpu-align.c", "STAPCONF_ALLOC_PERCPU_ALIGN", NULL);
332 output_autoconf(s, o, "autoconf-x86-gs.c", "STAPCONF_X86_GS", NULL);
333 output_autoconf(s, o, "autoconf-grsecurity.c", "STAPCONF_GRSECURITY", NULL);
334 output_autoconf(s, o, "autoconf-trace-printk.c", "STAPCONF_TRACE_PRINTK", NULL);
335 output_autoconf(s, o, "autoconf-regset.c", "STAPCONF_REGSET", NULL);
336 output_autoconf(s, o, "autoconf-utrace-regset.c", "STAPCONF_UTRACE_REGSET", NULL);
337 output_autoconf(s, o, "autoconf-uprobe-get-pc.c", "STAPCONF_UPROBE_GET_PC", NULL);
338 output_autoconf(s, o, "autoconf-hlist-4args.c", "STAPCONF_HLIST_4ARGS", NULL);
339 output_exportconf(s, o, "tsc_khz", "STAPCONF_TSC_KHZ");
340 output_exportconf(s, o, "cpu_khz", "STAPCONF_CPU_KHZ");
341 output_exportconf(s, o, "__module_text_address", "STAPCONF_MODULE_TEXT_ADDRESS");
342 output_exportconf(s, o, "add_timer_on", "STAPCONF_ADD_TIMER_ON");
343
344 output_dual_exportconf(s, o, "probe_kernel_read", "probe_kernel_write", "STAPCONF_PROBE_KERNEL");
345 output_autoconf(s, o, "autoconf-hw_breakpoint_context.c",
346 "STAPCONF_HW_BREAKPOINT_CONTEXT", NULL);
347 output_autoconf(s, o, "autoconf-save-stack-trace.c",
348 "STAPCONF_KERNEL_STACKTRACE", NULL);
349 output_autoconf(s, o, "autoconf-save-stack-trace-no-bp.c",
350 "STAPCONF_KERNEL_STACKTRACE_NO_BP", NULL);
351 output_autoconf(s, o, "autoconf-asm-syscall.c",
352 "STAPCONF_ASM_SYSCALL_H", NULL);
353 output_autoconf(s, o, "autoconf-ring_buffer-flags.c", "STAPCONF_RING_BUFFER_FLAGS", NULL);
354 output_autoconf(s, o, "autoconf-ring_buffer_lost_events.c", "STAPCONF_RING_BUFFER_LOST_EVENTS", NULL);
355 output_autoconf(s, o, "autoconf-ring_buffer_read_prepare.c", "STAPCONF_RING_BUFFER_READ_PREPARE", NULL);
356 output_autoconf(s, o, "autoconf-kallsyms-on-each-symbol.c", "STAPCONF_KALLSYMS_ON_EACH_SYMBOL", NULL);
357 output_autoconf(s, o, "autoconf-walk-stack.c", "STAPCONF_WALK_STACK", NULL);
358 output_autoconf(s, o, "autoconf-stacktrace_ops-warning.c",
359 "STAPCONF_STACKTRACE_OPS_WARNING", NULL);
360 output_autoconf(s, o, "autoconf-mm-context-vdso.c", "STAPCONF_MM_CONTEXT_VDSO", NULL);
361 output_autoconf(s, o, "autoconf-mm-context-vdso-base.c", "STAPCONF_MM_CONTEXT_VDSO_BASE", NULL);
362 output_autoconf(s, o, "autoconf-blk-types.c", "STAPCONF_BLK_TYPES", NULL);
363 output_autoconf(s, o, "autoconf-perf-structpid.c", "STAPCONF_PERF_STRUCTPID", NULL);
364 output_autoconf(s, o, "perf_event_counter_context.c",
365 "STAPCONF_PERF_COUNTER_CONTEXT", NULL);
366 output_autoconf(s, o, "perf_probe_handler_nmi.c",
367 "STAPCONF_PERF_HANDLER_NMI", NULL);
368 output_exportconf(s, o, "path_lookup", "STAPCONF_PATH_LOOKUP");
369 output_exportconf(s, o, "kern_path_parent", "STAPCONF_KERN_PATH_PARENT");
370 output_exportconf(s, o, "vfs_path_lookup", "STAPCONF_VFS_PATH_LOOKUP");
371 output_exportconf(s, o, "kern_path", "STAPCONF_KERN_PATH");
372 output_exportconf(s, o, "proc_create_data", "STAPCONF_PROC_CREATE_DATA");
373 output_exportconf(s, o, "PDE_DATA", "STAPCONF_PDE_DATA");
374 output_autoconf(s, o, "autoconf-module-sect-attrs.c", "STAPCONF_MODULE_SECT_ATTRS", NULL);
375
376 output_autoconf(s, o, "autoconf-utrace-via-tracepoints.c", "STAPCONF_UTRACE_VIA_TRACEPOINTS", NULL);
377 output_autoconf(s, o, "autoconf-task_work-struct.c", "STAPCONF_TASK_WORK_STRUCT", NULL);
378 output_autoconf(s, o, "autoconf-vm-area-pte.c", "STAPCONF_VM_AREA_PTE", NULL);
379 output_autoconf(s, o, "autoconf-relay-umode_t.c", "STAPCONF_RELAY_UMODE_T", NULL);
380 output_autoconf(s, o, "autoconf-fs_supers-hlist.c", "STAPCONF_FS_SUPERS_HLIST", NULL);
381 output_autoconf(s, o, "autoconf-compat_sigaction.c", "STAPCONF_COMPAT_SIGACTION", NULL);
382 output_autoconf(s, o, "autoconf-netfilter.c", "STAPCONF_NETFILTER_V313", NULL);
383
384 // used by tapset/timestamp_monotonic.stp
385 output_exportconf(s, o, "cpu_clock", "STAPCONF_CPU_CLOCK");
386 output_exportconf(s, o, "local_clock", "STAPCONF_LOCAL_CLOCK");
387
388 // used by runtime/uprobe-inode.c
389 output_either_exportconf(s, o, "uprobe_register", "register_uprobe",
390 "STAPCONF_UPROBE_REGISTER_EXPORTED");
391 output_either_exportconf(s, o, "uprobe_unregister", "unregister_uprobe",
392 "STAPCONF_UPROBE_UNREGISTER_EXPORTED");
393 output_autoconf(s, o, "autoconf-old-inode-uprobes.c", "STAPCONF_OLD_INODE_UPROBES", NULL);
394 output_autoconf(s, o, "autoconf-inode-uretprobes.c", "STAPCONF_INODE_URETPROBES", NULL);
395
396 // used by tapsets.cxx inode uprobe generated code
397 output_exportconf(s, o, "uprobe_get_swbp_addr", "STAPCONF_UPROBE_GET_SWBP_ADDR_EXPORTED");
398
399 // used by runtime/loc2c-runtime.h
400 output_exportconf(s, o, "task_user_regset_view", "STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED");
401
402 // used by runtime/stp_utrace.c
403 output_exportconf(s, o, "task_work_add", "STAPCONF_TASK_WORK_ADD_EXPORTED");
404 output_exportconf(s, o, "signal_wake_up_state", "STAPCONF_SIGNAL_WAKE_UP_STATE_EXPORTED");
405 output_exportconf(s, o, "signal_wake_up", "STAPCONF_SIGNAL_WAKE_UP_EXPORTED");
406 output_exportconf(s, o, "__lock_task_sighand", "STAPCONF___LOCK_TASK_SIGHAND_EXPORTED");
407
408 output_autoconf(s, o, "autoconf-pagefault_disable.c", "STAPCONF_PAGEFAULT_DISABLE", NULL);
409 output_exportconf(s, o, "kallsyms_lookup_name", "STAPCONF_KALLSYMS");
410 output_autoconf(s, o, "autoconf-uidgid.c", "STAPCONF_LINUX_UIDGID_H", NULL);
411 output_exportconf(s, o, "sigset_from_compat", "STAPCONF_SIGSET_FROM_COMPAT_EXPORTED");
412 output_exportconf(s, o, "vzalloc", "STAPCONF_VZALLOC");
413 output_exportconf(s, o, "vzalloc_node", "STAPCONF_VZALLOC_NODE");
414 output_exportconf(s, o, "vmalloc_node", "STAPCONF_VMALLOC_NODE");
415
416 o << module_cflags << " += -include $(STAPCONF_HEADER)" << endl;
417
418 for (unsigned i=0; i<s.c_macros.size(); i++)
419 o << "EXTRA_CFLAGS += -D " << lex_cast_qstring(s.c_macros[i]) << endl; // XXX right quoting?
420
421 if (s.verbose > 3)
422 o << "EXTRA_CFLAGS += -ftime-report -Q" << endl;
423
424 // XXX: unfortunately, -save-temps can't work since linux kbuild cwd
425 // is not writable.
426 //
427 // if (s.keep_tmpdir)
428 // o << "CFLAGS += -fverbose-asm -save-temps" << endl;
429
430 // Kernels can be compiled with CONFIG_CC_OPTIMIZE_FOR_SIZE to select
431 // -Os, otherwise -O2 is the default.
432 o << "EXTRA_CFLAGS += -freorder-blocks" << endl; // improve on -Os
433
434 // We used to allow the user to override default optimization when so
435 // requested by adding a -O[0123s] so they could determine the
436 // time/space/speed tradeoffs themselves, but we cannot guantantee that
437 // the (un)optimized code actually compiles and/or generates functional
438 // code, so we had to remove it.
439 // o << "EXTRA_CFLAGS += " << s.gcc_flags << endl; // Add -O[0123s]
440
441 // o << "CFLAGS += -fno-unit-at-a-time" << endl;
442
443 // 256^W512 bytes should be enough for anybody
444 // XXX this doesn't validate varargs, per gcc bug #41633
445 o << "EXTRA_CFLAGS += $(call cc-option,-Wframe-larger-than=512)" << endl;
446
447 // Assumes linux 2.6 kbuild
448 o << "EXTRA_CFLAGS += -Wno-unused" << (s.omit_werror ? "" : " -Werror") << endl;
449 #if CHECK_POINTER_ARITH_PR5947
450 o << "EXTRA_CFLAGS += -Wpointer-arith" << endl;
451 #endif
452 o << "EXTRA_CFLAGS += -I\"" << s.runtime_path << "\"" << endl;
453 // XXX: this may help ppc toc overflow
454 // o << "CFLAGS := $(subst -Os,-O2,$(CFLAGS)) -fminimal-toc" << endl;
455 o << "obj-m := " << s.module_name << ".o" << endl;
456
457 // print out all the auxiliary source (->object) file names
458 o << s.module_name << "-y := ";
459 for (unsigned i=0; i<s.auxiliary_outputs.size(); i++)
460 {
461 string srcname = s.auxiliary_outputs[i]->filename;
462 assert (srcname != "" && srcname.rfind('/') != string::npos);
463 string objname = srcname.substr(srcname.rfind('/')+1); // basename
464 assert (objname != "" && objname[objname.size()-1] == 'c');
465 objname[objname.size()-1] = 'o'; // now objname
466 o << " " + objname;
467 }
468 // and once again, for the translated_source file. It can't simply
469 // be named MODULENAME.c, since kbuild doesn't allow a foo.ko file
470 // consisting of multiple .o's to have foo.o/foo.c as a source.
471 // (It uses ld -r -o foo.o EACH.o EACH.o).
472 {
473 string srcname = s.translated_source;
474 assert (srcname != "" && srcname.rfind('/') != string::npos);
475 string objname = srcname.substr(srcname.rfind('/')+1); // basename
476 assert (objname != "" && objname[objname.size()-1] == 'c');
477 objname[objname.size()-1] = 'o'; // now objname
478 o << " " + objname;
479 }
480 o << endl;
481
482 // add all stapconf dependencies
483 o << s.translated_source << ": $(STAPCONF_HEADER)" << endl;
484 for (unsigned i=0; i<s.auxiliary_outputs.size(); i++)
485 o << s.auxiliary_outputs[i]->filename << ": $(STAPCONF_HEADER)" << endl;
486
487
488 o.close ();
489
490 // Generate module directory pathname and make sure it exists.
491 string module_dir = s.kernel_build_tree;
492 string module_dir_makefile = module_dir + "/Makefile";
493 struct stat st;
494 rc = stat(module_dir_makefile.c_str(), &st);
495 if (rc != 0)
496 {
497 clog << _F("Checking \" %s \" failed with error: %s\nEnsure kernel development headers & makefiles are installed.",
498 module_dir_makefile.c_str(), strerror(errno)) << endl;
499 s.set_try_server ();
500 return rc;
501 }
502
503 // Run make
504 vector<string> make_cmd = make_make_cmd(s, s.tmpdir);
505 rc = run_make_cmd(s, make_cmd);
506 if (rc)
507 s.set_try_server ();
508 return rc;
509 }
510
511 /*
512 * If uprobes was built as part of the kernel build (either built-in
513 * or as a module), the uprobes exports should show up. This is to be
514 * as distinct from the stap-built uprobes.ko from the runtime.
515 */
516 static bool
517 kernel_built_uprobes (systemtap_session& s)
518 {
519 if (s.runtime_usermode_p())
520 return true; // sort of, via dyninst
521
522 // see also tapsets.cxx:kernel_supports_inode_uprobes()
523 return ((s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y" && s.kernel_config["CONFIG_UPROBES"] == "y") ||
524 (s.kernel_exports.find("unregister_uprobe") != s.kernel_exports.end()));
525 }
526
527 static int
528 make_uprobes (systemtap_session& s)
529 {
530 if (s.verbose > 1)
531 clog << _("Pass 4, preamble: (re)building SystemTap's version of uprobes.")
532 << endl;
533
534 // create a subdirectory for the uprobes module
535 string dir(s.tmpdir + "/uprobes");
536 if (create_dir(dir.c_str()) != 0)
537 {
538 s.print_warning("failed to create directory for build uprobes.");
539 s.set_try_server ();
540 return 1;
541 }
542
543 // create a simple Makefile
544 string makefile(dir + "/Makefile");
545 ofstream omf(makefile.c_str());
546 omf << "obj-m := uprobes.o" << endl;
547 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
548 omf << "CONFIG_MODULE_SIG := n" << endl;
549 omf.close();
550
551 // create a simple #include-chained source file
552 string runtimesourcefile(s.runtime_path + "/linux/uprobes/uprobes.c");
553 string sourcefile(dir + "/uprobes.c");
554 ofstream osrc(sourcefile.c_str());
555 osrc << "#include \"" << runtimesourcefile << "\"" << endl;
556
557 // pass --modinfo k=v to uprobes build too
558 for (unsigned i = 0; i < s.modinfos.size(); i++)
559 {
560 const string& mi = s.modinfos[i];
561 size_t loc = mi.find('=');
562 string tag = mi.substr (0, loc);
563 string value = mi.substr (loc+1);
564 osrc << "MODULE_INFO(" << tag << "," << lex_cast_qstring(value) << ");" << endl;
565 }
566
567 osrc.close();
568
569 // make the module
570 vector<string> make_cmd = make_make_cmd(s, dir);
571 int rc = run_make_cmd(s, make_cmd);
572 if (!rc && !copy_file(dir + "/Module.symvers",
573 s.tmpdir + "/Module.symvers"))
574 rc = -1;
575
576 if (s.verbose > 1)
577 clog << _("uprobes rebuild exit code: ") << rc << endl;
578 if (rc)
579 s.set_try_server ();
580 else
581 s.uprobes_path = dir + "/uprobes.ko";
582 return rc;
583 }
584
585 static bool
586 get_cached_uprobes(systemtap_session& s)
587 {
588 s.uprobes_hash = s.use_cache ? find_uprobes_hash(s) : "";
589 if (!s.uprobes_hash.empty())
590 {
591 // NB: We always put uprobes.ko in its own directory, especially so
592 // stap-serverd can more easily locate it.
593 string dir(s.tmpdir + "/uprobes");
594 if (create_dir(dir.c_str()) != 0)
595 return false;
596
597 string cacheko = s.uprobes_hash + ".ko";
598 string tmpko = dir + "/uprobes.ko";
599
600 // The symvers file still needs to go in the script module's directory.
601 string cachesyms = s.uprobes_hash + ".symvers";
602 string tmpsyms = s.tmpdir + "/Module.symvers";
603
604 if (get_file_size(cacheko) > 0 && copy_file(cacheko, tmpko) &&
605 get_file_size(cachesyms) > 0 && copy_file(cachesyms, tmpsyms))
606 {
607 s.uprobes_path = tmpko;
608 return true;
609 }
610 }
611 return false;
612 }
613
614 static void
615 set_cached_uprobes(systemtap_session& s)
616 {
617 if (s.use_cache && !s.uprobes_hash.empty())
618 {
619 string cacheko = s.uprobes_hash + ".ko";
620 string tmpko = s.tmpdir + "/uprobes/uprobes.ko";
621 copy_file(tmpko, cacheko);
622
623 string cachesyms = s.uprobes_hash + ".symvers";
624 string tmpsyms = s.tmpdir + "/uprobes/Module.symvers";
625 copy_file(tmpsyms, cachesyms);
626 }
627 }
628
629 int
630 uprobes_pass (systemtap_session& s)
631 {
632 if (!s.need_uprobes || kernel_built_uprobes(s))
633 return 0;
634
635 if (s.kernel_config["CONFIG_UTRACE"] != string("y"))
636 {
637 clog << _("user-space process-tracking facilities not available [man error::process-tracking]") << endl;
638 s.set_try_server ();
639 return 1;
640 }
641
642 /*
643 * We need to use the version of uprobes that comes with SystemTap. Try to
644 * get it from the cache first. If not found, build it and try to save it to
645 * the cache for future reuse.
646 */
647 int rc = 0;
648 if (!get_cached_uprobes(s))
649 {
650 rc = make_uprobes(s);
651 if (!rc)
652 set_cached_uprobes(s);
653 }
654 if (rc)
655 s.set_try_server ();
656 return rc;
657 }
658
659 static
660 vector<string>
661 make_dyninst_run_command (systemtap_session& s, const string& remotedir,
662 const string& version)
663 {
664 vector<string> cmd;
665 cmd.push_back(getenv("SYSTEMTAP_STAPDYN") ?: BINDIR "/stapdyn");
666
667 // use slightly less verbosity
668 for (unsigned i=1; i<s.verbose; i++)
669 cmd.push_back("-v");
670 if (s.suppress_warnings)
671 cmd.push_back("-w");
672
673 if (!s.cmd.empty())
674 {
675 cmd.push_back("-c");
676 cmd.push_back(s.cmd);
677 }
678
679 if (s.target_pid)
680 {
681 cmd.push_back("-x");
682 cmd.push_back(lex_cast(s.target_pid));
683 }
684
685 if (!s.output_file.empty())
686 {
687 cmd.push_back("-o");
688 cmd.push_back(s.output_file);
689 }
690
691 if (s.color_mode != s.color_auto)
692 {
693 cmd.push_back("-C");
694 if (s.color_mode == s.color_always)
695 cmd.push_back("always");
696 else
697 cmd.push_back("never");
698 }
699
700 cmd.push_back((remotedir.empty() ? s.tmpdir : remotedir)
701 + "/" + s.module_filename());
702
703 // add module arguments
704 cmd.insert(cmd.end(), s.globalopts.begin(), s.globalopts.end());
705
706 return cmd;
707 }
708
709 vector<string>
710 make_run_command (systemtap_session& s, const string& remotedir,
711 const string& version)
712 {
713 if (s.runtime_usermode_p())
714 return make_dyninst_run_command(s, remotedir, version);
715
716 // for now, just spawn staprun
717 vector<string> staprun_cmd;
718 staprun_cmd.push_back(getenv("SYSTEMTAP_STAPRUN") ?: BINDIR "/staprun");
719
720 // use slightly less verbosity
721 for (unsigned i=1; i<s.verbose; i++)
722 staprun_cmd.push_back("-v");
723 if (s.suppress_warnings)
724 staprun_cmd.push_back("-w");
725
726 if (!s.output_file.empty())
727 {
728 staprun_cmd.push_back("-o");
729 staprun_cmd.push_back(s.output_file);
730 }
731
732 if (!s.cmd.empty())
733 {
734 staprun_cmd.push_back("-c");
735 staprun_cmd.push_back(s.cmd);
736 }
737
738 if (s.target_pid)
739 {
740 staprun_cmd.push_back("-t");
741 staprun_cmd.push_back(lex_cast(s.target_pid));
742 }
743
744 if (s.buffer_size)
745 {
746 staprun_cmd.push_back("-b");
747 staprun_cmd.push_back(lex_cast(s.buffer_size));
748 }
749
750 if (s.need_uprobes && !kernel_built_uprobes(s))
751 {
752 string opt_u = "-u";
753 if (!s.uprobes_path.empty() &&
754 strverscmp("1.4", version.c_str()) <= 0)
755 {
756 if (remotedir.empty())
757 opt_u.append(s.uprobes_path);
758 else
759 opt_u.append(remotedir + "/" + basename(s.uprobes_path.c_str()));
760 }
761 staprun_cmd.push_back(opt_u);
762 }
763
764 if (s.load_only)
765 staprun_cmd.push_back(s.output_file.empty() ? "-L" : "-D");
766
767 // Note that if this system requires signed modules, we can't rename
768 // it after it has been signed.
769 if (!s.modname_given && (strverscmp("1.6", version.c_str()) <= 0)
770 && s.mok_fingerprints.empty())
771 staprun_cmd.push_back("-R");
772
773 if (!s.size_option.empty())
774 {
775 staprun_cmd.push_back("-S");
776 staprun_cmd.push_back(s.size_option);
777 }
778
779 if (s.color_mode != s.color_auto)
780 {
781 staprun_cmd.push_back("-C");
782 if (s.color_mode == s.color_always)
783 staprun_cmd.push_back("always");
784 else
785 staprun_cmd.push_back("never");
786 }
787
788 staprun_cmd.push_back((remotedir.empty() ? s.tmpdir : remotedir)
789 + "/" + s.module_filename());
790
791 // add module arguments
792 staprun_cmd.insert(staprun_cmd.end(),
793 s.globalopts.begin(), s.globalopts.end());
794
795 return staprun_cmd;
796 }
797
798
799 // Build tiny kernel modules to query tracepoints.
800 // Given a (header-file -> test-contents) map, compile them ASAP, and return
801 // a (header-file -> obj-filename) map.
802
803 map<string,string>
804 make_tracequeries(systemtap_session& s, const map<string,string>& contents)
805 {
806 static unsigned tick = 0;
807 string basename("tracequery_kmod_" + lex_cast(++tick));
808 map<string,string> objs;
809
810 // create a subdirectory for the module
811 string dir(s.tmpdir + "/" + basename);
812 if (create_dir(dir.c_str()) != 0)
813 {
814 s.print_warning("failed to create directory for querying tracepoints.");
815 s.set_try_server ();
816 return objs;
817 }
818
819 // create a simple Makefile
820 string makefile(dir + "/Makefile");
821 ofstream omf(makefile.c_str());
822 // force debuginfo generation, and relax implicit functions
823 omf << "EXTRA_CFLAGS := -g -Wno-implicit-function-declaration" << (s.omit_werror ? "" : " -Werror") << endl;
824 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
825 omf << "CONFIG_MODULE_SIG := n" << endl;
826
827 if (s.kernel_source_tree != "")
828 omf << "EXTRA_CFLAGS += -I" + s.kernel_source_tree << endl;
829
830 omf << "obj-m := " << endl;
831 // write out each header-specific source file into a separate file
832 for (map<string,string>::const_iterator it = contents.begin(); it != contents.end(); it++)
833 {
834 string sbasename = basename + "_" + lex_cast(++tick); // suffixed
835
836 // write out source code
837 string srcname = dir + "/" + sbasename + ".c";
838 string src = it->second;
839 ofstream osrc(srcname.c_str());
840 osrc << src;
841 osrc.close();
842
843 if (s.verbose > 2)
844 clog << _F("Processing tracepoint header %s with query %s",
845 it->first.c_str(), srcname.c_str())
846 << endl;
847
848 // arrange to build it
849 omf << "obj-m += " + sbasename + ".o" << endl; // NB: without <dir> prefix
850 objs[it->first] = dir + "/" + sbasename + ".o";
851 }
852 omf.close();
853
854 // make the module
855 vector<string> make_cmd = make_make_objs_cmd(s, dir);
856 make_cmd.push_back ("-i"); // ignore errors, give rc 0 even in case of tracepoint header nits
857 bool quiet = (s.verbose < 4);
858 int rc = run_make_cmd(s, make_cmd, quiet, quiet);
859 if (rc)
860 s.set_try_server ();
861
862 // Sometimes we fail a tracequery due to PR9993 / PR11649 type
863 // kernel trace header problems. In this case, due to PR12729, we
864 // used to get a lovely "Warning: make exited with status: 2" but no
865 // other useful diagnostic. -vvvv would let a user see what's up,
866 // but the user can't fix the problem even with that.
867
868 return objs;
869 }
870
871
872 // Build a tiny kernel module to query type information
873 static int
874 make_typequery_kmod(systemtap_session& s, const vector<string>& headers, string& name)
875 {
876 static unsigned tick = 0;
877 string basename("typequery_kmod_" + lex_cast(++tick));
878
879 // create a subdirectory for the module
880 string dir(s.tmpdir + "/" + basename);
881 if (create_dir(dir.c_str()) != 0)
882 {
883 s.print_warning("failed to create directory for querying types.");
884 s.set_try_server ();
885 return 1;
886 }
887
888 name = dir + "/" + basename + ".ko";
889
890 // create a simple Makefile
891 string makefile(dir + "/Makefile");
892 ofstream omf(makefile.c_str());
893 omf << "EXTRA_CFLAGS := -g -fno-eliminate-unused-debug-types" << endl;
894
895 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
896 omf << "CONFIG_MODULE_SIG := n" << endl;
897
898 // NB: We use -include instead of #include because that gives us more power.
899 // Using #include searches relative to the source's path, which in this case
900 // is /tmp/..., so that's not helpful. Using -include will search relative
901 // to the cwd, which will be the kernel build root. This means if you have a
902 // full kernel build tree, it's possible to get at types that aren't in the
903 // normal include path, e.g.:
904 // @cast(foo, "bsd_acct_struct", "kernel<kernel/acct.c>")->...
905 omf << "CFLAGS_" << basename << ".o :=";
906 for (size_t i = 0; i < headers.size(); ++i)
907 omf << " -include " << lex_cast_qstring(headers[i]); // XXX right quoting?
908 omf << endl;
909
910 omf << "obj-m := " + basename + ".o" << endl;
911 omf.close();
912
913 // create our empty source file
914 string source(dir + "/" + basename + ".c");
915 ofstream osrc(source.c_str());
916 osrc.close();
917
918 // make the module
919 vector<string> make_cmd = make_make_cmd(s, dir);
920 bool quiet = (s.verbose < 4);
921 int rc = run_make_cmd(s, make_cmd, quiet, quiet);
922 if (rc)
923 s.set_try_server ();
924 return rc;
925 }
926
927
928 // Build a tiny user module to query type information
929 static int
930 make_typequery_umod(systemtap_session& s, const vector<string>& headers, string& name)
931 {
932 static unsigned tick = 0;
933
934 name = s.tmpdir + "/typequery_umod_" + lex_cast(++tick) + ".so";
935
936 // make the module
937 //
938 // NB: As with kmod, using -include makes relative paths more useful. The
939 // cwd in this case will be the cwd of stap itself though, which may be
940 // trickier to deal with. It might be better to "cd `dirname $script`"
941 // first...
942 vector<string> cmd;
943 cmd.push_back("gcc");
944 cmd.push_back("-shared");
945 cmd.push_back("-g");
946 cmd.push_back("-fno-eliminate-unused-debug-types");
947 cmd.push_back("-xc");
948 cmd.push_back("/dev/null");
949 cmd.push_back("-o");
950 cmd.push_back(name);
951 for (size_t i = 0; i < headers.size(); ++i)
952 {
953 cmd.push_back("-include");
954 cmd.push_back(headers[i]);
955 }
956 bool quiet = (s.verbose < 4);
957 int rc = stap_system (s.verbose, cmd, quiet, quiet);
958 if (rc)
959 s.set_try_server ();
960 return rc;
961 }
962
963
964 int
965 make_typequery(systemtap_session& s, string& module)
966 {
967 int rc;
968 string new_module;
969 vector<string> headers;
970 bool kernel = startswith(module, "kernel");
971
972 for (size_t end, i = kernel ? 6 : 0; i < module.size(); i = end + 1)
973 {
974 if (module[i] != '<')
975 return -1;
976 end = module.find('>', ++i);
977 if (end == string::npos)
978 return -1;
979 string header = module.substr(i, end - i);
980 vector<string> matches;
981 if (regexp_match(header, "^[a-zA-Z0-9/_.+-]+$", matches))
982 s.print_warning("skipping malformed @cast header \""+ header + "\"");
983 else
984 headers.push_back(header);
985 }
986 if (headers.empty())
987 return -1;
988
989 if (kernel)
990 rc = make_typequery_kmod(s, headers, new_module);
991 else
992 rc = make_typequery_umod(s, headers, new_module);
993
994 if (!rc)
995 module = new_module;
996
997 return rc;
998 }
999
1000 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.096969 seconds and 6 git commands to generate.