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