Thread: Reading mmap() values....

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    4

    Reading mmap() values....

    So i've mapped a section of code utilizing the mmap() call.

    Code:
    size_t pSize = getpagesize();
    printf("Page Size: %zu\n", pSize);
    int dh = open("/dev/mem", O_RDWR | O_SYNC); // Open /dev/mem which represents the whole physical memory
    
    unsignedint *addr = mmap(NULL, pSize, PROT_READ, MAP_SHARED, dh, 0x4000000);  // Memory map source address
    Now I have two different functions that are reading the memory at what I believe to be the same address.

    Code:
    voidcapture(void *addr, intbyteCnt)
    {
      unsigned int *p = addr;
      int offset;
      for (offset = 0; offset < byteCnt; offset++) {
        printf("%02X", p[offset]);
        if (offset % 4 == 3) {
          printf(" ");
        }
      }
      printf("\n");
    
    }
    and

    Code:
    voidmemCapture(void *addr, intbyteCnt)
    {
    
      unsigned int a;
      char *p = addr;
      memcpy(&a, p, byteCnt);
      printf("%X\n", a);
    
    }
    Why am i seeing two different values when I run both functions back to back.

    If I attempt to capture four bytes from both I get values such as
    587eb357
    57b3e758
    Last edited by nightpoison; 04-22-2020 at 12:03 PM. Reason: removed dog-food font

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what are your actual results?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    4
    Quote Originally Posted by Salem View Post
    So what are your actual results?
    sorry about that. I've updated the OP, but here are the values that I'm seeing
    587eb357
    57b3e758

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well one looks like the big-endian version of the other.

    Or if you want to see bytes in the order of significance in a little endian integer, you need to print them backwards.
    So that the MSB is on the left.
    Code:
    for (offset = byteCnt-1; offset >= 0; offset--)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    4
    Quote Originally Posted by Salem View Post
    Well one looks like the big-endian version of the other.

    Or if you want to see bytes in the order of significance in a little endian integer, you need to print them backwards.
    So that the MSB is on the left.
    Code:
    for (offset = byteCnt-1; offset >= 0; offset--)
    I kept looking and looking and I didn't notice that. its 99% the same

    except for one part.

    587eb357
    57b3e758

    the 7e e7 appears to be flipped. you have everthing else matching

    58 7e b3 57

    57 b3 e7 58

    I don't work with memory at this level, so this is fairly new to me.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the other problem is you're stepping through memory an int at a time here
    unsigned int *p = addr;

    Before you go poking around, make sure the basics work.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void capture(void *addr, int byteCnt)
    {
      unsigned char *p = addr;
      int offset;
      for (offset = byteCnt-1; offset >= 0; offset--) {
        printf("%02X", p[offset]);
        if (offset % 4 == 3) {
          printf(" ");
        }
      }
      printf("\n");
    }
    
    void memCapture(void *addr, int byteCnt)
    {
      unsigned int a;
      char *p = addr;
      memcpy(&a, p, byteCnt);
      printf("%X\n", a);
    }
    
    int main() {
      char test[] = "hello world";
      capture(&test[5],4);
      memCapture(&test[5],4);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Hex Values From A File
    By Xecutive in forum C Programming
    Replies: 8
    Last Post: 11-14-2016, 02:13 AM
  2. Reading two values from a file as one value?
    By Flotonic in forum C Programming
    Replies: 4
    Last Post: 04-08-2011, 05:53 PM
  3. Reading Wrong values
    By Tration in forum C Programming
    Replies: 2
    Last Post: 12-29-2010, 09:42 AM
  4. Reading Registry Values
    By phantomlord in forum Windows Programming
    Replies: 2
    Last Post: 10-09-2003, 02:09 PM
  5. reading in char values?
    By Daniel in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 09:18 PM

Tags for this Thread