From c76b4180fd8e0c58a66b8d28527ff05b0391a10c Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 14 Mar 2011 14:15:21 -0700 Subject: [PATCH] stapsh: Add debugging macros * runtime/staprun/stapsh.c (dbug, vdbug, die): New debugging macros. (usage, parse_args): New, just dealing with -v for now. (main): Parse arguments, and start printing debugging information. * remote.cxx (ssh_remote::init): Set stapsh verbosity according to p5. (direct_stapsh::direct_stapsh): Ditto. (remote::run): Make sure subsession->verbose is updated for pass-5. --- remote.cxx | 9 +++++ runtime/staprun/stapsh.c | 76 +++++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/remote.cxx b/remote.cxx index 7736ed76c..f2fb4fc63 100644 --- a/remote.cxx +++ b/remote.cxx @@ -336,6 +336,10 @@ class direct_stapsh : public stapsh { int in, out; vector cmd; cmd.push_back(BINDIR "/stapsh"); + if (s.perpass_verbose[4] > 1) + cmd.push_back("-v"); + if (s.perpass_verbose[4] > 2) + cmd.push_back("-v"); child = stap_spawn_piped(s.verbose, cmd, &in, &out); if (child <= 0) throw runtime_error("error launching stapsh!"); @@ -413,6 +417,10 @@ class ssh_remote : public stapsh { cmd.push_back("-q"); cmd.push_back(host); cmd.push_back("stapsh"); // NB: relies on remote $PATH + if (s->perpass_verbose[4] > 1) + cmd.push_back("-v"); + if (s->perpass_verbose[4] > 2) + cmd.push_back("-v"); child = stap_spawn_piped(s->verbose, cmd, &in, &out); sigprocmask (SIG_SETMASK, &oldmask, NULL); // back to normal signals if (child <= 0) @@ -706,6 +714,7 @@ remote::run(const vector& remotes) for (unsigned i = 0; i < remotes.size() && !pending_interrupts; ++i) { + remotes[i]->s->verbose = remotes[i]->s->perpass_verbose[4]; rc = remotes[i]->prepare(); if (rc) return rc; diff --git a/runtime/staprun/stapsh.c b/runtime/staprun/stapsh.c index 2d5292e37..dcbeeed50 100644 --- a/runtime/staprun/stapsh.c +++ b/runtime/staprun/stapsh.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -85,11 +86,23 @@ static const struct stapsh_handler commands[] = { { "run", do_run }, { "quit", do_quit }, }; +static const unsigned ncommands = sizeof(commands) / sizeof(*commands); static char tmpdir[FILENAME_MAX] = ""; static pid_t staprun_pid = -1; +static unsigned verbose = 0; + +#define dbug(level, format, args...) ( (verbose < level) ? 0 : \ + fprintf (stderr, "stapsh:%s:%d " format, __FUNCTION__, __LINE__, ## args) ) + +#define vdbug(level, format, args) ( (verbose < level) ? 0 : \ + fprintf (stderr, "stapsh:%s:%d ", __FUNCTION__, __LINE__) + \ + vfprintf (stderr, format, args) ) + +#define die(format, args...) ({ dbug(1, format, ## args); cleanup(2); }) + static void __attribute__ ((noreturn)) cleanup(int status) @@ -149,6 +162,34 @@ setup_signals (void) sigaction (signals[i], &sa, NULL); } +static void __attribute__ ((noreturn)) +usage (char *prog, int status) +{ + fprintf (stderr, "%s [-v]\n", prog); + exit (status); +} + +static void +parse_args(int argc, char* const argv[]) +{ + int c; + while ((c = getopt (argc, argv, "v")) != -1) + switch (c) + { + case 'v': + ++verbose; + break; + case '?': + default: + usage (argv[0], 2); + } + if (optind < argc) + { + fprintf (stderr, "%s: invalid extraneous arguments\n", argv[0]); + usage (argv[0], 2); + } +} + // Decode a quoted-printable string in-place static int @@ -191,6 +232,9 @@ qpdecode(char* s) static int do_hello() { + if (staprun_pid > 0) + return 1; + // XXX check caller's version compatibility struct utsname uts; @@ -326,47 +370,37 @@ do_quit() } int -main(int argc __attribute__ ((unused)), - const char* const argv[] __attribute__ ((unused))) +main(int argc, char* const argv[]) { + parse_args(argc, argv); + setup_signals(); umask(0077); snprintf(tmpdir, sizeof(tmpdir), "%s/stapsh.XXXXXX", getenv("TMPDIR") ?: "/tmp"); if (!mkdtemp(tmpdir)) - { - fprintf(stderr, "no tmpdir\n"); - return 2; - } + die ("Can't make a temporary working directory!\n"); if (chdir(tmpdir)) - { - fprintf(stderr, "bad tmpdir\n"); - cleanup(2); - } + die ("Can't change to temporary working directory \"%s\"!\n", tmpdir); - char command[1024]; + char command[4096]; while (fgets(command, sizeof(command), stdin)) { + dbug(1, "command: %s", command); int rc = -1; unsigned i; const char* arg = strtok(command, STAPSH_TOK_DELIM) ?: "(null)"; - for (i = 0; i < sizeof(commands) / sizeof(*commands); ++i) + for (i = 0; i < ncommands; ++i) if (strcmp(arg, commands[i].name) == 0) { rc = commands[i].fn(); if (rc) - { - fprintf(stderr, "failed %s, rc=%d\n", arg, rc); - //cleanup(rc); - } + dbug(2, "failed command %s, rc=%d\n", arg, rc); break; } - if (rc < 0) - { - fprintf(stderr, "bad command %s\n", arg); - //cleanup(1); - } + if (i >= ncommands) + dbug(2, "invalid command %s\n", arg); } cleanup(0); -- 2.43.5