[Pdbg] [PATCH v5 4/5] main: Use hexdump to dump memory from gemem

Alistair Popple alistair at popple.id.au
Wed Apr 17 15:37:15 AEST 2019


On Wednesday, 17 April 2019 3:26:00 PM AEST Amitay Isaacs wrote:
> On Wed, 2019-04-17 at 15:20 +1000, Alistair Popple wrote:
> > On Wednesday, 10 April 2019 6:01:50 PM AEST Amitay Isaacs wrote:
> > > If getmem is being run on a terminal and if the output contains
> > > non-printable characters, then use hexdump() to dump the data.
> > 
> > To be honest really don't like this behavior being dependent on where
> > stdout
> > is going to. It seems like it would be suprising. For example if
> > someone piped
> > this into grep they would expect it to be running against the hexdump
> > not the
> > raw binary which could be confusing.
> 
> Fair enough. So by default it will use hexdump() unless the buffer is
> printable.  And when given the extra flag, it'll be always binary.

Sounds good! Thanks.

- Alistair
 
> > The printable character detection is a great idea though. Can we just
> > add a
> > flag to force raw binary output instead of doing it based on write
> > location?
> > 
> > - Alistair
> > 
> > > Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
> > > ---
> > > 
> > >  src/mem.c | 20 ++++++++++++++++++--
> > >  1 file changed, 18 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/src/mem.c b/src/mem.c
> > > index 8045069..b264d98 100644
> > > --- a/src/mem.c
> > > +++ b/src/mem.c
> > > @@ -21,6 +21,7 @@
> > > 
> > >  #include <unistd.h>
> > >  #include <assert.h>
> > >  #include <stdbool.h>
> > > 
> > > +#include <ctype.h>
> > > 
> > >  #include <libpdbg.h>
> > > 
> > > @@ -28,6 +29,7 @@
> > > 
> > >  #include "progress.h"
> > >  #include "optcmd.h"
> > >  #include "parsers.h"
> > > 
> > > +#include "util.h"
> > > 
> > >  #define PR_ERROR(x, args...) \
> > >  
> > >  	pdbg_log(PDBG_ERROR, x, ##args)
> > > 
> > > @@ -75,8 +77,22 @@ static int _getmem(uint64_t addr, uint64_t size,
> > > uint8_t
> > > block_size, bool ci) }
> > > 
> > >  	if (count > 0) {
> > > 
> > > -		if (write(STDOUT_FILENO, buf, size) < 0)
> > > -			PR_ERROR("Unable to write stdout.\n");
> > > +		uint64_t i;
> > > +		bool printable = true;
> > > +
> > > +		for (i=0; i<size; i++) {
> > > +			if (!isprint(buf[i])) {
> > > +				printable = false;
> > > +				break;
> > > +			}
> > > +		}
> > > +
> > > +		if (isatty(STDOUT_FILENO) && !printable) {
> > > +			hexdump(addr, buf, size, 1);
> > > +		} else {
> > > +			if (write(STDOUT_FILENO, buf, size) < 0)
> > > +				PR_ERROR("Unable to write stdout.\n");
> > > +		}
> > > 
> > >  	}
> > >  	
> > >  	free(buf);
> 
> Amitay.




More information about the Pdbg mailing list