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