4 // Virtual Memory Wrapper
6 // Copyright (c) 2004, Charles McGarvey
7 // All rights reserved.
9 // Redistribution and use in source and binary forms, with or without modification, are
10 // permitted provided that the following conditions are met:
12 // 1. Redistributions of source code must retain the above copyright notice, this list
13 // of conditions and the following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright notice, this
16 // list of conditions and the following disclaimer in the documentation and/or other
17 // materials provided with the distribution.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22 // SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 #import <Cocoa/Cocoa.h>
33 #include <mach/mach_vm.h>
34 #include <mach/vm_map.h>
38 #if defined( __cplusplus )
39 #define VMREGION_EXPORT extern "C"
40 #define VMREGION_IMPORT extern "C"
42 #define VMREGION_EXPORT extern
43 #define VMREGION_IMPORT extern
46 #if !defined( VMREGION_STATIC_INLINE )
47 #define VMREGION_STATIC_INLINE static __inline__
50 #if !defined( VMREGION_EXTERN_INLINE )
51 #define VMREGION_EXTERN_INLINE extern __inline__
55 // attributes of memory regions
56 enum _VMRegionAttributes
58 VMREGION_READABLE
= 1,
59 VMREGION_WRITABLE
= 2,
60 VMREGION_EXECUTABLE
= 4
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 typedef struct _VMRegion
70 // process information
73 mach_vm_address_t _address
;
79 VMREGION_EXPORT
const VMRegion VMNullRegion
; /* <0,0,0> */
83 #pragma mark Utility VM Functions
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
86 // get the number of regions a process has.
87 unsigned VMCountRegions( pid_t process
);
88 unsigned VMCountRegionsWithAttributes( pid_t process
, unsigned attribs
);
90 // these functions return allocated VMRegion objects.
91 // use VMReleaseRegion(1) to free up used memory.
92 // returns nil on error or if there is no region after prevRegion.
93 // pass nil for prevRegion to access the first region.
94 VMRegion
VMNextRegion( pid_t process
, VMRegion previous
);
95 VMRegion
VMNextRegionWithAttributes( pid_t process
, VMRegion previous
, unsigned attribs
);
97 // UTILITY functions - stop/resume processes
98 // returns YES on success, NO on failure
99 VMREGION_STATIC_INLINE BOOL
VMStopProcess( pid_t process
) { return (kill( process
, SIGSTOP
) == 0); }
100 VMREGION_STATIC_INLINE BOOL
VMContinueProcess( pid_t process
) { return (kill( process
, SIGCONT
) == 0); }
102 // lower-level reading/writing functions
103 // the returned NSData object should be retained by the caller.
104 NSData
*VMReadData( pid_t process
, mach_vm_address_t address
, mach_vm_size_t size
);
105 BOOL
VMReadBytes( pid_t process
, mach_vm_address_t address
, void *bytes
, mach_vm_size_t
*size
); // size is # bytes read after call
106 BOOL
VMWriteData( pid_t process
, mach_vm_address_t address
, NSData
*data
); // returns YES on success, NO on failure
107 BOOL
VMWriteBytes( pid_t process
, mach_vm_address_t address
, const void *bytes
, mach_vm_size_t size
);
111 #pragma mark Exported VM Functions
112 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
114 VMREGION_EXPORT VMRegion
VMMakeRegion( pid_t process
, mach_vm_address_t address
, mach_vm_size_t size
);
116 VMREGION_EXPORT BOOL
VMRegionSetData( VMRegion region
, NSData
*data
);
118 VMREGION_EXPORT NSString
*VMStringFromRegion( VMRegion region
);
122 #pragma mark Imported VM Functions
123 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
125 VMREGION_STATIC_INLINE mach_vm_address_t
VMRegionProcess( VMRegion region
)
127 return region
._process
;
130 VMREGION_STATIC_INLINE mach_vm_address_t
VMRegionAddress( VMRegion region
)
132 return region
._address
;
135 VMREGION_STATIC_INLINE mach_vm_size_t
VMRegionSize( VMRegion region
)
140 VMREGION_STATIC_INLINE
unsigned VMRegionAttributes( VMRegion region
)
142 return region
._attributes
;
145 VMREGION_STATIC_INLINE BOOL
VMRegionReadable( VMRegion region
)
147 return region
._attributes
& VMREGION_READABLE
;
150 VMREGION_STATIC_INLINE BOOL
VMRegionWritable( VMRegion region
)
152 return region
._attributes
& VMREGION_WRITABLE
;
155 VMREGION_STATIC_INLINE BOOL
VMRegionExecutable( VMRegion region
)
157 return region
._attributes
& VMREGION_EXECUTABLE
;
161 VMREGION_STATIC_INLINE NSData
*VMRegionData( VMRegion region
)
163 return VMReadData( region
._process
, region
._address
, region
._size
);
166 VMREGION_STATIC_INLINE BOOL
VMRegionBytes( VMRegion region
, void *bytes
, mach_vm_size_t
*size
)
168 *size
= region
._size
;
169 return VMReadBytes( region
._process
, region
._address
, bytes
, size
);
174 VMREGION_STATIC_INLINE BOOL
VMRegionIsNotNull( VMRegion region
)
176 return (region
._process
!= 0);
179 VMREGION_STATIC_INLINE BOOL
VMEqualRegions( VMRegion region1
, VMRegion region2
)
181 return (region1
._process
== region2
._process
&&
182 region1
._address
== region2
._address
&&
183 region1
._size
== region2
._size
&&
184 region1
._attributes
== region2
._attributes
);