Skip to content

Commit

Permalink
examples/frame: add hexdump support
Browse files Browse the repository at this point in the history
  • Loading branch information
quarium authored and cmassiot committed Feb 13, 2025
1 parent 9373dc4 commit 60a6784
Showing 1 changed file with 84 additions and 31 deletions.
115 changes: 84 additions & 31 deletions examples/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,43 @@ static bool decode = false;
static bool dump_date = false;
static bool dump_size = false;
static bool dump_random = false;
static bool dump_hex = false;
static int dump_hex_size = -1;

enum {
OPT_VERBOSE,
OPT_QUIET,
OPT_HELP,
OPT_TS,
OPT_FRAMER,
OPT_REFRAME,
OPT_DECODE,
OPT_PID_FILTER_OUT,
OPT_PID,
OPT_DATE,
OPT_SIZE,
OPT_RANDOM,
OPT_HEX,
OPT_HEX_SIZE,
};

static struct option options[] = {
{ "verbose", no_argument, NULL, OPT_VERBOSE },
{ "quiet", no_argument, NULL, OPT_QUIET },
{ "help", no_argument, NULL, OPT_HELP },
{ "ts", no_argument, NULL, OPT_TS },
{ "framer", required_argument, NULL, OPT_FRAMER },
{ "reframe", no_argument, NULL, OPT_REFRAME },
{ "decode", no_argument, NULL, OPT_DECODE },
{ "pid-filter-out", no_argument, NULL, OPT_PID_FILTER_OUT },
{ "pid", required_argument, NULL, OPT_PID },
{ "date", no_argument, NULL, OPT_DATE },
{ "size", no_argument, NULL, OPT_SIZE },
{ "random", no_argument, NULL, OPT_RANDOM },
{ "hex", no_argument, NULL, OPT_HEX},
{ "hex-size", required_argument, NULL, OPT_HEX_SIZE },
{ NULL, 0, NULL, 0 },
};

struct pid {
struct uchain uchain;
Expand Down Expand Up @@ -181,6 +218,45 @@ static int catch_uref(struct uprobe *uprobe, struct upipe *upipe,
size, vsize, sample_size);
}

if (dump_hex) {
size_t block_size = 0;
if (ubase_check(uref_block_size(uref, &block_size))) {
const uint8_t *buffer = NULL;
int offset = 0;

printf("hexdump uref %p (block_size %zu)\n", uref, block_size);

if (dump_hex_size > 0 && dump_hex_size < block_size)
block_size = dump_hex_size;

while (block_size) {
int size = block_size;
if (!ubase_check(uref_block_read(uref, offset, &size, &buffer)) || !size) {
upipe_err(upipe, "fail to read buffer");
break;
}

for (int i = offset; i < offset + size; i++)
{
if (!(i % 16)) {
if (i)
printf("\n");
printf("%08x:", i);
}
if (!(i % 2 ))
printf(" ");
printf("%02x", buffer[i - offset]);
}
block_size -= size;
offset += size;
}
if (offset)
printf("\n");
} else {
upipe_err(upipe, "block size failed");
}
}

return UBASE_ERR_NONE;
}

Expand Down Expand Up @@ -449,37 +525,6 @@ static struct upipe *upipe_source_alloc(const char *uri, struct uprobe *uprobe)
return upipe_src;
}

enum {
OPT_VERBOSE,
OPT_QUIET,
OPT_HELP,
OPT_TS,
OPT_FRAMER,
OPT_REFRAME,
OPT_DECODE,
OPT_PID_FILTER_OUT,
OPT_PID,
OPT_DATE,
OPT_SIZE,
OPT_RANDOM,
};

static struct option options[] = {
{ "verbose", no_argument, NULL, OPT_VERBOSE },
{ "quiet", no_argument, NULL, OPT_QUIET },
{ "help", no_argument, NULL, OPT_HELP },
{ "ts", no_argument, NULL, OPT_TS },
{ "framer", required_argument, NULL, OPT_FRAMER },
{ "reframe", no_argument, NULL, OPT_REFRAME },
{ "decode", no_argument, NULL, OPT_DECODE },
{ "pid-filter-out", no_argument, NULL, OPT_PID_FILTER_OUT },
{ "pid", required_argument, NULL, OPT_PID },
{ "date", no_argument, NULL, OPT_DATE },
{ "size", no_argument, NULL, OPT_SIZE },
{ "random", no_argument, NULL, OPT_RANDOM },
{ NULL, 0, NULL, 0 },
};

static void usage(const char *name)
{
fprintf(stderr, "usage: %s [options] <source>\n", name);
Expand Down Expand Up @@ -556,6 +601,14 @@ int main(int argc, char *argv[])
dump_random = true;
break;

case OPT_HEX:
dump_hex = true;
break;

case OPT_HEX_SIZE:
dump_hex_size = atoi(optarg);
break;

default:
fprintf(stderr, "unknown option -%c\n", opt);
break;
Expand Down

0 comments on commit 60a6784

Please sign in to comment.