doc: Add talloc tutorial.
[Samba/vl.git] / lib / talloc / doc / tutorial_debugging.dox
blob0cc08663701d3767277d416e0ed8a2e00e334deb
1 /**
2 @page libtalloc_debugging Chapter 6: Debugging
4 Although talloc makes memory management significantly easier than the C standard
5 library, developers are still only humans and can make mistakes. Therefore, it
6 can be handy to know some tools for the inspection of talloc memory usage.
8 @section log-abort Talloc log and abort
10 We have already encountered the abort function in section @ref dts.
11 In that case it was used when a type mismatch was detected. However, talloc
12 calls this abort function in several more situations:
14 - when the provided pointer is not a valid talloc context,
15 - when the meta data is invalid - probably due to memory corruption,
16 - and when an access after free is detected.
18 The third one is probably the most interesting. It can help us with detecting
19 an attempt to double-free a context or any other manipulation with it via
20 talloc functions (using it as a parent, stealing it, etc.).
22 Before the context is freed talloc sets a flag in the meta data. This is then
23 used to detect the access after free. It basically works on the assumption that
24 the memory stays unchanged (at least for a while) even when it is properly
25 deallocated. This will work even if the memory is filled with the value
26 specified in <code>TALLOC_FREE_FILL</code> environment variable, because it
27 fills only the data part and leaves the meta data intact.
29 Apart from the abort function, talloc uses a log function to provide additional
30 information to the aforementioned violations. To enable logging we shall set the
31 log function with one of:
33 - talloc_set_log_fn()
34 - talloc_set_log_stderr()
36 Below given is an sample output of accessing a context after it has been freed:
38 @code
39 talloc_set_log_stderr();
40 TALLOC_CTX *ctx = talloc_new(NULL);
42 talloc_free(ctx);
43 talloc_free(ctx);
45 results in:
46 talloc: access after free error - first free may be at ../src/main.c:55
47 Bad talloc magic value - access after free
48 @endcode
50 Another example below is an example of the invalid context:
52 @code
53 talloc_set_log_stderr();
54 TALLOC_CTX *ctx = talloc_new(NULL);
55 char *str = strdup("not a talloc context");
56 talloc_steal(ctx, str);
58 results in:
59 Bad talloc magic value - unknown value
60 @endcode
62 @section reports Memory usage reports
64 Talloc can print reports of memory usage of specified talloc context to a file
65 (or to <code>stdout</code> or <code>stderr</code>). The report can be simple or
66 full. The simple report provides information only about the context itself and
67 its direct descendants. The full report goes recursively through the entire
68 context tree. See:
70 - talloc_report()
71 - talloc_report_full()
73 We will use following code to retrieve the sample report:
75 @code
76 struct foo {
77   char *str;
80 TALLOC_CTX *ctx = talloc_new(NULL);
81 char *str =  talloc_strdup(ctx, "my string");
82 struct foo *foo = talloc_zero(ctx, struct foo);
83 foo->str = talloc_strdup(foo, "I am Foo");
84 char *str2 = talloc_strdup(foo, "Foo is my parent");
86 /* print full report */
87 talloc_report_full(ctx, stdout);
88 @endcode
90 It will print a full report of <code>ctx</code> to the standard output.
91 The message should be similar to:
93 @code
94 full talloc report on 'talloc_new: ../src/main.c:82' (total 46 bytes in 5 blocks)
95   struct foo contains 34 bytes in 3 blocks (ref 0) 0x1495130
96     Foo is my parent contains 17 bytes in 1 blocks (ref 0) 0x1495200
97     I am Foo contains 9 bytes in 1 blocks (ref 0) 0x1495190
98   my string contains 10 bytes in 1 blocks (ref 0) 0x14950c0
99 @endcode
101 We can notice in this report that something is wrong with the context containing
102 <code>struct foo</code>. We know that the structure has only one string element.
103 However, we can see in the report that it has two children. This indicates that
104 we have either violated the memory hierarchy or forgotten to free it as
105 temporary data. Looking into the code, we can see that <code>"Foo is my parent"
106 </code> should be attached to <code>ctx</code>.
108 See also:
110 - talloc_enable_null_tracking()
111 - talloc_disable_null_tracking()
112 - talloc_enable_leak_report()
113 - talloc_enable_leak_report_full()