pahole: Hex dump a type from stdio when it isn't a tty

For now the only aspect of the type that is considered is its size, so
one line each sizeof(type) will be hex dumped, e.g.:

  $ objcopy -O binary --only-section=.altinstructions ~/git/build/v5.7-rc2+/vmlinux .altinstructions
  $ ls -la .altinstructions
  -rwxrwxr-x. 1 acme acme 18551 Jun 24 09:09 .altinstructions
  $ pahole -C alt_instr ~/git/build/v5.7-rc2+/vmlinux
  struct alt_instr {
  	s32                        instr_offset;         /*     0     4 */
  	s32                        repl_offset;          /*     4     4 */
  	u16                        cpuid;                /*     8     2 */
  	u8                         instrlen;             /*    10     1 */
  	u8                         replacementlen;       /*    11     1 */
  	u8                         padlen;               /*    12     1 */

  	/* size: 13, cachelines: 1, members: 6 */
  	/* last cacheline: 13 bytes */
  } __attribute__((__packed__));
  $ pahole -C alt_instr ~/git/build/v5.7-rc2+/vmlinux < .altinstructions  | head
  0x90 0xe6 0xe0 0xff 0x73 0x48 0x00 0x00 0x70 0x00 0x05 0x05 0x00,
  0x83 0xe6 0xe0 0xff 0x6b 0x48 0x00 0x00 0x29 0x01 0x05 0x05 0x00,
  0x2a 0x72 0xc5 0xfe 0x63 0x48 0x00 0x00 0xeb 0x00 0x02 0x00 0x00,
  0x22 0x72 0xc5 0xfe 0x56 0x48 0x00 0x00 0x91 0x00 0x05 0x05 0x05,
  0x0c 0x73 0xc5 0xfe 0x4e 0x48 0x00 0x00 0xeb 0x00 0x02 0x00 0x00,
  0x04 0x73 0xc5 0xfe 0x41 0x48 0x00 0x00 0x91 0x00 0x02 0x00 0x00,
  0x72 0x73 0xc5 0xfe 0x34 0x48 0x00 0x00 0xf3 0x00 0x2b 0x2b 0x29,
  0xca 0x73 0xc5 0xfe 0x52 0x48 0x00 0x00 0xec 0x00 0x18 0x18 0x16,
  0xbd 0x73 0xc5 0xfe 0x5d 0x48 0x00 0x00 0xed 0x00 0x18 0x05 0x16,
  0xd3 0x7a 0xc5 0xfe 0x55 0x48 0x00 0x00 0x34 0x01 0x03 0x03 0x03,
  $

Suggested-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2020-06-24 09:08:17 -03:00
parent 38109ab45f
commit d8079c6d37
1 changed files with 48 additions and 0 deletions

View File

@ -14,6 +14,7 @@
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dwarves_reorganize.h"
#include "dwarves.h"
@ -1197,6 +1198,44 @@ static void do_reorg(struct tag *class, struct cu *cu)
*/
}
static int tag__fprintf_hexdump_value(struct tag *type, struct cu *cu, void *instance, int _sizeof, FILE *fp)
{
uint8_t *contents = instance;
int i, printed = 0;
for (i = 0; i < _sizeof; ++i) {
if (i != 0) {
fputc(' ', fp);
++printed;
}
printed += fprintf(fp, "0x%02x", contents[i]);
}
return printed;
}
static int tag__fprintf_value(struct tag *type, struct cu *cu, void *instance, int _sizeof, FILE *fp)
{
return tag__fprintf_hexdump_value(type, cu, instance, _sizeof, fp);
}
static int tag__stdio_fprintf_value(struct tag *type, struct cu *cu, FILE *fp)
{
int _sizeof = tag__size(type, cu), printed = 0;
void *instance = malloc(_sizeof);
if (instance == NULL)
return -ENOMEM;
while (fread(instance, _sizeof, 1, stdin) == 1) {
printed += tag__fprintf_value(type, cu, instance, _sizeof, fp);
printed += fprintf(fp, ",\n");
}
return printed;
}
static enum load_steal_kind pahole_stealer(struct cu *cu,
struct conf_load *conf_load __unused)
{
@ -1253,6 +1292,15 @@ static enum load_steal_kind pahole_stealer(struct cu *cu,
}
}
if (!isatty(0)) {
/*
* For the pretty printer only the first class is considered,
* ignore the rest.
*/
tag__stdio_fprintf_value(class, cu, stdout);
return LSK__STOP_LOADING;
}
if (defined_in) {
puts(cu->name);
goto dump_it;