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

NAME

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

Talloc log and abort

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

Memory usage reports

70       Talloc can print reports of memory usage of a specified talloc context
71       to a file (to stdout or stderr). The report can be simple or full. The
72       simple report provides information only about the context itself and
73       its direct descendants. The full report goes recursively through the
74       entire context tree. See:
75
76talloc_report()
77
78talloc_report_full()
79
80       We will use the following code to retrieve the sample report:
81
82       struct foo {
83         char *str;
84       };
85
86       TALLOC_CTX *ctx = talloc_new(NULL);
87       char *str =  talloc_strdup(ctx, "my string");
88       struct foo *foo = talloc_zero(ctx, struct foo);
89       foo->str = talloc_strdup(foo, "I am Foo");
90       char *str2 = talloc_strdup(foo, "Foo is my parent");
91
92       /* print full report */
93       talloc_report_full(ctx, stdout);
94
95       It will print a full report of ctx to the standard output. The message
96       should be similar to:
97
98       full talloc report on 'talloc_new: ../src/main.c:82' (total 46 bytes in 5 blocks)
99         struct foo contains 34 bytes in 3 blocks (ref 0) 0x1495130
100           Foo is my parent contains 17 bytes in 1 blocks (ref 0) 0x1495200
101           I am Foo contains 9 bytes in 1 blocks (ref 0) 0x1495190
102         my string contains 10 bytes in 1 blocks (ref 0) 0x14950c0
103
104       We can notice in this report that something is wrong with the context
105       containing struct foo. We know that the structure has only one string
106       element. However, we can see in the report that it has two children.
107       This indicates that we have either violated the memory hierarchy or
108       forgotten to free it as temporary data. Looking into the code, we can
109       see that 'Foo is my parent'  should be attached to ctx.
110
111       See also:
112
113talloc_enable_null_tracking()
114
115talloc_disable_null_tracking()
116
117talloc_enable_leak_report()
118
119talloc_enable_leak_report_full()
120
121Version 2.0                     Thu Jan 19 2023         libtalloc_debugging(3)
Impressum