C programming question

John Wildberger wildberger-iRg7kjdsKiH3fQ9qLvQP4Q at public.gmane.org
Fri Dec 26 00:06:37 UTC 2003


On December 24, 2003 12:41 pm, Tim Writer wrote:

> A better way to write this is to use C99's exact size types (from
> <inttypes.h>):
I followed your advice and modified the program accordingly
___________________________________________________
/* mem_displ.cpp
 * Original program 'mem.c' by  Greg Franks  Dec 22 2003
 * modified, converted to C++ and commented  by J.Wildberger
 * execute with: ./mem_displ + arg (memloc in decimal number)
 * compile with: c++ -o mem_displ mem_displ.cpp
 */
#include <iostream>
#include <fcntl.h>       //required for O_RDONLY
#include <unistd.h>      //required for lseek
#include <iomanip>       //required for setbase
#include <inttypes.h>    //required for uint16_t
using namespace std;

int main(int argc, char *argv[] )
{
    int i;
    int max=10; // display 10 bytes starting at specified offset
    if (argc != 2)
        {
        cout <<"Specify memory offset from zero"<<endl;
        exit (1);
        }
    int n=atoi(argv[1]);
    int fd = open( "/dev/mem", O_RDONLY ); //open for read only
    if ( fd < 0 )
        {
	perror( "Cannot open /dev/mem: " );
	exit( 1 );
        }
    else
        {
	off_t offset = lseek( fd,  n, SEEK_SET );
	                             // fd ..file descriptor
                                     // n  ..number of offset bytes
				     // SEEK_SET ..the file offset shall be set to offset bytes
	if ( offset == (off_t)-1 )
	    {
	    perror( "Seek: "  );
	    exit( 1 );
	    }
	else
	    {
	    uint8_t buf[max];    //unsigned char
	    size_t nbytes;
	    ssize_t bytes_read;
	    nbytes = sizeof(buf);
	    bytes_read= read( fd, buf, nbytes);
	    cout <<"At offset "<<n<< " :";
            for (i=0; i<max; i++)
	      printf(" %.2X ",buf[i]);       //%.2X display hex in width of 2
	    }
        }
    exit ( 0 );
}

./mem_displ 20
==> At offset 20 : 54  FF  00  F0  57  FF  00  F0  5A  FF 

*****************************************************************************

> > On December 23, 2003 06:45 pm, Matthew Rice wrote:
> > > And this is better :
> > >
> > >         od -j 20 -N100 -t x2 /dev/mem  ( I modified this from 2 to 100 
                                                             and offset 20)
==>
0000000 0001 0000 ff5a f000 e2c3 f000 ff5a f000
0000020 ff5a f000 ff54 f000 ff57 f000 ff5a f000
0000040 fea5 f000 e987 f000 007c f000 007c f000
0000060 007c f000 007c f000 ef57 f000 007c f000
0000100 0014 c000 f84d f000 f841 f000 e024 f000
0000120 e739 f000 f859 f000 e82e f000 efd2 f000
0000140 ff53 f000
0000144

This might be shorter, but very confusing. Note the numbering for each line. 
It is double of what it should be.(2 times hex)
Also the bytes are grouped in double bytes, the higher byte first, followed by 
lower byte. Compare this with the above result, that shows the proper 
sequence of bytes.
********************************************************************************
I am now happy to report, that it *IS* possible to display physical memory in 
a working Linux system. The "why" is a different issue.

John
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list