Skip to content

Commit

Permalink
external/trace: Add follow option to dump_trace
Browse files Browse the repository at this point in the history
When monitoring traces, an option like the tail command's '-f' (follow)
is very useful. This option continues to append to the output as more
data arrives. Add an '-f' option to allow dump_trace to operate
similarly.

Tail also provides a '-s' (sleep time) option that
accompanies '-f'.  This controls how often new input will be polled. Add
a '-s' option that will make dump_trace sleep for N milliseconds before
checking for new input.

Signed-off-by: Jordan Niethe <jniethe5@gmail.com>
Signed-off-by: Stewart Smith <stewart@linux.ibm.com>
  • Loading branch information
iamjpn authored and stewartsmith committed May 20, 2019
1 parent a5038b4 commit e0ed7e9
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions external/trace/dump_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#include "../../ccan/endian/endian.h"
#include "../../ccan/short_types/short_types.h"
Expand All @@ -40,6 +41,9 @@ struct trace_entry {
struct list_node link;
};

static int follow;
static long poll_msecs;

static void *ezalloc(size_t size)
{
void *p;
Expand Down Expand Up @@ -243,19 +247,53 @@ static void display_traces(struct trace_reader *trs, int count)
heap_free(h);
}


/* Can't poll for 0 msec, so use 0 to signify failure */
static long get_mseconds(char *s)
{
char *end;
long ms;

errno = 0;
ms = strtol(s, &end, 10);
if (errno || *end || ms < 0)
return 0;
return ms;
}

static void usage(void)
{
errx(1, "Usage: dump_trace [-f [-s msecs]] file...");
}

int main(int argc, char *argv[])
{
struct trace_reader *trs;
struct trace_info *ti;
struct stat sb;
int fd, i;
int fd, opt, i;

poll_msecs = 1000;
while ((opt = getopt(argc, argv, "fs:")) != -1) {
switch (opt) {
case 'f':
follow++;
break;
case 's':
poll_msecs = get_mseconds(optarg);
if (follow && poll_msecs)
break;
/* fallthru */
default:
usage();
}
}
argc -= optind;
argv += optind;

if (argc < 2)
errx(1, "Usage: dump_trace file...");
if (argc < 1)
usage();

argc--;
argv++;
trs = ezalloc(sizeof(struct trace_reader) * argc);

for (i = 0; i < argc; i++) {
Expand All @@ -274,8 +312,12 @@ int main(int argc, char *argv[])
list_head_init(&trs[i].traces);
}

load_traces(trs, argc);
display_traces(trs, argc);
do {
load_traces(trs, argc);
display_traces(trs, argc);
if (follow)
usleep(poll_msecs * 1000);
} while (follow);

return 0;
}

0 comments on commit e0ed7e9

Please sign in to comment.