]> sourceware.org Git - systemtap.git/blame - buildrun.cxx
Add testcase for PR19525 (script doesn't finish in bulk mode).
[systemtap.git] / buildrun.cxx
CommitLineData
f4b28491 1// build/run probes
04ffc218 2// Copyright (C) 2005-2014 Red Hat Inc.
f4b28491
FCE
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"
dc38c0ae 11#include "session.h"
72dbc915 12#include "util.h"
7d26ee02 13#include "hash.h"
a4b9c3b3 14#include "translate.h"
f4b28491 15
3b579393 16#include <cstdlib>
f4b28491 17#include <fstream>
d04cf5ff
FCE
18#include <sstream>
19
20extern "C" {
49abf162 21#include <signal.h>
0c6296b2 22#include <sys/wait.h>
70404fc5 23#include <pwd.h>
0da3e7a0 24#include <grp.h>
b40af7ee
DS
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include <string.h>
29#include <errno.h>
3c4476ac 30#include <sys/resource.h>
d04cf5ff
FCE
31}
32
2537a880
JS
33// A bit of obfuscation for Gentoo's sake.
34// We *need* -Werror for stapconf to work correctly.
b586ab77 35// https://bugs.gentoo.org/show_bug.cgi?id=522908
2537a880 36#define WERROR ("-W" "error")
f4b28491
FCE
37
38using namespace std;
39
6274464e
JK
40/* Adjust and run make_cmd to build a kernel module. */
41static int
ff520ff4
JS
42run_make_cmd(systemtap_session& s, vector<string>& make_cmd,
43 bool null_out=false, bool null_err=false)
6274464e 44{
e19ebcf7 45 assert_no_interrupts();
576acc1a 46
e8474d2a
FCE
47 // PR14168: we used to unsetenv values here; instead do it via
48 // env(1) in make_any_make_cmd().
6274464e 49
b51455af
JS
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
b54581ce 56 if (s.verbose > 2)
ff520ff4 57 make_cmd.push_back("V=1");
b54581ce 58 else if (s.verbose > 1)
ff520ff4 59 make_cmd.push_back("--no-print-directory");
6274464e 60 else
ff520ff4
JS
61 {
62 make_cmd.push_back("-s");
63 make_cmd.push_back("--no-print-directory");
64 }
dff50e09 65
70fb16aa 66 // Exploit SMP parallelism, if available.
9fa0ffac 67 long smp = sysconf(_SC_NPROCESSORS_ONLN);
3c4476ac
FCE
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);
5dd70393 72 const unsigned int severely_limited = smp*30; // WAG at number of gcc+make etc. nested processes
3c4476ac
FCE
73 bool nproc_limited = (rlimit_rc == 0 && (rlim.rlim_max <= severely_limited ||
74 rlim.rlim_cur <= severely_limited));
75 if (smp >= 1 && !nproc_limited)
70fb16aa 76 make_cmd.push_back("-j" + lex_cast(smp+1));
9fa0ffac 77
e0ccd368
FCE
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.
ff520ff4 83 null_out = true;
e0ccd368
FCE
84 }
85
b13c6a37 86 int rc = stap_system (s.verbose, "kbuild", make_cmd, null_out, null_err);
3e1ec884
DB
87 if (rc != 0)
88 s.set_try_server ();
89 return rc;
6274464e 90}
f4b28491 91
7d26ee02 92static vector<string>
8e0a2563 93make_any_make_cmd(systemtap_session& s, const string& dir, const string& target)
7d26ee02
JS
94{
95 vector<string> make_cmd;
e8474d2a
FCE
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
7d26ee02
JS
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?
8e0a2563 112 make_cmd.push_back(target);
7d26ee02
JS
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
150f2075
FCE
119 // PR13847: suppress debuginfo creation by default
120 make_cmd.insert(make_cmd.end(), "CONFIG_DEBUG_INFO=");
121
7d26ee02
JS
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
8e0a2563
JS
128static vector<string>
129make_make_cmd(systemtap_session& s, const string& dir)
130{
131 return make_any_make_cmd(s, dir, "modules");
132}
133
134static vector<string>
135make_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
de0db58a
JS
149static void
150output_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 << "@";
2ba1736a 156 o << "if $(CHECK_BUILD) $(SYSTEMTAP_RUNTIME)/linux/" << autoconf_c;
de0db58a
JS
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
9e62f7ba
FCE
167
168void output_exportconf(systemtap_session& s, ofstream& o, const char *symbol,
169 const char *deftrue)
e25ab03c 170{
9e62f7ba
FCE
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;
e25ab03c
WH
177}
178
9e62f7ba 179
08c29612
DS
180void 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
13128684
JS
194void 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
3a894f7e
JS
208static int
209compile_dyninst (systemtap_session& s)
210{
4441e344 211 const string module = s.tmpdir + "/" + s.module_filename();
3a894f7e
JS
212
213 vector<string> cmd;
214 cmd.push_back("gcc");
215 cmd.push_back("--std=gnu99");
216 cmd.push_back("-Wall");
2537a880 217 cmd.push_back(WERROR);
3a894f7e
JS
218 cmd.push_back("-Wno-unused");
219 cmd.push_back("-Wno-strict-aliasing");
ba2daa43
FCE
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")
e1c3538b 227 cmd.push_back("-march=i586");
ba2daa43 228
81bc77cc 229 cmd.push_back("-fvisibility=hidden");
3a894f7e 230 cmd.push_back("-O2");
2466bf70
JS
231 cmd.push_back("-I" + s.runtime_path);
232 cmd.push_back("-D__DYNINST__");
8140a78d
JS
233 for (size_t i = 0; i < s.c_macros.size(); ++i)
234 cmd.push_back("-D" + s.c_macros[i]);
3a894f7e
JS
235 cmd.push_back(s.translated_source);
236 cmd.push_back("-pthread");
2b5ecafd 237 cmd.push_back("-lrt");
3a894f7e
JS
238 cmd.push_back("-fPIC");
239 cmd.push_back("-shared");
240 cmd.push_back("-o");
241 cmd.push_back(module);
8140a78d
JS
242 if (s.verbose > 3)
243 {
244 cmd.push_back("-ftime-report");
245 cmd.push_back("-Q");
246 }
3a894f7e 247
6da8678d 248 int rc = stap_system (s.verbose, cmd);
3a894f7e
JS
249 if (rc)
250 s.set_try_server ();
251 return rc;
252}
3a894f7e
JS
253
254
f4b28491
FCE
255int
256compile_pass (systemtap_session& s)
257{
ac3af990 258 if (s.runtime_usermode_p())
4441e344 259 return compile_dyninst (s);
3a894f7e 260
db3a383b
JK
261 int rc = uprobes_pass (s);
262 if (rc)
3e1ec884
DB
263 {
264 s.set_try_server ();
265 return rc;
266 }
db3a383b 267
f4b28491 268 // fill in a quick Makefile
92ade41d
FCE
269 string makefile_nm = s.tmpdir + "/Makefile";
270 ofstream o (makefile_nm.c_str());
92ade41d
FCE
271
272 // Create makefile
ed10c639 273
255e4c68 274 // Clever hacks copied from vmware modules
ecf19454
FCE
275 string superverbose;
276 if (s.verbose > 3)
277 superverbose = "set -x;";
522518f1 278
e9737939
MM
279 string redirecterrors = "> /dev/null 2>&1";
280 if (s.verbose > 6)
281 redirecterrors = "";
282
e5976ba0
MH
283 // Support O= (or KBUILD_OUTPUT) option
284 o << "_KBUILD_CFLAGS := $(call flags,KBUILD_CFLAGS)" << endl;
285
2537a880
JS
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;
255e4c68
FCE
295
296 o << "SYSTEMTAP_RUNTIME = \"" << s.runtime_path << "\"" << endl;
297
298 // "autoconf" options go here
299
18222191
FCE
300 // RHBZ 543529: early rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
301 o << "CONFIG_MODULE_SIG := n" << endl;
302
ecf19454 303 string module_cflags = "EXTRA_CFLAGS";
522518f1 304 o << module_cflags << " :=" << endl;
84c4da88
FCE
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;
18b13994 312 o << module_cflags << " += -I" + s.kernel_build_tree << endl;
d4393459
FCE
313 if (s.kernel_source_tree != "")
314 o << module_cflags << " += -I" + s.kernel_source_tree << endl;
ac4f2837
DS
315 for (unsigned i = 0; i < s.kernel_extra_cflags.size(); i++)
316 o << module_cflags << " += " + s.kernel_extra_cflags[i] << endl;
84c4da88 317
6046b840
FCE
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
de0db58a 322 o << "STAPCONF_HEADER := " << s.tmpdir << "/" << s.stapconf_name << endl;
de0db58a
JS
323 o << "$(STAPCONF_HEADER):" << endl;
324 o << "\t@echo -n > $@" << endl;
325 output_autoconf(s, o, "autoconf-hrtimer-rel.c", "STAPCONF_HRTIMER_REL", NULL);
86f726b7 326 output_exportconf(s, o, "hrtimer_get_res", "STAPCONF_HRTIMER_GET_RES");
1d381a0d 327 output_autoconf(s, o, "autoconf-generated-compile.c", "STAPCONF_GENERATED_COMPILE", NULL);
de0db58a
JS
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);
de0db58a
JS
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);
08c29612 334 output_dual_exportconf(s, o, "unregister_kprobes", "unregister_kretprobes", "STAPCONF_UNREGISTER_KPROBES");
766cee5f 335 output_autoconf(s, o, "autoconf-kprobe-symbol-name.c", "STAPCONF_KPROBE_SYMBOL_NAME", NULL);
de0db58a
JS
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);
b9197b0f
JS
340 output_exportconf(s, o, "synchronize_kernel", "STAPCONF_SYNCHRONIZE_KERNEL");
341 output_exportconf(s, o, "synchronize_rcu", "STAPCONF_SYNCHRONIZE_RCU");
08c29612 342 output_exportconf(s, o, "synchronize_sched", "STAPCONF_SYNCHRONIZE_SCHED");
de0db58a 343 output_autoconf(s, o, "autoconf-task-uid.c", "STAPCONF_TASK_UID", NULL);
ca89d289 344 output_autoconf(s, o, "autoconf-from_kuid_munged.c", "STAPCONF_FROM_KUID_MUNGED", NULL);
1c70d655 345 output_exportconf(s, o, "get_mm_exe_file", "STAPCONF_GET_MM_EXE_FILE");
08c29612 346 output_dual_exportconf(s, o, "alloc_vm_area", "free_vm_area", "STAPCONF_VM_AREA");
de0db58a 347 output_autoconf(s, o, "autoconf-procfs-owner.c", "STAPCONF_PROCFS_OWNER", NULL);
b1f85b93 348 output_autoconf(s, o, "autoconf-alloc-percpu-align.c", "STAPCONF_ALLOC_PERCPU_ALIGN", NULL);
6ddf07ac
JS
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);
5cad2d3b 351 output_autoconf(s, o, "autoconf-x86-gs.c", "STAPCONF_X86_GS", NULL);
f098eb6d 352 output_autoconf(s, o, "autoconf-grsecurity.c", "STAPCONF_GRSECURITY", NULL);
450718c9 353 output_autoconf(s, o, "autoconf-trace-printk.c", "STAPCONF_TRACE_PRINTK", NULL);
be004140
FCE
354 output_autoconf(s, o, "autoconf-regset.c", "STAPCONF_REGSET", NULL);
355 output_autoconf(s, o, "autoconf-utrace-regset.c", "STAPCONF_UTRACE_REGSET", NULL);
23258335 356 output_autoconf(s, o, "autoconf-uprobe-get-pc.c", "STAPCONF_UPROBE_GET_PC", NULL);
85804472 357 output_autoconf(s, o, "autoconf-hlist-4args.c", "STAPCONF_HLIST_4ARGS", NULL);
b09417ad 358 output_exportconf(s, o, "tsc_khz", "STAPCONF_TSC_KHZ");
9e62f7ba 359 output_exportconf(s, o, "cpu_khz", "STAPCONF_CPU_KHZ");
031fda59 360 output_exportconf(s, o, "__module_text_address", "STAPCONF_MODULE_TEXT_ADDRESS");
af6c651f 361 output_exportconf(s, o, "add_timer_on", "STAPCONF_ADD_TIMER_ON");
8d59b39f 362
08c29612 363 output_dual_exportconf(s, o, "probe_kernel_read", "probe_kernel_write", "STAPCONF_PROBE_KERNEL");
b273669e
MW
364 output_autoconf(s, o, "autoconf-hw_breakpoint_context.c",
365 "STAPCONF_HW_BREAKPOINT_CONTEXT", NULL);
8c5905d0
TM
366 output_autoconf(s, o, "autoconf-save-stack-trace.c",
367 "STAPCONF_KERNEL_STACKTRACE", NULL);
9957bbd2
MW
368 output_autoconf(s, o, "autoconf-save-stack-trace-no-bp.c",
369 "STAPCONF_KERNEL_STACKTRACE_NO_BP", NULL);
d5cd287f
DS
370 output_autoconf(s, o, "autoconf-asm-syscall.c",
371 "STAPCONF_ASM_SYSCALL_H", NULL);
c145b9cb 372 output_autoconf(s, o, "autoconf-ring_buffer-flags.c", "STAPCONF_RING_BUFFER_FLAGS", NULL);
0d0f0d84 373 output_autoconf(s, o, "autoconf-ring_buffer_lost_events.c", "STAPCONF_RING_BUFFER_LOST_EVENTS", NULL);
c38602b5 374 output_autoconf(s, o, "autoconf-ring_buffer_read_prepare.c", "STAPCONF_RING_BUFFER_READ_PREPARE", NULL);
03a4ec63 375 output_autoconf(s, o, "autoconf-kallsyms-on-each-symbol.c", "STAPCONF_KALLSYMS_ON_EACH_SYMBOL", NULL);
c265cd25 376 output_autoconf(s, o, "autoconf-walk-stack.c", "STAPCONF_WALK_STACK", NULL);
0bbb8009
JS
377 output_autoconf(s, o, "autoconf-stacktrace_ops-warning.c",
378 "STAPCONF_STACKTRACE_OPS_WARNING", NULL);
18da5887 379 output_autoconf(s, o, "autoconf-mm-context-vdso.c", "STAPCONF_MM_CONTEXT_VDSO", NULL);
3b62d74b 380 output_autoconf(s, o, "autoconf-mm-context-vdso-base.c", "STAPCONF_MM_CONTEXT_VDSO_BASE", NULL);
c1d2d085 381 output_autoconf(s, o, "autoconf-blk-types.c", "STAPCONF_BLK_TYPES", NULL);
aec17ca6 382 output_autoconf(s, o, "autoconf-perf-structpid.c", "STAPCONF_PERF_STRUCTPID", NULL);
2f3df331
MW
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);
7f2d56c3
DS
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");
33f02716 390 output_exportconf(s, o, "kern_path", "STAPCONF_KERN_PATH");
b85e750d
DS
391 output_exportconf(s, o, "proc_create_data", "STAPCONF_PROC_CREATE_DATA");
392 output_exportconf(s, o, "PDE_DATA", "STAPCONF_PDE_DATA");
2c9caccb 393 output_autoconf(s, o, "autoconf-module-sect-attrs.c", "STAPCONF_MODULE_SECT_ATTRS", NULL);
04ffc218 394
db7dcdff 395 output_autoconf(s, o, "autoconf-utrace-via-tracepoints.c", "STAPCONF_UTRACE_VIA_TRACEPOINTS", NULL);
0e0e60b2 396 output_autoconf(s, o, "autoconf-task_work-struct.c", "STAPCONF_TASK_WORK_STRUCT", NULL);
abc69bab 397 output_autoconf(s, o, "autoconf-vm-area-pte.c", "STAPCONF_VM_AREA_PTE", NULL);
56111fdc 398 output_autoconf(s, o, "autoconf-relay-umode_t.c", "STAPCONF_RELAY_UMODE_T", NULL);
52dea427 399 output_autoconf(s, o, "autoconf-fs_supers-hlist.c", "STAPCONF_FS_SUPERS_HLIST", NULL);
fce34f35 400 output_autoconf(s, o, "autoconf-compat_sigaction.c", "STAPCONF_COMPAT_SIGACTION", NULL);
4d914c37 401 output_autoconf(s, o, "autoconf-netfilter.c", "STAPCONF_NETFILTER_V313", NULL);
7cfb10ec 402 output_autoconf(s, o, "autoconf-netfilter-313b.c", "STAPCONF_NETFILTER_V313B", NULL);
502f3816 403 output_autoconf(s, o, "autoconf-netfilter-4_1.c", "STAPCONF_NETFILTER_V41", NULL);
2736131d 404 output_autoconf(s, o, "autoconf-netfilter-4_4.c", "STAPCONF_NETFILTER_V44", NULL);
590a9ae1 405 output_autoconf(s, o, "autoconf-smpcall-5args.c", "STAPCONF_SMPCALL_5ARGS", NULL);
ceeaa82b 406 output_autoconf(s, o, "autoconf-smpcall-4args.c", "STAPCONF_SMPCALL_4ARGS", NULL);
db7dcdff 407
de5f477b
JS
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
64e807c2 412 // used by runtime/uprobe-inode.c
13128684
JS
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);
79af55c3 418 output_autoconf(s, o, "autoconf-inode-uretprobes.c", "STAPCONF_INODE_URETPROBES", NULL);
39e63bae
DS
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
fe6feb40 423 // used by runtime/loc2c-runtime.h
64e807c2
FCE
424 output_exportconf(s, o, "task_user_regset_view", "STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED");
425
36de3b61
DS
426 // used by runtime/stp_utrace.c
427 output_exportconf(s, o, "task_work_add", "STAPCONF_TASK_WORK_ADD_EXPORTED");
d9d07e99
DS
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");
dfdbaa3d 430 output_exportconf(s, o, "signal_wake_up_state", "STAPCONF_SIGNAL_WAKE_UP_STATE_EXPORTED");
f346b8b3
DS
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");
36de3b61 433
8fee9cc5 434 output_autoconf(s, o, "autoconf-pagefault_disable.c", "STAPCONF_PAGEFAULT_DISABLE", NULL);
15cf9503 435 output_exportconf(s, o, "kallsyms_lookup_name", "STAPCONF_KALLSYMS");
85716315 436 output_autoconf(s, o, "autoconf-uidgid.c", "STAPCONF_LINUX_UIDGID_H", NULL);
4fe11374 437 output_exportconf(s, o, "sigset_from_compat", "STAPCONF_SIGSET_FROM_COMPAT_EXPORTED");
42c2d1ed
DS
438 output_exportconf(s, o, "vzalloc", "STAPCONF_VZALLOC");
439 output_exportconf(s, o, "vzalloc_node", "STAPCONF_VZALLOC_NODE");
c9c633a8 440 output_exportconf(s, o, "vmalloc_node", "STAPCONF_VMALLOC_NODE");
8fee9cc5 441
3ef9830a 442 output_autoconf(s, o, "autoconf-tracepoint-strings.c", "STAPCONF_TRACEPOINT_STRINGS", NULL);
bac8aa5a 443 output_autoconf(s, o, "autoconf-timerfd.c", "STAPCONF_TIMERFD_H", NULL);
3ef9830a 444
3f040971
DS
445 output_autoconf(s, o, "autoconf-module_layout.c",
446 "STAPCONF_MODULE_LAYOUT", NULL);
447
de0db58a 448 o << module_cflags << " += -include $(STAPCONF_HEADER)" << endl;
5c54d49e 449
08fce398
SM
450 for (unsigned i=0; i<s.c_macros.size(); i++)
451 o << "EXTRA_CFLAGS += -D " << lex_cast_qstring(s.c_macros[i]) << endl; // XXX right quoting?
ed10c639 452
e4c58386 453 if (s.verbose > 3)
0986ac38 454 o << "EXTRA_CFLAGS += -ftime-report -Q" << endl;
db22e55f 455
ee6dda6f 456 // XXX: unfortunately, -save-temps can't work since linux kbuild cwd
100a540e 457 // is not writable.
ee6dda6f
FCE
458 //
459 // if (s.keep_tmpdir)
460 // o << "CFLAGS += -fverbose-asm -save-temps" << endl;
461
ec6fdef5
MW
462 // Kernels can be compiled with CONFIG_CC_OPTIMIZE_FOR_SIZE to select
463 // -Os, otherwise -O2 is the default.
464 o << "EXTRA_CFLAGS += -freorder-blocks" << endl; // improve on -Os
977b259e
JS
465
466 // Generate eh_frame for self-backtracing
467 o << "EXTRA_CFLAGS += -fasynchronous-unwind-tables" << endl;
ec6fdef5 468
01116596
MW
469 // We used to allow the user to override default optimization when so
470 // requested by adding a -O[0123s] so they could determine the
471 // time/space/speed tradeoffs themselves, but we cannot guantantee that
472 // the (un)optimized code actually compiles and/or generates functional
473 // code, so we had to remove it.
474 // o << "EXTRA_CFLAGS += " << s.gcc_flags << endl; // Add -O[0123s]
35d4ab18
FCE
475
476 // o << "CFLAGS += -fno-unit-at-a-time" << endl;
dff50e09 477
de4123d6 478 // 256^W512 bytes should be enough for anybody
01a2eba8 479 // XXX this doesn't validate varargs, per gcc bug #41633
de4123d6 480 o << "EXTRA_CFLAGS += $(call cc-option,-Wframe-larger-than=512)" << endl;
730c3efc 481
c1e83543
FCE
482 // gcc 5.0.0-0.13.fc23 ipa-icf seems to consume gigacpu on stap-generated code
483 o << "EXTRA_CFLAGS += $(call cc-option,-fno-ipa-icf)" << endl;
484
cbfbbf69 485 // Assumes linux 2.6 kbuild
2537a880 486 o << "EXTRA_CFLAGS += -Wno-unused " << WERROR << endl;
29bdbdb1
FCE
487 #if CHECK_POINTER_ARITH_PR5947
488 o << "EXTRA_CFLAGS += -Wpointer-arith" << endl;
489 #endif
0986ac38 490 o << "EXTRA_CFLAGS += -I\"" << s.runtime_path << "\"" << endl;
4b2c4ab5
FCE
491 // XXX: this may help ppc toc overflow
492 // o << "CFLAGS := $(subst -Os,-O2,$(CFLAGS)) -fminimal-toc" << endl;
cbfbbf69 493 o << "obj-m := " << s.module_name << ".o" << endl;
f4b28491 494
a4b9c3b3
FCE
495 // print out all the auxiliary source (->object) file names
496 o << s.module_name << "-y := ";
497 for (unsigned i=0; i<s.auxiliary_outputs.size(); i++)
498 {
80d74c6b 499 if (s.auxiliary_outputs[i]->trailer_p) continue;
a4b9c3b3
FCE
500 string srcname = s.auxiliary_outputs[i]->filename;
501 assert (srcname != "" && srcname.rfind('/') != string::npos);
502 string objname = srcname.substr(srcname.rfind('/')+1); // basename
503 assert (objname != "" && objname[objname.size()-1] == 'c');
504 objname[objname.size()-1] = 'o'; // now objname
505 o << " " + objname;
506 }
507 // and once again, for the translated_source file. It can't simply
508 // be named MODULENAME.c, since kbuild doesn't allow a foo.ko file
509 // consisting of multiple .o's to have foo.o/foo.c as a source.
510 // (It uses ld -r -o foo.o EACH.o EACH.o).
511 {
512 string srcname = s.translated_source;
513 assert (srcname != "" && srcname.rfind('/') != string::npos);
514 string objname = srcname.substr(srcname.rfind('/')+1); // basename
515 assert (objname != "" && objname[objname.size()-1] == 'c');
516 objname[objname.size()-1] = 'o'; // now objname
517 o << " " + objname;
518 }
80d74c6b
FCE
519 // and once again, for the trailer type auxiliary outputs.
520 for (unsigned i=0; i<s.auxiliary_outputs.size(); i++)
521 {
522 if (! s.auxiliary_outputs[i]->trailer_p) continue;
523 string srcname = s.auxiliary_outputs[i]->filename;
524 assert (srcname != "" && srcname.rfind('/') != string::npos);
525 string objname = srcname.substr(srcname.rfind('/')+1); // basename
526 assert (objname != "" && objname[objname.size()-1] == 'c');
527 objname[objname.size()-1] = 'o'; // now objname
528 o << " " + objname;
529 }
a4b9c3b3
FCE
530 o << endl;
531
532 // add all stapconf dependencies
533 o << s.translated_source << ": $(STAPCONF_HEADER)" << endl;
534 for (unsigned i=0; i<s.auxiliary_outputs.size(); i++)
535 o << s.auxiliary_outputs[i]->filename << ": $(STAPCONF_HEADER)" << endl;
536
537
ed10c639
FCE
538 o.close ();
539
b40af7ee 540 // Generate module directory pathname and make sure it exists.
4d34dac1
FCE
541 string module_dir = s.kernel_build_tree;
542 string module_dir_makefile = module_dir + "/Makefile";
b40af7ee 543 struct stat st;
4d34dac1 544 rc = stat(module_dir_makefile.c_str(), &st);
b40af7ee
DS
545 if (rc != 0)
546 {
ce0f6648 547 clog << _F("Checking \" %s \" failed with error: %s\nEnsure kernel development headers & makefiles are installed.",
46a1a151 548 module_dir_makefile.c_str(), strerror(errno)) << endl;
3e1ec884 549 s.set_try_server ();
b40af7ee 550 return rc;
dff50e09 551 }
b40af7ee
DS
552
553 // Run make
7d26ee02 554 vector<string> make_cmd = make_make_cmd(s, s.tmpdir);
6274464e 555 rc = run_make_cmd(s, make_cmd);
3e1ec884
DB
556 if (rc)
557 s.set_try_server ();
f4b28491
FCE
558 return rc;
559}
560
db3a383b
JK
561/*
562 * If uprobes was built as part of the kernel build (either built-in
dbb9345d
FCE
563 * or as a module), the uprobes exports should show up. This is to be
564 * as distinct from the stap-built uprobes.ko from the runtime.
db3a383b
JK
565 */
566static bool
567kernel_built_uprobes (systemtap_session& s)
6274464e 568{
ac3af990 569 if (s.runtime_usermode_p())
4441e344
JS
570 return true; // sort of, via dyninst
571
95113339
FCE
572 // see also tapsets.cxx:kernel_supports_inode_uprobes()
573 return ((s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y" && s.kernel_config["CONFIG_UPROBES"] == "y") ||
574 (s.kernel_exports.find("unregister_uprobe") != s.kernel_exports.end()));
6274464e
JK
575}
576
7d26ee02
JS
577static int
578make_uprobes (systemtap_session& s)
6274464e 579{
7ef486ad 580 if (s.verbose > 1)
7d26ee02 581 clog << _("Pass 4, preamble: (re)building SystemTap's version of uprobes.")
db3a383b
JK
582 << endl;
583
7d26ee02
JS
584 // create a subdirectory for the uprobes module
585 string dir(s.tmpdir + "/uprobes");
586 if (create_dir(dir.c_str()) != 0)
587 {
2713ea24 588 s.print_warning("failed to create directory for build uprobes.");
7d26ee02
JS
589 s.set_try_server ();
590 return 1;
0da3e7a0 591 }
6274464e 592
7d26ee02
JS
593 // create a simple Makefile
594 string makefile(dir + "/Makefile");
595 ofstream omf(makefile.c_str());
596 omf << "obj-m := uprobes.o" << endl;
597 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
598 omf << "CONFIG_MODULE_SIG := n" << endl;
599 omf.close();
6274464e 600
7d26ee02 601 // create a simple #include-chained source file
2ba1736a 602 string runtimesourcefile(s.runtime_path + "/linux/uprobes/uprobes.c");
7d26ee02
JS
603 string sourcefile(dir + "/uprobes.c");
604 ofstream osrc(sourcefile.c_str());
605 osrc << "#include \"" << runtimesourcefile << "\"" << endl;
76d01f36
FCE
606
607 // pass --modinfo k=v to uprobes build too
608 for (unsigned i = 0; i < s.modinfos.size(); i++)
609 {
610 const string& mi = s.modinfos[i];
611 size_t loc = mi.find('=');
612 string tag = mi.substr (0, loc);
613 string value = mi.substr (loc+1);
614 osrc << "MODULE_INFO(" << tag << "," << lex_cast_qstring(value) << ");" << endl;
615 }
616
7d26ee02
JS
617 osrc.close();
618
619 // make the module
620 vector<string> make_cmd = make_make_cmd(s, dir);
ae5e0bbb 621 int rc = run_make_cmd(s, make_cmd);
7d26ee02
JS
622 if (!rc && !copy_file(dir + "/Module.symvers",
623 s.tmpdir + "/Module.symvers"))
624 rc = -1;
6274464e 625
f4d5049b 626 if (s.verbose > 1)
ce0f6648 627 clog << _("uprobes rebuild exit code: ") << rc << endl;
3e1ec884
DB
628 if (rc)
629 s.set_try_server ();
91ea0ce6 630 else
7d26ee02 631 s.uprobes_path = dir + "/uprobes.ko";
6274464e
JK
632 return rc;
633}
634
7d26ee02
JS
635static bool
636get_cached_uprobes(systemtap_session& s)
637{
638 s.uprobes_hash = s.use_cache ? find_uprobes_hash(s) : "";
639 if (!s.uprobes_hash.empty())
640 {
641 // NB: We always put uprobes.ko in its own directory, especially so
642 // stap-serverd can more easily locate it.
643 string dir(s.tmpdir + "/uprobes");
644 if (create_dir(dir.c_str()) != 0)
645 return false;
646
647 string cacheko = s.uprobes_hash + ".ko";
648 string tmpko = dir + "/uprobes.ko";
649
650 // The symvers file still needs to go in the script module's directory.
651 string cachesyms = s.uprobes_hash + ".symvers";
652 string tmpsyms = s.tmpdir + "/Module.symvers";
653
654 if (get_file_size(cacheko) > 0 && copy_file(cacheko, tmpko) &&
655 get_file_size(cachesyms) > 0 && copy_file(cachesyms, tmpsyms))
656 {
657 s.uprobes_path = tmpko;
658 return true;
659 }
660 }
661 return false;
662}
663
664static void
665set_cached_uprobes(systemtap_session& s)
db3a383b 666{
7d26ee02
JS
667 if (s.use_cache && !s.uprobes_hash.empty())
668 {
669 string cacheko = s.uprobes_hash + ".ko";
670 string tmpko = s.tmpdir + "/uprobes/uprobes.ko";
671 copy_file(tmpko, cacheko);
36ef6d6a 672
7d26ee02
JS
673 string cachesyms = s.uprobes_hash + ".symvers";
674 string tmpsyms = s.tmpdir + "/uprobes/Module.symvers";
675 copy_file(tmpsyms, cachesyms);
676 }
db3a383b
JK
677}
678
7d26ee02 679int
db3a383b
JK
680uprobes_pass (systemtap_session& s)
681{
682 if (!s.need_uprobes || kernel_built_uprobes(s))
683 return 0;
0944a81e 684
6c7e2241 685 if (s.kernel_config["CONFIG_UTRACE"] != string("y"))
01fa08f2 686 {
6c9f0b7b 687 clog << _("user-space process-tracking facilities not available [man error::process-tracking]") << endl;
01fa08f2
DS
688 s.set_try_server ();
689 return 1;
690 }
0944a81e 691
db3a383b 692 /*
7d26ee02
JS
693 * We need to use the version of uprobes that comes with SystemTap. Try to
694 * get it from the cache first. If not found, build it and try to save it to
695 * the cache for future reuse.
db3a383b 696 */
7d26ee02
JS
697 int rc = 0;
698 if (!get_cached_uprobes(s))
699 {
700 rc = make_uprobes(s);
701 if (!rc)
702 set_cached_uprobes(s);
703 }
3e1ec884
DB
704 if (rc)
705 s.set_try_server ();
db3a383b
JK
706 return rc;
707}
708
3a894f7e
JS
709static
710vector<string>
711make_dyninst_run_command (systemtap_session& s, const string& remotedir,
dabd71bb 712 const string&)
3a894f7e
JS
713{
714 vector<string> cmd;
56658530 715 cmd.push_back(getenv("SYSTEMTAP_STAPDYN") ?: BINDIR "/stapdyn");
e75a4464
JS
716
717 // use slightly less verbosity
718 for (unsigned i=1; i<s.verbose; i++)
41300e6d
JS
719 cmd.push_back("-v");
720 if (s.suppress_warnings)
721 cmd.push_back("-w");
3a894f7e
JS
722
723 if (!s.cmd.empty())
724 {
725 cmd.push_back("-c");
726 cmd.push_back(s.cmd);
727 }
728
02bff028
JS
729 if (s.target_pid)
730 {
731 cmd.push_back("-x");
732 cmd.push_back(lex_cast(s.target_pid));
733 }
734
6d842dc7
DS
735 if (!s.output_file.empty())
736 {
737 cmd.push_back("-o");
738 cmd.push_back(s.output_file);
739 }
740
192130b3
JL
741 if (s.color_mode != s.color_auto)
742 {
743 cmd.push_back("-C");
744 if (s.color_mode == s.color_always)
745 cmd.push_back("always");
746 else
747 cmd.push_back("never");
748 }
749
3a894f7e 750 cmd.push_back((remotedir.empty() ? s.tmpdir : remotedir)
4441e344 751 + "/" + s.module_filename());
3a894f7e 752
bd268288
SM
753 // add module arguments
754 cmd.insert(cmd.end(), s.globalopts.begin(), s.globalopts.end());
755
3a894f7e
JS
756 return cmd;
757}
3a894f7e 758
5eea6ed1 759vector<string>
18630fb8 760make_run_command (systemtap_session& s, const string& remotedir,
4112f219 761 const string& version)
f4b28491 762{
ac3af990 763 if (s.runtime_usermode_p())
4441e344
JS
764 return make_dyninst_run_command(s, remotedir, version);
765
a63a95dc 766 // for now, just spawn staprun
5eea6ed1
JS
767 vector<string> staprun_cmd;
768 staprun_cmd.push_back(getenv("SYSTEMTAP_STAPRUN") ?: BINDIR "/staprun");
e75a4464
JS
769
770 // use slightly less verbosity
771 for (unsigned i=1; i<s.verbose; i++)
5eea6ed1
JS
772 staprun_cmd.push_back("-v");
773 if (s.suppress_warnings)
774 staprun_cmd.push_back("-w");
775
776 if (!s.output_file.empty())
777 {
778 staprun_cmd.push_back("-o");
779 staprun_cmd.push_back(s.output_file);
780 }
dff50e09 781
5eea6ed1
JS
782 if (!s.cmd.empty())
783 {
784 staprun_cmd.push_back("-c");
785 staprun_cmd.push_back(s.cmd);
786 }
dff50e09 787
cbfbbf69 788 if (s.target_pid)
5eea6ed1
JS
789 {
790 staprun_cmd.push_back("-t");
791 staprun_cmd.push_back(lex_cast(s.target_pid));
792 }
dff50e09 793
e9bb2221
AJ
794 if (s.target_namespaces_pid)
795 {
796 staprun_cmd.push_back("-N");
797 staprun_cmd.push_back(lex_cast(s.target_namespaces_pid));
798 }
799
cbfbbf69 800 if (s.buffer_size)
5eea6ed1
JS
801 {
802 staprun_cmd.push_back("-b");
803 staprun_cmd.push_back(lex_cast(s.buffer_size));
804 }
dff50e09 805
13128684 806 if (s.need_uprobes && !kernel_built_uprobes(s))
5eea6ed1 807 {
18630fb8
JS
808 string opt_u = "-u";
809 if (!s.uprobes_path.empty() &&
810 strverscmp("1.4", version.c_str()) <= 0)
811 {
812 if (remotedir.empty())
813 opt_u.append(s.uprobes_path);
814 else
815 opt_u.append(remotedir + "/" + basename(s.uprobes_path.c_str()));
816 }
817 staprun_cmd.push_back(opt_u);
5eea6ed1 818 }
6274464e 819
2fa2a091 820 if (s.load_only)
5eea6ed1 821 staprun_cmd.push_back(s.output_file.empty() ? "-L" : "-D");
701c41be 822
b3367f63
DS
823 // Note that if this system requires signed modules, we can't rename
824 // it after it has been signed.
825 if (!s.modname_given && (strverscmp("1.6", version.c_str()) <= 0)
cd1418c7 826 && s.mok_fingerprints.empty())
5c854d7c
CM
827 staprun_cmd.push_back("-R");
828
701c41be 829 if (!s.size_option.empty())
5eea6ed1
JS
830 {
831 staprun_cmd.push_back("-S");
832 staprun_cmd.push_back(s.size_option);
833 }
2fa2a091 834
192130b3 835 if (s.color_mode != s.color_auto)
579f0947
JL
836 {
837 staprun_cmd.push_back("-C");
6d3599fc
JL
838 if (s.color_mode == s.color_always)
839 staprun_cmd.push_back("always");
840 else
192130b3 841 staprun_cmd.push_back("never");
579f0947
JL
842 }
843
10bb5efc
FL
844 if (s.monitor)
845 {
846 staprun_cmd.push_back("-M");
09ddc83c 847 staprun_cmd.push_back(lex_cast(s.monitor_interval));
10bb5efc
FL
848 }
849
18630fb8 850 staprun_cmd.push_back((remotedir.empty() ? s.tmpdir : remotedir)
4441e344 851 + "/" + s.module_filename());
dff50e09 852
bb25d08f 853 // add module arguments
5eea6ed1
JS
854 staprun_cmd.insert(staprun_cmd.end(),
855 s.globalopts.begin(), s.globalopts.end());
bb25d08f 856
daa75206 857 return staprun_cmd;
f4b28491 858}
73267b89 859
0a6f5a3f 860
c9ccb642
FCE
861// Build tiny kernel modules to query tracepoints.
862// Given a (header-file -> test-contents) map, compile them ASAP, and return
2a0e62a8 863// a (header-file -> obj-filename) map.
c9ccb642
FCE
864
865map<string,string>
866make_tracequeries(systemtap_session& s, const map<string,string>& contents)
0a6f5a3f 867{
f982c59b 868 static unsigned tick = 0;
aca66a36 869 string basename("tracequery_kmod_" + lex_cast(++tick));
2a0e62a8 870 map<string,string> objs;
f982c59b 871
0a6f5a3f 872 // create a subdirectory for the module
f982c59b 873 string dir(s.tmpdir + "/" + basename);
0a6f5a3f
JS
874 if (create_dir(dir.c_str()) != 0)
875 {
2713ea24 876 s.print_warning("failed to create directory for querying tracepoints.");
3e1ec884 877 s.set_try_server ();
2a0e62a8 878 return objs;
0a6f5a3f
JS
879 }
880
0a6f5a3f
JS
881 // create a simple Makefile
882 string makefile(dir + "/Makefile");
883 ofstream omf(makefile.c_str());
9ccda279 884 // force debuginfo generation, and relax implicit functions
2537a880 885 omf << "EXTRA_CFLAGS := -g -Wno-implicit-function-declaration " << WERROR << endl;
a7ec2317
FCE
886 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
887 omf << "CONFIG_MODULE_SIG := n" << endl;
888
8bb62408
JS
889 // PR18389: disable GCC's Identical Code Folding, since the stubs may look identical
890 omf << "EXTRA_CFLAGS += $(call cc-option,-fno-ipa-icf)" << endl;
891
18b13994 892 omf << "EXTRA_CFLAGS += -I" + s.kernel_build_tree << endl;
c9ccb642
FCE
893 if (s.kernel_source_tree != "")
894 omf << "EXTRA_CFLAGS += -I" + s.kernel_source_tree << endl;
ac4f2837
DS
895 for (unsigned i = 0; i < s.kernel_extra_cflags.size(); i++)
896 omf << "EXTRA_CFLAGS += " + s.kernel_extra_cflags[i] << endl;
0a6f5a3f 897
c9ccb642
FCE
898 omf << "obj-m := " << endl;
899 // write out each header-specific source file into a separate file
900 for (map<string,string>::const_iterator it = contents.begin(); it != contents.end(); it++)
901 {
902 string sbasename = basename + "_" + lex_cast(++tick); // suffixed
903
904 // write out source code
905 string srcname = dir + "/" + sbasename + ".c";
906 string src = it->second;
907 ofstream osrc(srcname.c_str());
908 osrc << src;
909 osrc.close();
910
77b163ae
FCE
911 if (s.verbose > 2)
912 clog << _F("Processing tracepoint header %s with query %s",
913 it->first.c_str(), srcname.c_str())
914 << endl;
915
c9ccb642
FCE
916 // arrange to build it
917 omf << "obj-m += " + sbasename + ".o" << endl; // NB: without <dir> prefix
2a0e62a8 918 objs[it->first] = dir + "/" + sbasename + ".o";
c9ccb642
FCE
919 }
920 omf.close();
0a6f5a3f
JS
921
922 // make the module
8e0a2563 923 vector<string> make_cmd = make_make_objs_cmd(s, dir);
e86f32ee 924 make_cmd.push_back ("-i"); // ignore errors, give rc 0 even in case of tracepoint header nits
ff520ff4
JS
925 bool quiet = (s.verbose < 4);
926 int rc = run_make_cmd(s, make_cmd, quiet, quiet);
3e1ec884
DB
927 if (rc)
928 s.set_try_server ();
c2cf1b87 929
e86f32ee
FCE
930 // Sometimes we fail a tracequery due to PR9993 / PR11649 type
931 // kernel trace header problems. In this case, due to PR12729, we
932 // used to get a lovely "Warning: make exited with status: 2" but no
c2cf1b87
FCE
933 // other useful diagnostic. -vvvv would let a user see what's up,
934 // but the user can't fix the problem even with that.
935
2a0e62a8 936 return objs;
0a6f5a3f
JS
937}
938
3e1c25aa
JS
939
940// Build a tiny kernel module to query type information
d90053e7 941static int
da9e11bd 942make_typequery_kmod(systemtap_session& s, const vector<string>& headers, string& name)
3e1c25aa
JS
943{
944 static unsigned tick = 0;
aca66a36 945 string basename("typequery_kmod_" + lex_cast(++tick));
3e1c25aa
JS
946
947 // create a subdirectory for the module
948 string dir(s.tmpdir + "/" + basename);
949 if (create_dir(dir.c_str()) != 0)
950 {
2713ea24 951 s.print_warning("failed to create directory for querying types.");
3e1ec884 952 s.set_try_server ();
3e1c25aa
JS
953 return 1;
954 }
955
956 name = dir + "/" + basename + ".ko";
957
958 // create a simple Makefile
959 string makefile(dir + "/Makefile");
960 ofstream omf(makefile.c_str());
961 omf << "EXTRA_CFLAGS := -g -fno-eliminate-unused-debug-types" << endl;
3ae4cdf9 962
a7ec2317
FCE
963 // RHBZ 655231: later rhel6 kernels' module-signing kbuild logic breaks out-of-tree modules
964 omf << "CONFIG_MODULE_SIG := n" << endl;
965
3ae4cdf9
JS
966 // NB: We use -include instead of #include because that gives us more power.
967 // Using #include searches relative to the source's path, which in this case
968 // is /tmp/..., so that's not helpful. Using -include will search relative
969 // to the cwd, which will be the kernel build root. This means if you have a
970 // full kernel build tree, it's possible to get at types that aren't in the
971 // normal include path, e.g.:
972 // @cast(foo, "bsd_acct_struct", "kernel<kernel/acct.c>")->...
da9e11bd
JS
973 omf << "CFLAGS_" << basename << ".o :=";
974 for (size_t i = 0; i < headers.size(); ++i)
ff520ff4 975 omf << " -include " << lex_cast_qstring(headers[i]); // XXX right quoting?
da9e11bd 976 omf << endl;
3ae4cdf9 977
3e1c25aa
JS
978 omf << "obj-m := " + basename + ".o" << endl;
979 omf.close();
980
981 // create our empty source file
982 string source(dir + "/" + basename + ".c");
983 ofstream osrc(source.c_str());
984 osrc.close();
985
986 // make the module
7d26ee02 987 vector<string> make_cmd = make_make_cmd(s, dir);
ff520ff4
JS
988 bool quiet = (s.verbose < 4);
989 int rc = run_make_cmd(s, make_cmd, quiet, quiet);
3e1ec884
DB
990 if (rc)
991 s.set_try_server ();
992 return rc;
3e1c25aa
JS
993}
994
995
996// Build a tiny user module to query type information
d90053e7 997static int
da9e11bd 998make_typequery_umod(systemtap_session& s, const vector<string>& headers, string& name)
3e1c25aa
JS
999{
1000 static unsigned tick = 0;
1001
aca66a36 1002 name = s.tmpdir + "/typequery_umod_" + lex_cast(++tick) + ".so";
3e1c25aa
JS
1003
1004 // make the module
3ae4cdf9
JS
1005 //
1006 // NB: As with kmod, using -include makes relative paths more useful. The
1007 // cwd in this case will be the cwd of stap itself though, which may be
1008 // trickier to deal with. It might be better to "cd `dirname $script`"
1009 // first...
ff520ff4
JS
1010 vector<string> cmd;
1011 cmd.push_back("gcc");
1012 cmd.push_back("-shared");
1013 cmd.push_back("-g");
1014 cmd.push_back("-fno-eliminate-unused-debug-types");
1015 cmd.push_back("-xc");
1016 cmd.push_back("/dev/null");
1017 cmd.push_back("-o");
1018 cmd.push_back(name);
da9e11bd 1019 for (size_t i = 0; i < headers.size(); ++i)
ff520ff4
JS
1020 {
1021 cmd.push_back("-include");
1022 cmd.push_back(headers[i]);
1023 }
1024 bool quiet = (s.verbose < 4);
1025 int rc = stap_system (s.verbose, cmd, quiet, quiet);
3e1ec884
DB
1026 if (rc)
1027 s.set_try_server ();
1028 return rc;
3e1c25aa
JS
1029}
1030
d90053e7
JS
1031
1032int
1033make_typequery(systemtap_session& s, string& module)
1034{
1035 int rc;
1036 string new_module;
da9e11bd 1037 vector<string> headers;
60d98537 1038 bool kernel = startswith(module, "kernel");
d90053e7 1039
da9e11bd 1040 for (size_t end, i = kernel ? 6 : 0; i < module.size(); i = end + 1)
d90053e7 1041 {
da9e11bd
JS
1042 if (module[i] != '<')
1043 return -1;
1044 end = module.find('>', ++i);
1045 if (end == string::npos)
1046 return -1;
1047 string header = module.substr(i, end - i);
504d2b61 1048 vector<string> matches;
5ef28d5a 1049 if (regexp_match(header, "^[a-zA-Z0-9/_.+-]+$", matches))
2713ea24 1050 s.print_warning("skipping malformed @cast header \""+ header + "\"");
504d2b61
JS
1051 else
1052 headers.push_back(header);
d90053e7 1053 }
da9e11bd 1054 if (headers.empty())
d90053e7
JS
1055 return -1;
1056
da9e11bd
JS
1057 if (kernel)
1058 rc = make_typequery_kmod(s, headers, new_module);
1059 else
1060 rc = make_typequery_umod(s, headers, new_module);
1061
d90053e7
JS
1062 if (!rc)
1063 module = new_module;
1064
1065 return rc;
1066}
1067
73267b89 1068/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.292458 seconds and 5 git commands to generate.