From f0b8d2671fa56e0e1dcb4cc09fd6f7edf70f8fa3 Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Fri, 20 Mar 2009 11:54:15 -0400 Subject: PR6930: staprun: supports error message to syslog Add an interface (eprintf) to output error messages to syslogd, because staprun has no stderr after detaching from console. --- runtime/staprun/common.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'runtime/staprun/common.c') diff --git a/runtime/staprun/common.c b/runtime/staprun/common.c index fd16b4b8..b8860248 100644 --- a/runtime/staprun/common.c +++ b/runtime/staprun/common.c @@ -344,3 +344,24 @@ int send_request(int type, void *data, int len) if (rc < 0) return rc; return (rc != len+4); } + +#include + +static int use_syslog = 0; + +void eprintf(const char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + if (use_syslog) + vsyslog(LOG_ERR, fmt, va); + else + vfprintf(stderr, fmt, va); + va_end(va); +} + +void switch_syslog(const char *name) +{ + openlog(name, LOG_PID, LOG_DAEMON); + use_syslog = 1; +} -- cgit From 54892f28a2747079fae4aa35b80598cbb993a4c3 Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Fri, 20 Mar 2009 11:59:06 -0400 Subject: PR6930: stapio: run in background as a daemon Add '-D'(daemon mode) option to staprun/stapio for daemon mode. In this mode, stapio shows just its pid and detachs from console. Since it has no stdio, this mode requires -o option. stapio will exit when it receives SIGTERM or detects some error. --- runtime/staprun/common.c | 52 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) (limited to 'runtime/staprun/common.c') diff --git a/runtime/staprun/common.c b/runtime/staprun/common.c index b8860248..a1b70d3b 100644 --- a/runtime/staprun/common.c +++ b/runtime/staprun/common.c @@ -27,6 +27,7 @@ int attach_mod; int delete_mod; int load_only; int need_uprobes; +int daemon_mode; /* module variables */ char *modname = NULL; @@ -35,6 +36,21 @@ char *modoptions[MAXMODOPTIONS]; int control_channel = -1; /* NB: fd==0 possible */ +static char path_buf[PATH_MAX]; +static char *get_abspath(char *path) +{ + int len; + if (path[0] == '/') + return path; + + len = strlen(getcwd(path_buf, PATH_MAX)); + if (len + 2 + strlen(path) >= PATH_MAX) + return NULL; + path_buf[len] = '/'; + strcpy(&path_buf[len + 1], path); + return path_buf; +} + void parse_args(int argc, char **argv) { int c; @@ -49,8 +65,9 @@ void parse_args(int argc, char **argv) delete_mod = 0; load_only = 0; need_uprobes = 0; + daemon_mode = 0; - while ((c = getopt(argc, argv, "ALuvb:t:dc:o:x:")) != EOF) { + while ((c = getopt(argc, argv, "ALuvb:t:dc:o:x:D")) != EOF) { switch (c) { case 'u': need_uprobes = 1; @@ -85,11 +102,20 @@ void parse_args(int argc, char **argv) case 'L': load_only = 1; break; + case 'D': + daemon_mode = 1; + break; default: usage(argv[0]); } } - + if (outfile_name) { + outfile_name = get_abspath(outfile_name); + if (outfile_name == NULL) { + err("File name is too long.\n"); + usage(argv[0]); + } + } if (attach_mod && load_only) { err("You can't specify the '-A' and '-L' options together.\n"); usage(argv[0]); @@ -118,12 +144,29 @@ void parse_args(int argc, char **argv) err("You can't specify the '-c' and '-x' options together.\n"); usage(argv[0]); } + + if (daemon_mode && load_only) { + err("You can't specify the '-D' and '-L' options together.\n"); + usage(argv[0]); + } + if (daemon_mode && delete_mod) { + err("You can't specify the '-D' and '-d' options together.\n"); + usage(argv[0]); + } + if (daemon_mode && target_cmd) { + err("You can't specify the '-D' and '-c' options together.\n"); + usage(argv[0]); + } + if (daemon_mode && outfile_name == NULL) { + err("You have to specify output FILE with '-D' option.\n"); + usage(argv[0]); + } } void usage(char *prog) { - err("\n%s [-v] [-c cmd ] [-x pid] [-u user]\n" - "\t[-A|-L] [-b bufsize] [-o FILE] MODULE [module-options]\n", prog); + err("\n%s [-v] [-c cmd ] [-x pid] [-u user] [-A|-L|-d]\n" + "\t[-b bufsize] [-o FILE [-D]] MODULE [module-options]\n", prog); err("-v Increase verbosity.\n"); err("-c cmd Command \'cmd\' will be run and staprun will\n"); err(" exit when it does. The '_stp_target' variable\n"); @@ -140,6 +183,7 @@ void usage(char *prog) err("-d Delete a module. Only detached or unused modules\n"); err(" the user has permission to access will be deleted. Use \"*\"\n"); err(" (quoted) to delete all unused modules.\n"); + err("-D Run in background. This requires '-o' option.\n"); err("MODULE can be either a module name or a module path. If a\n"); err("module name is used, it is looked for in the following\n"); err("directory: /lib/modules/`uname -r`/systemtap\n"); -- cgit From acd56c22068963ad48f39890f5307600ff7d5278 Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Fri, 20 Mar 2009 12:11:30 -0400 Subject: PR6930: stapio: support file switching Add file-switching option(-S size[,N]) to stapio. This option has two arguments, 'size' and 'N', and requires -o option. - When the size of output file exceeds specified 'size'MB, staprun switches output file to the next file. For this purpose, all output file has a serial number as a suffix only when user specifies this option. - Using this option in bulk mode, the output file name will be 'FILE_cpuX.SERIAL'. - When the number of files exceeds specified N, staprun removes the oldest file. This argument can be omitted. --- runtime/staprun/common.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'runtime/staprun/common.c') diff --git a/runtime/staprun/common.c b/runtime/staprun/common.c index a1b70d3b..194488ef 100644 --- a/runtime/staprun/common.c +++ b/runtime/staprun/common.c @@ -28,6 +28,8 @@ int delete_mod; int load_only; int need_uprobes; int daemon_mode; +off_t fsize_max; +int fnum_max; /* module variables */ char *modname = NULL; @@ -54,6 +56,7 @@ static char *get_abspath(char *path) void parse_args(int argc, char **argv) { int c; + char *s; /* Initialize option variables. */ verbose = 0; @@ -66,8 +69,10 @@ void parse_args(int argc, char **argv) load_only = 0; need_uprobes = 0; daemon_mode = 0; + fsize_max = 0; + fnum_max = 0; - while ((c = getopt(argc, argv, "ALuvb:t:dc:o:x:D")) != EOF) { + while ((c = getopt(argc, argv, "ALuvb:t:dc:o:x:S:D")) != EOF) { switch (c) { case 'u': need_uprobes = 1; @@ -105,6 +110,16 @@ void parse_args(int argc, char **argv) case 'D': daemon_mode = 1; break; + case 'S': + fsize_max = strtoul(optarg, &s, 10); + fsize_max <<= 20; + if (s[0] == ',') + fnum_max = (int)strtoul(&s[1], &s, 10); + if (s[0] != '\0') { + err("Invalid file size option '%s'.\n", optarg); + usage(argv[0]); + } + break; default: usage(argv[0]); } @@ -161,12 +176,16 @@ void parse_args(int argc, char **argv) err("You have to specify output FILE with '-D' option.\n"); usage(argv[0]); } + if (outfile_name == NULL && fsize_max != 0) { + err("You have to specify output FILE with '-S' option.\n"); + usage(argv[0]); + } } void usage(char *prog) { err("\n%s [-v] [-c cmd ] [-x pid] [-u user] [-A|-L|-d]\n" - "\t[-b bufsize] [-o FILE [-D]] MODULE [module-options]\n", prog); + "\t[-b bufsize] [-o FILE [-D] [-S size[,N]]] MODULE [module-options]\n", prog); err("-v Increase verbosity.\n"); err("-c cmd Command \'cmd\' will be run and staprun will\n"); err(" exit when it does. The '_stp_target' variable\n"); @@ -184,6 +203,13 @@ void usage(char *prog) err(" the user has permission to access will be deleted. Use \"*\"\n"); err(" (quoted) to delete all unused modules.\n"); err("-D Run in background. This requires '-o' option.\n"); + err("-S size[,N] Switches output file to next file when the size\n"); + err(" of file reaches the specified size. The value\n"); + err(" should be an integer greater than 1 which is\n"); + err(" assumed to be the maximum file size in MB.\n"); + err(" When the number of output files reaches N, it\n"); + err(" switches to the first output file. You can omit\n"); + err(" the second argument.\n"); err("MODULE can be either a module name or a module path. If a\n"); err("module name is used, it is looked for in the following\n"); err("directory: /lib/modules/`uname -r`/systemtap\n"); -- cgit From 04ae1b090781725631ba3477ff77721b012cdaba Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Fri, 20 Mar 2009 13:38:29 -0400 Subject: PR9821: staprun supports subset of strftime. Add strftime subset format support for output file name to systemtap. This format will be evaluated when opening a new output file. --- runtime/staprun/common.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) (limited to 'runtime/staprun/common.c') diff --git a/runtime/staprun/common.c b/runtime/staprun/common.c index 194488ef..8200ec9d 100644 --- a/runtime/staprun/common.c +++ b/runtime/staprun/common.c @@ -53,6 +53,113 @@ static char *get_abspath(char *path) return path_buf; } +int stap_strfloctime(char *buf, size_t max, const char *fmt, time_t t) +{ + char *c = buf; + const char *c2 = fmt, *end = buf + max; + int ret, num; + struct tm tm; + if (buf == NULL || fmt == NULL || max <= 1) + return -EINVAL; + localtime_r(&t, &tm); + + while (*c2 != '\0'){ + if (c + 1 >= end) + return -EINVAL; + if (*c2 != '%') { + *c++ = *c2++; + continue; + } + c2++; + switch (*c2++) { + case '%': + *c++ = '%'; + break; + case 'Y': + num = tm.tm_year + 1900; + goto numbering; + case 'y': + num = tm.tm_year % 100; + goto numbering02; + case 'C': + num = ((tm.tm_year + 1900 - 1) / 100) + 1; + goto numbering; + case 'm': + num = tm.tm_mon + 1; + goto numbering02; + case 'd': + num = tm.tm_mday; + goto numbering02; + case 'e': + num = tm.tm_mday; + goto numbering; + case 'F': + ret = snprintf(c, end - c, "%d-%02d-%02d", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); + if (ret < 0) return ret; + c += ret; + break; + case 'H': + num = tm.tm_hour; + goto numbering02; + case 'I': + num = tm.tm_hour % 12; + if (num == 0) num = 12; + goto numbering02; + case 'j': + ret = snprintf(c, end - c, "%03d", tm.tm_yday); + if (ret < 0) return ret; + c += ret; + break; + case 'k': + num = tm.tm_hour; + goto numbering; + case 'l': + num = tm.tm_hour % 12; + if (num == 0) num = 12; + goto numbering; + case 'M': + num = tm.tm_min; + goto numbering02; + case 'S': + num = tm.tm_sec; + goto numbering02; + case 'R': + ret = snprintf(c, end - c, "%02d:%02d", + tm.tm_hour, tm.tm_min); + if (ret < 0) return ret; + c += ret; + break; + case 'T': + ret = snprintf(c, end - c, "%02d:%02d:%02d", + tm.tm_hour, tm.tm_min, tm.tm_sec); + if (ret < 0) return ret; + c += ret; + break; + case 'u': + num = tm.tm_wday == 0 ? 7 : tm.tm_wday; + goto numbering; + case 'w': + num = tm.tm_wday; + goto numbering; + default: + return -EINVAL; + } + continue; +numbering: + ret = snprintf(c, end - c, "%d", num); + if (ret < 0) return ret; + c += ret; + continue; +numbering02: + ret = snprintf(c, end - c, "%02d", num); + if (ret < 0) return ret; + c += ret; + } + *c = '\0'; + return c - buf; +} + void parse_args(int argc, char **argv) { int c; @@ -125,11 +232,19 @@ void parse_args(int argc, char **argv) } } if (outfile_name) { + char tmp[PATH_MAX]; + int ret; outfile_name = get_abspath(outfile_name); if (outfile_name == NULL) { err("File name is too long.\n"); usage(argv[0]); } + ret = stap_strfloctime(tmp, PATH_MAX - 18, /* = _cpuNNN.SSSSSSSSSS */ + outfile_name, time(NULL)); + if (ret < 0) { + err("Filename format is invalid or too long.\n"); + usage(argv[0]); + } } if (attach_mod && load_only) { err("You can't specify the '-A' and '-L' options together.\n"); @@ -191,7 +306,9 @@ void usage(char *prog) err(" exit when it does. The '_stp_target' variable\n"); err(" will contain the pid for the command.\n"); err("-x pid Sets the '_stp_target' variable to pid.\n"); - err("-o FILE Send output to FILE.\n"); + err("-o FILE Send output to FILE. This supports a subset of\n"); + err(" strftime(3) (%%%%,%%C,%%Y,%%y,%%m,%%d,%%e,%%F,%%H,%%I\n"); + err(" %%j,%%k,%%l,%%M,%%S,%%R,%%T,%%u,%%w) for FILE.\n"); err("-b buffer size The systemtap module specifies a buffer size.\n"); err(" Setting one here will override that value. The\n"); err(" value should be an integer between 1 and 4095 \n"); -- cgit From b9e80fe13706ae1c820e565e9b26f00b5f5a8dca Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Thu, 2 Apr 2009 18:44:34 -0400 Subject: Fix strftime format bug This fixes bugs in strftime-subset function. This modifies %C, %l and %j to fit the output of date command. --- runtime/staprun/common.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'runtime/staprun/common.c') diff --git a/runtime/staprun/common.c b/runtime/staprun/common.c index 8200ec9d..26b166c2 100644 --- a/runtime/staprun/common.c +++ b/runtime/staprun/common.c @@ -82,7 +82,7 @@ int stap_strfloctime(char *buf, size_t max, const char *fmt, time_t t) num = tm.tm_year % 100; goto numbering02; case 'C': - num = ((tm.tm_year + 1900 - 1) / 100) + 1; + num = ((tm.tm_year + 1900) / 100); goto numbering; case 'm': num = tm.tm_mon + 1; @@ -107,7 +107,7 @@ int stap_strfloctime(char *buf, size_t max, const char *fmt, time_t t) if (num == 0) num = 12; goto numbering02; case 'j': - ret = snprintf(c, end - c, "%03d", tm.tm_yday); + ret = snprintf(c, end - c, "%03d", tm.tm_yday + 1); if (ret < 0) return ret; c += ret; break; @@ -117,7 +117,10 @@ int stap_strfloctime(char *buf, size_t max, const char *fmt, time_t t) case 'l': num = tm.tm_hour % 12; if (num == 0) num = 12; - goto numbering; + ret = snprintf(c, end - c, "%2d", num); + if (ret < 0) return ret; + c += ret; + break; case 'M': num = tm.tm_min; goto numbering02; -- cgit From 54f1da8faec0408630348f70d78fc0f8ca07f876 Mon Sep 17 00:00:00 2001 From: Masami Hiramatsu Date: Wed, 22 Apr 2009 13:16:25 -0400 Subject: PR 9821: Use genuine strftime in staprun/stapio * Makefile.am: Add -fno-builtin-strftime to stapio_CFLAGS. * Makefile.in: Ditto. * runtime/staprun/common.c (stap_strfloctime): Use strftime(3). (parse_args): Remove strftime format limitation message. * main.cxx (usage): Ditto. * stap.1.in: Ditto. * staprun.8.in: Ditto. --- runtime/staprun/common.c | 112 +++-------------------------------------------- 1 file changed, 7 insertions(+), 105 deletions(-) (limited to 'runtime/staprun/common.c') diff --git a/runtime/staprun/common.c b/runtime/staprun/common.c index 26b166c2..c67ce340 100644 --- a/runtime/staprun/common.c +++ b/runtime/staprun/common.c @@ -55,112 +55,15 @@ static char *get_abspath(char *path) int stap_strfloctime(char *buf, size_t max, const char *fmt, time_t t) { - char *c = buf; - const char *c2 = fmt, *end = buf + max; - int ret, num; struct tm tm; + size_t ret; if (buf == NULL || fmt == NULL || max <= 1) return -EINVAL; localtime_r(&t, &tm); - - while (*c2 != '\0'){ - if (c + 1 >= end) - return -EINVAL; - if (*c2 != '%') { - *c++ = *c2++; - continue; - } - c2++; - switch (*c2++) { - case '%': - *c++ = '%'; - break; - case 'Y': - num = tm.tm_year + 1900; - goto numbering; - case 'y': - num = tm.tm_year % 100; - goto numbering02; - case 'C': - num = ((tm.tm_year + 1900) / 100); - goto numbering; - case 'm': - num = tm.tm_mon + 1; - goto numbering02; - case 'd': - num = tm.tm_mday; - goto numbering02; - case 'e': - num = tm.tm_mday; - goto numbering; - case 'F': - ret = snprintf(c, end - c, "%d-%02d-%02d", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); - if (ret < 0) return ret; - c += ret; - break; - case 'H': - num = tm.tm_hour; - goto numbering02; - case 'I': - num = tm.tm_hour % 12; - if (num == 0) num = 12; - goto numbering02; - case 'j': - ret = snprintf(c, end - c, "%03d", tm.tm_yday + 1); - if (ret < 0) return ret; - c += ret; - break; - case 'k': - num = tm.tm_hour; - goto numbering; - case 'l': - num = tm.tm_hour % 12; - if (num == 0) num = 12; - ret = snprintf(c, end - c, "%2d", num); - if (ret < 0) return ret; - c += ret; - break; - case 'M': - num = tm.tm_min; - goto numbering02; - case 'S': - num = tm.tm_sec; - goto numbering02; - case 'R': - ret = snprintf(c, end - c, "%02d:%02d", - tm.tm_hour, tm.tm_min); - if (ret < 0) return ret; - c += ret; - break; - case 'T': - ret = snprintf(c, end - c, "%02d:%02d:%02d", - tm.tm_hour, tm.tm_min, tm.tm_sec); - if (ret < 0) return ret; - c += ret; - break; - case 'u': - num = tm.tm_wday == 0 ? 7 : tm.tm_wday; - goto numbering; - case 'w': - num = tm.tm_wday; - goto numbering; - default: - return -EINVAL; - } - continue; -numbering: - ret = snprintf(c, end - c, "%d", num); - if (ret < 0) return ret; - c += ret; - continue; -numbering02: - ret = snprintf(c, end - c, "%02d", num); - if (ret < 0) return ret; - c += ret; - } - *c = '\0'; - return c - buf; + ret = strftime(buf, max, fmt, &tm); + if (ret == 0) + return -EINVAL; + return (int)ret; } void parse_args(int argc, char **argv) @@ -309,9 +212,8 @@ void usage(char *prog) err(" exit when it does. The '_stp_target' variable\n"); err(" will contain the pid for the command.\n"); err("-x pid Sets the '_stp_target' variable to pid.\n"); - err("-o FILE Send output to FILE. This supports a subset of\n"); - err(" strftime(3) (%%%%,%%C,%%Y,%%y,%%m,%%d,%%e,%%F,%%H,%%I\n"); - err(" %%j,%%k,%%l,%%M,%%S,%%R,%%T,%%u,%%w) for FILE.\n"); + err("-o FILE Send output to FILE. This supports strftime(3)\n"); + err(" formats for FILE.\n"); err("-b buffer size The systemtap module specifies a buffer size.\n"); err(" Setting one here will override that value. The\n"); err(" value should be an integer between 1 and 4095 \n"); -- cgit