summaryrefslogtreecommitdiffstats
path: root/staplog.c
diff options
context:
space:
mode:
authorhiramatu <hiramatu>2007-10-19 20:04:35 +0000
committerhiramatu <hiramatu>2007-10-19 20:04:35 +0000
commit982b7e1512c618617f56e276a86519c8c45a131b (patch)
tree68f3b9357881deb7f3ddddbcd643a5e493bc4480 /staplog.c
parent1673e81efbe105a5645d180eee487f3f24950619 (diff)
downloadsystemtap-steved-982b7e1512c618617f56e276a86519c8c45a131b.tar.gz
systemtap-steved-982b7e1512c618617f56e276a86519c8c45a131b.tar.xz
systemtap-steved-982b7e1512c618617f56e276a86519c8c45a131b.zip
2007-10-19 Masami Hiramatsu <mhiramat@redhat.com>
* staplog.c: Cleanup code and remove end-of-line spaces. (create_output_filename): Use snprintf instead of sprintf and remove malloc(). (print_rchan_info): Merge into output_cpu_logs(). (open_output_file): Use GETBUF() instead of malloc(). (output_cpu_logs): Simplify logic and remove unused code. (do_staplog): Merge into cmd_staplog(). (help_staplog): Fix typos. * Makefile.am (STAPLOG): Add -Wall option. * Makefile.in: Regenerated.
Diffstat (limited to 'staplog.c')
-rw-r--r--staplog.c164
1 files changed, 62 insertions, 102 deletions
diff --git a/staplog.c b/staplog.c
index 6b6e70ba..d425d24d 100644
--- a/staplog.c
+++ b/staplog.c
@@ -1,8 +1,9 @@
/*
crash shared object for retrieving systemtap buffer
- Copyright (c) 2007 Hitachi,Ltd.,
+ Copyright (c) 2007, Hitachi, Ltd.,
Created by Satoru Moriya <satoru.moriya.br@hitachi.com>
-
+ Updated by Masami Hiramatsu <mhiramat@redhat.com>
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
@@ -77,12 +78,11 @@ struct per_cpu_data {
static struct rchan_offsets rchan_offsets;
static struct fake_rchan chan;
static struct per_cpu_data per_cpu[NR_CPUS];
-static FILE *outfp;
-static char *subbuf;
-static int is_global;
-static int is_broken;
-static int old_format;
-static int retrieve_all;
+static FILE *outfp = NULL;
+static char *subbuf = NULL;
+static int is_global = 0;
+static int old_format = 0;
+static int retrieve_all = 0;
void cmd_staplog(void);
void cmd_staplog_cleanup(void);
@@ -124,7 +124,7 @@ ERR:
error(FATAL, "cannot get rchan offset\n");
}
-static ulong get_rchan(ulong chan_addr)
+static ulong get_rchan(ulong chan_addr)
{
ulong rchan;
@@ -139,7 +139,7 @@ static ulong get_rchan(ulong chan_addr)
return rchan;
}
-static void get_rchan_buf(int cpu, ulong rchan)
+static void get_rchan_buf(int cpu, ulong rchan)
{
ulong rchan_buf;
struct per_cpu_data *pcd;
@@ -201,7 +201,7 @@ static int check_global_buffer(ulong rchan)
{
int cpu;
ulong rchan_buf[2];
-
+
for (cpu = 0; cpu < 2; cpu++) {
readmem(rchan + rchan_offsets.buf + sizeof(void*) * cpu,
KVADDR, &rchan_buf[cpu], sizeof(void*),
@@ -212,7 +212,7 @@ static int check_global_buffer(ulong rchan)
return 0;
}
-static void setup_global_data(char *module)
+static void setup_global_data(char *module)
{
int i;
ulong stp_utt_addr = 0;
@@ -244,52 +244,16 @@ static void setup_global_data(char *module)
return;
}
-static char *create_output_filename(int cpu)
+static void create_output_filename(char *buf, int len, int cpu)
{
- size_t max = 128;
- char *fname;
-
- fname = (char *)malloc(sizeof(char) * max);
if (is_global) {
- sprintf(fname, "global");
+ snprintf(buf, len, "global");
} else {
- sprintf(fname, "cpu%d", cpu);
- }
- if (is_broken) {
- strcat(fname, ".may_broken");
+ snprintf(buf, len, "cpu%d", cpu);
}
- return fname;
}
-static void print_rchan_info(size_t start, size_t end, size_t ready,
- size_t offset, char *dname, char *fname, int cpu)
-{
- size_t start_subbuf, end_subbuf;
-
- start_subbuf = start ? start - chan.n_subbufs : start;
- end_subbuf = start ? end - 1 - chan.n_subbufs : end - 1;
-
- fprintf(fp, "--- generating '%s/%s' ---\n", dname, fname);
- if (is_broken) {
- fprintf(fp, " read subbuf %ld(%ld) (offset:%ld-%ld)\n",
- (long)end_subbuf - chan.n_subbufs,
- (long)(end_subbuf % chan.n_subbufs),
- (long)offset,
- (long)chan.subbuf_size);
- } else {
- fprintf(fp, " subbufs ready on relayfs:%ld\n", (long)ready);
- fprintf(fp, " n_subbufs:%ld, read subbuf from:%ld(%ld) "
- "to:%ld(%ld) (offset:0-%ld)\n\n",
- (long)chan.n_subbufs,
- (long)start_subbuf,
- (long)(start_subbuf % chan.n_subbufs),
- (long)end_subbuf,
- (long)(end_subbuf % chan.n_subbufs),
- (long)offset);
- }
-}
-
-static void create_output_dir(char *dirname)
+static void create_output_dir(const char *dirname)
{
DIR *dir;
dir = opendir(dirname);
@@ -302,17 +266,15 @@ static void create_output_dir(char *dirname)
}
}
-static FILE *open_output_file(char *dname, char *fname)
+static FILE *open_output_file(const char *dname, const char *fname)
{
FILE *filp = NULL;
char *output_file;
- size_t dlength, flength;
-
- dlength = strlen(dname);
- flength = strlen(fname);
- output_file = (char *)malloc(sizeof(char) * (dlength + flength) + 1);
- output_file[dlength + flength] = '\0';
+ output_file = GETBUF(sizeof(char) * (strlen(dname) + strlen(fname) + 2));
+ if (output_file == NULL) {
+ error(FATAL, "cannot allocate memory for logfile name\n");
+ }
create_output_dir(dname);
sprintf(output_file,"%s/%s", dname, fname);
@@ -321,17 +283,20 @@ static FILE *open_output_file(char *dname, char *fname)
if (!filp) {
error(FATAL, "cannot create log file '%s'\n", output_file);
}
+ FREEBUF(output_file);
return filp;
}
+#define MAX_FNAME 128
+
static void output_cpu_logs(char *dirname)
{
int i;
struct per_cpu_data *pcd;
size_t n, idx, start, end, len;
size_t padding;
- char *source, *fname;
+ char *source, fname[MAX_FNAME + 1];
/* allocate subbuf memory */
subbuf = GETBUF(chan.subbuf_size);
@@ -340,7 +305,6 @@ static void output_cpu_logs(char *dirname)
}
for (i = 0; i < kt->cpus; i++) {
- is_broken = 0;
pcd = &per_cpu[i];
if (pcd->buf.subbufs_produced == 0 && pcd->buf.offset == 0) {
@@ -353,17 +317,24 @@ static void output_cpu_logs(char *dirname)
}
}
+ end = pcd->buf.subbufs_produced + 1;
if (pcd->buf.subbufs_produced >= chan.n_subbufs) {
- start = pcd->buf.subbufs_produced + 1;
- end = start + chan.n_subbufs;
+ start = end - chan.n_subbufs;
} else {
start = 0;
- end = pcd->buf.subbufs_produced + 1;
}
- fname = create_output_filename(i);
- print_rchan_info(start, end, pcd->buf.subbufs_produced + 1,
- pcd->buf.offset, dirname, fname, i);
+ create_output_filename(fname, MAX_FNAME, i);
+ fprintf(fp, "--- generating '%s/%s' ---\n", dirname, fname);
+ fprintf(fp, " subbufs ready on relayfs:%ld\n", (long)end);
+ fprintf(fp, " n_subbufs:%ld, read subbuf from:%ld(%ld) "
+ "to:%ld(%ld) (offset:0-%ld)\n\n",
+ (long)chan.n_subbufs,
+ (long)start,
+ (long)(start % chan.n_subbufs),
+ (long)end-1,
+ (long)((end-1) % chan.n_subbufs),
+ (long) pcd->buf.offset);
outfp = open_output_file(dirname, fname);
for (n = start; n < end; n++) {
@@ -373,19 +344,18 @@ static void output_cpu_logs(char *dirname)
readmem((ulong)pcd->buf.padding + sizeof(padding) * idx,
KVADDR, &padding, sizeof(padding),
"padding", FAULT_ON_ERROR);
- if (n == end - 1 &&
- pcd->buf.offset < chan.subbuf_size) {
+ if (n == end - 1) {
len = pcd->buf.offset;
} else {
len = chan.subbuf_size;
- }
+ }
if (old_format == 1) {
source += sizeof(unsigned int);
len -= sizeof(unsigned int) + padding;
} else {
len -= padding;
}
- if (len) {
+ if (len > 0) {
readmem((ulong)source, KVADDR, subbuf, len,
"subbuf", FAULT_ON_ERROR);
if (fwrite(subbuf, len, 1, outfp) != 1) {
@@ -395,40 +365,35 @@ static void output_cpu_logs(char *dirname)
}
fclose(outfp);
outfp = NULL;
- free(fname);
- fname = NULL;
/*
- * -a option retrieve the old data of subbuffer where the
+ * -a option retrieve the old data of subbuffer where the
* probe record is written at that time.
*/
- if (retrieve_all == 1 &&
- pcd->buf.subbufs_produced >= chan.n_subbufs) {
- is_broken = 1;
- fname = create_output_filename(i);
- print_rchan_info(start, end,
- pcd->buf.subbufs_produced + 1,
- pcd->buf.offset, dirname, fname, i);
+ if (retrieve_all == 1 && start != 0) {
+ strncat(fname, ".may_broken", MAX_FNAME);
+ fprintf(fp, "--- generating '%s/%s' ---\n", dirname, fname);
+ fprintf(fp, " read subbuf %ld(%ld) (offset:%ld-%ld)\n",
+ (long)start-1,
+ (long)((start-1) % chan.n_subbufs),
+ (long)pcd->buf.offset,
+ (long)chan.subbuf_size);
outfp = open_output_file(dirname, fname);
- idx = (end - 1) % chan.n_subbufs;
- source = pcd->buf.start + idx * chan.subbuf_size +
+
+ idx = (start - 1) % chan.n_subbufs;
+ source = pcd->buf.start + idx * chan.subbuf_size +
pcd->buf.offset;
- readmem((ulong)pcd->buf.padding + sizeof(padding)*idx,
- KVADDR, &padding, sizeof(padding),
- "padding", FAULT_ON_ERROR);
len = chan.subbuf_size - pcd->buf.offset;
if (len) {
readmem((ulong)source, KVADDR, subbuf, len,
"may_broken_subbuf", FAULT_ON_ERROR);
if(fwrite(subbuf, len, 1, outfp) != 1) {
- error(FATAL,
+ error(FATAL,
"cannot write log data(may_broken)\n");
}
}
fclose(outfp);
outfp = NULL;
- free(fname);
- fname = NULL;
}
if (is_global == 1)
break;
@@ -440,13 +405,6 @@ static void output_cpu_logs(char *dirname)
return;
}
-static void do_staplog(char *module, char *dirname)
-{
- setup_global_data(module);
- output_cpu_logs(dirname);
- return;
-}
-
void cmd_staplog(void)
{
@@ -474,7 +432,9 @@ void cmd_staplog(void)
if (dirname == NULL && module != NULL)
dirname = module;
- do_staplog(module, dirname);
+
+ setup_global_data(module);
+ output_cpu_logs(dirname);
return;
}
@@ -496,17 +456,17 @@ char *help_staplog[] = {
" is 'module_name' are written into log files. This command starts",
" to retrieve log data from the subbuffer which is next to current",
" written subbuffer. Therefore some old data in the current written",
- " subbuffer may not be retrieved. But -a option retrieve these data",
+ " subbuffer may not be retrieved. But -a option retrieves these data",
" and write them into another log file which have the special ",
" postfix `.may_broken`.",
" If you don't use -o option, the log files are created in",
" `module_name` directory. The name of each log file is cpu0, cpu1..",
- " ...cpuN. This command don't change the log data format, but remove",
+ " ...cpuN. This command doesn't change the log data format, but remove",
" only padding.",
"",
" -a Retrieve the old data which is recorded in",
- " current written subbufer and create another file",
- " that have the special postfix `.may_broken`",
+ " current written subbuffer and create another files",
+ " which have the special postfix `.may_broken`",
" for these data.",
" -o file_name Specify the output directory.",
NULL,
@@ -522,7 +482,7 @@ char *help_staplog_cleanup[] = {
NULL,
};
-static void __attribute__ ((constructor)) _init(void)
+static void __attribute__ ((constructor)) _init(void)
{
get_rchan_offsets();
register_extension(command_table);