(Partial?) fix for building outside the source tree.
[cake.git] / compiler / purify / src / error.c
blobe2bb9229c607bf2bb65e40bd115b809a5c0af1d4
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include "error.h"
4 #include "hash.h"
5 #include "posinfo.h"
6 #include "memory.h"
8 static char * _Purify_MemTypeNames[] =
10 "Heap",
11 "Stack",
12 "Code",
13 "Data"
16 int Purify_Error;
18 static const char * purify_ErrorMsgs[] =
20 #undef _ERROR_DEF
21 #define _ERROR_DEF(n,s) s,
22 #include "error.def"
25 void Purify_PrePrintError (void)
27 RememberData rd;
29 Purify_RememberCallers (&rd);
31 fprintf (stderr, "*** Purify %s ***\n", purify_ErrorMsgs[Purify_Error]);
32 Purify_PrintCallers (&rd);
35 void Purify_PostPrintError (void)
37 fprintf (stderr, "***\n");
40 void Purify_PrintError (const char * fmt, ...)
42 va_list args;
44 Purify_PrePrintError ();
45 va_start (args, fmt);
46 vfprintf (stderr, fmt, args);
47 va_end (args);
48 putc ('\n', stderr);
49 Purify_PostPrintError ();
52 void Purify_PrintAccessError (const char * access, const void * addr, int size)
54 if (Purify_LastNode)
56 long offset = (long)addr - (long)(Purify_LastNode->mem);
58 if (Purify_LastNode->type != PURIFY_MemType_Heap)
60 Purify_PrintError (
61 "%s of %d bytes at %p. This is %d bytes into the block\n"
62 "%s%s%sat %p with the size of %d bytes (type=%s)",
63 access, size, addr, offset,
64 Purify_LastNode->data ? "\"" : "",
65 Purify_LastNode->data ? (char *)Purify_LastNode->data : "",
66 Purify_LastNode->data ? "\" " : "",
67 Purify_LastNode->mem, Purify_LastNode->size,
68 _Purify_MemTypeNames[Purify_LastNode->type]
71 else
73 PMemoryNode * node = (PMemoryNode *)Purify_LastNode->data;
75 Purify_PrePrintError ();
76 fprintf (stderr,
77 "%s of %d bytes at %p. This is %ld bytes into the block\n"
78 "allocated at %p with the size of %d bytes (type=%s)\n"
79 "The block was allocated ",
80 access, size, addr, offset,
81 Purify_LastNode->mem, Purify_LastNode->size,
82 _Purify_MemTypeNames[Purify_LastNode->type]
84 Purify_PrintCallers (&node->alloc);
85 if (node->free.nstack != -1)
87 fprintf (stderr, "It was freed ");
88 Purify_PrintCallers (&node->free);
90 Purify_PostPrintError ();
93 else
95 int offset;
96 MemHash * next = Purify_FindNextMemory (addr, &offset);
98 if (next->type != PURIFY_MemType_Heap)
100 Purify_PrintError (
101 "%s of %d bytes at %p. This is %d bytes %s the block\n"
102 "%s%s%sat %p with the size of %d bytes (type=%s)",
103 access, size, addr, offset, (offset < 0 ? "before" : "after"),
104 next->data ? "\"" : "",
105 next->data ? (char *)next->data : "",
106 next->data ? "\" " : "",
107 next->mem, next->size,
108 _Purify_MemTypeNames[next->type]
111 else
113 PMemoryNode * node = (PMemoryNode *)next->data;
115 Purify_PrePrintError ();
116 fprintf (stderr,
117 "%s of %d bytes at %p. This is %d bytes %s the block\n"
118 "allocated at %p with the size of %d bytes (type=%s)\n"
119 "The block was allocated ",
120 access, size, addr, offset, (offset < 0 ? "before" : "after"),
121 next->mem, next->size,
122 _Purify_MemTypeNames[next->type]
124 Purify_PrintCallers (&node->alloc);
125 if (node->free.nstack != -1)
127 fprintf (stderr, "It was freed ");
128 Purify_PrintCallers (&node->free);
130 Purify_PostPrintError ();