Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dump memory of a process

When reading the /proc/$PID/maps you get the mapped memory regions. Is ther a way to dump one of this regions?

$ cat /proc/18448/maps
...[snip]...
0059e000-005b1000 r-xp 00000000 08:11 40         /usr/local/lib/libgstlightning.so.0.0.0
005b1000-005b2000 r--p 00012000 08:11 40         /usr/local/lib/libgstlightning.so.0.0.0
005b2000-005b3000 rw-p 00013000 08:11 40         /usr/local/lib/libgstlightning.so.0.0.0
...[snip]...

Thanks

like image 559
mathk Avatar asked Jul 27 '10 09:07

mathk


People also ask

How do I dump all memory in a process?

Pick one batch of memory (so for example 00621000-00622000) then use gdb as root to attach to the process and dump that memory: $ gdb --pid [pid] (gdb) dump memory /root/output 0x00621000 0x00622000 Then analyse /root/output with the strings command, less you want the PuTTY all over your screen.

How do I get a dump of all processes running?

Launch Process Explorer using "Run as Administrator" so that it will have permission to see all processes. 3. Find the hung process (es) and right-click to select "Create Dump -> Create Full Dump". Note Process Explorer creates a 32-bit dump of 32-bit processes, even when the 64-bit version of Process Explorer is running.

How to create a rule for dump collection of memory?

And a pop-up will come, to create a rule for dump collection. Select Memory and Handle Leak, then Next. You should see the list of processes now: please select the worker process from the list which you’re having the memory problem. Press Next.

What is the size of a process dump file?

Each process dump will take space in the disk approximately the same size the process uses in memory (column Commit Size in Task Manager). For example, if the w3wp.exe process memory usage is ~2 GB, then the size of each dump file will be around 2 GB.


3 Answers

Nah! Call ptrace() with PTRACE ATTACH. Then open /proc/<pid>/mem, seek to the region offset, and read the length of the region as given in /proc</pid>/maps.

Here's a program I wrote that does it in C. Here's a module I wrote that does it in Python (and the ptrace binding). For the finish, a program that dumps all regions of a process to files.

Enjoy!

like image 166
Matt Joiner Avatar answered Sep 25 '22 20:09

Matt Joiner


You can attach gdb to the process then dump memory region of length X words starting at location L with this: x/Xw L.

Attaching gdb when you start your process is simple: gdb ./executable then run. If you need to attach to a running process, start gdb then gdb attach pid where pid is is the process ID you care about.

like image 20
nmichaels Avatar answered Sep 25 '22 20:09

nmichaels


Using dd(1):

sudo dd if=/dev/mem bs=1 skip=$(( 16#0059e000 - 1 )) \
        count=$(( 16#005b1000 - 16#0059e000 + 1)) | hexdump -C
like image 30
jackIT Avatar answered Sep 25 '22 20:09

jackIT