[Pdbg] [PATCH 1/2] main: Add hexdump() function to dump bytes

Joel Stanley joel at jms.id.au
Mon Mar 4 15:26:51 AEDT 2019


On Thu, 21 Feb 2019 at 10:39, Amitay Isaacs <amitay at ozlabs.org> wrote:
>
> Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>

Reviewed-by: Joel Stanley <joel at jms.id.au>

This would make a handy ccan module.

> ---
>  Makefile.am              |  5 ++++-
>  src/tests/hexdump_test.c | 26 ++++++++++++++++++++++++++
>  src/util.c               | 35 +++++++++++++++++++++++++++++++++++
>  src/util.h               | 15 +++++++++++++++
>  4 files changed, 80 insertions(+), 1 deletion(-)
>  create mode 100644 src/tests/hexdump_test.c
>
> diff --git a/Makefile.am b/Makefile.am
> index f1e06f6..6258405 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -9,7 +9,7 @@ libpdbg_tests = libpdbg_target_test \
>                 libpdbg_probe_test3
>
>  bin_PROGRAMS = pdbg
> -check_PROGRAMS = $(libpdbg_tests) optcmd_test
> +check_PROGRAMS = $(libpdbg_tests) optcmd_test hexdump_test
>
>  PDBG_TESTS = \
>         tests/test_selection.sh         \
> @@ -67,6 +67,9 @@ DT_headers = $(DT:.dts=.dt.h)
>  optcmd_test_SOURCES = src/optcmd.c src/parsers.c src/tests/optcmd_test.c
>  optcmd_test_CFLAGS = -Wall -g
>
> +hexdump_test_SOURCES = src/util.c src/tests/hexdump_test.c
> +hexdump_test_CFLAGS = -Wall -g
> +
>  pdbg_SOURCES = \
>         src/cfam.c \
>         src/htm.c \
> diff --git a/src/tests/hexdump_test.c b/src/tests/hexdump_test.c
> new file mode 100644
> index 0000000..35a43fa
> --- /dev/null
> +++ b/src/tests/hexdump_test.c
> @@ -0,0 +1,26 @@
> +#include <stdio.h>
> +#include <stdlib.h>
> +
> +#include "../util.h"
> +
> +int main(int argc, const char **argv)
> +{
> +       uint8_t bytes[1000];
> +       int size, i;
> +
> +       size = 0;
> +       if (argc == 2)
> +               size = atoi(argv[1]);
> +       if (size <= 0 || size > 1000)
> +               size = 1000;
> +
> +       for (i=0; i<1000; i++)
> +               bytes[i] = i % 0xff;
> +
> +       hexdump(0, bytes, size, 1);
> +       hexdump(0, bytes, size, 2);
> +       hexdump(0, bytes, size, 4);
> +       hexdump(0, bytes, size, 8);
> +
> +       return 0;
> +}
> diff --git a/src/util.c b/src/util.c
> index 3a4520d..3093e36 100644
> --- a/src/util.c
> +++ b/src/util.c
> @@ -19,6 +19,9 @@
>  #include <stdbool.h>
>  #include <limits.h>
>  #include <assert.h>
> +#include <inttypes.h>
> +
> +#include "util.h"
>
>  /* Parse argument of the form 0-5,7,9-11,15,17 */
>  bool parse_list(const char *arg, int max, int *list, int *count)
> @@ -93,3 +96,35 @@ bool parse_list(const char *arg, int max, int *list, int *count)
>         return true;
>  }
>
> +void hexdump(uint64_t addr, uint8_t *buf, uint64_t size, uint8_t group_size)
> +{
> +       uint64_t i;
> +       int j, k;
> +
> +       if (group_size == 0)
> +               group_size = 1;
> +
> +       assert(group_size == 1 || group_size == 2 || group_size == 4 || group_size == 8);
> +
> +       printf("\n");
> +       for (i = 0; i < size + 15; i += 16) {
> +               bool do_prefix = true;
> +
> +               for (j = 0; j < 16; j += group_size) {
> +                       for (k = j; k < j+group_size; k++) {
> +                               if (i + k >= size) {
> +                                       printf("\n");
> +                                       return;
> +                               }
> +
> +                               if (do_prefix) {
> +                                       printf("0x%016"PRIx64": ", addr + i);
> +                                       do_prefix = false;
> +                               }
> +                               printf("%02x", buf[i + k]);
> +                       }
> +                       printf(" ");
> +               }
> +               printf("\n");
> +       }
> +}
> diff --git a/src/util.h b/src/util.h
> index b246e15..7bc6bb1 100644
> --- a/src/util.h
> +++ b/src/util.h
> @@ -16,6 +16,9 @@
>  #ifndef __UTIL_H
>  #define __UTIL_H
>
> +#include <stdbool.h>
> +#include <stdint.h>
> +
>  #define PPC_BIT(bit)           (0x8000000000000000 >> (bit))
>
>  /**
> @@ -33,4 +36,16 @@
>   */
>  bool parse_list(const char *arg, int max, int *list, int *count);
>
> +/**
> + * @brief Dump bytes in hex similar to hexdump format
> + *
> + * Prints 16 bytes per line in the specified groups.
> + *
> + * @param[in]  addr Address
> + * @param[in]  buf Data to print
> + * @param[in]  size Number of bytes to print
> + * @param[in]  group_size How to group bytes (valid values 1, 2, 4, 8)
> + */
> +void hexdump(uint64_t addr, uint8_t *buf, uint64_t size, uint8_t group_size);
> +
>  #endif
> --
> 2.20.1
>
> --
> Pdbg mailing list
> Pdbg at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/pdbg


More information about the Pdbg mailing list