| 1 |
'\" |
| 2 |
'\" Copyright (c) 1992-1999 Karl Lehenbauer & Mark Diekhans. |
| 3 |
'\" Copyright (c) 2000 Scriptics Corporation. |
| 4 |
'\" All rights reserved. |
| 5 |
'\" |
| 6 |
.TH TCL_MEM_DEBUG 3 8.1 Tcl "Tcl Library Procedures" |
| 7 |
.so man.macros |
| 8 |
.BS |
| 9 |
.SH NAME |
| 10 |
TCL_MEM_DEBUG \- Compile-time flag to enable Tcl memory debugging |
| 11 |
.BE |
| 12 |
.SH DESCRIPTION |
| 13 |
When Tcl is compiled with \fBTCL_MEM_DEBUG\fR defined, a powerful set |
| 14 |
of memory debugging aids is included in the compiled binary. This |
| 15 |
includes C and Tcl functions which can aid with debugging |
| 16 |
memory leaks, memory allocation overruns, and other memory related |
| 17 |
errors. |
| 18 |
.SH "ENABLING MEMORY DEBUGGING" |
| 19 |
.PP |
| 20 |
To enable memory debugging, Tcl should be recompiled from scratch with |
| 21 |
\fBTCL_MEM_DEBUG\fR defined (e.g. by passing the |
| 22 |
\fI\-\-enable\-symbols=mem\fR flag to the \fIconfigure\fR script when |
| 23 |
building). This will also compile in a non-stub |
| 24 |
version of \fBTcl_InitMemory\fR to add the \fBmemory\fR command to Tcl. |
| 25 |
.PP |
| 26 |
\fBTCL_MEM_DEBUG\fR must be either left defined for all modules or undefined |
| 27 |
for all modules that are going to be linked together. If they are not, link |
| 28 |
errors will occur, with either \fBTcl_DbCkfree\fR and \fBTcl_DbCkalloc\fR or |
| 29 |
\fBTcl_Alloc\fR and \fBTcl_Free\fR being undefined. |
| 30 |
.PP |
| 31 |
Once memory debugging support has been compiled into Tcl, the C |
| 32 |
functions \fBTcl_ValidateAllMemory\fR, and \fBTcl_DumpActiveMemory\fR, |
| 33 |
and the Tcl \fBmemory\fR command can be used to validate and examine |
| 34 |
memory usage. |
| 35 |
.SH "GUARD ZONES" |
| 36 |
.PP |
| 37 |
When memory debugging is enabled, whenever a call to \fBckalloc\fR is |
| 38 |
made, slightly more memory than requested is allocated so the memory |
| 39 |
debugging code can keep track of the allocated memory, and eight-byte |
| 40 |
.QW "guard zones" |
| 41 |
are placed in front of and behind the space that will be |
| 42 |
returned to the caller. (The sizes of the guard zones are defined by the |
| 43 |
C #define \fBLOW_GUARD_SIZE\fR and #define \fBHIGH_GUARD_SIZE\fR |
| 44 |
in the file \fIgeneric/tclCkalloc.c\fR \(em it can |
| 45 |
be extended if you suspect large overwrite problems, at some cost in |
| 46 |
performance.) A known pattern is written into the guard zones and, on |
| 47 |
a call to \fBckfree\fR, the guard zones of the space being freed are |
| 48 |
checked to see if either zone has been modified in any way. If one |
| 49 |
has been, the guard bytes and their new contents are identified, and a |
| 50 |
.QW "low guard failed" |
| 51 |
or |
| 52 |
.QW "high guard failed" |
| 53 |
message is issued. The |
| 54 |
.QW "guard failed" |
| 55 |
message includes the address of the memory packet and |
| 56 |
the file name and line number of the code that called \fBckfree\fR. |
| 57 |
This allows you to detect the common sorts of one-off problems, where |
| 58 |
not enough space was allocated to contain the data written, for |
| 59 |
example. |
| 60 |
.SH "DEBUGGING DIFFICULT MEMORY CORRUPTION PROBLEMS" |
| 61 |
.PP |
| 62 |
Normally, Tcl compiled with memory debugging enabled will make it easy |
| 63 |
to isolate a corruption problem. Turning on memory validation with |
| 64 |
the memory command can help isolate difficult problems. If you |
| 65 |
suspect (or know) that corruption is occurring before the Tcl |
| 66 |
interpreter comes up far enough for you to issue commands, you can set |
| 67 |
\fBMEM_VALIDATE\fR define, recompile tclCkalloc.c and rebuild Tcl. |
| 68 |
This will enable memory validation from the first call to |
| 69 |
\fBckalloc\fR, again, at a large performance impact. |
| 70 |
.PP |
| 71 |
If you are desperate and validating memory on every call to |
| 72 |
\fBckalloc\fR and \fBckfree\fR is not enough, you can explicitly call |
| 73 |
\fBTcl_ValidateAllMemory\fR directly at any point. It takes a \fIchar |
| 74 |
*\fR and an \fIint\fR which are normally the filename and line number |
| 75 |
of the caller, but they can actually be anything you want. Remember |
| 76 |
to remove the calls after you find the problem. |
| 77 |
.SH "SEE ALSO" |
| 78 |
ckalloc, memory, Tcl_ValidateAllMemory, Tcl_DumpActiveMemory |
| 79 |
.SH KEYWORDS |
| 80 |
memory, debug |