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