logo
down
shadow

examining virtual memory block reported by pmap


examining virtual memory block reported by pmap

Content Index :

examining virtual memory block reported by pmap
Tag : memory , By : Matt
Date : January 12 2021, 08:33 AM

Hope this helps I am seeing a leak in my program. It does not get caught with "valgrind memcheck" ( I confirmed this with summary report, it was no were near the top usage I can see). I could get something closer to my memory usage upon using "valgrind massif --pages-as-heap". However it does not report a full Traceback for the portion thats does mmap and allocates big portions of memory and I can't do a examine of memory allocation also because I can collect massif output only after program is killed. Another thing i tried was to inspect the memory blocks taking lot of RSS space. However I don't know how to look at the contents of the memory block reported by pmap. putting that addr on gdb dint help. I heard some address randomization is used by gdb. Can some one help me how to get the symbol that corresponds to the memory location reported by pmap output. ,
putting that addr on gdb dint help.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

typedef struct Node { struct Node *next; char payload[64]; } Node;

int main()
{
  int j;   
  Node *head = NULL;

  for (j = 0; j < 1000000; j++) {
    Node *n = malloc(sizeof(*n));
    n->next = head;
    sprintf(n->payload, "string %d", j);
    head = n;
  }
  return 0;
}

gcc -Wall -g -std=c99 t.c && gdb -q ./a.out

(gdb) b 17
Breakpoint 1 at 0x4005e3: file t.c, line 17.
(gdb) r
Starting program: /tmp/a.out

Breakpoint 1, main () at t.c:17
17    return 0;
(gdb) info prog
    Using the running image of child process 23785.
Program stopped at 0x4005e3.
It stopped at breakpoint 1.
Type "info stack" or "info registers" for more information.
(gdb) shell pmap 23785
23785:   /tmp/a.out
0000000000400000      4K r-x-- a.out
0000000000600000      4K r---- a.out
0000000000601000      4K rw--- a.out
0000000000602000  78144K rw---   [ anon ]
00007ffff7a11000   1784K r-x-- libc-2.19.so
00007ffff7bcf000   2048K ----- libc-2.19.so
00007ffff7dcf000     16K r---- libc-2.19.so
00007ffff7dd3000      8K rw--- libc-2.19.so
00007ffff7dd5000     20K rw---   [ anon ]
00007ffff7dda000    140K r-x-- ld-2.19.so
00007ffff7fd1000     12K rw---   [ anon ]
00007ffff7ff6000      8K rw---   [ anon ]
00007ffff7ff8000      8K r----   [ anon ]
00007ffff7ffa000      8K r-x--   [ anon ]
00007ffff7ffc000      4K r---- ld-2.19.so
00007ffff7ffd000      4K rw--- ld-2.19.so
00007ffff7ffe000      4K rw---   [ anon ]
00007ffffffde000    132K rw---   [ stack ]
ffffffffff600000      4K r-x--   [ anon ]
 total            82356K
(gdb) x/30gx 0x602000
0x602000:   0x0000000000000000  0x0000000000000051
0x602010:   0x0000000000000000  0x3020676e69727473
0x602020:   0x0000000000000000  0x0000000000000000
0x602030:   0x0000000000000000  0x0000000000000000
0x602040:   0x0000000000000000  0x0000000000000000
0x602050:   0x0000000000000000  0x0000000000000051
0x602060:   0x0000000000602010  0x3120676e69727473
0x602070:   0x0000000000000000  0x0000000000000000
0x602080:   0x0000000000000000  0x0000000000000000
0x602090:   0x0000000000000000  0x0000000000000000
0x6020a0:   0x0000000000000000  0x0000000000000051
0x6020b0:   0x0000000000602060  0x3220676e69727473
0x6020c0:   0x0000000000000000  0x0000000000000000
0x6020d0:   0x0000000000000000  0x0000000000000000
0x6020e0:   0x0000000000000000  0x0000000000000000
(gdb) x/s 0x602018
0x602018:   "string 0"
(gdb) x/s 0x602068
0x602068:   "string 1"
(gdb) x/s 0x6020b8
0x6020b8:   "string 2"
(gdb) p *(Node*)0x602060
$1 = {next = 0x602010, payload = "string 1", '\000' <repeats 55 times>}
(gdb) p *(Node*)0x6020b0
$2 = {next = 0x602060, payload = "string 2", '\000' <repeats 55 times>}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

x86 - examining the memory


Tag : assembly , By : mlapida
Date : March 29 2020, 07:55 AM
I wish did fix the issue. So assume I want to examine an offset of a register in memory in x86-64 (AT&T style) in gdb.
x/10s $rbp + 0xa

How to use the same virtual memory block each time program is run in C#


Tag : chash , By : user92243
Date : March 29 2020, 07:55 AM
I wish this helpful for you I realize I never came back to answer this question after I figured it out. It turns out it's rather simple using the VirtualAlloc function from kernel32.dll.
In my case, I needed to reserve the same virtual address for an external process each time the program was opened (or in each process if multiple copies were running at the same time). So what I did was basically choose a virtual address and hard-code that into the program. This way, the external process would always be able to use the same virtual-address block of memory:
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType lAllocationType, MemoryProtection flProtect);

// use same virtual address in all processes
const uint reserveAddress = 0x00CF0000;

// reserve 160MB at that address immediately at the start of the program
VirtualAlloc(new IntPtr(reserveAddress), new UIntPtr(160*1024*1024), AllocationType.RESERVE, MemoryProtection.EXECUTE_READWRITE);

// now just pass that same address to my external process when needed
int result = StartExternalProcess(reserveAddress);

Why the memory not freed in top and pmap?


Tag : cpp , By : Debashree
Date : March 29 2020, 07:55 AM
hop of those help? Caching, caching, caching. Oh yeah, and fragmentation.
Memory is allocated via different layers. If your application allocates memory, it will ask it to the C/C++ runtime. The C/C++ runtime will check its own datastructures for available memory, and if it doesn't have any, it will forward the call to the OS. Depending on the C/C++ runtime (and version), the C/C++ runtime data structures might be extensive, or the C/C++ runtime might simply always forward the call to the OS directly. For Microsoft Visual Studio (not using Linux at this moment, sorry), I know that:

Solaris : pmap reports a different virtual memory size than ps


Tag : development , By : UpperLuck
Date : March 29 2020, 07:55 AM
seems to work fine I noticed that this question was still open and wanted to add how this story ended.
After a lot more digging I contacted customer support from Solari and send them a way to reproduce the problem. They confirmed that there was a bug in the kernel which caused this behavior.

Alternative to massive if/else block for examining different exception conditions?


Tag : java , By : geo
Date : March 29 2020, 07:55 AM
shadow
Privacy Policy - Terms - Contact Us © scrbit.com