Skip to content

Commit 4e954e0

Browse files
committed
examples/frame: add hexdump support
1 parent 0c4261c commit 4e954e0

File tree

1 file changed

+84
-31
lines changed

1 file changed

+84
-31
lines changed

examples/frame.c

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,43 @@ static bool decode = false;
9393
static bool dump_date = false;
9494
static bool dump_size = false;
9595
static bool dump_random = false;
96+
static bool dump_hex = false;
97+
static int dump_hex_size = -1;
98+
99+
enum {
100+
OPT_VERBOSE,
101+
OPT_QUIET,
102+
OPT_HELP,
103+
OPT_TS,
104+
OPT_FRAMER,
105+
OPT_REFRAME,
106+
OPT_DECODE,
107+
OPT_PID_FILTER_OUT,
108+
OPT_PID,
109+
OPT_DATE,
110+
OPT_SIZE,
111+
OPT_RANDOM,
112+
OPT_HEX,
113+
OPT_HEX_SIZE,
114+
};
115+
116+
static struct option options[] = {
117+
{ "verbose", no_argument, NULL, OPT_VERBOSE },
118+
{ "quiet", no_argument, NULL, OPT_QUIET },
119+
{ "help", no_argument, NULL, OPT_HELP },
120+
{ "ts", no_argument, NULL, OPT_TS },
121+
{ "framer", required_argument, NULL, OPT_FRAMER },
122+
{ "reframe", no_argument, NULL, OPT_REFRAME },
123+
{ "decode", no_argument, NULL, OPT_DECODE },
124+
{ "pid-filter-out", no_argument, NULL, OPT_PID_FILTER_OUT },
125+
{ "pid", required_argument, NULL, OPT_PID },
126+
{ "date", no_argument, NULL, OPT_DATE },
127+
{ "size", no_argument, NULL, OPT_SIZE },
128+
{ "random", no_argument, NULL, OPT_RANDOM },
129+
{ "hex", no_argument, NULL, OPT_HEX},
130+
{ "hex-size", required_argument, NULL, OPT_HEX_SIZE },
131+
{ NULL, 0, NULL, 0 },
132+
};
96133

97134
struct pid {
98135
struct uchain uchain;
@@ -181,6 +218,45 @@ static int catch_uref(struct uprobe *uprobe, struct upipe *upipe,
181218
size, vsize, sample_size);
182219
}
183220

221+
if (dump_hex) {
222+
size_t block_size = 0;
223+
if (ubase_check(uref_block_size(uref, &block_size))) {
224+
const uint8_t *buffer = NULL;
225+
int offset = 0;
226+
227+
printf("hexdump uref %p (block_size %zu)\n", uref, block_size);
228+
229+
if (dump_hex_size > 0 && dump_hex_size < block_size)
230+
block_size = dump_hex_size;
231+
232+
while (block_size) {
233+
int size = block_size;
234+
if (!ubase_check(uref_block_read(uref, offset, &size, &buffer)) || !size) {
235+
upipe_err(upipe, "fail to read buffer");
236+
break;
237+
}
238+
239+
for (int i = offset; i < offset + size; i++)
240+
{
241+
if (!(i % 16)) {
242+
if (i)
243+
printf("\n");
244+
printf("%08x:", i);
245+
}
246+
if (!(i % 2 ))
247+
printf(" ");
248+
printf("%02x", buffer[i - offset]);
249+
}
250+
block_size -= size;
251+
offset += size;
252+
}
253+
if (offset)
254+
printf("\n");
255+
} else {
256+
upipe_err(upipe, "block size failed");
257+
}
258+
}
259+
184260
return UBASE_ERR_NONE;
185261
}
186262

@@ -449,37 +525,6 @@ static struct upipe *upipe_source_alloc(const char *uri, struct uprobe *uprobe)
449525
return upipe_src;
450526
}
451527

452-
enum {
453-
OPT_VERBOSE,
454-
OPT_QUIET,
455-
OPT_HELP,
456-
OPT_TS,
457-
OPT_FRAMER,
458-
OPT_REFRAME,
459-
OPT_DECODE,
460-
OPT_PID_FILTER_OUT,
461-
OPT_PID,
462-
OPT_DATE,
463-
OPT_SIZE,
464-
OPT_RANDOM,
465-
};
466-
467-
static struct option options[] = {
468-
{ "verbose", no_argument, NULL, OPT_VERBOSE },
469-
{ "quiet", no_argument, NULL, OPT_QUIET },
470-
{ "help", no_argument, NULL, OPT_HELP },
471-
{ "ts", no_argument, NULL, OPT_TS },
472-
{ "framer", required_argument, NULL, OPT_FRAMER },
473-
{ "reframe", no_argument, NULL, OPT_REFRAME },
474-
{ "decode", no_argument, NULL, OPT_DECODE },
475-
{ "pid-filter-out", no_argument, NULL, OPT_PID_FILTER_OUT },
476-
{ "pid", required_argument, NULL, OPT_PID },
477-
{ "date", no_argument, NULL, OPT_DATE },
478-
{ "size", no_argument, NULL, OPT_SIZE },
479-
{ "random", no_argument, NULL, OPT_RANDOM },
480-
{ NULL, 0, NULL, 0 },
481-
};
482-
483528
static void usage(const char *name)
484529
{
485530
fprintf(stderr, "usage: %s [options] <source>\n", name);
@@ -556,6 +601,14 @@ int main(int argc, char *argv[])
556601
dump_random = true;
557602
break;
558603

604+
case OPT_HEX:
605+
dump_hex = true;
606+
break;
607+
608+
case OPT_HEX_SIZE:
609+
dump_hex_size = atoi(optarg);
610+
break;
611+
559612
default:
560613
fprintf(stderr, "unknown option -%c\n", opt);
561614
break;

0 commit comments

Comments
 (0)