LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-10-2006, 10:50 AM   #1
magnus.therning
LQ Newbie
 
Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 20

Rep: Reputation: 0
Reading /proc/<pid>/mem


I'm having some problems with reading /proc/<pid>/mem

What I'm doing is the following:

1. attach to another process using ptrace (it's paused automatically doing this)
2. read that processes' memory map (/proc/<pid>/maps)
3. do a pread() on each "segment" in the map

I've verified that the ptrace attaching works, the process is paused as expected.

The problem is reading the data, and I have two problems:
  1. It seems I'm reading nothing but zeros.
  2. I can read the three first segments just fine:
    8048000 - 80bb000
    80bb000 - 80be000
    80be000 - 8179000
    but when I hit the next one:
    a7a63000 - a7a81000
    I get an "Invalid argument" from pread. I'm guessing it's the offset that's invalid since all the other arguments are unchanged from previous calls.

Any suggestions?
 
Old 07-10-2006, 12:02 PM   #2
magnus.therning
LQ Newbie
 
Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 20

Original Poster
Rep: Reputation: 0
lseek [Was: /proc/<pid>/mem]

Quote:
Originally Posted by magnus.therning
I'm having some problems with reading /proc/<pid>/mem

What I'm doing is the following:

1. attach to another process using ptrace (it's paused automatically doing this)
2. read that processes' memory map (/proc/<pid>/maps)
3. do a pread() on each "segment" in the map

I've verified that the ptrace attaching works, the process is paused as expected.

The problem is reading the data, and I have two problems:
  1. It seems I'm reading nothing but zeros.
  2. I can read the three first segments just fine:
    8048000 - 80bb000
    80bb000 - 80be000
    80be000 - 8179000
    but when I hit the next one:
    a7a63000 - a7a81000
    I get an "Invalid argument" from pread. I'm guessing it's the offset that's invalid since all the other arguments are unchanged from previous calls.

Any suggestions?
After some more testing it seems that the pread doesn't handle seeking to offsets large enough to read /proc/<pid>/mem. I've tried passing -D_FILE_OFFSET_BITS=64 on the command line when compiling, but that didn't seem to do it (I stuck an lseek in there as well just to make sure and it behaves exactly like pread).

Hmmm, am I missing something here?
 
Old 07-16-2006, 08:29 AM   #3
magnus.therning
LQ Newbie
 
Registered: Jul 2006
Location: Cambridge
Distribution: Debian
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by magnus.therning
After some more testing it seems that the pread doesn't handle seeking to offsets large enough to read /proc/<pid>/mem. I've tried passing -D_FILE_OFFSET_BITS=64 on the command line when compiling, but that didn't seem to do it (I stuck an lseek in there as well just to make sure and it behaves exactly like pread).

Hmmm, am I missing something here?
The problem is solved and I thought that for completeness I should write something on LinuxQuestions... even though I never managed to get anyone interested enough in my problem

Following the lseek64(3) man page I stuck the following at the top of the source to turn on 64 bit file offsets:

Code:
#define _FILE_OFFSET_BITS 64
What I had failed to do was to include unistd.h. It didn't result in any compiler or linker problems, so it took me some time to figure out. Once I had stuck the following in my source it all worked fine

Code:
#define _XOPEN_SOURCE 500
#include <unistd.h>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VmSize : Regarding /proc/<pid>/status shivaligupta Programming 0 04-06-2006 04:33 AM
Process Resurrection and /proc/pid lackluster Programming 3 03-20-2006 05:17 PM
Reading from a /proc file russelh Programming 2 12-02-2004 05:30 PM
api for reading files under /proc sreddy Programming 2 09-01-2004 02:52 PM
about /proc/$pid/stat 's start time henryluo Programming 2 06-21-2004 09:15 AM


All times are GMT -5. The time now is 05:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration