#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/*
 *   Print 16 bytes at the given kernel address. Run as root,
 *    or else you won't be able to open /dev/kmem. Take addresses
 *    from 'mdb -k'. Remember to prefix hexadecimal addresses with 0x
 */

int main(int argc, char** argv)
{
  unsigned long long addr;
  int fd;
  unsigned char buff[16];
  int i;

  if( argc < 2 ){
    printf("Usage: %s <address>\n", argv[0]);
    return(0);
  }

  addr = strtoull(argv[1], NULL, 0);
  if( errno ){
    perror("parse address");
    return(1);
  }

  if( (fd = open("/dev/kmem", O_RDONLY, NULL)) == -1 ){
    perror("open");
    return(2);
  }

  if( pread64(fd, buff, 16, addr) == -1 ){
    perror("pread");
    return(3);
  }

  for( i=0; i < 16 ; i++ )
    printf("%02x ", buff[i]);

  puts("");

  return 0;
}
