2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: Support files for purify (incomplete)
10 #include <exec/lists.h>
11 #include <exec/execbase.h>
14 #include <proto/arossupport.h>
15 #include <proto/exec.h>
16 #include <aros/purify.h>
27 static const char * pmsNames
[] =
29 "free", "empty", "init.", "ro"
32 static struct List P_Memory
;
33 static int InitWasCalled
;
35 static struct PNode
* FindNode (APTR memPtr
);
36 static int FindNextNodes (APTR memPtr
, struct PNode
**, struct PNode
**);
38 extern void RT_ShowRTStack (void);
40 /*****************************************************************************
43 #include <aros/purify.h>
60 This function is not part of any library and may thus be called at
73 ******************************************************************************/
79 /*****************************************************************************
82 #include <aros/purify.h>
84 void Purify_AddMemory (
91 Add size bytes of memory at memPtr to watch. Any access to this
92 memory will be checked after this call.
95 memPtr - Start of the memory block
96 size - The size of the memory block
102 This function is not part of any library and may thus be called at
115 ******************************************************************************/
122 if (!(node
= malloc (sizeof (struct PNode
) + size
)) )
125 node
->Memory
= memPtr
;
127 memset (node
->State
, PMS_FREE
, size
);
129 AddHead (&P_Memory
, (struct Node
*)node
);
130 } /* Purify_AddMemory */
133 /*****************************************************************************
136 #include <aros/purify.h>
138 void Purify_SetState (
146 Brings a block of memory into a certain state (eg. empty, initialized,
147 read-only). memPtr and size must be within a block beforehand
148 declared with Purify_AddMemory().
151 memPtr - Where to start
152 size - How many bytes after memPtr
153 state - The new state of the memory:
154 PMS_EMPTY - The memory may be read from and written to,
155 but any read before the first write will yield an
156 error (read from uninitialized memory).
157 PMS_INITIALIZED - The memory has been initialized and may
158 be read and writen to.
159 PMS_READONLY - The memory may be read but not written to.
160 PMS_FREE - The memory may not be read from or written to.
177 ******************************************************************************/
179 struct PNode
* node
, * nodeBefore
, * nodeAfter
;
185 /* Look for a node which contains memPtr */
186 node
= FindNode (memPtr
);
193 /* Look for the node we know about which comes least before and
194 right next after memPtr. This will _not_ return any node
195 which contains memPtr because there is none. If there are
196 none or the area we look for is in none of them, print a
198 if (!FindNextNodes (memPtr
, &nodeBefore
, &nodeAfter
)
199 || mem
+size
<= nodeAfter
->Memory
202 kprintf ("Purify: Tried to set state %s for unpurifed memory at %p:%ld\n"
210 /* If we get here, we have found a node which comes after memPtr
211 but memPtr is not inside the node */
215 if (mem
+size
<= node
->Memory
+node
->Size
)
216 memset (&node
->State
[mem
- node
->Memory
], state
, size
);
218 kprintf ("Purify: %p:%ld exceeds PNode %p:%ld\n"
225 } /* Purify_SetState */
228 /*****************************************************************************
231 #include <aros/purify.h>
233 void Purify_CheckAccess (
241 Checks a specific kind of access to memPtr[0...size-1].
244 memPtr - Where the access happens
245 size - How many bytes are accessed
246 type - Kind of access (PMA_READ, PMA_WRITE or PMA_MODIFY)
263 ******************************************************************************/
271 /* Look for a node which contains memPtr */
272 node
= FindNode (memPtr
);
279 kprintf ("Purify: Illegal access %p:%ld\n"
287 if (mem
+size
> node
->Memory
+node
->Size
)
288 kprintf ("Purify: Access %p:%ld beyond bounds %p:%ld\n"
299 mem
= &node
->State
[mem
- node
->Memory
];
301 for ( ; size
; size
--)
306 kprintf ("Purify: Read of undefined memory at %p (%ld bytes from the beginning of the block)\n"
307 , node
->Memory
- node
->State
+ mem
314 case PMS_INITIALIZED
:
321 kprintf ("Purify: Read of freed memory at %p (%ld bytes from the beginning of the block)\n"
322 , node
->Memory
- node
->State
+ mem
334 mem
= &node
->State
[mem
- node
->Memory
];
336 for ( ; size
; size
--)
341 *mem
= PMS_INITIALIZED
;
344 case PMS_INITIALIZED
:
348 kprintf ("Purify: Write to readonly memory at %p (%ld bytes from the beginning of the block)\n"
349 , node
->Memory
- node
->State
+ mem
358 kprintf ("Purify: Write to freed memory at %p (%ld bytes from the beginning of the block)\n"
359 , node
->Memory
- node
->State
+ mem
371 mem
= &node
->State
[mem
- node
->Memory
];
373 for ( ; size
; size
--)
378 kprintf ("Purify: Modify of undefined memory at %p (%ld bytes from the beginning of the block)\n"
379 , node
->Memory
- node
->State
+ mem
386 case PMS_INITIALIZED
:
390 kprintf ("Purify: Modify of readonly memory at %p (%ld bytes from the beginning of the block)\n"
391 , node
->Memory
- node
->State
+ mem
399 kprintf ("Purify: Modify of freed memory at %p (%ld bytes from the beginning of the block)\n"
400 , node
->Memory
- node
->State
+ mem
411 } /* switch (access type) */
412 } /* Complete within bounds ? */
413 } /* Node with memPtr found ? */
414 } /* Purify_CheckAccess */
417 /*****************************************************************************
420 static struct PNode
* FindNode (
426 Searches for the PNode which contains memPtr.
429 memPtr - A pointer into a piece of memory previously made known
430 with Purify_AddMemory.
433 A pointer to a PNode which contains the memPtr or NULL if there
434 is no such pointer. No error will be printed.
437 Must not be called before Purify_Init().
449 ******************************************************************************/
453 for (node
=(struct PNode
*)GetHead(&P_Memory
); node
; node
=(struct PNode
*)GetSucc(node
))
455 if (node
->Memory
<= (UBYTE
*)memPtr
456 || (UBYTE
*)memPtr
< node
->Memory
+node
->Size
465 /*****************************************************************************
468 static int FindNextNodes (
472 struct PNode
** before
,
473 struct PNode
** after
)
476 Returns the addresses of the PNodes right before and right right
480 memPtr - The address to look for
481 before - Pointer to a pointer to PNode where the address of
482 the node right before memPtr will be stored.
483 after - Pointer to a pointer to PNode where the address of
484 the node right after memPtr will be stored.
487 The number of found PNodes. *before will contain a pointer to
488 the PNode which is before memPtr or which contains memPtr or NULL
489 if there is no node before PNode. *after will contain a pointer
490 to the first PNode which comes right after memPtr or NULL if no
491 PNode follows memPtr.
494 Must not be called before Purify_Init().
506 ******************************************************************************/
514 for (node
=(struct PNode
*)GetHead(&P_Memory
); node
; node
=(struct PNode
*)GetSucc(node
))
518 if (node
->Memory
< (UBYTE
*)memPtr
)
526 if (node
->Memory
< (UBYTE
*)memPtr
527 && (*before
)->Memory
< node
->Memory
537 if (node
->Memory
> (UBYTE
*)memPtr
)
545 if (node
->Memory
> (UBYTE
*)memPtr
546 && (*after
)->Memory
> node
->Memory
561 } /* FindNextNodes */
564 /*****************************************************************************
588 ******************************************************************************/
591 /*****************************************************************************
615 ******************************************************************************/
618 /*****************************************************************************
642 ******************************************************************************/