This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] enhance quoting for command string


hi! everyone

When I was using systemtap to test the following command,
I found that it didn't work as expected.

$ stap -vv -c 'echo "Hello World"' -e "probe begin{}"
......
Pass 5: starting run.
Running sudo /usr/bin/staprun -v -u lai -d 8994 -c "echo "Hello World""
/tmp/staplDwTCz/stap_ef6615c837006e3d89fccba6a245b8bc_142.ko
insmod: can't read 'World': No such file or directory
ERROR, couldn't insmod probe module World
Pass 5: run completed in 14usr/131sys/147real ms.
Pass 5: run failed.  Try again with more '-v' (verbose) options.
......


the command string 'echo "Hello World"' was parsed to "echo "Hello, and the "World" was considered as the kernel module name,that's why error occured! I read the code of systemtap,and found that it just simply quoeted the command string by "". So I enhanced it by adding a function cmdstr_quoted() in util.cxx. I used this function for -c option (run_pass), but this function can be also used for compile_pass( buildrun.cxx:110,111)

I have tested it under the following 3*3=9 conditions after fixing it:
1) stap executed on bash, ksh, tcsh (3)
2) /bin/sh is symbolic link to bash, ksh, tcsh (3)
total 3*3=9

the patch follows:

Signed-off-by: Lai Jiangshan <laijs@cn.fujitsu.com>

diff -Nur systemtap-20070804-old/buildrun.cxx systemtap-20070804/buildrun.cxx
--- systemtap-20070804-old/buildrun.cxx 2007-06-21 03:00:39.000000000 +0900
+++ systemtap-20070804/buildrun.cxx 2007-08-10 14:21:29.000000000 +0900
@@ -141,7 +141,7 @@
staprun_cmd += "-d " + stringify(getpid()) + " ";
if (s.cmd != "")
- staprun_cmd += "-c \"" + s.cmd + "\" ";
+ staprun_cmd += "-c " + cmdstr_quoted(s.cmd) + " ";
if (s.target_pid)
staprun_cmd += "-t " + stringify(s.target_pid) + " ";
diff -Nur systemtap-20070804-old/util.cxx systemtap-20070804/util.cxx
--- systemtap-20070804-old/util.cxx 2006-10-24 07:09:51.000000000 +0900
+++ systemtap-20070804/util.cxx 2007-08-16 09:06:17.000000000 +0900
@@ -197,3 +197,34 @@


return false;
}
+
+const string cmdstr_quoted(const string& cmd)
+{
+ // original cmd : substr1
+ // or : substr1'substr2
+ // or : substr1'substr2'substr3......
+ // after quoted :
+ // every substr(even it's empty) is quoted by ''
+ // every single-quote(') is quoted by ""
+ // examples: substr1 --> 'substr1'
+ // substr1'substr2 --> 'substr1'"'"'substr2'
+
+ string quoted_cmd;
+ string quote("'");
+ string replace("'\"'\"'");
+ string::size_type pos = 0;
+
+ quoted_cmd += quote;
+ for (string::size_type quote_pos = cmd.find(quote, pos); + quote_pos != string::npos; + quote_pos = cmd.find(quote, pos)) {
+ quoted_cmd += cmd.substr(pos, quote_pos - pos);
+ quoted_cmd += replace;
+ pos = quote_pos + 1;
+ }
+ quoted_cmd += cmd.substr(pos, cmd.length() - pos);
+ quoted_cmd += quote;
+
+ return quoted_cmd;
+}
+
diff -Nur systemtap-20070804-old/util.h systemtap-20070804/util.h
--- systemtap-20070804-old/util.h 2006-12-20 07:00:15.000000000 +0900
+++ systemtap-20070804/util.h 2007-08-10 13:36:01.000000000 +0900
@@ -11,6 +11,7 @@
void tokenize(const std::string& str, std::vector<std::string>& tokens,
const std::string& delimiters);
bool find_executable(const char *name, std::string& retpath);
+const std::string cmdstr_quoted(const std::string& cmd);



// stringification generics



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]