4 // Virtual Memory Wrapper
6 // Copyright (c) 2004, Chaz 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 // 3. Neither the name of the BrokenZipper nor the names of its contributors may be
20 // used to endorse or promote products derived from this software without specific
21 // prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
24 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26 // SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
34 // Web: http://www.brokenzipper.com/
35 // Email: chaz@brokenzipper.com
38 #import <Cocoa/Cocoa.h>
40 #include <mach/vm_region.h>
41 #include <mach/vm_map.h>
45 #if defined( __cplusplus )
46 #define VMREGION_EXPORT extern "C"
47 #define VMREGION_IMPORT extern "C"
49 #define VMREGION_EXPORT extern
50 #define VMREGION_IMPORT extern
53 #if !defined( VMREGION_STATIC_INLINE )
54 #define VMREGION_STATIC_INLINE static __inline__
57 #if !defined( VMREGION_EXTERN_INLINE )
58 #define VMREGION_EXTERN_INLINE extern __inline__
62 // attributes of memory regions
63 enum _VMRegionAttributes
65 VMREGION_READABLE
= 1,
66 VMREGION_WRITABLE
= 2,
67 VMREGION_EXECUTABLE
= 4
73 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 typedef struct _VMRegion
77 // process information
80 vm_address_t _address
;
86 VMREGION_EXPORT
const VMRegion VMNullRegion
; /* <0,0,0> */
90 #pragma mark Utility VM Functions
91 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
93 // get the number of regions a process has.
94 unsigned VMCountRegions( pid_t process
);
95 unsigned VMCountRegionsWithAttributes( pid_t process
, unsigned attribs
);
97 // these functions return allocated VMRegion objects.
98 // use VMReleaseRegion(1) to free up used memory.
99 // returns nil on error or if there is no region after prevRegion.
100 // pass nil for prevRegion to access the first region.
101 VMRegion
VMNextRegion( pid_t process
, VMRegion previous
);
102 VMRegion
VMNextRegionWithAttributes( pid_t process
, VMRegion previous
, unsigned attribs
);
104 // UTILITY functions - stop/resume processes
105 // returns YES on success, NO on failure
106 VMREGION_STATIC_INLINE BOOL
VMStopProcess( pid_t process
) { return (kill( process
, SIGSTOP
) == 0); }
107 VMREGION_STATIC_INLINE BOOL
VMContinueProcess( pid_t process
) { return (kill( process
, SIGCONT
) == 0); }
109 // lower-level reading/writing functions
110 // the returned NSData object should be retained by the caller.
111 NSData
*VMReadData( pid_t process
, vm_address_t address
, vm_size_t size
);
112 BOOL
VMReadBytes( pid_t process
, vm_address_t address
, void *bytes
, vm_size_t
*size
); // size is # bytes read after call
113 BOOL
VMWriteData( pid_t process
, vm_address_t address
, NSData
*data
); // returns YES on success, NO on failure
114 BOOL
VMWriteBytes( pid_t process
, vm_address_t address
, const void *bytes
, vm_size_t size
);
118 #pragma mark Exported VM Functions
119 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
121 VMREGION_EXPORT VMRegion
VMMakeRegion( pid_t process
, vm_address_t address
, vm_size_t size
);
123 VMREGION_EXPORT BOOL
VMRegionSetData( VMRegion region
, NSData
*data
);
125 VMREGION_EXPORT NSString
*VMStringFromRegion( VMRegion region
);
129 #pragma mark Imported VM Functions
130 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
132 VMREGION_STATIC_INLINE vm_address_t
VMRegionProcess( VMRegion region
)
134 return region
._process
;
137 VMREGION_STATIC_INLINE vm_address_t
VMRegionAddress( VMRegion region
)
139 return region
._address
;
142 VMREGION_STATIC_INLINE vm_size_t
VMRegionSize( VMRegion region
)
147 VMREGION_STATIC_INLINE
unsigned VMRegionAttributes( VMRegion region
)
149 return region
._attributes
;
152 VMREGION_STATIC_INLINE BOOL
VMRegionReadable( VMRegion region
)
154 return region
._attributes
& VMREGION_READABLE
;
157 VMREGION_STATIC_INLINE BOOL
VMRegionWritable( VMRegion region
)
159 return region
._attributes
& VMREGION_WRITABLE
;
162 VMREGION_STATIC_INLINE BOOL
VMRegionExecutable( VMRegion region
)
164 return region
._attributes
& VMREGION_EXECUTABLE
;
168 VMREGION_STATIC_INLINE NSData
*VMRegionData( VMRegion region
)
170 return VMReadData( region
._process
, region
._address
, region
._size
);
173 VMREGION_STATIC_INLINE BOOL
VMRegionBytes( VMRegion region
, void *bytes
, vm_size_t
*size
)
175 *size
= region
._size
;
176 return VMReadBytes( region
._process
, region
._address
, bytes
, size
);
181 VMREGION_STATIC_INLINE BOOL
VMRegionIsNotNull( VMRegion region
)
183 return (region
._process
!= 0);
186 VMREGION_STATIC_INLINE BOOL
VMEqualRegions( VMRegion region1
, VMRegion region2
)
188 return (region1
._process
== region2
._process
&&
189 region1
._address
== region2
._address
&&
190 region1
._size
== region2
._size
&&
191 region1
._attributes
== region2
._attributes
);