]> sourceware.org Git - systemtap.git/blame - buildrun.cxx
2006-09-26 David Smith <dsmith@redhat.com>
[systemtap.git] / buildrun.cxx
CommitLineData
f4b28491 1// build/run probes
70404fc5 2// Copyright (C) 2005, 2006 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"
f4b28491
FCE
12
13#include <fstream>
d04cf5ff
FCE
14#include <sstream>
15
16extern "C" {
17#include "signal.h"
0c6296b2 18#include <sys/wait.h>
70404fc5 19#include <pwd.h>
b40af7ee
DS
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <unistd.h>
23#include <string.h>
24#include <errno.h>
d04cf5ff
FCE
25}
26
f4b28491
FCE
27
28using namespace std;
29
30
ed10c639
FCE
31// return as quoted string, with at least '"' backslash-escaped
32template <typename IN> inline string
33lex_cast_qstring(IN const & in)
34{
35 stringstream ss;
36 string out, out2;
37 if (!(ss << in))
38 throw runtime_error("bad lexical cast");
39 out = ss.str();
40 out2 += '"';
41 for (unsigned i=0; i<out.length(); i++)
42 {
43 if (out[i] == '"') // XXX others?
44 out2 += '\\';
45 out2 += out[i];
46 }
47 out2 += '"';
48 return out2;
49}
50
d04cf5ff 51
0c6296b2
FCE
52template <typename T>
53static string
54stringify(T t)
55{
56 ostringstream s;
57 s << t;
58 return s.str ();
59}
60
61
62
f4b28491
FCE
63int
64compile_pass (systemtap_session& s)
65{
66 // fill in a quick Makefile
92ade41d
FCE
67 string makefile_nm = s.tmpdir + "/Makefile";
68 ofstream o (makefile_nm.c_str());
69 int rc = 0;
70
71 // Create makefile
ed10c639
FCE
72
73 for (unsigned i=0; i<s.macros.size(); i++)
74 o << "CFLAGS += -D " << lex_cast_qstring(s.macros[i]) << endl;
75
35d4ab18
FCE
76 if (s.verbose > 2)
77 o << "CFLAGS += -ftime-report -Q" << endl;
db22e55f 78
35d4ab18
FCE
79 o << "CFLAGS += -freorder-blocks" << endl; // improve on -Os
80
81 // o << "CFLAGS += -fno-unit-at-a-time" << endl;
82
cbfbbf69
FCE
83 // Assumes linux 2.6 kbuild
84 o << "CFLAGS += -Wno-unused -Werror" << endl;
de2ca07e 85 o << "CFLAGS += -I\"" << s.runtime_path << "\"" << endl;
cbfbbf69 86 o << "obj-m := " << s.module_name << ".o" << endl;
f4b28491 87
ed10c639
FCE
88 o.close ();
89
b40af7ee 90 // Generate module directory pathname and make sure it exists.
cbfbbf69
FCE
91 string module_dir = string("/lib/modules/")
92 + s.kernel_release + "/build";
b40af7ee
DS
93 struct stat st;
94 rc = stat(module_dir.c_str(), &st);
95 if (rc != 0)
96 {
97 clog << "Module directory " << module_dir << " check failed: "
98 << strerror(errno) << endl
99 << "Make sure kernel devel is installed." << endl;
100 return rc;
101 }
102
103 // Run make
cbfbbf69
FCE
104 string make_cmd = string("make")
105 + string (" -C \"") + module_dir + string("\"");
106 make_cmd += string(" M=\"") + s.tmpdir + string("\" modules");
107
b0ee93c4 108 if (s.verbose > 1)
cbfbbf69 109 make_cmd += " V=1";
92ade41d 110 else
cbfbbf69
FCE
111 make_cmd += " -s >/dev/null 2>&1";
112
b0ee93c4 113 if (s.verbose > 1) clog << "Running " << make_cmd << endl;
cbfbbf69
FCE
114 rc = system (make_cmd.c_str());
115
f4b28491
FCE
116 return rc;
117}
118
119
120int
121run_pass (systemtap_session& s)
122{
92ade41d
FCE
123 int rc = 0;
124
70404fc5
MH
125 struct passwd *pw = getpwuid(getuid());
126 string username = string(pw->pw_name);
127
cbfbbf69
FCE
128 // for now, just spawn stpd
129 string stpd_cmd = string("sudo ")
130 + string(PKGLIBDIR) + "/stpd "
b0ee93c4 131 + (s.verbose>1 ? "" : "-q ")
70404fc5 132 + "-u " + username + " "
cbfbbf69
FCE
133 + (s.output_file.empty() ? "" : "-o " + s.output_file + " ");
134
135 stpd_cmd += "-d " + stringify(getpid()) + " ";
136
137 if (s.cmd != "")
138 stpd_cmd += "-c \"" + s.cmd + "\" ";
139
140 if (s.target_pid)
141 stpd_cmd += "-t " + stringify(s.target_pid) + " ";
142
143 if (s.buffer_size)
144 stpd_cmd += "-b " + stringify(s.buffer_size) + " ";
145
146 stpd_cmd += s.tmpdir + "/" + s.module_name + ".ko";
147
b0ee93c4 148 if (s.verbose>1) clog << "Running " << stpd_cmd << endl;
cbfbbf69
FCE
149
150 signal (SIGHUP, SIG_IGN);
151 signal (SIGINT, SIG_IGN);
152 rc = system (stpd_cmd.c_str ());
92ade41d
FCE
153
154 return rc;
f4b28491 155}
This page took 0.057445 seconds and 5 git commands to generate.