2 * Debugger memory handling
4 * Copyright 1993 Eric Youngdale
5 * Copyright 1995 Alexandre Julliard
14 /************************************************************
16 * Check if linear pointer in [addr, addr+size[
20 ************************************************************/
22 #if defined(linux) || defined(__FreeBSD__)
23 BOOL32
DEBUG_checkmap_bad( const char *addr
, size_t size
, int rwflag
)
26 char buf
[80]; /* temporary line buffer */
27 char prot
[5]; /* protection string */
33 The entries in /proc/self/maps are of the form:
34 08000000-08002000 r-xp 00000000 03:41 2361
35 08002000-08003000 rw-p 00001000 03:41 2361
36 08003000-08005000 rwxp 00000000 00:00 0
37 40000000-40005000 r-xp 00000000 03:41 67219
38 40005000-40006000 rw-p 00004000 03:41 67219
39 40006000-40007000 rw-p 00000000 00:00 0
41 start end perm ??? major:minor inode
43 Only permissions start and end are used here
47 % cat /proc/curproc/map
48 start end resident private perm type
49 0x1000 0xe000 12 0 r-x COW vnode
50 0xe000 0x10000 2 2 rwx COW vnode
51 0x10000 0x27000 4 4 rwx default
52 0x800e000 0x800f000 1 1 rw- default
53 0xefbde000 0xefbfe000 1 1 rwx default
61 if (!(fp
= fopen("/proc/self/maps", "r")))
63 if (!(fp
= fopen("/proc/curproc/map", "r")))
67 while (fgets( buf
, 79, fp
)) {
69 sscanf(buf
, "%x-%x %3s", (int *) &start
, (int *) &end
, prot
);
71 sscanf(buf
, "%x %x %*d %*d %3s", (int *) &start
, (int *) &end
, prot
);
75 if (start
<= addr
&& addr
+size
< end
) {
77 ret
= (prot
[0] != 'r'); /* test for reading */
79 ret
= (prot
[1] != 'w'); /* test for writing */
86 #else /* linux || FreeBSD */
87 /* FIXME: code needed for BSD et al. */
88 BOOL32
DEBUG_checkmap_bad(char *addr
, size_t size
, int rwflag
)
92 #endif /* linux || FreeBSD */
95 /***********************************************************************
98 * Check if we are allowed to read memory at 'address'.
100 BOOL32
DEBUG_IsBadReadPtr( const DBG_ADDR
*address
, int size
)
102 if (!IS_SELECTOR_V86(address
->seg
))
103 if (address
->seg
) /* segmented addr */
105 if (IsBadReadPtr16( (SEGPTR
)MAKELONG( (WORD
)address
->off
,
106 (WORD
)address
->seg
), size
))
109 return DEBUG_checkmap_bad( DBG_ADDR_TO_LIN(address
), size
, 1);
113 /***********************************************************************
114 * DEBUG_IsBadWritePtr
116 * Check if we are allowed to write memory at 'address'.
118 BOOL32
DEBUG_IsBadWritePtr( const DBG_ADDR
*address
, int size
)
120 if (!IS_SELECTOR_V86(address
->seg
))
121 if (address
->seg
) /* segmented addr */
123 /* Note: we use IsBadReadPtr here because we are */
124 /* always allowed to write to read-only segments */
125 if (IsBadReadPtr16( (SEGPTR
)MAKELONG( (WORD
)address
->off
,
126 (WORD
)address
->seg
), size
))
129 return DEBUG_checkmap_bad( DBG_ADDR_TO_LIN(address
), size
, 0);
133 /***********************************************************************
136 * Read a memory value.
138 int DEBUG_ReadMemory( const DBG_ADDR
*address
)
140 DBG_ADDR addr
= *address
;
142 DBG_FIX_ADDR_SEG( &addr
, DS_reg(&DEBUG_context
) );
143 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(int) )) return 0;
144 return *(int *)DBG_ADDR_TO_LIN( &addr
);
148 /***********************************************************************
151 * Store a value in memory.
153 void DEBUG_WriteMemory( const DBG_ADDR
*address
, int value
)
155 DBG_ADDR addr
= *address
;
157 DBG_FIX_ADDR_SEG( &addr
, DS_reg(&DEBUG_context
) );
158 if (!DBG_CHECK_WRITE_PTR( &addr
, sizeof(int) )) return;
159 *(int *)DBG_ADDR_TO_LIN( &addr
) = value
;
163 /***********************************************************************
164 * DEBUG_ExamineMemory
166 * Implementation of the 'x' command.
168 void DEBUG_ExamineMemory( const DBG_ADDR
*address
, int count
, char format
)
170 DBG_ADDR addr
= * address
;
175 struct datatype
* testtype
;
176 unsigned short int * wdump
;
178 DBG_FIX_ADDR_SEG( &addr
, (format
== 'i') ?
179 CS_reg(&DEBUG_context
) : DS_reg(&DEBUG_context
) );
182 * Dereference pointer to get actual memory address we need to be
183 * reading. We will use the same segment as what we have already,
184 * and hope that this is a sensible thing to do.
186 if( addr
.type
!= NULL
)
188 if( addr
.type
== DEBUG_TypeIntConst
)
191 * We know that we have the actual offset stored somewhere
192 * else in 32-bit space. Grab it, and we
197 addr
.off
= DEBUG_GetExprValue(&addr
, NULL
);
202 if (!DBG_CHECK_READ_PTR( &addr
, 1 )) return;
203 DEBUG_TypeDerefPointer(&addr
, &testtype
);
204 if( testtype
!= NULL
|| addr
.type
== DEBUG_TypeIntConst
)
206 addr
.off
= DEBUG_GetExprValue(&addr
, NULL
);
210 else if (!addr
.seg
&& !addr
.off
)
212 fprintf(stderr
,"Invalid expression\n");
216 if (format
!= 'i' && count
> 1)
218 DEBUG_PrintAddress( &addr
, dbg_mode
, FALSE
);
219 fprintf(stderr
,": ");
222 pnt
= DBG_ADDR_TO_LIN( &addr
);
227 WCHAR
*ptr
= (WCHAR
*)pnt
;
228 if (count
== 1) count
= 256;
231 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(WCHAR
) )) return;
234 fputc( (char)*ptr
++, stderr
);
236 fprintf(stderr
,"\n");
240 if (count
== 1) count
= 256;
243 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(char) )) return;
246 fputc( *pnt
++, stderr
);
248 fprintf(stderr
,"\n");
254 DEBUG_PrintAddress( &addr
, dbg_mode
, TRUE
);
255 fprintf(stderr
,": ");
256 if (!DBG_CHECK_READ_PTR( &addr
, 1 )) return;
257 DEBUG_Disasm( &addr
, TRUE
);
258 fprintf(stderr
,"\n");
262 dump
= (unsigned int *)pnt
;
263 for(i
=0; i
<count
; i
++)
265 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(int) )) return;
266 fprintf(stderr
," %8.8x", *dump
++);
267 addr
.off
+= sizeof(int);
270 fprintf(stderr
,"\n");
271 DEBUG_PrintAddress( &addr
, dbg_mode
, FALSE
);
272 fprintf(stderr
,": ");
275 fprintf(stderr
,"\n");
279 dump
= (unsigned int *)pnt
;
280 for(i
=0; i
<count
; i
++)
282 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(int) )) return;
283 fprintf(stderr
," %d", *dump
++);
284 addr
.off
+= sizeof(int);
287 fprintf(stderr
,"\n");
288 DEBUG_PrintAddress( &addr
, dbg_mode
, FALSE
);
289 fprintf(stderr
,": ");
292 fprintf(stderr
,"\n");
296 wdump
= (unsigned short *)pnt
;
297 for(i
=0; i
<count
; i
++)
299 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(short) )) return;
300 fprintf(stderr
," %04x", *wdump
++);
301 addr
.off
+= sizeof(short);
304 fprintf(stderr
,"\n");
305 DEBUG_PrintAddress( &addr
, dbg_mode
, FALSE
);
306 fprintf(stderr
,": ");
309 fprintf(stderr
,"\n");
313 for(i
=0; i
<count
; i
++)
315 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(char) )) return;
321 else fprintf(stderr
," %c", *pnt
++);
325 fprintf(stderr
,"\n");
326 DEBUG_PrintAddress( &addr
, dbg_mode
, FALSE
);
327 fprintf(stderr
,": ");
330 fprintf(stderr
,"\n");
334 for(i
=0; i
<count
; i
++)
336 if (!DBG_CHECK_READ_PTR( &addr
, sizeof(char) )) return;
337 fprintf(stderr
," %02x", (*pnt
++) & 0xff);
341 fprintf(stderr
,"\n");
342 DEBUG_PrintAddress( &addr
, dbg_mode
, FALSE
);
343 fprintf(stderr
,": ");
346 fprintf(stderr
,"\n");