]> sourceware.org Git - systemtap.git/blob - hash.cxx
2006-12-11 David Smith <dsmith@redhat.com>
[systemtap.git] / hash.cxx
1 // Copyright (C) Andrew Tridgell 2002 (original file)
2 // Copyright (C) 2006 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 #include <sstream>
23 #include <iomanip>
24 #include <cerrno>
25
26 extern "C" {
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 }
31
32 using namespace std;
33
34 void
35 hash::start()
36 {
37 mdfour_begin(&md4);
38 }
39
40
41 void
42 hash::add(const unsigned char *buffer, size_t size)
43 {
44 mdfour_update(&md4, buffer, size);
45 }
46
47
48 void
49 hash::result(string& r)
50 {
51 ostringstream rstream;
52 unsigned char sum[16];
53
54 mdfour_update(&md4, NULL, 0);
55 mdfour_result(&md4, sum);
56
57 for (int i=0; i<16; i++)
58 {
59 rstream << hex << setfill('0') << setw(2) << (unsigned)sum[i];
60 }
61 rstream << "_" << setw(0) << dec << (unsigned)md4.totalN;
62 r = rstream.str();
63 }
64
65
66 // Grabbed from linux/module.h kernel include.
67 #define MODULE_NAME_LEN (64 - sizeof(unsigned long))
68
69 void
70 find_hash (systemtap_session& s, const string& script)
71 {
72 hash h;
73 int nlevels = 1;
74 struct stat st;
75
76 // We use a N level subdir for the cache path. Let N be adjustable.
77 const char *s_n;
78 if ((s_n = getenv("SYSTEMTAP_NLEVELS")))
79 {
80 nlevels = atoi(s_n);
81 if (nlevels < 1) nlevels = 1;
82 if (nlevels > 8) nlevels = 8;
83 }
84
85 // Hash getuid. This really shouldn't be necessary (since who you
86 // are doesn't change the generated output), but the hash gets used
87 // as the module name. If two different users try to run the same
88 // script at the same time, we need something to differentiate the
89 // module name.
90 h.add(getuid());
91
92 // Hash kernel release and arch.
93 h.add(s.kernel_release);
94 h.add(s.architecture);
95
96 // Hash user-specified arguments (that change the generated module).
97 h.add(s.bulk_mode);
98 for (unsigned i = 0; i < s.macros.size(); i++)
99 h.add(s.macros[i]);
100
101 // Hash runtime path (that gets added in as "-I path").
102 h.add(s.runtime_path);
103
104 // Hash compiler path, size, and mtime. We're just going to assume
105 // we'll be using gcc, which should be correct most of the time.
106 string gcc_path;
107 if (find_executable("gcc", gcc_path))
108 {
109 if (stat(gcc_path.c_str(), &st) == 0)
110 {
111 h.add(gcc_path);
112 h.add(st.st_size);
113 h.add(st.st_mtime);
114 }
115 }
116
117 // Hash the systemtap size and mtime. We could use VERSION/DATE,
118 // but when developing systemtap that doesn't work well (since you
119 // can compile systemtap multiple times in 1 day). Since we don't
120 // know exactly where we're getting run from, we'll use
121 // /proc/self/exe.
122 if (stat("/proc/self/exe", &st) == 0)
123 {
124 h.add(st.st_size);
125 h.add(st.st_mtime);
126 }
127
128 // Add in pass 2 script output.
129 h.add(script);
130
131 // Use a N level subdir for the cache path to reduce the impact on
132 // filesystems which are slow for large directories.
133 string hashdir = s.cache_path;
134 string result;
135 h.result(result);
136
137 for (int i = 0; i < nlevels; i++)
138 {
139 hashdir += string("/") + result[i*2] + result[i*2 + 1];
140 if (create_dir(hashdir.c_str()) != 0)
141 {
142 cerr << "Warning: failed to create cache directory (\""
143 << hashdir + "\"): " << strerror(errno) << endl;
144 cerr << "Disabling cache support." << endl;
145 s.use_cache = false;
146 return;
147 }
148 }
149
150 // Update module name to be 'stap_{hash start}'. '{hash start}'
151 // must not be too long. This shouldn't happen, since the maximum
152 // size of a hash is 32 fixed chars + 1 (for the '_') + a max of 11.
153 s.module_name = "stap_" + result;
154 if (s.module_name.size() >= (MODULE_NAME_LEN - 1))
155 s.module_name.resize(MODULE_NAME_LEN - 1);
156
157 // 'ccache' would use a hash path of something like:
158 // s.hash_path = hashdir + "/" + result.substr(nlevels);
159 // which would look like:
160 // ~/.stap_cache/A/B/CDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX
161 //
162 // We're using the following so that the module can be used straight
163 // from the cache if desired. This ends up looking like this:
164 // ~/.stap_cache/A/B/stap_ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF_XXX.ko
165 s.hash_path = hashdir + "/" + s.module_name + ".ko";
166
167 // Update C source name with new module_name.
168 s.translated_source = string(s.tmpdir) + "/" + s.module_name + ".c";
169 }
This page took 0.045939 seconds and 6 git commands to generate.