diff options
author | hunt <hunt> | 2007-03-26 16:15:11 +0000 |
---|---|---|
committer | hunt <hunt> | 2007-03-26 16:15:11 +0000 |
commit | 5d65678dde08d0b2e34ee79c2e0f6c26ff3e4592 (patch) | |
tree | 05cdd33db206b646c424dd8b207221413cbb2455 | |
parent | 98f0ea565abd496f7f4f065f652456101370751e (diff) | |
download | systemtap-steved-5d65678dde08d0b2e34ee79c2e0f6c26ff3e4592.tar.gz systemtap-steved-5d65678dde08d0b2e34ee79c2e0f6c26ff3e4592.tar.xz systemtap-steved-5d65678dde08d0b2e34ee79c2e0f6c26ff3e4592.zip |
2007-03-26 Martin Hunt <hunt@redhat.com>
* mainloop.c (run_stp_check): Just use system() call.
(init_staprun): Remove _stp_pid module parameter.
(cleanup_and_exit): If closed==2, just exit without removing module.
(driver_poll): Remove. We no longer require stap running.
(_stp_main_loop): Remove call to driver_poll.
* ctl.c (init_ctl_channel): Don't put files in systemtap_pid,
revert back to systemtap/modulename.
* relay.c: Revert back to systemtap/modulename paths.
* relay_old.c: Ditto.
* staprun.c: Add -L and -A args.
-rw-r--r-- | runtime/staprun/ChangeLog | 16 | ||||
-rw-r--r-- | runtime/staprun/ctl.c | 10 | ||||
-rw-r--r-- | runtime/staprun/mainloop.c | 104 | ||||
-rw-r--r-- | runtime/staprun/relay.c | 48 | ||||
-rw-r--r-- | runtime/staprun/relay_old.c | 17 | ||||
-rw-r--r-- | runtime/staprun/staprun.c | 68 | ||||
-rw-r--r-- | runtime/staprun/staprun.h | 9 |
7 files changed, 149 insertions, 123 deletions
diff --git a/runtime/staprun/ChangeLog b/runtime/staprun/ChangeLog index 50d56ce3..324e9bf8 100644 --- a/runtime/staprun/ChangeLog +++ b/runtime/staprun/ChangeLog @@ -1,3 +1,19 @@ +2007-03-26 Martin Hunt <hunt@redhat.com> + + * mainloop.c (run_stp_check): Just use system() call. + (init_staprun): Remove _stp_pid module parameter. + (cleanup_and_exit): If closed==2, just exit without removing module. + (driver_poll): Remove. We no longer require stap running. + (_stp_main_loop): Remove call to driver_poll. + + * ctl.c (init_ctl_channel): Don't put files in systemtap_pid, + revert back to systemtap/modulename. + + * relay.c: Revert back to systemtap/modulename paths. + * relay_old.c: Ditto. + + * staprun.c: Add -L and -A args. + 2007-03-20 Martin Hunt <hunt@redhat.com> * symbols.c (send_module): If send returns < 0 then diff --git a/runtime/staprun/ctl.c b/runtime/staprun/ctl.c index 9336631c..a217d270 100644 --- a/runtime/staprun/ctl.c +++ b/runtime/staprun/ctl.c @@ -18,14 +18,18 @@ int init_ctl_channel(void) struct statfs st; if (statfs("/sys/kernel/debug", &st) == 0 && (int) st.f_type == (int) DEBUGFS_MAGIC) - sprintf (buf, "/sys/kernel/debug/systemtap_%d/cmd", getpid()); + sprintf (buf, "/sys/kernel/debug/systemtap/%s/cmd", modname); else - sprintf (buf, "/proc/systemtap_%d/cmd", getpid()); + sprintf (buf, "/proc/systemtap/%s/cmd", modname); dbug("Opening %s\n", buf); control_channel = open(buf, O_RDWR); if (control_channel < 0) { - fprintf(stderr, "ERROR: couldn't open control channel %s: errcode = %s\n", buf, strerror(errno)); + if (attach_mod) + fprintf (stderr, "ERROR: Cannot connect to module \"%s\".\n", modname); + else + fprintf (stderr, "ERROR: couldn't open control channel %s\n", buf); + fprintf (stderr, "errcode = %s\n", strerror(errno)); return -1; } return 0; diff --git a/runtime/staprun/mainloop.c b/runtime/staprun/mainloop.c index 0bede8cf..d3b3d3b7 100644 --- a/runtime/staprun/mainloop.c +++ b/runtime/staprun/mainloop.c @@ -28,6 +28,10 @@ int use_old_transport = 0; int send_request(int type, void *data, int len) { char buf[1024]; + if (len > (int)sizeof(buf)) { + err("exceeded maximum send_request size.\n"); + return -1; + } memcpy(buf, &type, 4); memcpy(&buf[4],data,len); return write(control_channel, buf, len+4); @@ -111,33 +115,14 @@ char *stp_check="stp_check"; static int run_stp_check (void) { - pid_t pid; - int wstat; - + int ret ; /* run the _stp_check script */ - dbug("stp_check\n"); - if ((pid = fork()) < 0) { - perror (stp_check); - fprintf(stderr, "Fork of %s failed.\n", stp_check); - return -1; - } else if (pid == 0) { - if (execlp(stp_check, stp_check, NULL) < 0) - _exit (-1); - } - if (waitpid(pid, &wstat, 0) < 0) { - perror("waitpid"); - return -1; - } - if (WIFEXITED(wstat) && WEXITSTATUS(wstat)) { - perror (stp_check); - fprintf(stderr, "Could not execute %s.\n", stp_check); - return -1; - } + dbug("executing %s\n", stp_check); + ret = system(stp_check); dbug("DONE\n"); - return 0; + return ret; } - /** * init_stp - initialize the app * @print_summary: boolean, print summary or not at end of run @@ -146,29 +131,34 @@ static int run_stp_check (void) */ int init_staprun(void) { - char buf[1024], bufcmd[20]; - pid_t pid; + char bufcmd[128]; int rstatus; + int pid; - ncpus = sysconf(_SC_NPROCESSORS_ONLN); - if (ncpus < 0) { - fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed\n"); - return 1; - } - if (system(VERSION_CMD)) { dbug("Using OLD TRANSPORT\n"); use_old_transport = 1; } + if (attach_mod) { + if (init_ctl_channel() < 0) + return -1; + if (init_relayfs() < 0) { + close_ctl_channel(); + return -1; + } + return 0; + } + + if (run_stp_check() < 0) + return -1; + /* insert module */ - sprintf(buf, "_stp_pid=%d", (int)getpid()); sprintf(bufcmd, "_stp_bufsize=%d", buffer_size); modoptions[0] = "insmod"; modoptions[1] = modpath; - modoptions[2] = buf; - modoptions[3] = bufcmd; - /* modoptions[4...N] set by command line parser. */ + modoptions[2] = bufcmd; + /* modoptions[3...N] set by command line parser. */ if ((pid = fork()) < 0) { perror ("fork"); @@ -185,13 +175,12 @@ int init_staprun(void) fprintf(stderr, "ERROR, couldn't insmod probe module %s\n", modpath); return -1; } - - if (run_stp_check() < 0) - return -1; /* create control channel */ - if (init_ctl_channel() < 0) + if (init_ctl_channel() < 0) { + err("Failed to initialize control channel.\n"); goto exit1; + } /* fork target_cmd if requested. */ /* It will not actually exec until signalled. */ @@ -201,8 +190,8 @@ int init_staprun(void) return 0; exit1: - snprintf(buf, sizeof(buf), "/sbin/rmmod -w %s", modname); - if (system(buf)) + snprintf(bufcmd, sizeof(bufcmd), "/sbin/rmmod -w %s", modname); + if (system(bufcmd)) fprintf(stderr, "ERROR: couldn't rmmod probe module %s.\n", modname); return -1; } @@ -235,7 +224,7 @@ void cleanup_and_exit (int closed) dbug("closing control channel\n"); close_ctl_channel(); - if (!closed) { + if (closed == 0) { dbug("removing module\n"); snprintf(tmpbuf, sizeof(tmpbuf), "/sbin/rmmod -w %s", modname); if (system(tmpbuf)) { @@ -243,7 +232,11 @@ void cleanup_and_exit (int closed) modname); exit(1); } + } else if (closed == 2) { + fprintf(stderr, "\nDisconnecting from systemtap module.\n"); + fprintf(stderr, "To reconnect, type \"staprun -A %s\"\n", modname); } + exit(0); } @@ -254,24 +247,12 @@ static void sigproc(int signum) pid_t pid = waitpid(-1, NULL, WNOHANG); if (pid != target_pid) return; - } + } else if (signum == SIGQUIT) + cleanup_and_exit(2); + send_request(STP_EXIT, NULL, 0); } -static void driver_poll (int signum __attribute__((unused))) -{ - /* See if the driver process is still alive. If not, time to exit. */ - if (kill (driver_pid, 0) < 0) { - send_request(STP_EXIT, NULL, 0); - return; - } else { - /* Check again later. Use any reasonable poll interval */ - signal (SIGALRM, driver_poll); - alarm (10); - } -} - - /** * stp_main_loop - loop forever reading data */ @@ -292,9 +273,6 @@ int stp_main_loop(void) signal(SIGCHLD, sigproc); signal(SIGQUIT, sigproc); - if (driver_pid) - driver_poll(0); - dbug("in main loop\n"); while (1) { /* handle messages from control channel */ @@ -347,14 +325,16 @@ int stp_main_loop(void) { struct _stp_msg_start ts; if (use_old_transport) { - if (init_oldrelayfs((struct _stp_msg_trans *)data) < 0) + if (init_oldrelayfs() < 0) cleanup_and_exit(0); } else { - if (init_relayfs((struct _stp_msg_trans *)data) < 0) + if (init_relayfs() < 0) cleanup_and_exit(0); } ts.target = target_pid; send_request(STP_START, &ts, sizeof(ts)); + if (load_only) + cleanup_and_exit(2); break; } case STP_MODULE: diff --git a/runtime/staprun/relay.c b/runtime/staprun/relay.c index 6d505a22..0a805999 100644 --- a/runtime/staprun/relay.c +++ b/runtime/staprun/relay.c @@ -79,36 +79,45 @@ static void *reader_thread(void *data) * * Returns 0 if successful, negative otherwise */ -int init_relayfs(struct _stp_msg_trans *t) +int init_relayfs(void) { int i; struct statfs st; char buf[128], relay_filebase[128]; - bulkmode = t->bulk_mode; - dbug("initializing relayfs. bulkmode = %d\n", bulkmode); + dbug("initializing relayfs\n"); reader[0] = (pthread_t)0; relay_fd[0] = 0; out_fd[0] = 0; if (statfs("/sys/kernel/debug", &st) == 0 && (int) st.f_type == (int) DEBUGFS_MAGIC) - sprintf(relay_filebase, "/sys/kernel/debug/systemtap_%d", getpid()); + sprintf(relay_filebase, "/sys/kernel/debug/systemtap/%s", modname); else { fprintf(stderr,"Cannot find relayfs or debugfs mount point.\n"); return -1; } + + for (i = 0; i < NR_CPUS; i++) { + sprintf(buf, "%s/trace%d", relay_filebase, i); + dbug("attempting to open %s\n", buf); + relay_fd[i] = open(buf, O_RDONLY | O_NONBLOCK); + if (relay_fd[i] < 0) + break; + } + ncpus = i; + dbug("ncpus=%d\n", ncpus); + + if (ncpus == 0) { + err("couldn't open %s.\n", buf); + return -1; + } + if (ncpus > 1) + bulkmode = 1; + if (bulkmode) { for (i = 0; i < ncpus; i++) { - sprintf(buf, "%s/trace%d", relay_filebase, i); - dbug("opening %s\n", buf); - relay_fd[i] = open(buf, O_RDONLY | O_NONBLOCK); - if (relay_fd[i] < 0) { - fprintf(stderr, "ERROR: couldn't open relayfs file %s.\n", buf); - return -1; - } - if (outfile_name) { /* special case: for testing we sometimes want to write to /dev/null */ if (strcmp(outfile_name, "/dev/null") == 0) @@ -117,7 +126,7 @@ int init_relayfs(struct _stp_msg_trans *t) sprintf(buf, "%s_%d", outfile_name, i); } else sprintf(buf, "stpd_cpu%d", i); - + out_fd[i] = open (buf, O_CREAT|O_TRUNC|O_WRONLY, 0666); dbug("out_fd[%d] = %d\n", i, out_fd[i]); if (out_fd[i] < 0) { @@ -125,19 +134,8 @@ int init_relayfs(struct _stp_msg_trans *t) return -1; } } - } else { /* stream mode */ - ncpus = 1; - sprintf(buf, "%s/trace0", relay_filebase); - dbug("opening %s\n", buf); - relay_fd[0] = open(buf, O_RDONLY | O_NONBLOCK); - dbug("got fd=%d\n", relay_fd[0]); - if (relay_fd[0] < 0) { - fprintf(stderr, "ERROR: couldn't open relayfs file %s.\n", buf); - return -1; - } - if (outfile_name) { out_fd[0] = open (outfile_name, O_CREAT|O_TRUNC|O_WRONLY, 0666); if (out_fd[0] < 0) { @@ -146,7 +144,7 @@ int init_relayfs(struct _stp_msg_trans *t) } } else out_fd[0] = STDOUT_FILENO; - + } dbug("starting threads\n"); for (i = 0; i < ncpus; i++) { diff --git a/runtime/staprun/relay_old.c b/runtime/staprun/relay_old.c index 049d193a..80c38a6c 100644 --- a/runtime/staprun/relay_old.c +++ b/runtime/staprun/relay_old.c @@ -210,7 +210,7 @@ static void *reader_thread(void *data) * * Returns 0 if successful, negative otherwise */ -int init_oldrelayfs(struct _stp_msg_trans *t) +int init_oldrelayfs(void) { int i, j; struct statfs st; @@ -221,7 +221,8 @@ int init_oldrelayfs(struct _stp_msg_trans *t) relay_fd[i] = 0; } - bulkmode = t->bulk_mode; +// t->bulk_mode; + bulkmode = 0; if (!bulkmode) { if (outfile_name) { out_fd[0] = open (outfile_name, O_CREAT|O_TRUNC|O_WRONLY, 0666); @@ -234,16 +235,16 @@ int init_oldrelayfs(struct _stp_msg_trans *t) return 0; } - n_subbufs = t->n_subbufs; - subbuf_size = t->subbuf_size; + n_subbufs = 0; /* t->n_subbufs;*/ + subbuf_size = 0; /* t->subbuf_size; */ dbug("initializing relayfs. n_subbufs=%d subbuf_size=%d\n",n_subbufs, subbuf_size); if (statfs("/sys/kernel/debug", &st) == 0 && (int) st.f_type == (int) DEBUGFS_MAGIC) { - sprintf(relay_filebase, "/sys/kernel/debug/systemtap_%d/trace", getpid()); - sprintf(proc_filebase, "/sys/kernel/debug/systemtap_%d/", getpid()); + sprintf(relay_filebase, "/sys/kernel/debug/systemtap/%s/trace", modname); + sprintf(proc_filebase, "/sys/kernel/debug/systemtap/%s/", modname); } else if (statfs("/mnt/relay", &st) == 0 && (int) st.f_type == (int) RELAYFS_MAGIC) { - sprintf(relay_filebase, "/mnt/relay/systemtap_%d/trace", getpid()); - sprintf(proc_filebase, "/proc/systemtap_%d/", getpid()); + sprintf(relay_filebase, "/mnt/relay/systemtap/%s/trace", modname); + sprintf(proc_filebase, "/proc/systemtap/%s/", modname); } else { fprintf(stderr,"Cannot find relayfs or debugfs mount point.\n"); return -1; diff --git a/runtime/staprun/staprun.c b/runtime/staprun/staprun.c index 4f781666..4ead7018 100644 --- a/runtime/staprun/staprun.c +++ b/runtime/staprun/staprun.c @@ -29,9 +29,8 @@ extern int optind; int verbose = 0; int target_pid = 0; -int driver_pid = 0; unsigned int buffer_size = 0; -char *modname = NULL; +char modname[128]; char *modpath = NULL; #define MAXMODOPTIONS 64 char *modoptions[MAXMODOPTIONS]; @@ -40,11 +39,32 @@ char *outfile_name = NULL; char *username = NULL; uid_t cmd_uid; gid_t cmd_gid; +int attach_mod = 0; +int load_only = 0; + +static void path_parse_modname (char *path) +{ + char *mptr = rindex (path, '/'); + if (mptr == NULL) + mptr = path; + else + mptr++; + + if (strlen(mptr) >= sizeof(modname)) { + err("Module name larger than modname buffer.\n"); + exit (-1); + } + strcpy(modname, mptr); + + mptr = rindex(modname, '.'); + if (mptr) + *mptr = '\0'; +} static void usage(char *prog) { - fprintf(stderr, "\n%s [-v] [-c cmd ] [-x pid] [-u user]\n" - "\t[-b bufsize] [-o FILE] kmod-name [kmod-options]\n", prog); + fprintf(stderr, "\n%s [-v] [-c cmd ] [-x pid] [-u user]\n" + "\t[-A modname]] [-L] [-b bufsize] [-o FILE] kmod-name [kmod-options]\n", prog); fprintf(stderr, "-v Verbose.\n"); fprintf(stderr, "-c cmd. Command \'cmd\' will be run and staprun will exit when it does.\n"); fprintf(stderr, " _stp_target will contain the pid for the command.\n"); @@ -55,6 +75,8 @@ static void usage(char *prog) fprintf(stderr, " Setting one here will override that value. The value should be\n"); fprintf(stderr, " an integer between 1 and 64 which be assumed to be the\n"); fprintf(stderr, " buffer size in MB. That value will be per-cpu in bulk mode.\n"); + fprintf(stderr, "-L Load module and start probes, then detach.\n"); + fprintf(stderr, "-A modname. Attach to systemtap module modname.\n"); exit(1); } @@ -62,7 +84,7 @@ int main(int argc, char **argv) { int c; - while ((c = getopt(argc, argv, "vb:t:d:c:o:u:x:")) != EOF) { + while ((c = getopt(argc, argv, "ALvb:t:d:c:o:u:x:")) != EOF) { switch (c) { case 'v': verbose = 1; @@ -84,8 +106,7 @@ int main(int argc, char **argv) target_pid = atoi(optarg); break; case 'd': - /* internal option used by stap */ - driver_pid = atoi(optarg); + /* obsolete internal option used by stap */ break; case 'c': target_cmd = optarg; @@ -96,6 +117,12 @@ int main(int argc, char **argv) case 'u': username = optarg; break; + case 'A': + attach_mod = 1; + break; + case 'L': + load_only = 1; + break; default: usage(argv[0]); } @@ -107,23 +134,24 @@ int main(int argc, char **argv) } if (optind < argc) { - /* Collect both full path and just the trailing module name. */ modpath = argv[optind++]; - modname = rindex (modpath, '/'); - if (modname == NULL) - modname = modpath; - else - modname++; /* skip over / */ + path_parse_modname(modpath); + dbug("modpath=\"%s\", modname=\"%s\"\n", modpath, modname); } if (optind < argc) { - unsigned start_idx = 4; /* reserve four slots in modoptions[] */ - while (optind < argc && start_idx+1 < MAXMODOPTIONS) - modoptions[start_idx++] = argv[optind++]; - modoptions[start_idx] = NULL; + if (attach_mod) { + fprintf(stderr, "Cannot have module options with attach (-A).\n"); + usage(argv[0]); + } else { + unsigned start_idx = 3; /* reserve three slots in modoptions[] */ + while (optind < argc && start_idx+1 < MAXMODOPTIONS) + modoptions[start_idx++] = argv[optind++]; + modoptions[start_idx] = NULL; + } } - if (!modname) { + if (!modpath) { fprintf (stderr, "Need a module to load.\n"); usage(argv[0]); } @@ -144,10 +172,8 @@ int main(int argc, char **argv) /* now bump the priority */ setpriority (PRIO_PROCESS, 0, -10); - if (init_staprun()) { - fprintf(stderr, "Couldn't initialize staprun. Exiting.\n"); + if (init_staprun()) exit(1); - } if (stp_main_loop()) { fprintf(stderr,"Couldn't enter main loop. Exiting.\n"); diff --git a/runtime/staprun/staprun.h b/runtime/staprun/staprun.h index 57a78809..36fbd940 100644 --- a/runtime/staprun/staprun.h +++ b/runtime/staprun/staprun.h @@ -65,9 +65,9 @@ int do_module(void *); void do_kernel_symbols(void); int init_ctl_channel(void); void close_ctl_channel(void); -int init_relayfs(struct _stp_msg_trans *); +int init_relayfs(void); void close_relayfs(void); -int init_oldrelayfs(struct _stp_msg_trans *); +int init_oldrelayfs(void); void close_oldrelayfs(void); /* @@ -79,13 +79,14 @@ extern int ncpus; /* flags */ extern int verbose; extern unsigned int buffer_size; -extern char *modname; +extern char modname[]; extern char *modpath; extern char *modoptions[]; extern int target_pid; -extern int driver_pid; extern char *target_cmd; extern char *outfile_name; +extern int attach_mod; +extern int load_only; /* uid/gid to use when execing external programs */ extern uid_t cmd_uid; |