2 // Copyright (C) 2005-2014 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"
25 #include <sys/types.h>
30 #include <sys/resource.h>
33 // A bit of obfuscation for Gentoo's sake.
34 // We *need* -Werror for stapconf to work correctly.
35 // https://bugs.gentoo.org/show_bug.cgi?id=522908
36 #define WERROR ("-W" "error")
40 /* Adjust and run make_cmd to build a kernel module. */
42 run_make_cmd(systemtap_session
& s
, vector
<string
>& make_cmd
,
43 bool null_out
=false, bool null_err
=false)
45 assert_no_interrupts();
47 // PR14168: we used to unsetenv values here; instead do it via
48 // env(1) in make_any_make_cmd().
50 // Disable ccache to avoid saving files that will never be reused.
51 // (ccache is useless to us, because our compiler commands always
52 // include the randomized tmpdir path.)
53 // It's not critical if this fails, so the return is ignored.
54 (void) setenv("CCACHE_DISABLE", "1", 0);
57 make_cmd
.push_back("V=1");
58 else if (s
.verbose
> 1)
59 make_cmd
.push_back("--no-print-directory");
62 make_cmd
.push_back("-s");
63 make_cmd
.push_back("--no-print-directory");
66 // Exploit SMP parallelism, if available.
67 long smp
= sysconf(_SC_NPROCESSORS_ONLN
);
68 if (smp
<= 0) smp
= 1;
69 // PR16276: but only if we're not running severely nproc-rlimited
71 int rlimit_rc
= getrlimit(RLIMIT_NPROC
, &rlim
);
72 const unsigned int severely_limited
= smp
*30; // WAG at number of gcc+make etc. nested processes
73 bool nproc_limited
= (rlimit_rc
== 0 && (rlim
.rlim_max
<= severely_limited
||
74 rlim
.rlim_cur
<= severely_limited
));
75 if (smp
>= 1 && !nproc_limited
)
76 make_cmd
.push_back("-j" + lex_cast(smp
+1));
78 if (strverscmp (s
.kernel_base_release
.c_str(), "2.6.29") < 0)
80 // Older kernels, before linux commit #fd54f502841c1, include
81 // gratuitous "echo"s in their Makefile. We need to suppress
82 // that with this bluntness.
86 int rc
= stap_system (s
.verbose
, "kbuild", make_cmd
, null_out
, null_err
);
93 make_any_make_cmd(systemtap_session
& s
, const string
& dir
, const string
& target
)
95 vector
<string
> make_cmd
;
97 // PR14168: sanitize environment variables for kbuild invocation
98 make_cmd
.push_back("env");
99 make_cmd
.push_back("-uARCH");
100 make_cmd
.push_back("-uKBUILD_EXTMOD");
101 make_cmd
.push_back("-uCROSS_COMPILE");
102 make_cmd
.push_back("-uKBUILD_IMAGE");
103 make_cmd
.push_back("-uKCONFIG_CONFIG");
104 make_cmd
.push_back("-uINSTALL_PATH");
105 string newpath
= string("PATH=/usr/bin:/bin:") + (getenv("PATH") ?: "");
106 make_cmd
.push_back(newpath
.c_str());
108 make_cmd
.push_back("make");
109 make_cmd
.push_back("-C");
110 make_cmd
.push_back(s
.kernel_build_tree
);
111 make_cmd
.push_back("M=" + dir
); // need make-quoting?
112 make_cmd
.push_back(target
);
114 // Add architecture, except for old powerpc (RHBZ669082)
115 if (s
.architecture
!= "powerpc" ||
116 (strverscmp (s
.kernel_base_release
.c_str(), "2.6.15") >= 0))
117 make_cmd
.push_back("ARCH=" + s
.architecture
); // need make-quoting?
119 // PR13847: suppress debuginfo creation by default
120 make_cmd
.insert(make_cmd
.end(), "CONFIG_DEBUG_INFO=");
122 // Add any custom kbuild flags
123 make_cmd
.insert(make_cmd
.end(), s
.kbuildflags
.begin(), s
.kbuildflags
.end());
128 static vector
<string
>
129 make_make_cmd(systemtap_session
& s
, const string
& dir
)
131 return make_any_make_cmd(s
, dir
, "modules");
134 static vector
<string
>
135 make_make_objs_cmd(systemtap_session
& s
, const string
& dir
)
137 // Kbuild uses these rules to build external modules:
139 // module-dirs := $(addprefix _module_,$(KBUILD_EXTMOD))
140 // modules: $(module-dirs)
141 // @$(kecho) ' Building modules, stage 2.';
142 // $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
144 // So if we're only interested in the stage 1 objects, we can
145 // cheat and make only the $(module-dirs) part.
146 return make_any_make_cmd(s
, dir
, "_module_" + dir
);
150 output_autoconf(systemtap_session
& s
, ofstream
& o
, const char *autoconf_c
,
151 const char *deftrue
, const char *deffalse
)
156 o
<< "if $(CHECK_BUILD) $(SYSTEMTAP_RUNTIME)/linux/" << autoconf_c
;
158 o
<< " > /dev/null 2>&1";
161 o
<< "echo \"#define " << deftrue
<< " 1\"";
163 o
<< "; else echo \"#define " << deffalse
<< " 1\"";
164 o
<< "; fi >> $@" << endl
;
168 void output_exportconf(systemtap_session
& s
, ofstream
& o
, const char *symbol
,
174 if (s
.kernel_exports
.find(symbol
) != s
.kernel_exports
.end())
175 o
<< "echo \"#define " << deftrue
<< " 1\"";
176 o
<< ">> $@" << endl
;
180 void output_dual_exportconf(systemtap_session
& s
, ofstream
& o
,
181 const char *symbol1
, const char *symbol2
,
187 if (s
.kernel_exports
.find(symbol1
) != s
.kernel_exports
.end()
188 && s
.kernel_exports
.find(symbol2
) != s
.kernel_exports
.end())
189 o
<< "echo \"#define " << deftrue
<< " 1\"";
190 o
<< ">> $@" << endl
;
194 void output_either_exportconf(systemtap_session
& s
, ofstream
& o
,
195 const char *symbol1
, const char *symbol2
,
201 if (s
.kernel_exports
.find(symbol1
) != s
.kernel_exports
.end()
202 || s
.kernel_exports
.find(symbol2
) != s
.kernel_exports
.end())
203 o
<< "echo \"#define " << deftrue
<< " 1\"";
204 o
<< ">> $@" << endl
;
209 compile_dyninst (systemtap_session
& s
)
211 const string module
= s
.tmpdir
+ "/" + s
.module_filename();
214 cmd
.push_back("gcc");
215 cmd
.push_back("--std=gnu99");
216 cmd
.push_back("-Wall");
217 cmd
.push_back(WERROR
);
218 cmd
.push_back("-Wno-unused");
219 cmd
.push_back("-Wno-strict-aliasing");
221 // BZ855981/948279. Since dyninst/runtime.h includes __sync_* calls,
222 // the compiler may generate different code for it depending on -march.
223 // For example, if the default is i386, we may get references to auxiliary
224 // functions like __sync_add_and_fetch_4, which appear to be defined
225 // nowhere. We hack around this problem thusly:
226 if (s
.architecture
== "i386")
227 cmd
.push_back("-march=i586");
229 cmd
.push_back("-fvisibility=hidden");
230 cmd
.push_back("-O2");
231 cmd
.push_back("-I" + s
.runtime_path
);
232 cmd
.push_back("-D__DYNINST__");
233 for (size_t i
= 0; i
< s
.c_macros
.size(); ++i
)
234 cmd
.push_back("-D" + s
.c_macros
[i
]);
235 cmd
.push_back(s
.translated_source
);
236 cmd
.push_back("-pthread");
237 cmd
.push_back("-lrt");
238 cmd
.push_back("-fPIC");
239 cmd
.push_back("-shared");
241 cmd
.push_back(module
);
244 cmd
.push_back("-ftime-report");
248 int rc
= stap_system (s
.verbose
, cmd
);
256 compile_pass (systemtap_session
& s
)
258 if (s
.runtime_usermode_p())
259 return compile_dyninst (s
);
261 int rc
= uprobes_pass (s
);
268 // fill in a quick Makefile
269 string makefile_nm
= s
.tmpdir
+ "/Makefile";
270 ofstream
o (makefile_nm
.c_str());
274 // Clever hacks copied from vmware modules
277 superverbose
= "set -x;";
279 string redirecterrors
= "> /dev/null 2>&1";
283 // Support O= (or KBUILD_OUTPUT) option
284 o
<< "_KBUILD_CFLAGS := $(call flags,KBUILD_CFLAGS)" << endl
;
286 o
<< "stap_check_gcc = $(shell " << superverbose
287 << " if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then "
288 << "echo \"$(1)\"; else echo \"$(2)\"; fi)" << endl
;
289 o
<< "CHECK_BUILD := $(CC) $(NOSTDINC_FLAGS) $(KBUILD_CPPFLAGS) $(CPPFLAGS) "
290 << "$(LINUXINCLUDE) $(_KBUILD_CFLAGS) $(CFLAGS_KERNEL) $(EXTRA_CFLAGS) "
291 << "$(CFLAGS) -DKBUILD_BASENAME=\\\"" << s
.module_name
<< "\\\" "
292 << WERROR
<< " -S -o /dev/null -xc " << endl
;
293 o
<< "stap_check_build = $(shell " << superverbose
<< " if $(CHECK_BUILD) $(1) "
294 << redirecterrors
<< " ; then echo \"$(2)\"; else echo \"$(3)\"; fi)" << endl
;
296 o
<< "SYSTEMTAP_RUNTIME = \"" << s
.runtime_path
<< "\"" << endl
;
298 // "autoconf" options go here
300 // RHBZ 543529: early rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
301 o
<< "CONFIG_MODULE_SIG := n" << endl
;
303 string module_cflags
= "EXTRA_CFLAGS";
304 o
<< module_cflags
<< " :=" << endl
;
306 // XXX: This gruesome hack is needed on some kernels built with separate O=directory,
307 // where files like 2.6.27 x86's asm/mach-*/mach_mpspec.h are not found on the cpp path.
308 // This could be a bug in arch/x86/Makefile that names
309 // mflags-y += -Iinclude/asm-x86/mach-default
310 // but that path does not exist in an O= build tree.
311 o
<< module_cflags
<< " += -Iinclude2/asm/mach-default" << endl
;
312 o
<< module_cflags
<< " += -I" + s
.kernel_build_tree
<< endl
;
313 if (s
.kernel_source_tree
!= "")
314 o
<< module_cflags
<< " += -I" + s
.kernel_source_tree
<< endl
;
315 for (unsigned i
= 0; i
< s
.kernel_extra_cflags
.size(); i
++)
316 o
<< module_cflags
<< " += " + s
.kernel_extra_cflags
[i
] << endl
;
319 // o << module_cflags << " += -Iusr/include" << endl;
320 // since such headers are cleansed of _KERNEL_ pieces that we need
322 o
<< "STAPCONF_HEADER := " << s
.tmpdir
<< "/" << s
.stapconf_name
<< endl
;
323 o
<< "$(STAPCONF_HEADER):" << endl
;
324 o
<< "\t@> $@" << endl
;
325 output_autoconf(s
, o
, "autoconf-hrtimer-rel.c", "STAPCONF_HRTIMER_REL", NULL
);
326 output_exportconf(s
, o
, "hrtimer_get_res", "STAPCONF_HRTIMER_GET_RES");
327 output_autoconf(s
, o
, "autoconf-generated-compile.c", "STAPCONF_GENERATED_COMPILE", NULL
);
328 output_autoconf(s
, o
, "autoconf-hrtimer-getset-expires.c", "STAPCONF_HRTIMER_GETSET_EXPIRES", NULL
);
329 output_autoconf(s
, o
, "autoconf-inode-private.c", "STAPCONF_INODE_PRIVATE", NULL
);
330 output_autoconf(s
, o
, "autoconf-constant-tsc.c", "STAPCONF_CONSTANT_TSC", NULL
);
331 output_autoconf(s
, o
, "autoconf-ktime-get-real.c", "STAPCONF_KTIME_GET_REAL", NULL
);
332 output_autoconf(s
, o
, "autoconf-x86-uniregs.c", "STAPCONF_X86_UNIREGS", NULL
);
333 output_autoconf(s
, o
, "autoconf-nameidata.c", "STAPCONF_NAMEIDATA_CLEANUP", NULL
);
334 output_dual_exportconf(s
, o
, "unregister_kprobes", "unregister_kretprobes", "STAPCONF_UNREGISTER_KPROBES");
335 output_autoconf(s
, o
, "autoconf-kprobe-symbol-name.c", "STAPCONF_KPROBE_SYMBOL_NAME", NULL
);
336 output_autoconf(s
, o
, "autoconf-real-parent.c", "STAPCONF_REAL_PARENT", NULL
);
337 output_autoconf(s
, o
, "autoconf-uaccess.c", "STAPCONF_LINUX_UACCESS_H", NULL
);
338 output_autoconf(s
, o
, "autoconf-oneachcpu-retry.c", "STAPCONF_ONEACHCPU_RETRY", NULL
);
339 output_autoconf(s
, o
, "autoconf-dpath-path.c", "STAPCONF_DPATH_PATH", NULL
);
340 output_exportconf(s
, o
, "synchronize_kernel", "STAPCONF_SYNCHRONIZE_KERNEL");
341 output_exportconf(s
, o
, "synchronize_rcu", "STAPCONF_SYNCHRONIZE_RCU");
342 output_exportconf(s
, o
, "synchronize_sched", "STAPCONF_SYNCHRONIZE_SCHED");
343 output_autoconf(s
, o
, "autoconf-task-uid.c", "STAPCONF_TASK_UID", NULL
);
344 output_autoconf(s
, o
, "autoconf-from_kuid_munged.c", "STAPCONF_FROM_KUID_MUNGED", NULL
);
345 output_exportconf(s
, o
, "get_mm_exe_file", "STAPCONF_GET_MM_EXE_FILE");
346 output_dual_exportconf(s
, o
, "alloc_vm_area", "free_vm_area", "STAPCONF_VM_AREA");
347 output_autoconf(s
, o
, "autoconf-procfs-owner.c", "STAPCONF_PROCFS_OWNER", NULL
);
348 output_autoconf(s
, o
, "autoconf-alloc-percpu-align.c", "STAPCONF_ALLOC_PERCPU_ALIGN", NULL
);
349 output_autoconf(s
, o
, "autoconf-x86-fs.c", "STAPCONF_X86_FS", NULL
);
350 output_autoconf(s
, o
, "autoconf-x86-xfs.c", "STAPCONF_X86_XFS", NULL
);
351 output_autoconf(s
, o
, "autoconf-x86-gs.c", "STAPCONF_X86_GS", NULL
);
352 output_autoconf(s
, o
, "autoconf-grsecurity.c", "STAPCONF_GRSECURITY", NULL
);
353 output_autoconf(s
, o
, "autoconf-trace-printk.c", "STAPCONF_TRACE_PRINTK", NULL
);
354 output_autoconf(s
, o
, "autoconf-regset.c", "STAPCONF_REGSET", NULL
);
355 output_autoconf(s
, o
, "autoconf-utrace-regset.c", "STAPCONF_UTRACE_REGSET", NULL
);
356 output_autoconf(s
, o
, "autoconf-uprobe-get-pc.c", "STAPCONF_UPROBE_GET_PC", NULL
);
357 output_autoconf(s
, o
, "autoconf-hlist-4args.c", "STAPCONF_HLIST_4ARGS", NULL
);
358 output_exportconf(s
, o
, "tsc_khz", "STAPCONF_TSC_KHZ");
359 output_exportconf(s
, o
, "cpu_khz", "STAPCONF_CPU_KHZ");
360 output_exportconf(s
, o
, "__module_text_address", "STAPCONF_MODULE_TEXT_ADDRESS");
361 output_exportconf(s
, o
, "add_timer_on", "STAPCONF_ADD_TIMER_ON");
363 output_dual_exportconf(s
, o
, "probe_kernel_read", "probe_kernel_write", "STAPCONF_PROBE_KERNEL");
364 output_autoconf(s
, o
, "autoconf-hw_breakpoint_context.c",
365 "STAPCONF_HW_BREAKPOINT_CONTEXT", NULL
);
366 output_autoconf(s
, o
, "autoconf-save-stack-trace.c",
367 "STAPCONF_KERNEL_STACKTRACE", NULL
);
368 output_autoconf(s
, o
, "autoconf-save-stack-trace-no-bp.c",
369 "STAPCONF_KERNEL_STACKTRACE_NO_BP", NULL
);
370 output_autoconf(s
, o
, "autoconf-asm-syscall.c",
371 "STAPCONF_ASM_SYSCALL_H", NULL
);
372 output_autoconf(s
, o
, "autoconf-ring_buffer-flags.c", "STAPCONF_RING_BUFFER_FLAGS", NULL
);
373 output_autoconf(s
, o
, "autoconf-ring_buffer_lost_events.c", "STAPCONF_RING_BUFFER_LOST_EVENTS", NULL
);
374 output_autoconf(s
, o
, "autoconf-ring_buffer_read_prepare.c", "STAPCONF_RING_BUFFER_READ_PREPARE", NULL
);
375 output_autoconf(s
, o
, "autoconf-kallsyms-on-each-symbol.c", "STAPCONF_KALLSYMS_ON_EACH_SYMBOL", NULL
);
376 output_autoconf(s
, o
, "autoconf-walk-stack.c", "STAPCONF_WALK_STACK", NULL
);
377 output_autoconf(s
, o
, "autoconf-stacktrace_ops-warning.c",
378 "STAPCONF_STACKTRACE_OPS_WARNING", NULL
);
379 output_autoconf(s
, o
, "autoconf-mm-context-vdso.c", "STAPCONF_MM_CONTEXT_VDSO", NULL
);
380 output_autoconf(s
, o
, "autoconf-mm-context-vdso-base.c", "STAPCONF_MM_CONTEXT_VDSO_BASE", NULL
);
381 output_autoconf(s
, o
, "autoconf-blk-types.c", "STAPCONF_BLK_TYPES", NULL
);
382 output_autoconf(s
, o
, "autoconf-perf-structpid.c", "STAPCONF_PERF_STRUCTPID", NULL
);
383 output_autoconf(s
, o
, "perf_event_counter_context.c",
384 "STAPCONF_PERF_COUNTER_CONTEXT", NULL
);
385 output_autoconf(s
, o
, "perf_probe_handler_nmi.c",
386 "STAPCONF_PERF_HANDLER_NMI", NULL
);
387 output_exportconf(s
, o
, "path_lookup", "STAPCONF_PATH_LOOKUP");
388 output_exportconf(s
, o
, "kern_path_parent", "STAPCONF_KERN_PATH_PARENT");
389 output_exportconf(s
, o
, "vfs_path_lookup", "STAPCONF_VFS_PATH_LOOKUP");
390 output_exportconf(s
, o
, "kern_path", "STAPCONF_KERN_PATH");
391 output_exportconf(s
, o
, "proc_create_data", "STAPCONF_PROC_CREATE_DATA");
392 output_exportconf(s
, o
, "PDE_DATA", "STAPCONF_PDE_DATA");
393 output_autoconf(s
, o
, "autoconf-module-sect-attrs.c", "STAPCONF_MODULE_SECT_ATTRS", NULL
);
395 output_autoconf(s
, o
, "autoconf-utrace-via-tracepoints.c", "STAPCONF_UTRACE_VIA_TRACEPOINTS", NULL
);
396 output_autoconf(s
, o
, "autoconf-task_work-struct.c", "STAPCONF_TASK_WORK_STRUCT", NULL
);
397 output_autoconf(s
, o
, "autoconf-vm-area-pte.c", "STAPCONF_VM_AREA_PTE", NULL
);
398 output_autoconf(s
, o
, "autoconf-relay-umode_t.c", "STAPCONF_RELAY_UMODE_T", NULL
);
399 output_autoconf(s
, o
, "autoconf-fs_supers-hlist.c", "STAPCONF_FS_SUPERS_HLIST", NULL
);
400 output_autoconf(s
, o
, "autoconf-compat_sigaction.c", "STAPCONF_COMPAT_SIGACTION", NULL
);
401 output_autoconf(s
, o
, "autoconf-netfilter.c", "STAPCONF_NETFILTER_V313", NULL
);
402 output_autoconf(s
, o
, "autoconf-netfilter-313b.c", "STAPCONF_NETFILTER_V313B", NULL
);
403 output_autoconf(s
, o
, "autoconf-netfilter-4_1.c", "STAPCONF_NETFILTER_V41", NULL
);
404 output_autoconf(s
, o
, "autoconf-netfilter-4_4.c", "STAPCONF_NETFILTER_V44", NULL
);
405 output_autoconf(s
, o
, "autoconf-smpcall-5args.c", "STAPCONF_SMPCALL_5ARGS", NULL
);
406 output_autoconf(s
, o
, "autoconf-smpcall-4args.c", "STAPCONF_SMPCALL_4ARGS", NULL
);
408 // used by tapset/timestamp_monotonic.stp
409 output_exportconf(s
, o
, "cpu_clock", "STAPCONF_CPU_CLOCK");
410 output_exportconf(s
, o
, "local_clock", "STAPCONF_LOCAL_CLOCK");
412 // used by runtime/uprobe-inode.c
413 output_either_exportconf(s
, o
, "uprobe_register", "register_uprobe",
414 "STAPCONF_UPROBE_REGISTER_EXPORTED");
415 output_either_exportconf(s
, o
, "uprobe_unregister", "unregister_uprobe",
416 "STAPCONF_UPROBE_UNREGISTER_EXPORTED");
417 output_autoconf(s
, o
, "autoconf-old-inode-uprobes.c", "STAPCONF_OLD_INODE_UPROBES", NULL
);
418 output_autoconf(s
, o
, "autoconf-inode-uretprobes.c", "STAPCONF_INODE_URETPROBES", NULL
);
420 // used by tapsets.cxx inode uprobe generated code
421 output_exportconf(s
, o
, "uprobe_get_swbp_addr", "STAPCONF_UPROBE_GET_SWBP_ADDR_EXPORTED");
423 // used by runtime/loc2c-runtime.h
424 output_exportconf(s
, o
, "task_user_regset_view", "STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED");
426 // used by runtime/stp_utrace.c
427 output_exportconf(s
, o
, "task_work_add", "STAPCONF_TASK_WORK_ADD_EXPORTED");
428 output_exportconf(s
, o
, "wake_up_state", "STAPCONF_WAKE_UP_STATE_EXPORTED");
429 output_exportconf(s
, o
, "try_to_wake_up", "STAPCONF_TRY_TO_WAKE_UP_EXPORTED");
430 output_exportconf(s
, o
, "signal_wake_up_state", "STAPCONF_SIGNAL_WAKE_UP_STATE_EXPORTED");
431 output_exportconf(s
, o
, "signal_wake_up", "STAPCONF_SIGNAL_WAKE_UP_EXPORTED");
432 output_exportconf(s
, o
, "__lock_task_sighand", "STAPCONF___LOCK_TASK_SIGHAND_EXPORTED");
434 output_autoconf(s
, o
, "autoconf-pagefault_disable.c", "STAPCONF_PAGEFAULT_DISABLE", NULL
);
435 output_exportconf(s
, o
, "kallsyms_lookup_name", "STAPCONF_KALLSYMS");
436 output_autoconf(s
, o
, "autoconf-uidgid.c", "STAPCONF_LINUX_UIDGID_H", NULL
);
437 output_exportconf(s
, o
, "sigset_from_compat", "STAPCONF_SIGSET_FROM_COMPAT_EXPORTED");
438 output_exportconf(s
, o
, "vzalloc", "STAPCONF_VZALLOC");
439 output_exportconf(s
, o
, "vzalloc_node", "STAPCONF_VZALLOC_NODE");
440 output_exportconf(s
, o
, "vmalloc_node", "STAPCONF_VMALLOC_NODE");
442 // RHBZ1233912 - s390 temporary workaround for non-atomic udelay()
443 output_exportconf(s
, o
, "udelay_simple", "STAPCONF_UDELAY_SIMPLE");
445 output_autoconf(s
, o
, "autoconf-tracepoint-strings.c", "STAPCONF_TRACEPOINT_STRINGS", NULL
);
446 output_autoconf(s
, o
, "autoconf-timerfd.c", "STAPCONF_TIMERFD_H", NULL
);
448 output_autoconf(s
, o
, "autoconf-module_layout.c",
449 "STAPCONF_MODULE_LAYOUT", NULL
);
450 output_autoconf(s
, o
, "autoconf-mod_kallsyms.c",
451 "STAPCONF_MOD_KALLSYMS", NULL
);
453 o
<< module_cflags
<< " += -include $(STAPCONF_HEADER)" << endl
;
455 for (unsigned i
=0; i
<s
.c_macros
.size(); i
++)
456 o
<< "EXTRA_CFLAGS += -D " << lex_cast_qstring(s
.c_macros
[i
]) << endl
; // XXX right quoting?
459 o
<< "EXTRA_CFLAGS += -ftime-report -Q" << endl
;
461 // XXX: unfortunately, -save-temps can't work since linux kbuild cwd
464 // if (s.keep_tmpdir)
465 // o << "CFLAGS += -fverbose-asm -save-temps" << endl;
467 // Kernels can be compiled with CONFIG_CC_OPTIMIZE_FOR_SIZE to select
468 // -Os, otherwise -O2 is the default.
469 o
<< "EXTRA_CFLAGS += -freorder-blocks" << endl
; // improve on -Os
471 // Generate eh_frame for self-backtracing
472 o
<< "EXTRA_CFLAGS += -fasynchronous-unwind-tables" << endl
;
474 // We used to allow the user to override default optimization when so
475 // requested by adding a -O[0123s] so they could determine the
476 // time/space/speed tradeoffs themselves, but we cannot guantantee that
477 // the (un)optimized code actually compiles and/or generates functional
478 // code, so we had to remove it.
479 // o << "EXTRA_CFLAGS += " << s.gcc_flags << endl; // Add -O[0123s]
481 // o << "CFLAGS += -fno-unit-at-a-time" << endl;
483 // 256^W512 bytes should be enough for anybody
484 // XXX this doesn't validate varargs, per gcc bug #41633
485 o
<< "EXTRA_CFLAGS += $(call cc-option,-Wframe-larger-than=512)" << endl
;
487 // gcc 5.0.0-0.13.fc23 ipa-icf seems to consume gigacpu on stap-generated code
488 o
<< "EXTRA_CFLAGS += $(call cc-option,-fno-ipa-icf)" << endl
;
490 // Assumes linux 2.6 kbuild
491 o
<< "EXTRA_CFLAGS += -Wno-unused " << WERROR
<< endl
;
492 #if CHECK_POINTER_ARITH_PR5947
493 o
<< "EXTRA_CFLAGS += -Wpointer-arith" << endl
;
495 o
<< "EXTRA_CFLAGS += -I\"" << s
.runtime_path
<< "\"" << endl
;
496 // XXX: this may help ppc toc overflow
497 // o << "CFLAGS := $(subst -Os,-O2,$(CFLAGS)) -fminimal-toc" << endl;
498 o
<< "obj-m := " << s
.module_name
<< ".o" << endl
;
500 // print out all the auxiliary source (->object) file names
501 o
<< s
.module_name
<< "-y := ";
502 for (unsigned i
=0; i
<s
.auxiliary_outputs
.size(); i
++)
504 if (s
.auxiliary_outputs
[i
]->trailer_p
) continue;
505 string srcname
= s
.auxiliary_outputs
[i
]->filename
;
506 assert (srcname
!= "" && srcname
.rfind('/') != string::npos
);
507 string objname
= srcname
.substr(srcname
.rfind('/')+1); // basename
508 assert (objname
!= "" && objname
[objname
.size()-1] == 'c');
509 objname
[objname
.size()-1] = 'o'; // now objname
512 // and once again, for the translated_source file. It can't simply
513 // be named MODULENAME.c, since kbuild doesn't allow a foo.ko file
514 // consisting of multiple .o's to have foo.o/foo.c as a source.
515 // (It uses ld -r -o foo.o EACH.o EACH.o).
517 string srcname
= s
.translated_source
;
518 assert (srcname
!= "" && srcname
.rfind('/') != string::npos
);
519 string objname
= srcname
.substr(srcname
.rfind('/')+1); // basename
520 assert (objname
!= "" && objname
[objname
.size()-1] == 'c');
521 objname
[objname
.size()-1] = 'o'; // now objname
524 // and once again, for the trailer type auxiliary outputs.
525 for (unsigned i
=0; i
<s
.auxiliary_outputs
.size(); i
++)
527 if (! s
.auxiliary_outputs
[i
]->trailer_p
) continue;
528 string srcname
= s
.auxiliary_outputs
[i
]->filename
;
529 assert (srcname
!= "" && srcname
.rfind('/') != string::npos
);
530 string objname
= srcname
.substr(srcname
.rfind('/')+1); // basename
531 assert (objname
!= "" && objname
[objname
.size()-1] == 'c');
532 objname
[objname
.size()-1] = 'o'; // now objname
537 // add all stapconf dependencies
538 o
<< s
.translated_source
<< ": $(STAPCONF_HEADER)" << endl
;
539 for (unsigned i
=0; i
<s
.auxiliary_outputs
.size(); i
++)
540 o
<< s
.auxiliary_outputs
[i
]->filename
<< ": $(STAPCONF_HEADER)" << endl
;
545 // Generate module directory pathname and make sure it exists.
546 string module_dir
= s
.kernel_build_tree
;
547 string module_dir_makefile
= module_dir
+ "/Makefile";
549 rc
= stat(module_dir_makefile
.c_str(), &st
);
552 clog
<< _F("Checking \" %s \" failed with error: %s\nEnsure kernel development headers & makefiles are installed.",
553 module_dir_makefile
.c_str(), strerror(errno
)) << endl
;
559 vector
<string
> make_cmd
= make_make_cmd(s
, s
.tmpdir
);
560 rc
= run_make_cmd(s
, make_cmd
);
567 * If uprobes was built as part of the kernel build (either built-in
568 * or as a module), the uprobes exports should show up. This is to be
569 * as distinct from the stap-built uprobes.ko from the runtime.
572 kernel_built_uprobes (systemtap_session
& s
)
574 if (s
.runtime_usermode_p())
575 return true; // sort of, via dyninst
577 // see also tapsets.cxx:kernel_supports_inode_uprobes()
578 return ((s
.kernel_config
["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y" && s
.kernel_config
["CONFIG_UPROBES"] == "y") ||
579 (s
.kernel_exports
.find("unregister_uprobe") != s
.kernel_exports
.end()));
583 make_uprobes (systemtap_session
& s
)
586 clog
<< _("Pass 4, preamble: (re)building SystemTap's version of uprobes.")
589 // create a subdirectory for the uprobes module
590 string
dir(s
.tmpdir
+ "/uprobes");
591 if (create_dir(dir
.c_str()) != 0)
593 s
.print_warning("failed to create directory for build uprobes.");
598 // create a simple Makefile
599 string
makefile(dir
+ "/Makefile");
600 ofstream
omf(makefile
.c_str());
601 omf
<< "obj-m := uprobes.o" << endl
;
602 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
603 omf
<< "CONFIG_MODULE_SIG := n" << endl
;
606 // create a simple #include-chained source file
607 string
runtimesourcefile(s
.runtime_path
+ "/linux/uprobes/uprobes.c");
608 string
sourcefile(dir
+ "/uprobes.c");
609 ofstream
osrc(sourcefile
.c_str());
610 osrc
<< "#include \"" << runtimesourcefile
<< "\"" << endl
;
612 // pass --modinfo k=v to uprobes build too
613 for (unsigned i
= 0; i
< s
.modinfos
.size(); i
++)
615 const string
& mi
= s
.modinfos
[i
];
616 size_t loc
= mi
.find('=');
617 string tag
= mi
.substr (0, loc
);
618 string value
= mi
.substr (loc
+1);
619 osrc
<< "MODULE_INFO(" << tag
<< "," << lex_cast_qstring(value
) << ");" << endl
;
625 vector
<string
> make_cmd
= make_make_cmd(s
, dir
);
626 int rc
= run_make_cmd(s
, make_cmd
);
627 if (!rc
&& !copy_file(dir
+ "/Module.symvers",
628 s
.tmpdir
+ "/Module.symvers"))
632 clog
<< _("uprobes rebuild exit code: ") << rc
<< endl
;
636 s
.uprobes_path
= dir
+ "/uprobes.ko";
641 get_cached_uprobes(systemtap_session
& s
)
643 s
.uprobes_hash
= s
.use_cache
? find_uprobes_hash(s
) : "";
644 if (!s
.uprobes_hash
.empty())
646 // NB: We always put uprobes.ko in its own directory, especially so
647 // stap-serverd can more easily locate it.
648 string
dir(s
.tmpdir
+ "/uprobes");
649 if (create_dir(dir
.c_str()) != 0)
652 string cacheko
= s
.uprobes_hash
+ ".ko";
653 string tmpko
= dir
+ "/uprobes.ko";
655 // The symvers file still needs to go in the script module's directory.
656 string cachesyms
= s
.uprobes_hash
+ ".symvers";
657 string tmpsyms
= s
.tmpdir
+ "/Module.symvers";
659 if (get_file_size(cacheko
) > 0 && copy_file(cacheko
, tmpko
) &&
660 get_file_size(cachesyms
) > 0 && copy_file(cachesyms
, tmpsyms
))
662 s
.uprobes_path
= tmpko
;
670 set_cached_uprobes(systemtap_session
& s
)
672 if (s
.use_cache
&& !s
.uprobes_hash
.empty())
674 string cacheko
= s
.uprobes_hash
+ ".ko";
675 string tmpko
= s
.tmpdir
+ "/uprobes/uprobes.ko";
676 copy_file(tmpko
, cacheko
);
678 string cachesyms
= s
.uprobes_hash
+ ".symvers";
679 string tmpsyms
= s
.tmpdir
+ "/uprobes/Module.symvers";
680 copy_file(tmpsyms
, cachesyms
);
685 uprobes_pass (systemtap_session
& s
)
687 if (!s
.need_uprobes
|| kernel_built_uprobes(s
))
690 if (s
.kernel_config
["CONFIG_UTRACE"] != string("y"))
692 clog
<< _("user-space process-tracking facilities not available [man error::process-tracking]") << endl
;
698 * We need to use the version of uprobes that comes with SystemTap. Try to
699 * get it from the cache first. If not found, build it and try to save it to
700 * the cache for future reuse.
703 if (!get_cached_uprobes(s
))
705 rc
= make_uprobes(s
);
707 set_cached_uprobes(s
);
716 make_dyninst_run_command (systemtap_session
& s
, const string
& remotedir
,
720 cmd
.push_back(getenv("SYSTEMTAP_STAPDYN") ?: BINDIR
"/stapdyn");
722 // use slightly less verbosity
723 for (unsigned i
=1; i
<s
.verbose
; i
++)
725 if (s
.suppress_warnings
)
731 cmd
.push_back(s
.cmd
);
737 cmd
.push_back(lex_cast(s
.target_pid
));
740 if (!s
.output_file
.empty())
743 cmd
.push_back(s
.output_file
);
746 if (s
.color_mode
!= s
.color_auto
)
749 if (s
.color_mode
== s
.color_always
)
750 cmd
.push_back("always");
752 cmd
.push_back("never");
755 cmd
.push_back((remotedir
.empty() ? s
.tmpdir
: remotedir
)
756 + "/" + s
.module_filename());
758 // add module arguments
759 cmd
.insert(cmd
.end(), s
.globalopts
.begin(), s
.globalopts
.end());
765 make_run_command (systemtap_session
& s
, const string
& remotedir
,
766 const string
& version
)
768 if (s
.runtime_usermode_p())
769 return make_dyninst_run_command(s
, remotedir
, version
);
771 // for now, just spawn staprun
772 vector
<string
> staprun_cmd
;
773 staprun_cmd
.push_back(getenv("SYSTEMTAP_STAPRUN") ?: BINDIR
"/staprun");
775 // use slightly less verbosity
776 for (unsigned i
=1; i
<s
.verbose
; i
++)
777 staprun_cmd
.push_back("-v");
778 if (s
.suppress_warnings
)
779 staprun_cmd
.push_back("-w");
781 if (!s
.output_file
.empty())
783 staprun_cmd
.push_back("-o");
784 staprun_cmd
.push_back(s
.output_file
);
789 staprun_cmd
.push_back("-c");
790 staprun_cmd
.push_back(s
.cmd
);
795 staprun_cmd
.push_back("-t");
796 staprun_cmd
.push_back(lex_cast(s
.target_pid
));
799 if (s
.target_namespaces_pid
)
801 staprun_cmd
.push_back("-N");
802 staprun_cmd
.push_back(lex_cast(s
.target_namespaces_pid
));
807 staprun_cmd
.push_back("-b");
808 staprun_cmd
.push_back(lex_cast(s
.buffer_size
));
811 if (s
.need_uprobes
&& !kernel_built_uprobes(s
))
814 if (!s
.uprobes_path
.empty() &&
815 strverscmp("1.4", version
.c_str()) <= 0)
817 if (remotedir
.empty())
818 opt_u
.append(s
.uprobes_path
);
820 opt_u
.append(remotedir
+ "/" + basename(s
.uprobes_path
.c_str()));
822 staprun_cmd
.push_back(opt_u
);
826 staprun_cmd
.push_back(s
.output_file
.empty() ? "-L" : "-D");
828 // Note that if this system requires signed modules, we can't rename
829 // it after it has been signed.
830 if (!s
.modname_given
&& (strverscmp("1.6", version
.c_str()) <= 0)
831 && s
.mok_fingerprints
.empty())
832 staprun_cmd
.push_back("-R");
834 if (!s
.size_option
.empty())
836 staprun_cmd
.push_back("-S");
837 staprun_cmd
.push_back(s
.size_option
);
840 if (s
.color_mode
!= s
.color_auto
)
842 staprun_cmd
.push_back("-C");
843 if (s
.color_mode
== s
.color_always
)
844 staprun_cmd
.push_back("always");
846 staprun_cmd
.push_back("never");
851 staprun_cmd
.push_back("-M");
852 staprun_cmd
.push_back(lex_cast(s
.monitor_interval
));
855 staprun_cmd
.push_back((remotedir
.empty() ? s
.tmpdir
: remotedir
)
856 + "/" + s
.module_filename());
858 // add module arguments
859 staprun_cmd
.insert(staprun_cmd
.end(),
860 s
.globalopts
.begin(), s
.globalopts
.end());
866 // Build tiny kernel modules to query tracepoints.
867 // Given a (header-file -> test-contents) map, compile them ASAP, and return
868 // a (header-file -> obj-filename) map.
871 make_tracequeries(systemtap_session
& s
, const map
<string
,string
>& contents
)
873 static unsigned tick
= 0;
874 string
basename("tracequery_kmod_" + lex_cast(++tick
));
875 map
<string
,string
> objs
;
877 // create a subdirectory for the module
878 string
dir(s
.tmpdir
+ "/" + basename
);
879 if (create_dir(dir
.c_str()) != 0)
881 s
.print_warning("failed to create directory for querying tracepoints.");
886 // create a simple Makefile
887 string
makefile(dir
+ "/Makefile");
888 ofstream
omf(makefile
.c_str());
889 // force debuginfo generation, and relax implicit functions
890 omf
<< "EXTRA_CFLAGS := -g -Wno-implicit-function-declaration " << WERROR
<< endl
;
891 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
892 omf
<< "CONFIG_MODULE_SIG := n" << endl
;
894 // PR18389: disable GCC's Identical Code Folding, since the stubs may look identical
895 omf
<< "EXTRA_CFLAGS += $(call cc-option,-fno-ipa-icf)" << endl
;
897 omf
<< "EXTRA_CFLAGS += -I" + s
.kernel_build_tree
<< endl
;
898 if (s
.kernel_source_tree
!= "")
899 omf
<< "EXTRA_CFLAGS += -I" + s
.kernel_source_tree
<< endl
;
900 for (unsigned i
= 0; i
< s
.kernel_extra_cflags
.size(); i
++)
901 omf
<< "EXTRA_CFLAGS += " + s
.kernel_extra_cflags
[i
] << endl
;
903 omf
<< "obj-m := " << endl
;
904 // write out each header-specific source file into a separate file
905 for (map
<string
,string
>::const_iterator it
= contents
.begin(); it
!= contents
.end(); it
++)
907 string sbasename
= basename
+ "_" + lex_cast(++tick
); // suffixed
909 // write out source code
910 string srcname
= dir
+ "/" + sbasename
+ ".c";
911 string src
= it
->second
;
912 ofstream
osrc(srcname
.c_str());
917 clog
<< _F("Processing tracepoint header %s with query %s",
918 it
->first
.c_str(), srcname
.c_str())
921 // arrange to build it
922 omf
<< "obj-m += " + sbasename
+ ".o" << endl
; // NB: without <dir> prefix
923 objs
[it
->first
] = dir
+ "/" + sbasename
+ ".o";
928 vector
<string
> make_cmd
= make_make_objs_cmd(s
, dir
);
929 make_cmd
.push_back ("-i"); // ignore errors, give rc 0 even in case of tracepoint header nits
930 bool quiet
= (s
.verbose
< 4);
931 int rc
= run_make_cmd(s
, make_cmd
, quiet
, quiet
);
935 // Sometimes we fail a tracequery due to PR9993 / PR11649 type
936 // kernel trace header problems. In this case, due to PR12729, we
937 // used to get a lovely "Warning: make exited with status: 2" but no
938 // other useful diagnostic. -vvvv would let a user see what's up,
939 // but the user can't fix the problem even with that.
945 // Build a tiny kernel module to query type information
947 make_typequery_kmod(systemtap_session
& s
, const vector
<string
>& headers
, string
& name
)
949 static unsigned tick
= 0;
950 string
basename("typequery_kmod_" + lex_cast(++tick
));
952 // create a subdirectory for the module
953 string
dir(s
.tmpdir
+ "/" + basename
);
954 if (create_dir(dir
.c_str()) != 0)
956 s
.print_warning("failed to create directory for querying types.");
961 name
= dir
+ "/" + basename
+ ".ko";
963 // create a simple Makefile
964 string
makefile(dir
+ "/Makefile");
965 ofstream
omf(makefile
.c_str());
966 omf
<< "EXTRA_CFLAGS := -g -fno-eliminate-unused-debug-types" << endl
;
968 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
969 omf
<< "CONFIG_MODULE_SIG := n" << endl
;
971 // NB: We use -include instead of #include because that gives us more power.
972 // Using #include searches relative to the source's path, which in this case
973 // is /tmp/..., so that's not helpful. Using -include will search relative
974 // to the cwd, which will be the kernel build root. This means if you have a
975 // full kernel build tree, it's possible to get at types that aren't in the
976 // normal include path, e.g.:
977 // @cast(foo, "bsd_acct_struct", "kernel<kernel/acct.c>")->...
978 omf
<< "CFLAGS_" << basename
<< ".o :=";
979 for (size_t i
= 0; i
< headers
.size(); ++i
)
980 omf
<< " -include " << lex_cast_qstring(headers
[i
]); // XXX right quoting?
983 omf
<< "obj-m := " + basename
+ ".o" << endl
;
986 // create our empty source file
987 string
source(dir
+ "/" + basename
+ ".c");
988 ofstream
osrc(source
.c_str());
992 vector
<string
> make_cmd
= make_make_cmd(s
, dir
);
993 bool quiet
= (s
.verbose
< 4);
994 int rc
= run_make_cmd(s
, make_cmd
, quiet
, quiet
);
1001 // Build a tiny user module to query type information
1003 make_typequery_umod(systemtap_session
& s
, const vector
<string
>& headers
, string
& name
)
1005 static unsigned tick
= 0;
1007 name
= s
.tmpdir
+ "/typequery_umod_" + lex_cast(++tick
) + ".so";
1011 // NB: As with kmod, using -include makes relative paths more useful. The
1012 // cwd in this case will be the cwd of stap itself though, which may be
1013 // trickier to deal with. It might be better to "cd `dirname $script`"
1016 cmd
.push_back("gcc");
1017 cmd
.push_back("-shared");
1018 cmd
.push_back("-g");
1019 cmd
.push_back("-fno-eliminate-unused-debug-types");
1020 cmd
.push_back("-xc");
1021 cmd
.push_back("/dev/null");
1022 cmd
.push_back("-o");
1023 cmd
.push_back(name
);
1024 for (size_t i
= 0; i
< headers
.size(); ++i
)
1026 cmd
.push_back("-include");
1027 cmd
.push_back(headers
[i
]);
1029 bool quiet
= (s
.verbose
< 4);
1030 int rc
= stap_system (s
.verbose
, cmd
, quiet
, quiet
);
1032 s
.set_try_server ();
1038 make_typequery(systemtap_session
& s
, string
& module
)
1042 vector
<string
> headers
;
1043 bool kernel
= startswith(module
, "kernel");
1045 for (size_t end
, i
= kernel
? 6 : 0; i
< module
.size(); i
= end
+ 1)
1047 if (module
[i
] != '<')
1049 end
= module
.find('>', ++i
);
1050 if (end
== string::npos
)
1052 string header
= module
.substr(i
, end
- i
);
1053 vector
<string
> matches
;
1054 if (regexp_match(header
, "^[a-zA-Z0-9/_.+-]+$", matches
))
1055 s
.print_warning("skipping malformed @cast header \""+ header
+ "\"");
1057 headers
.push_back(header
);
1059 if (headers
.empty())
1063 rc
= make_typequery_kmod(s
, headers
, new_module
);
1065 rc
= make_typequery_umod(s
, headers
, new_module
);
1068 module
= new_module
;
1073 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */