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