]> sourceware.org Git - systemtap.git/blob - util.h
Rename CONTEXT regflags to probe_flags. Now simply indicates user mode.
[systemtap.git] / util.h
1 #include "config.h"
2 #include <cstring>
3 #include <cerrno>
4 #include <string>
5 #include <vector>
6 #include <iostream>
7 #include <sstream>
8 #include <stdexcept>
9 #include <cctype>
10 #include <set>
11 #include <iomanip>
12 extern "C" {
13 #include <libintl.h>
14 #include <locale.h>
15 #include <signal.h>
16 #include <stdint.h>
17 #include <spawn.h>
18 #include <assert.h>
19 }
20
21 #if ENABLE_NLS
22 #define _(string) gettext(string)
23 #define _N(string, string_plural, count) \
24 ngettext((string), (string_plural), (count))
25 #else
26 #define _(string) (string)
27 #define _N(string, string_plural, count) \
28 ( (count) == 1 ? (string) : (string_plural) )
29 #endif
30 #define _F(format, ...) autosprintf(_(format), __VA_ARGS__)
31 #define _NF(format, format_plural, count, ...) \
32 autosprintf(_N((format), (format_plural), (count)), __VA_ARGS__)
33
34 const char *get_home_directory(void);
35 size_t get_file_size(const std::string &path);
36 size_t get_file_size(int fd);
37 bool file_exists (const std::string &path);
38 bool copy_file(const std::string& src, const std::string& dest,
39 bool verbose=false);
40 int create_dir(const char *dir, int mode = 0777);
41 int remove_file_or_dir(const char *dir);
42 bool in_group_id (gid_t target_gid);
43 std::string getmemusage ();
44 void tokenize(const std::string& str, std::vector<std::string>& tokens,
45 const std::string& delimiters);
46 std::string find_executable(const std::string& name,
47 const std::string& env_path = "PATH");
48 const std::string cmdstr_quoted(const std::string& cmd);
49 const std::string cmdstr_join(const std::vector<std::string>& cmds);
50 int stap_waitpid(int verbose, pid_t pid);
51 pid_t stap_spawn(int verbose, const std::vector<std::string>& args);
52 pid_t stap_spawn(int verbose, const std::vector<std::string>& args,
53 posix_spawn_file_actions_t* fa, const std::vector<std::string>& envVec = std::vector<std::string> ());
54 pid_t stap_spawn_piped(int verbose, const std::vector<std::string>& args,
55 int* child_in=NULL, int* child_out=NULL, int* child_err=NULL);
56 int stap_system(int verbose, const std::vector<std::string>& args,
57 bool null_out=false, bool null_err=false);
58 int stap_system_read(int verbose, const std::vector<std::string>& args, std::ostream& out);
59 int kill_stap_spawn(int sig);
60 void assert_regexp_match (const std::string& name, const std::string& value, const std::string& re);
61 int regexp_match (const std::string& value, const std::string& re, std::vector<std::string>& matches);
62 bool contains_glob_chars (const std::string &str);
63 std::string kernel_release_from_build_tree (const std::string &kernel_build_tree, int verbose = 0);
64 std::string normalize_machine(const std::string& machine);
65 std::string autosprintf(const char* format, ...) __attribute__ ((format (printf, 1, 2)));
66 const std::set<std::string>& localization_variables();
67
68 // stringification generics
69
70
71 template <typename IN>
72 inline std::string lex_cast(IN const & in)
73 {
74 std::ostringstream ss;
75 if (!(ss << in))
76 throw std::runtime_error(_("bad lexical cast"));
77 return ss.str();
78 }
79
80
81 template <typename OUT>
82 inline OUT lex_cast(std::string const & in)
83 {
84 std::istringstream ss(in);
85 OUT out;
86 if (!(ss >> out && ss.eof()))
87 throw std::runtime_error(_("bad lexical cast"));
88 return out;
89 }
90
91
92 // We want [u]int8_t to be treated numerically, not just extracting a char.
93 template <>
94 inline int8_t lex_cast(std::string const & in)
95 {
96 int16_t out = lex_cast<int16_t>(in);
97 if (out < -128 || out > 127)
98 throw std::runtime_error(_("bad lexical cast"));
99 return out;
100 }
101 template <>
102 inline uint8_t lex_cast(std::string const & in)
103 {
104 uint16_t out = lex_cast<uint16_t>(in);
105 if (out > 0xff && out < 0xff80) // don't error if it looks sign-extended
106 throw std::runtime_error(_("bad lexical cast"));
107 return out;
108 }
109
110
111 template <typename IN>
112 inline std::string
113 lex_cast_hex(IN const & in)
114 {
115 std::ostringstream ss;
116 if (!(ss << std::showbase << std::hex << in << std::dec))
117 throw std::runtime_error(_("bad lexical cast"));
118 return ss.str();
119 }
120
121 //Convert binary data to hex data.
122 template <typename IN>
123 inline std::string
124 hex_dump(IN const & in, size_t len)
125 {
126 std::ostringstream ss;
127 unsigned i;
128 if (!(ss << std::hex << std::setfill('0')))
129 throw std::runtime_error(_("bad lexical cast"));
130
131 for(i = 0; i < len; i++)
132 {
133 int temp = in[i];
134 ss << std::setw(2) << temp;
135 }
136 std::string hex = ss.str();
137 assert(hex.length() == 2 * len);
138 return hex;
139 }
140
141 // Return as quoted string, so that when compiled as a C literal, it
142 // would print to the user out nicely.
143 template <typename IN>
144 inline std::string
145 lex_cast_qstring(IN const & in)
146 {
147 std::stringstream ss;
148 if (!(ss << in))
149 throw std::runtime_error(_("bad lexical cast"));
150 return lex_cast_qstring(ss.str());
151 }
152
153
154 template <>
155 inline std::string
156 lex_cast_qstring(std::string const & in)
157 {
158 std::string out;
159 out += '"';
160 for (const char *p = in.c_str(); *p; ++p)
161 {
162 unsigned char c = *p;
163 if (! isprint(c))
164 {
165 out += '\\';
166 // quick & dirty octal converter
167 out += "01234567" [(c >> 6) & 0x07];
168 out += "01234567" [(c >> 3) & 0x07];
169 out += "01234567" [(c >> 0) & 0x07];
170 }
171 else if (c == '"' || c == '\\')
172 {
173 out += '\\';
174 out += c;
175 }
176 else
177 out += c;
178 }
179 out += '"';
180 return out;
181 }
182
183
184 // Delete all values from a map-like container and clear it
185 // (The template is permissive -- be good!)
186 template <typename T>
187 void delete_map(T& t)
188 {
189 for (typename T::iterator i = t.begin(); i != t.end(); ++i)
190 delete i->second;
191 t.clear();
192 }
193
194
195 // Returns whether a string starts with the given prefix
196 inline bool
197 startswith(const std::string & s, const char * prefix)
198 {
199 return (s.compare(0, std::strlen(prefix), prefix) == 0);
200 }
201
202
203 // Returns whether a string ends with the given suffix
204 inline bool
205 endswith(const std::string & s, const char * suffix)
206 {
207 size_t s_len = s.size(), suffix_len = std::strlen(suffix);
208 if (suffix_len > s_len)
209 return false;
210 return (s.compare(s_len - suffix_len, suffix_len, suffix) == 0);
211 }
212
213
214 // Mask our usual signals for the life of this object.
215 struct stap_sigmasker {
216 sigset_t old;
217 stap_sigmasker()
218 {
219 sigset_t mask;
220 sigemptyset (&mask);
221 sigaddset (&mask, SIGHUP);
222 sigaddset (&mask, SIGPIPE);
223 sigaddset (&mask, SIGINT);
224 sigaddset (&mask, SIGTERM);
225 sigprocmask (SIG_BLOCK, &mask, &old);
226 }
227 ~stap_sigmasker()
228 {
229 sigprocmask (SIG_SETMASK, &old, NULL);
230 }
231 };
232
233
234 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.048323 seconds and 5 git commands to generate.