]> sourceware.org Git - systemtap.git/commitdiff
stapsh: Add debugging macros
authorJosh Stone <jistone@redhat.com>
Mon, 14 Mar 2011 21:15:21 +0000 (14:15 -0700)
committerJosh Stone <jistone@redhat.com>
Mon, 14 Mar 2011 21:15:21 +0000 (14:15 -0700)
* 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
runtime/staprun/stapsh.c

index 7736ed76c5cd75dd8ce7d948351c742a63577c33..f2fb4fc63440531367900b347d7156f29bdb622f 100644 (file)
@@ -336,6 +336,10 @@ class direct_stapsh : public stapsh {
         int in, out;
         vector<string> 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<remote*>& 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;
index 2d5292e37eceb801dd46e8536cd240b6f97be3d9..dcbeeed50e9b47e32dcfd38f1fde940da14202e8 100644 (file)
@@ -49,6 +49,7 @@
 #include <fcntl.h>
 #include <spawn.h>
 #include <stdio.h>
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -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);
This page took 0.032392 seconds and 5 git commands to generate.