2 // Copyright (C) 2005-2016 Red Hat Inc.
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
14 #include "translate.h"
26 #include <sys/types.h>
31 #include <sys/resource.h>
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")
41 /* Adjust and run make_cmd to build a kernel module. */
43 run_make_cmd(systemtap_session
& s
, vector
<string
>& make_cmd
,
44 bool null_out
=false, bool null_err
=false)
46 assert_no_interrupts();
48 // PR14168: we used to unsetenv values here; instead do it via
49 // env(1) in make_any_make_cmd().
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);
58 make_cmd
.push_back("V=1");
59 else if (s
.verbose
> 1)
60 make_cmd
.push_back("--no-print-directory");
63 make_cmd
.push_back("-s");
64 make_cmd
.push_back("--no-print-directory");
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
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));
79 if (strverscmp (s
.kernel_base_release
.c_str(), "2.6.29") < 0)
81 // Older kernels, before linux commit #fd54f502841c1, include
82 // gratuitous "echo"s in their Makefile. We need to suppress
83 // that with this bluntness.
87 int rc
= stap_system (s
.verbose
, "kbuild", make_cmd
, null_out
, null_err
);
94 make_any_make_cmd(systemtap_session
& s
, const string
& dir
, const string
& target
)
96 string
newpath("PATH=/usr/bin:/bin");
97 const char *oldpath
= getenv("PATH");
104 vector
<string
> make_cmd
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
,
110 "make", "-C", s
.kernel_build_tree
,
111 "M=" + dir
, // need make-quoting?
114 // PR13847: suppress debuginfo creation by default
115 "CONFIG_DEBUG_INFO=",
117 // RHBZ1321628: suppress stack validation; expected to be temporary
118 "CONFIG_STACK_VALIDATION=",
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?
126 // Add any custom kbuild flags
127 make_cmd
.insert(make_cmd
.end(), s
.kbuildflags
.begin(), s
.kbuildflags
.end());
132 static vector
<string
>
133 make_make_cmd(systemtap_session
& s
, const string
& dir
)
135 return make_any_make_cmd(s
, dir
, "modules");
138 static vector
<string
>
139 make_make_objs_cmd(systemtap_session
& s
, const string
& dir
)
141 // Kbuild uses these rules to build external modules:
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
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
);
154 output_autoconf(systemtap_session
& s
, ofstream
& o
, const char *autoconf_c
,
155 const char *deftrue
, const char *deffalse
)
160 o
<< "if $(CHECK_BUILD) $(SYSTEMTAP_RUNTIME)/linux/" << autoconf_c
;
162 o
<< " > /dev/null 2>&1";
165 o
<< "echo \"#define " << deftrue
<< " 1\"";
167 o
<< "; else echo \"#define " << deffalse
<< " 1\"";
168 o
<< "; fi >> $@" << endl
;
172 void output_exportconf(systemtap_session
& s
, ofstream
& o
, const char *symbol
,
178 if (s
.kernel_exports
.find(symbol
) != s
.kernel_exports
.end())
179 o
<< "echo \"#define " << deftrue
<< " 1\"";
180 o
<< ">> $@" << endl
;
184 void output_dual_exportconf(systemtap_session
& s
, ofstream
& o
,
185 const char *symbol1
, const char *symbol2
,
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
;
198 void output_either_exportconf(systemtap_session
& s
, ofstream
& o
,
199 const char *symbol1
, const char *symbol2
,
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
;
213 compile_dyninst (systemtap_session
& s
)
215 const string module
= s
.tmpdir
+ "/" + s
.module_filename();
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",
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");
233 for (size_t i
= 0; i
< s
.c_macros
.size(); ++i
)
234 cmd
.push_back("-D" + s
.c_macros
[i
]);
237 cmd
.insert(cmd
.end(), { "-ftime-report", "-Q" });
239 // Add any custom kbuild flags
240 cmd
.insert(cmd
.end(), s
.kbuildflags
.begin(), s
.kbuildflags
.end());
242 int rc
= stap_system (s
.verbose
, cmd
);
250 compile_pass (systemtap_session
& s
)
252 if (s
.runtime_usermode_p())
253 return compile_dyninst (s
);
255 int rc
= uprobes_pass (s
);
262 // fill in a quick Makefile
263 string makefile_nm
= s
.tmpdir
+ "/Makefile";
264 ofstream
o (makefile_nm
.c_str());
268 // Clever hacks copied from vmware modules
271 superverbose
= "set -x;";
273 string redirecterrors
= "> /dev/null 2>&1";
277 // Support O= (or KBUILD_OUTPUT) option
278 o
<< "_KBUILD_CFLAGS := $(call flags,KBUILD_CFLAGS)" << endl
;
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
;
290 o
<< "SYSTEMTAP_RUNTIME = \"" << s
.runtime_path
<< "\"" << endl
;
292 // "autoconf" options go here
294 // RHBZ 543529: early rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
295 o
<< "CONFIG_MODULE_SIG := n" << endl
;
297 string module_cflags
= "EXTRA_CFLAGS";
298 o
<< module_cflags
<< " :=" << endl
;
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
;
313 // o << module_cflags << " += -Iusr/include" << endl;
314 // since such headers are cleansed of _KERNEL_ pieces that we need
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");
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
);
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
);
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");
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
);
414 // used by tapsets.cxx inode uprobe generated code
415 output_exportconf(s
, o
, "uprobe_get_swbp_addr", "STAPCONF_UPROBE_GET_SWBP_ADDR_EXPORTED");
417 // used by runtime/loc2c-runtime.h
418 output_exportconf(s
, o
, "task_user_regset_view", "STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED");
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");
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");
436 // RHBZ1233912 - s390 temporary workaround for non-atomic udelay()
437 output_exportconf(s
, o
, "udelay_simple", "STAPCONF_UDELAY_SIMPLE");
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
);
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");
448 o
<< module_cflags
<< " += -include $(STAPCONF_HEADER)" << endl
;
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?
454 o
<< "EXTRA_CFLAGS += -ftime-report -Q" << endl
;
456 // XXX: unfortunately, -save-temps can't work since linux kbuild cwd
459 // if (s.keep_tmpdir)
460 // o << "CFLAGS += -fverbose-asm -save-temps" << endl;
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
466 // Generate eh_frame for self-backtracing
467 o
<< "EXTRA_CFLAGS += -fasynchronous-unwind-tables" << endl
;
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]
476 // o << "CFLAGS += -fno-unit-at-a-time" << endl;
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
;
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
;
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
;
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
;
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
++)
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
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).
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
519 // and once again, for the trailer type auxiliary outputs.
520 for (unsigned i
=0; i
<s
.auxiliary_outputs
.size(); i
++)
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
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
;
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";
544 rc
= stat(module_dir_makefile
.c_str(), &st
);
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
;
554 vector
<string
> make_cmd
= make_make_cmd(s
, s
.tmpdir
);
555 rc
= run_make_cmd(s
, make_cmd
);
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.
567 kernel_built_uprobes (systemtap_session
& s
)
569 if (s
.runtime_usermode_p())
570 return true; // sort of, via dyninst
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()));
578 make_uprobes (systemtap_session
& s
)
581 clog
<< _("Pass 4, preamble: (re)building SystemTap's version of uprobes.")
584 // create a subdirectory for the uprobes module
585 string
dir(s
.tmpdir
+ "/uprobes");
586 if (create_dir(dir
.c_str()) != 0)
588 s
.print_warning("failed to create directory for build uprobes.");
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
;
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
;
607 // pass --modinfo k=v to uprobes build too
608 for (unsigned i
= 0; i
< s
.modinfos
.size(); i
++)
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
;
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"))
627 clog
<< _("uprobes rebuild exit code: ") << rc
<< endl
;
631 s
.uprobes_path
= dir
+ "/uprobes.ko";
636 get_cached_uprobes(systemtap_session
& s
)
638 s
.uprobes_hash
= s
.use_cache
? find_uprobes_hash(s
) : "";
639 if (!s
.uprobes_hash
.empty())
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)
647 string cacheko
= s
.uprobes_hash
+ ".ko";
648 string tmpko
= dir
+ "/uprobes.ko";
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";
654 if (get_file_size(cacheko
) > 0 && copy_file(cacheko
, tmpko
) &&
655 get_file_size(cachesyms
) > 0 && copy_file(cachesyms
, tmpsyms
))
657 s
.uprobes_path
= tmpko
;
665 set_cached_uprobes(systemtap_session
& s
)
667 if (s
.use_cache
&& !s
.uprobes_hash
.empty())
669 string cacheko
= s
.uprobes_hash
+ ".ko";
670 string tmpko
= s
.tmpdir
+ "/uprobes/uprobes.ko";
671 copy_file(tmpko
, cacheko
);
673 string cachesyms
= s
.uprobes_hash
+ ".symvers";
674 string tmpsyms
= s
.tmpdir
+ "/uprobes/Module.symvers";
675 copy_file(tmpsyms
, cachesyms
);
680 uprobes_pass (systemtap_session
& s
)
682 if (!s
.need_uprobes
|| kernel_built_uprobes(s
))
685 if (s
.kernel_config
["CONFIG_UTRACE"] != string("y"))
687 clog
<< _("user-space process-tracking facilities not available [man error::process-tracking]") << endl
;
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.
698 if (!get_cached_uprobes(s
))
700 rc
= make_uprobes(s
);
702 set_cached_uprobes(s
);
711 make_dyninst_run_command (systemtap_session
& s
, const string
& remotedir
,
714 vector
<string
> cmd
{ getenv("SYSTEMTAP_STAPDYN") ?: BINDIR
"/stapdyn" };
716 // use slightly less verbosity
718 cmd
.insert(cmd
.end(), s
.verbose
- 1, "-v");
720 if (s
.suppress_warnings
)
724 cmd
.insert(cmd
.end(), { "-c", s
.cmd
});
727 cmd
.insert(cmd
.end(), { "-x", lex_cast(s
.target_pid
) });
729 if (!s
.output_file
.empty())
730 cmd
.insert(cmd
.end(), { "-o", s
.output_file
});
732 if (s
.color_mode
!= s
.color_auto
)
734 auto mode
= s
.color_mode
== s
.color_always
? "always" : "never";
735 cmd
.insert(cmd
.end(), { "-C", mode
});
738 cmd
.push_back((remotedir
.empty() ? s
.tmpdir
: remotedir
)
739 + "/" + s
.module_filename());
741 // add module arguments
742 cmd
.insert(cmd
.end(), s
.globalopts
.begin(), s
.globalopts
.end());
748 make_run_command (systemtap_session
& s
, const string
& remotedir
,
749 const string
& version
)
751 if (s
.runtime_usermode_p())
752 return make_dyninst_run_command(s
, remotedir
, version
);
754 // for now, just spawn staprun
755 vector
<string
> cmd
{ getenv("SYSTEMTAP_STAPRUN") ?: BINDIR
"/staprun" };
757 // use slightly less verbosity
759 cmd
.insert(cmd
.end(), s
.verbose
- 1, "-v");
761 if (s
.suppress_warnings
)
764 if (!s
.output_file
.empty())
765 cmd
.insert(cmd
.end(), { "-o", s
.output_file
});
768 cmd
.insert(cmd
.end(), { "-c", s
.cmd
});
771 cmd
.insert(cmd
.end(), { "-t", lex_cast(s
.target_pid
) });
773 if (s
.target_namespaces_pid
)
774 cmd
.insert(cmd
.end(), { "-N", lex_cast(s
.target_namespaces_pid
) });
777 cmd
.insert(cmd
.end(), { "-b", lex_cast(s
.buffer_size
) });
779 if (s
.need_uprobes
&& !kernel_built_uprobes(s
))
782 if (!s
.uprobes_path
.empty() &&
783 strverscmp("1.4", version
.c_str()) <= 0)
785 if (remotedir
.empty())
786 opt_u
.append(s
.uprobes_path
);
788 opt_u
.append(remotedir
+ "/" + basename(s
.uprobes_path
.c_str()));
790 cmd
.push_back(opt_u
);
794 cmd
.push_back(s
.output_file
.empty() ? "-L" : "-D");
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())
802 if (!s
.size_option
.empty())
803 cmd
.insert(cmd
.end(), { "-S", s
.size_option
});
805 if (s
.color_mode
!= s
.color_auto
)
807 auto mode
= s
.color_mode
== s
.color_always
? "always" : "never";
808 cmd
.insert(cmd
.end(), { "-C", mode
});
812 cmd
.insert(cmd
.end(), { "-M", lex_cast(s
.monitor_interval
) });
814 cmd
.push_back((remotedir
.empty() ? s
.tmpdir
: remotedir
)
815 + "/" + s
.module_filename());
817 // add module arguments
818 cmd
.insert(cmd
.end(), s
.globalopts
.begin(), s
.globalopts
.end());
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.
829 make_tracequeries(systemtap_session
& s
, const map
<string
,string
>& contents
)
831 static unsigned tick
= 0;
832 string
basename("tracequery_kmod_" + lex_cast(++tick
));
833 map
<string
,string
> objs
;
835 // create a subdirectory for the module
836 string
dir(s
.tmpdir
+ "/" + basename
);
837 if (create_dir(dir
.c_str()) != 0)
839 s
.print_warning("failed to create directory for querying tracepoints.");
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
;
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
;
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
;
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
++)
865 string sbasename
= basename
+ "_" + lex_cast(++tick
); // suffixed
867 // write out source code
868 string srcname
= dir
+ "/" + sbasename
+ ".c";
869 string src
= it
->second
;
870 ofstream
osrc(srcname
.c_str());
875 clog
<< _F("Processing tracepoint header %s with query %s",
876 it
->first
.c_str(), srcname
.c_str())
879 // arrange to build it
880 omf
<< "obj-m += " + sbasename
+ ".o" << endl
; // NB: without <dir> prefix
881 objs
[it
->first
] = dir
+ "/" + sbasename
+ ".o";
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
);
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.
903 // Build a tiny kernel module to query type information
905 make_typequery_kmod(systemtap_session
& s
, const vector
<string
>& headers
, string
& name
)
907 static unsigned tick
= 0;
908 string
basename("typequery_kmod_" + lex_cast(++tick
));
910 // create a subdirectory for the module
911 string
dir(s
.tmpdir
+ "/" + basename
);
912 if (create_dir(dir
.c_str()) != 0)
914 s
.print_warning("failed to create directory for querying types.");
919 name
= dir
+ "/" + basename
+ ".ko";
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
;
926 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
927 omf
<< "CONFIG_MODULE_SIG := n" << endl
;
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?
941 omf
<< "obj-m := " + basename
+ ".o" << endl
;
944 // create our empty source file
945 string
source(dir
+ "/" + basename
+ ".c");
946 ofstream
osrc(source
.c_str());
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
);
959 // Build a tiny user module to query type information
961 make_typequery_umod(systemtap_session
& s
, const vector
<string
>& headers
, string
& name
)
963 static unsigned tick
= 0;
965 name
= s
.tmpdir
+ "/typequery_umod_" + lex_cast(++tick
) + ".so";
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`"
975 "gcc", "-shared", "-g", "-fno-eliminate-unused-debug-types",
976 "-xc", "/dev/null", "-o", name
,
978 for (size_t i
= 0; i
< headers
.size(); ++i
)
979 cmd
.insert(cmd
.end(), { "-include", headers
[i
] });
981 bool quiet
= (s
.verbose
< 4);
982 int rc
= stap_system (s
.verbose
, cmd
, quiet
, quiet
);
990 make_typequery(systemtap_session
& s
, string
& module
)
994 vector
<string
> headers
;
995 bool kernel
= startswith(module
, "kernel");
997 for (size_t end
, i
= kernel
? 6 : 0; i
< module
.size(); i
= end
+ 1)
999 if (module
[i
] != '<')
1001 end
= module
.find('>', ++i
);
1002 if (end
== string::npos
)
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
+ "\"");
1009 headers
.push_back(header
);
1011 if (headers
.empty())
1015 rc
= make_typequery_kmod(s
, headers
, new_module
);
1017 rc
= make_typequery_umod(s
, headers
, new_module
);
1020 module
= new_module
;
1025 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */