]> sourceware.org Git - systemtap.git/blob - buildrun.cxx
RHBZ1319085: hack for s390 udelay_simple()
[systemtap.git] / buildrun.cxx
1 // build/run probes
2 // Copyright (C) 2005-2014 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9 #include "config.h"
10 #include "buildrun.h"
11 #include "session.h"
12 #include "util.h"
13 #include "hash.h"
14 #include "translate.h"
15
16 #include <cstdlib>
17 #include <fstream>
18 #include <sstream>
19
20 extern "C" {
21 #include <signal.h>
22 #include <sys/wait.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/resource.h>
31 }
32
33 // 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")
37
38 using namespace std;
39
40 /* Adjust and run make_cmd to build a kernel module. */
41 static int
42 run_make_cmd(systemtap_session& s, vector<string>& make_cmd,
43 bool null_out=false, bool null_err=false)
44 {
45 assert_no_interrupts();
46
47 // PR14168: we used to unsetenv values here; instead do it via
48 // env(1) in make_any_make_cmd().
49
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);
55
56 if (s.verbose > 2)
57 make_cmd.push_back("V=1");
58 else if (s.verbose > 1)
59 make_cmd.push_back("--no-print-directory");
60 else
61 {
62 make_cmd.push_back("-s");
63 make_cmd.push_back("--no-print-directory");
64 }
65
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
70 struct rlimit rlim;
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));
77
78 if (strverscmp (s.kernel_base_release.c_str(), "2.6.29") < 0)
79 {
80 // Older kernels, before linux commit #fd54f502841c1, include
81 // gratuitous "echo"s in their Makefile. We need to suppress
82 // that with this bluntness.
83 null_out = true;
84 }
85
86 int rc = stap_system (s.verbose, "kbuild", make_cmd, null_out, null_err);
87 if (rc != 0)
88 s.set_try_server ();
89 return rc;
90 }
91
92 static vector<string>
93 make_any_make_cmd(systemtap_session& s, const string& dir, const string& target)
94 {
95 vector<string> make_cmd;
96
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());
107
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);
113
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?
118
119 // PR13847: suppress debuginfo creation by default
120 make_cmd.insert(make_cmd.end(), "CONFIG_DEBUG_INFO=");
121
122 // Add any custom kbuild flags
123 make_cmd.insert(make_cmd.end(), s.kbuildflags.begin(), s.kbuildflags.end());
124
125 return make_cmd;
126 }
127
128 static vector<string>
129 make_make_cmd(systemtap_session& s, const string& dir)
130 {
131 return make_any_make_cmd(s, dir, "modules");
132 }
133
134 static vector<string>
135 make_make_objs_cmd(systemtap_session& s, const string& dir)
136 {
137 // Kbuild uses these rules to build external modules:
138 //
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
143 //
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);
147 }
148
149 static void
150 output_autoconf(systemtap_session& s, ofstream& o, const char *autoconf_c,
151 const char *deftrue, const char *deffalse)
152 {
153 o << "\t";
154 if (s.verbose < 4)
155 o << "@";
156 o << "if $(CHECK_BUILD) $(SYSTEMTAP_RUNTIME)/linux/" << autoconf_c;
157 if (s.verbose < 5)
158 o << " > /dev/null 2>&1";
159 o << "; then ";
160 if (deftrue)
161 o << "echo \"#define " << deftrue << " 1\"";
162 if (deffalse)
163 o << "; else echo \"#define " << deffalse << " 1\"";
164 o << "; fi >> $@" << endl;
165 }
166
167
168 void output_exportconf(systemtap_session& s, ofstream& o, const char *symbol,
169 const char *deftrue)
170 {
171 o << "\t";
172 if (s.verbose < 4)
173 o << "@";
174 if (s.kernel_exports.find(symbol) != s.kernel_exports.end())
175 o << "echo \"#define " << deftrue << " 1\"";
176 o << ">> $@" << endl;
177 }
178
179
180 void output_dual_exportconf(systemtap_session& s, ofstream& o,
181 const char *symbol1, const char *symbol2,
182 const char *deftrue)
183 {
184 o << "\t";
185 if (s.verbose < 4)
186 o << "@";
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;
191 }
192
193
194 void output_either_exportconf(systemtap_session& s, ofstream& o,
195 const char *symbol1, const char *symbol2,
196 const char *deftrue)
197 {
198 o << "\t";
199 if (s.verbose < 4)
200 o << "@";
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;
205 }
206
207
208 static int
209 compile_dyninst (systemtap_session& s)
210 {
211 const string module = s.tmpdir + "/" + s.module_filename();
212
213 vector<string> cmd;
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");
220
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");
228
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");
240 cmd.push_back("-o");
241 cmd.push_back(module);
242 if (s.verbose > 3)
243 {
244 cmd.push_back("-ftime-report");
245 cmd.push_back("-Q");
246 }
247
248 int rc = stap_system (s.verbose, cmd);
249 if (rc)
250 s.set_try_server ();
251 return rc;
252 }
253
254
255 int
256 compile_pass (systemtap_session& s)
257 {
258 if (s.runtime_usermode_p())
259 return compile_dyninst (s);
260
261 int rc = uprobes_pass (s);
262 if (rc)
263 {
264 s.set_try_server ();
265 return rc;
266 }
267
268 // fill in a quick Makefile
269 string makefile_nm = s.tmpdir + "/Makefile";
270 ofstream o (makefile_nm.c_str());
271
272 // Create makefile
273
274 // Clever hacks copied from vmware modules
275 string superverbose;
276 if (s.verbose > 3)
277 superverbose = "set -x;";
278
279 string redirecterrors = "> /dev/null 2>&1";
280 if (s.verbose > 6)
281 redirecterrors = "";
282
283 // Support O= (or KBUILD_OUTPUT) option
284 o << "_KBUILD_CFLAGS := $(call flags,KBUILD_CFLAGS)" << endl;
285
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;
295
296 o << "SYSTEMTAP_RUNTIME = \"" << s.runtime_path << "\"" << endl;
297
298 // "autoconf" options go here
299
300 // RHBZ 543529: early rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
301 o << "CONFIG_MODULE_SIG := n" << endl;
302
303 string module_cflags = "EXTRA_CFLAGS";
304 o << module_cflags << " :=" << endl;
305
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;
317
318 // NB: don't try
319 // o << module_cflags << " += -Iusr/include" << endl;
320 // since such headers are cleansed of _KERNEL_ pieces that we need
321
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");
362
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);
394
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);
407
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");
411
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);
419
420 // used by tapsets.cxx inode uprobe generated code
421 output_exportconf(s, o, "uprobe_get_swbp_addr", "STAPCONF_UPROBE_GET_SWBP_ADDR_EXPORTED");
422
423 // used by runtime/loc2c-runtime.h
424 output_exportconf(s, o, "task_user_regset_view", "STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED");
425
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");
433
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");
441
442 // RHBZ1233912 - s390 temporary workaround for non-atomic udelay()
443 output_exportconf(s, o, "udelay_simple", "STAPCONF_UDELAY_SIMPLE");
444
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);
447
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);
452
453 o << module_cflags << " += -include $(STAPCONF_HEADER)" << endl;
454
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?
457
458 if (s.verbose > 3)
459 o << "EXTRA_CFLAGS += -ftime-report -Q" << endl;
460
461 // XXX: unfortunately, -save-temps can't work since linux kbuild cwd
462 // is not writable.
463 //
464 // if (s.keep_tmpdir)
465 // o << "CFLAGS += -fverbose-asm -save-temps" << endl;
466
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
470
471 // Generate eh_frame for self-backtracing
472 o << "EXTRA_CFLAGS += -fasynchronous-unwind-tables" << endl;
473
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]
480
481 // o << "CFLAGS += -fno-unit-at-a-time" << endl;
482
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;
486
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;
489
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;
494 #endif
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;
499
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++)
503 {
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
510 o << " " + objname;
511 }
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).
516 {
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
522 o << " " + objname;
523 }
524 // and once again, for the trailer type auxiliary outputs.
525 for (unsigned i=0; i<s.auxiliary_outputs.size(); i++)
526 {
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
533 o << " " + objname;
534 }
535 o << endl;
536
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;
541
542
543 o.close ();
544
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";
548 struct stat st;
549 rc = stat(module_dir_makefile.c_str(), &st);
550 if (rc != 0)
551 {
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;
554 s.set_try_server ();
555 return rc;
556 }
557
558 // Run make
559 vector<string> make_cmd = make_make_cmd(s, s.tmpdir);
560 rc = run_make_cmd(s, make_cmd);
561 if (rc)
562 s.set_try_server ();
563 return rc;
564 }
565
566 /*
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.
570 */
571 static bool
572 kernel_built_uprobes (systemtap_session& s)
573 {
574 if (s.runtime_usermode_p())
575 return true; // sort of, via dyninst
576
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()));
580 }
581
582 static int
583 make_uprobes (systemtap_session& s)
584 {
585 if (s.verbose > 1)
586 clog << _("Pass 4, preamble: (re)building SystemTap's version of uprobes.")
587 << endl;
588
589 // create a subdirectory for the uprobes module
590 string dir(s.tmpdir + "/uprobes");
591 if (create_dir(dir.c_str()) != 0)
592 {
593 s.print_warning("failed to create directory for build uprobes.");
594 s.set_try_server ();
595 return 1;
596 }
597
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;
604 omf.close();
605
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;
611
612 // pass --modinfo k=v to uprobes build too
613 for (unsigned i = 0; i < s.modinfos.size(); i++)
614 {
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;
620 }
621
622 osrc.close();
623
624 // make the module
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"))
629 rc = -1;
630
631 if (s.verbose > 1)
632 clog << _("uprobes rebuild exit code: ") << rc << endl;
633 if (rc)
634 s.set_try_server ();
635 else
636 s.uprobes_path = dir + "/uprobes.ko";
637 return rc;
638 }
639
640 static bool
641 get_cached_uprobes(systemtap_session& s)
642 {
643 s.uprobes_hash = s.use_cache ? find_uprobes_hash(s) : "";
644 if (!s.uprobes_hash.empty())
645 {
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)
650 return false;
651
652 string cacheko = s.uprobes_hash + ".ko";
653 string tmpko = dir + "/uprobes.ko";
654
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";
658
659 if (get_file_size(cacheko) > 0 && copy_file(cacheko, tmpko) &&
660 get_file_size(cachesyms) > 0 && copy_file(cachesyms, tmpsyms))
661 {
662 s.uprobes_path = tmpko;
663 return true;
664 }
665 }
666 return false;
667 }
668
669 static void
670 set_cached_uprobes(systemtap_session& s)
671 {
672 if (s.use_cache && !s.uprobes_hash.empty())
673 {
674 string cacheko = s.uprobes_hash + ".ko";
675 string tmpko = s.tmpdir + "/uprobes/uprobes.ko";
676 copy_file(tmpko, cacheko);
677
678 string cachesyms = s.uprobes_hash + ".symvers";
679 string tmpsyms = s.tmpdir + "/uprobes/Module.symvers";
680 copy_file(tmpsyms, cachesyms);
681 }
682 }
683
684 int
685 uprobes_pass (systemtap_session& s)
686 {
687 if (!s.need_uprobes || kernel_built_uprobes(s))
688 return 0;
689
690 if (s.kernel_config["CONFIG_UTRACE"] != string("y"))
691 {
692 clog << _("user-space process-tracking facilities not available [man error::process-tracking]") << endl;
693 s.set_try_server ();
694 return 1;
695 }
696
697 /*
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.
701 */
702 int rc = 0;
703 if (!get_cached_uprobes(s))
704 {
705 rc = make_uprobes(s);
706 if (!rc)
707 set_cached_uprobes(s);
708 }
709 if (rc)
710 s.set_try_server ();
711 return rc;
712 }
713
714 static
715 vector<string>
716 make_dyninst_run_command (systemtap_session& s, const string& remotedir,
717 const string&)
718 {
719 vector<string> cmd;
720 cmd.push_back(getenv("SYSTEMTAP_STAPDYN") ?: BINDIR "/stapdyn");
721
722 // use slightly less verbosity
723 for (unsigned i=1; i<s.verbose; i++)
724 cmd.push_back("-v");
725 if (s.suppress_warnings)
726 cmd.push_back("-w");
727
728 if (!s.cmd.empty())
729 {
730 cmd.push_back("-c");
731 cmd.push_back(s.cmd);
732 }
733
734 if (s.target_pid)
735 {
736 cmd.push_back("-x");
737 cmd.push_back(lex_cast(s.target_pid));
738 }
739
740 if (!s.output_file.empty())
741 {
742 cmd.push_back("-o");
743 cmd.push_back(s.output_file);
744 }
745
746 if (s.color_mode != s.color_auto)
747 {
748 cmd.push_back("-C");
749 if (s.color_mode == s.color_always)
750 cmd.push_back("always");
751 else
752 cmd.push_back("never");
753 }
754
755 cmd.push_back((remotedir.empty() ? s.tmpdir : remotedir)
756 + "/" + s.module_filename());
757
758 // add module arguments
759 cmd.insert(cmd.end(), s.globalopts.begin(), s.globalopts.end());
760
761 return cmd;
762 }
763
764 vector<string>
765 make_run_command (systemtap_session& s, const string& remotedir,
766 const string& version)
767 {
768 if (s.runtime_usermode_p())
769 return make_dyninst_run_command(s, remotedir, version);
770
771 // for now, just spawn staprun
772 vector<string> staprun_cmd;
773 staprun_cmd.push_back(getenv("SYSTEMTAP_STAPRUN") ?: BINDIR "/staprun");
774
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");
780
781 if (!s.output_file.empty())
782 {
783 staprun_cmd.push_back("-o");
784 staprun_cmd.push_back(s.output_file);
785 }
786
787 if (!s.cmd.empty())
788 {
789 staprun_cmd.push_back("-c");
790 staprun_cmd.push_back(s.cmd);
791 }
792
793 if (s.target_pid)
794 {
795 staprun_cmd.push_back("-t");
796 staprun_cmd.push_back(lex_cast(s.target_pid));
797 }
798
799 if (s.target_namespaces_pid)
800 {
801 staprun_cmd.push_back("-N");
802 staprun_cmd.push_back(lex_cast(s.target_namespaces_pid));
803 }
804
805 if (s.buffer_size)
806 {
807 staprun_cmd.push_back("-b");
808 staprun_cmd.push_back(lex_cast(s.buffer_size));
809 }
810
811 if (s.need_uprobes && !kernel_built_uprobes(s))
812 {
813 string opt_u = "-u";
814 if (!s.uprobes_path.empty() &&
815 strverscmp("1.4", version.c_str()) <= 0)
816 {
817 if (remotedir.empty())
818 opt_u.append(s.uprobes_path);
819 else
820 opt_u.append(remotedir + "/" + basename(s.uprobes_path.c_str()));
821 }
822 staprun_cmd.push_back(opt_u);
823 }
824
825 if (s.load_only)
826 staprun_cmd.push_back(s.output_file.empty() ? "-L" : "-D");
827
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");
833
834 if (!s.size_option.empty())
835 {
836 staprun_cmd.push_back("-S");
837 staprun_cmd.push_back(s.size_option);
838 }
839
840 if (s.color_mode != s.color_auto)
841 {
842 staprun_cmd.push_back("-C");
843 if (s.color_mode == s.color_always)
844 staprun_cmd.push_back("always");
845 else
846 staprun_cmd.push_back("never");
847 }
848
849 if (s.monitor)
850 {
851 staprun_cmd.push_back("-M");
852 staprun_cmd.push_back(lex_cast(s.monitor_interval));
853 }
854
855 staprun_cmd.push_back((remotedir.empty() ? s.tmpdir : remotedir)
856 + "/" + s.module_filename());
857
858 // add module arguments
859 staprun_cmd.insert(staprun_cmd.end(),
860 s.globalopts.begin(), s.globalopts.end());
861
862 return staprun_cmd;
863 }
864
865
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.
869
870 map<string,string>
871 make_tracequeries(systemtap_session& s, const map<string,string>& contents)
872 {
873 static unsigned tick = 0;
874 string basename("tracequery_kmod_" + lex_cast(++tick));
875 map<string,string> objs;
876
877 // create a subdirectory for the module
878 string dir(s.tmpdir + "/" + basename);
879 if (create_dir(dir.c_str()) != 0)
880 {
881 s.print_warning("failed to create directory for querying tracepoints.");
882 s.set_try_server ();
883 return objs;
884 }
885
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;
893
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;
896
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;
902
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++)
906 {
907 string sbasename = basename + "_" + lex_cast(++tick); // suffixed
908
909 // write out source code
910 string srcname = dir + "/" + sbasename + ".c";
911 string src = it->second;
912 ofstream osrc(srcname.c_str());
913 osrc << src;
914 osrc.close();
915
916 if (s.verbose > 2)
917 clog << _F("Processing tracepoint header %s with query %s",
918 it->first.c_str(), srcname.c_str())
919 << endl;
920
921 // arrange to build it
922 omf << "obj-m += " + sbasename + ".o" << endl; // NB: without <dir> prefix
923 objs[it->first] = dir + "/" + sbasename + ".o";
924 }
925 omf.close();
926
927 // make the module
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);
932 if (rc)
933 s.set_try_server ();
934
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.
940
941 return objs;
942 }
943
944
945 // Build a tiny kernel module to query type information
946 static int
947 make_typequery_kmod(systemtap_session& s, const vector<string>& headers, string& name)
948 {
949 static unsigned tick = 0;
950 string basename("typequery_kmod_" + lex_cast(++tick));
951
952 // create a subdirectory for the module
953 string dir(s.tmpdir + "/" + basename);
954 if (create_dir(dir.c_str()) != 0)
955 {
956 s.print_warning("failed to create directory for querying types.");
957 s.set_try_server ();
958 return 1;
959 }
960
961 name = dir + "/" + basename + ".ko";
962
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;
967
968 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
969 omf << "CONFIG_MODULE_SIG := n" << endl;
970
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?
981 omf << endl;
982
983 omf << "obj-m := " + basename + ".o" << endl;
984 omf.close();
985
986 // create our empty source file
987 string source(dir + "/" + basename + ".c");
988 ofstream osrc(source.c_str());
989 osrc.close();
990
991 // make the module
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);
995 if (rc)
996 s.set_try_server ();
997 return rc;
998 }
999
1000
1001 // Build a tiny user module to query type information
1002 static int
1003 make_typequery_umod(systemtap_session& s, const vector<string>& headers, string& name)
1004 {
1005 static unsigned tick = 0;
1006
1007 name = s.tmpdir + "/typequery_umod_" + lex_cast(++tick) + ".so";
1008
1009 // make the module
1010 //
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`"
1014 // first...
1015 vector<string> cmd;
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)
1025 {
1026 cmd.push_back("-include");
1027 cmd.push_back(headers[i]);
1028 }
1029 bool quiet = (s.verbose < 4);
1030 int rc = stap_system (s.verbose, cmd, quiet, quiet);
1031 if (rc)
1032 s.set_try_server ();
1033 return rc;
1034 }
1035
1036
1037 int
1038 make_typequery(systemtap_session& s, string& module)
1039 {
1040 int rc;
1041 string new_module;
1042 vector<string> headers;
1043 bool kernel = startswith(module, "kernel");
1044
1045 for (size_t end, i = kernel ? 6 : 0; i < module.size(); i = end + 1)
1046 {
1047 if (module[i] != '<')
1048 return -1;
1049 end = module.find('>', ++i);
1050 if (end == string::npos)
1051 return -1;
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 + "\"");
1056 else
1057 headers.push_back(header);
1058 }
1059 if (headers.empty())
1060 return -1;
1061
1062 if (kernel)
1063 rc = make_typequery_kmod(s, headers, new_module);
1064 else
1065 rc = make_typequery_umod(s, headers, new_module);
1066
1067 if (!rc)
1068 module = new_module;
1069
1070 return rc;
1071 }
1072
1073 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.099657 seconds and 6 git commands to generate.