]> sourceware.org Git - systemtap.git/blob - hash.cxx
prep find_executable() for use by process() probes
[systemtap.git] / hash.cxx
1 // Copyright (C) Andrew Tridgell 2002 (original file)
2 // Copyright (C) 2006-2008 Red Hat Inc. (systemtap changes)
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 #include "config.h"
19 #include "session.h"
20 #include "hash.h"
21 #include "util.h"
22
23 #include <cstdlib>
24 #include <sstream>
25 #include <iomanip>
26 #include <cerrno>
27
28 extern "C" {
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 }
33
34 using namespace std;
35
36 void
37 hash::start()
38 {
39 mdfour_begin(&md4);
40 }
41
42
43 void
44 hash::add(const unsigned char *buffer, size_t size)
45 {
46 mdfour_update(&md4, buffer, size);
47 }
48
49
50 void
51 hash::result(string& r)
52 {
53 ostringstream rstream;
54 unsigned char sum[16];
55
56 mdfour_update(&md4, NULL, 0);
57 mdfour_result(&md4, sum);
58
59 for (int i=0; i<16; i++)
60 {
61 rstream << hex << setfill('0') << setw(2) << (unsigned)sum[i];
62 }
63 rstream << "_" << setw(0) << dec << (unsigned)md4.totalN;
64 r = rstream.str();
65 }
66
67
68 void
69 find_hash (systemtap_session& s, const string& script)
70 {
71 hash h;
72 int nlevels = 1;
73 struct stat st;
74
75 // We use a N level subdir for the cache path. Let N be adjustable.
76 const char *s_n;
77 if ((s_n = getenv("SYSTEMTAP_NLEVELS")))
78 {
79 nlevels = atoi(s_n);
80 if (nlevels < 1) nlevels = 1;
81 if (nlevels > 8) nlevels = 8;
82 }
83
84 // Hash getuid. This really shouldn't be necessary (since who you
85 // are doesn't change the generated output), but the hash gets used
86 // as the module name. If two different users try to run the same
87 // script at the same time, we need something to differentiate the
88 // module name.
89 h.add(getuid());
90
91 // Hash kernel release and arch.
92 h.add(s.kernel_release);
93 h.add(s.architecture);
94
95 // Hash user-specified arguments (that change the generated module).
96 h.add(s.bulk_mode); // '-b'
97 h.add(s.merge); // '-M'
98 h.add(s.timing); // '-t'
99 h.add(s.prologue_searching); // '-P'
100 h.add(s.ignore_vmlinux); // --ignore-vmlinux
101 h.add(s.ignore_dwarf); // --ignore-dwarf
102 h.add(s.consult_symtab); // --kelf, --kmap
103 if (!s.kernel_symtab_path.empty()) // --kmap
104 {
105 h.add(s.kernel_symtab_path);
106 if (stat(s.kernel_symtab_path.c_str(), &st) == 0)
107 {
108 // NB: stat of /proc/kallsyms always returns size=0, mtime=now...
109 // which is a good reason to use the default /boot/System.map-2.6.xx
110 // instead.
111 h.add(st.st_size);
112 h.add(st.st_mtime);
113 }
114 }
115 for (unsigned i = 0; i < s.macros.size(); i++)
116 h.add(s.macros[i]);
117
118 // -d MODULE
119 for (set<string>::iterator it = s.unwindsym_modules.begin();
120 it != s.unwindsym_modules.end();
121 it++)
122 h.add(*it);
123 // XXX: a build-id of each module might be even better
124
125 // Hash runtime path (that gets added in as "-R path").
126 h.add(s.runtime_path);
127
128 // Hash compiler path, size, and mtime. We're just going to assume
129 // we'll be using gcc. XXX: getting kbuild to spit out out would be
130 // better.
131 string gcc_path = find_executable ("gcc");
132 if (stat(gcc_path.c_str(), &st) == 0)
133 {
134 h.add(gcc_path);
135 h.add(st.st_size);
136 h.add(st.st_mtime);
137 }
138
139 // Hash the systemtap size and mtime. We could use VERSION/DATE,
140 // but when developing systemtap that doesn't work well (since you
141 // can compile systemtap multiple times in 1 day). Since we don't
142 // know exactly where we're getting run from, we'll use
143 // /proc/self/exe.
144 if (stat("/proc/self/exe", &st) == 0)
145 {
146 h.add(st.st_size);
147 h.add(st.st_mtime);
148 }
149
150 // Add in pass 2 script output.
151 h.add(script);
152
153 // Use a N level subdir for the cache path to reduce the impact on
154 // filesystems which are slow for large directories.
155 string hashdir = s.cache_path;
156 string result;
157 h.result(result);
158
159 for (int i = 0; i < nlevels; i++)
160 {
161 hashdir += string("/") + result[i*2] + result[i*2 + 1];
162 if (create_dir(hashdir.c_str()) != 0)
163 {
164 cerr << "Warning: failed to create cache directory (\""
165 << hashdir + "\"): " << strerror(errno) << endl;
166 cerr << "Disabling cache support." << endl;
167 s.use_cache = false;
168 return;
169 }
170 }
171
172 // Update module name to be 'stap_{hash start}'. '{hash start}'
173 // must not be too long. This shouldn't happen, since the maximum
174 // size of a hash is 32 fixed chars + 1 (for the '_') + a max of 11.
175 s.module_name = "stap_" + result;
176 if (s.module_name.size() >= (MODULE_NAME_LEN - 1))
177 s.module_name.resize(MODULE_NAME_LEN - 1);
178
179 // 'ccache' would use a hash path of something like:
180 // s.hash_path = hashdir + "/" + result.substr(nlevels);
181 // which would look like:
182 // ~/.stap_cache/A/B/CDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX
183 //
184 // We're using the following so that the module can be used straight
185 // from the cache if desired. This ends up looking like this:
186 // ~/.stap_cache/A/B/stap_ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX.ko
187 s.hash_path = hashdir + "/" + s.module_name + ".ko";
188
189 // Update C source name with new module_name.
190 s.translated_source = string(s.tmpdir) + "/" + s.module_name + ".c";
191 }
This page took 0.048624 seconds and 6 git commands to generate.