1libtalloc_debugging(3)              talloc              libtalloc_debugging(3)
2
3
4

NAME

6       libtalloc_debugging - Chapter 6: Debugging Although talloc makes memory
7       management significantly easier than the C standard library, developers
8       are still only humans and can make mistakes. Therefore, it can be handy
9       to know some tools for the inspection of talloc memory usage.
10

Talloc log and abort

12       We have already encountered the abort function in section Dynamic type
13       system. In that case it was used when a type mismatch was detected.
14       However, talloc calls this abort function in several more situations:
15
16       · when the provided pointer is not a valid talloc context,
17
18       · when the meta data is invalid - probably due to memory corruption,
19
20       · and when an access after free is detected.
21
22       The third one is probably the most interesting. It can help us with
23       detecting an attempt to double-free a context or any other manipulation
24       with it via talloc functions (using it as a parent, stealing it, etc.).
25
26       Before the context is freed talloc sets a flag in the meta data. This
27       is then used to detect the access after free. It basically works on the
28       assumption that the memory stays unchanged (at least for a while) even
29       when it is properly deallocated. This will work even if the memory is
30       filled with the value specified in TALLOC_FREE_FILL environment
31       variable, because it fills only the data part and leaves the meta data
32       intact.
33
34       Apart from the abort function, talloc uses a log function to provide
35       additional information to the aforementioned violations. To enable
36       logging we shall set the log function with one of:
37
38       · talloc_set_log_fn()
39
40       · talloc_set_log_stderr()
41
42       The following code is a sample output of accessing a context after it
43       has been freed:
44
45       talloc_set_log_stderr();
46       TALLOC_CTX *ctx = talloc_new(NULL);
47
48       talloc_free(ctx);
49       talloc_free(ctx);
50
51       results in:
52       talloc: access after free error - first free may be at ../src/main.c:55
53       Bad talloc magic value - access after free
54
55       Another example is an invalid context:
56
57       talloc_set_log_stderr();
58       TALLOC_CTX *ctx = talloc_new(NULL);
59       char *str = strdup("not a talloc context");
60       talloc_steal(ctx, str);
61
62       results in:
63       Bad talloc magic value - unknown value
64

Memory usage reports

66       Talloc can print reports of memory usage of a specified talloc context
67       to a file (to stdout or stderr). The report can be simple or full. The
68       simple report provides information only about the context itself and
69       its direct descendants. The full report goes recursively through the
70       entire context tree. See:
71
72       · talloc_report()
73
74       · talloc_report_full()
75
76       We will use the following code to retrieve the sample report:
77
78       struct foo {
79         char *str;
80       };
81
82       TALLOC_CTX *ctx = talloc_new(NULL);
83       char *str =  talloc_strdup(ctx, "my string");
84       struct foo *foo = talloc_zero(ctx, struct foo);
85       foo->str = talloc_strdup(foo, "I am Foo");
86       char *str2 = talloc_strdup(foo, "Foo is my parent");
87
88       /* print full report */
89       talloc_report_full(ctx, stdout);
90
91       It will print a full report of ctx to the standard output. The message
92       should be similar to:
93
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
100       We can notice in this report that something is wrong with the context
101       containing struct foo. We know that the structure has only one string
102       element. However, we can see in the report that it has two children.
103       This indicates that we have either violated the memory hierarchy or
104       forgotten to free it as temporary data. Looking into the code, we can
105       see that 'Foo is my parent'  should be attached to ctx.
106
107       See also:
108
109       · talloc_enable_null_tracking()
110
111       · talloc_disable_null_tracking()
112
113       · talloc_enable_leak_report()
114
115       · talloc_enable_leak_report_full()
116
117Version 2.0                     Wed Jan 29 2020         libtalloc_debugging(3)
Impressum