1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Michael Zucchi <notzed@ximian.com>
*/
#include "evolution-data-server-config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_BACKTRACE_SYMBOLS
#include <execinfo.h>
#ifdef HAVE_ELFUTILS_LIBDWFL
#include <elfutils/libdwfl.h>
#include <errno.h>
#include <unistd.h>
#endif
#endif
#include <glib-object.h>
#include "camel-debug.h"
gint camel_verbose_debug;
static GHashTable *debug_table = NULL;
/**
* camel_debug_init:
*
* Init camel debug.
*
* CAMEL_DEBUG is set to a comma separated list of modules to debug.
* The modules can contain module-specific specifiers after a ':', or
* just act as a wildcard for the module or even specifier. e.g. 'imap'
* for imap debug, or 'imap:folder' for imap folder debug. Additionaly,
* ':folder' can be used for a wildcard for any folder operations.
**/
void
camel_debug_init (void)
{
gchar *d;
d = g_strdup (getenv ("CAMEL_DEBUG"));
if (d) {
gchar *p;
debug_table = g_hash_table_new (g_str_hash, g_str_equal);
p = d;
while (*p) {
while (*p && *p != ',')
p++;
if (*p)
*p++ = 0;
g_hash_table_insert (debug_table, d, d);
d = p;
}
if (g_hash_table_lookup (debug_table, "all"))
camel_verbose_debug = 1;
}
}
/**
* camel_debug:
* @mode: string name of the mode to check for
*
* Check to see if a debug mode is activated. @mode takes one of two forms,
* a fully qualified 'module:target', or a wildcard 'module' name. It
* returns a boolean to indicate if the module or module and target is
* currently activated for debug output.
*
* Returns: Whether the debug @mode is activated
**/
gboolean
camel_debug (const gchar *mode)
{
if (camel_verbose_debug)
return TRUE;
if (debug_table) {
gchar *colon;
gchar *fallback;
gsize fallback_len;
if (g_hash_table_lookup (debug_table, mode))
return TRUE;
/* Check for fully qualified debug */
colon = strchr (mode, ':');
if (colon) {
fallback_len = strlen (mode) + 1;
fallback = g_alloca (fallback_len);
g_strlcpy (fallback, mode, fallback_len);
colon = (colon - mode) + fallback;
/* Now check 'module[:*]' */
*colon = 0;
if (g_hash_table_lookup (debug_table, fallback))
return TRUE;
/* Now check ':subsystem' */
*colon = ':';
if (g_hash_table_lookup (debug_table, colon))
return TRUE;
}
}
return FALSE;
}
static GMutex debug_lock;
/**
* camel_debug_start:
* @mode: string name of the mode to start the debug for
*
* Start debug output for a given mode, used to make sure debug output
* is output atomically and not interspersed with unrelated stuff.
*
* Returns: %TRUE if mode is set, and in which case, you must
* call camel_debug_end() when finished any screen output.
**/
gboolean
camel_debug_start (const gchar *mode)
{
if (camel_debug (mode)) {
g_mutex_lock (&debug_lock);
printf ("Thread %p >\n", g_thread_self ());
return TRUE;
}
return FALSE;
}
/**
* camel_debug_end:
*
* Call this when you're done with your debug output. If and only if
* you called camel_debug_start, and if it returns TRUE.
**/
void
camel_debug_end (void)
{
printf ("< %p >\n", g_thread_self ());
g_mutex_unlock (&debug_lock);
}
#if 0
#include <sys/debugreg.h>
static unsigned
i386_length_and_rw_bits (gint len,
enum target_hw_bp_type type)
{
unsigned rw;
switch (type)
{
case hw_execute:
rw = DR_RW_EXECUTE;
break;
case hw_write:
rw = DR_RW_WRITE;
break;
case hw_read: /* x86 doesn't support data-read watchpoints */
case hw_access:
rw = DR_RW_READ;
break;
#if 0
case hw_io_access: /* not yet supported */
rw = DR_RW_IORW;
break;
#endif
default:
internal_error (__FILE__, __LINE__, "Invalid hw breakpoint type %d in i386_length_and_rw_bits.\n", (gint) type);
}
switch (len)
{
case 1:
return (DR_LEN_1 | rw);
case 2:
return (DR_LEN_2 | rw);
case 4:
return (DR_LEN_4 | rw);
case 8:
if (TARGET_HAS_DR_LEN_8)
return (DR_LEN_8 | rw);
default:
internal_error (__FILE__, __LINE__, "Invalid hw breakpoint length %d in i386_length_and_rw_bits.\n", len);
}
}
#define I386_DR_SET_RW_LEN(i,rwlen) \
do { \
dr_control_mirror &= ~(0x0f << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
dr_control_mirror |= ((rwlen) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (i))); \
} while (0)
#define I386_DR_LOCAL_ENABLE(i) \
dr_control_mirror |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (i)))
#define set_dr(regnum, val) \
__asm__("movl %0,%%db" #regnum \
: /* no output */ \
:"r" (val))
#define get_dr(regnum, val) \
__asm__("movl %%db" #regnum ", %0" \
:"=r" (val))
/* fine idea, but it doesn't work, crashes in get_dr :-/ */
void
camel_debug_hwatch (gint wp,
gpointer addr)
{
guint32 control, rw;
g_return_if_fail (wp <= DR_LASTADDR);
g_return_if_fail (sizeof (addr) == 4);
get_dr (7, control);
/* set watch mode + size */
rw = DR_RW_WRITE | DR_LEN_4;
control &= ~(((1 << DR_CONTROL_SIZE) - 1) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * wp));
control |= rw << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * wp);
/* set watch enable */
control |= ( 1<< (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * wp));
control |= DR_LOCAL_SLOWDOWN;
control &= ~DR_CONTROL_RESERVED;
switch (wp) {
case 0:
set_dr (0, addr);
break;
case 1:
set_dr (1, addr);
break;
case 2:
set_dr (2, addr);
break;
case 3:
set_dr (3, addr);
break;
}
set_dr (7, control);
}
#endif
G_LOCK_DEFINE_STATIC (ptr_tracker);
static GHashTable *ptr_tracker = NULL;
struct pt_data {
gpointer ptr;
gchar *info;
GString *backtrace;
};
static void
free_pt_data (gpointer ptr)
{
struct pt_data *ptd = ptr;
if (!ptd)
return;
g_free (ptd->info);
if (ptd->backtrace)
g_string_free (ptd->backtrace, TRUE);
g_free (ptd);
}
static void demangle_bt (GString *bt);
static void
dump_left_ptrs_cb (gpointer key,
gpointer value,
gpointer user_data)
{
guint *left = user_data;
struct pt_data *ptd = value;
gboolean have_info = ptd && ptd->info;
gboolean have_bt = ptd && ptd->backtrace && ptd->backtrace->str && *ptd->backtrace->str;
if (have_bt)
demangle_bt (ptd->backtrace);
*left = (*left) - 1;
g_print (" %p %s%s%s%s%s%s\n", key, have_info ? "(" : "", have_info ? ptd->info : "", have_info ? ")" : "", have_bt ? "\n" : "", have_bt ? ptd->backtrace->str : "", have_bt && *left > 0 ? "\n" : "");
}
#ifdef HAVE_BACKTRACE_SYMBOLS
static guint
by_backtrace_hash (gconstpointer ptr)
{
const struct pt_data *ptd = ptr;
if (!ptd || !ptd->backtrace)
return 0;
return g_str_hash (ptd->backtrace->str);
}
static gboolean
by_backtrace_equal (gconstpointer ptr1,
gconstpointer ptr2)
{
const struct pt_data *ptd1 = ptr1, *ptd2 = ptr2;
if ((!ptd1 || !ptd1->backtrace) && (!ptd2 || !ptd2->backtrace))
return TRUE;
return ptd1 && ptd1->backtrace && ptd2 && ptd2->backtrace && g_str_equal (ptd1->backtrace->str, ptd2->backtrace->str);
}
static void
dump_by_backtrace_cb (gpointer key,
gpointer value,
gpointer user_data)
{
guint *left = user_data;
struct pt_data *ptd = key;
guint count = GPOINTER_TO_UINT (value);
if (count == 1) {
dump_left_ptrs_cb (ptd->ptr, ptd, left);
} else {
gboolean have_info = ptd && ptd->info;
gboolean have_bt = ptd && ptd->backtrace && ptd->backtrace->str && *ptd->backtrace->str;
if (have_bt)
demangle_bt (ptd->backtrace);
*left = (*left) - 1;
g_print (" %d x %s%s%s%s%s%s\n", count, have_info ? "(" : "", have_info ? ptd->info : "", have_info ? ")" : "", have_bt ? "\n" : "", have_bt ? ptd->backtrace->str : "", have_bt && *left > 0 ? "\n" : "");
}
}
static void
dump_by_backtrace (GHashTable *ptrs)
{
GHashTable *by_bt = g_hash_table_new (by_backtrace_hash, by_backtrace_equal);
GHashTableIter iter;
gpointer key, value;
struct ptr_data *ptd;
guint count;
g_hash_table_iter_init (&iter, ptrs);
while (g_hash_table_iter_next (&iter, &key, &value)) {
guint cnt;
ptd = value;
if (!ptd)
continue;
cnt = GPOINTER_TO_UINT (g_hash_table_lookup (by_bt, ptd));
cnt++;
g_hash_table_insert (by_bt, ptd, GUINT_TO_POINTER (cnt));
}
count = g_hash_table_size (by_bt);
g_hash_table_foreach (by_bt, dump_by_backtrace_cb, &count);
g_hash_table_destroy (by_bt);
}
#endif /* HAVE_BACKTRACE_SYMBOLS */
static void
dump_tracked_ptrs (gboolean is_at_exit)
{
G_LOCK (ptr_tracker);
if (ptr_tracker) {
g_print ("\n----------------------------------------------------------\n");
if (g_hash_table_size (ptr_tracker) == 0) {
g_print (" All tracked pointers were properly removed\n");
} else {
guint count = g_hash_table_size (ptr_tracker);
g_print (" Left %d tracked pointers:\n", count);
#ifdef HAVE_BACKTRACE_SYMBOLS
dump_by_backtrace (ptr_tracker);
#else
g_hash_table_foreach (ptr_tracker, dump_left_ptrs_cb, &count);
#endif
}
g_print ("----------------------------------------------------------\n");
} else if (!is_at_exit) {
g_print ("\n----------------------------------------------------------\n");
g_print (" Did not track any pointers yet\n");
g_print ("----------------------------------------------------------\n");
}
G_UNLOCK (ptr_tracker);
}
#ifdef HAVE_BACKTRACE_SYMBOLS
#ifdef HAVE_ELFUTILS_LIBDWFL
static Dwfl *
dwfl_get (gboolean reload)
{
static gchar *debuginfo_path = NULL;
static Dwfl *dwfl = NULL;
static gboolean checked_for_dwfl = FALSE;
static GMutex dwfl_mutex;
static const Dwfl_Callbacks proc_callbacks = {
.find_debuginfo = dwfl_standard_find_debuginfo,
.debuginfo_path = &debuginfo_path,
.find_elf = dwfl_linux_proc_find_elf
};
g_mutex_lock (&dwfl_mutex);
if (checked_for_dwfl) {
if (!reload) {
g_mutex_unlock (&dwfl_mutex);
return dwfl;
}
dwfl_end (dwfl);
dwfl = NULL;
}
checked_for_dwfl = TRUE;
dwfl = dwfl_begin (&proc_callbacks);
if (!dwfl) {
g_mutex_unlock (&dwfl_mutex);
return NULL;
}
errno = 0;
if (dwfl_linux_proc_report (dwfl, getpid ()) != 0 || dwfl_report_end (dwfl, NULL, NULL) != 0) {
dwfl_end (dwfl);
dwfl = NULL;
}
g_mutex_unlock (&dwfl_mutex);
return dwfl;
}
struct getmodules_callback_arg
{
gpointer addr;
const gchar *func_name;
const gchar *file_path;
gint lineno;
};
static gint
getmodules_callback (Dwfl_Module *module,
gpointer *module_userdata_pointer,
const gchar *module_name,
Dwarf_Addr module_low_addr,
gpointer arg_voidp)
{
struct getmodules_callback_arg *arg = arg_voidp;
Dwfl_Line *line;
arg->func_name = dwfl_module_addrname (module, (GElf_Addr) arg->addr);
line = dwfl_module_getsrc (module, (GElf_Addr) arg->addr);
if (line) {
arg->file_path = dwfl_lineinfo (line, NULL, &arg->lineno, NULL, NULL, NULL);
} else {
arg->file_path = NULL;
}
return arg->func_name ? DWARF_CB_ABORT : DWARF_CB_OK;
}
#endif /* HAVE_ELFUTILS_LIBDWFL */
static const gchar *
addr_lookup_dwfl (gpointer addr,
const gchar **file_path,
gint *lineno,
const gchar *fallback)
{
#ifdef HAVE_ELFUTILS_LIBDWFL
Dwfl *dwfl = dwfl_get (FALSE);
struct getmodules_callback_arg arg;
if (!dwfl)
return NULL;
arg.addr = addr;
arg.func_name = NULL;
arg.file_path = NULL;
arg.lineno = -1;
dwfl_getmodules (dwfl, getmodules_callback, &arg, 0);
if (!arg.func_name && fallback && strstr (fallback, "/lib") != fallback && strstr (fallback, "/usr/lib") != fallback) {
dwfl = dwfl_get (TRUE);
if (dwfl)
dwfl_getmodules (dwfl, getmodules_callback, &arg, 0);
}
*file_path = arg.file_path;
*lineno = arg.lineno;
return arg.func_name;
#else /* HAVE_ELFUTILS_LIBDWFL */
return NULL;
#endif /* HAVE_ELFUTILS_LIBDWFL */
}
typedef struct _SymbolData {
gchar *func_name;
gchar *file_path;
gint lineno;
} SymbolData;
static void
symbol_data_free (gpointer ptr)
{
SymbolData *sd = ptr;
if (sd) {
g_free (sd->func_name);
g_free (sd->file_path);
g_free (sd);
}
}
static const gchar *
addr_lookup (gpointer addr,
const gchar **out_file_path,
gint *out_lineno,
const gchar *fallback)
{
static GHashTable *symbols_cache = NULL;
static GMutex mutex;
SymbolData *sd;
const gchar *func_name, *file_path = NULL;
gint lineno = -1;
g_mutex_lock (&mutex);
if (symbols_cache) {
sd = g_hash_table_lookup (symbols_cache, addr);
if (sd) {
g_mutex_unlock (&mutex);
*out_file_path = sd->file_path;
*out_lineno = sd->lineno;
return sd->func_name;
}
}
func_name = addr_lookup_dwfl (addr, &file_path, &lineno, fallback);
if (func_name) {
if (!symbols_cache)
symbols_cache = g_hash_table_new_full (NULL, NULL, NULL, symbol_data_free);
sd = g_new (SymbolData, 1);
sd->func_name = g_strdup (func_name);
sd->file_path = g_strdup (file_path);
sd->lineno = lineno;
g_hash_table_insert (symbols_cache, addr, sd);
}
g_mutex_unlock (&mutex);
*out_file_path = file_path;
*out_lineno = lineno;
return func_name;
}
#endif /* HAVE_BACKTRACE_SYMBOLS */
static void
demangle_bt (GString *bt)
{
#ifdef HAVE_BACKTRACE_SYMBOLS
gchar **btparts;
gint ii;
gboolean any_changed = FALSE;
if (!bt || !bt->len)
return;
btparts = g_strsplit (bt->str, "\n", -1);
if (!btparts)
return;
g_string_truncate (bt, 0);
for (ii = 0; btparts[ii]; ii++) {
gint lineno = -1;
const gchar *file_path = NULL;
const gchar *str, *bt_sym;
gpointer btptr = NULL;
if (!g_str_has_prefix (btparts[ii], "0x") ||
!strchr (btparts[ii], '\t') ||
sscanf (btparts[ii], "%p\t", &btptr) != 1) {
btptr = NULL;
}
if (btptr) {
bt_sym = strchr (btparts[ii], '\t');
if (bt_sym)
bt_sym++;
} else {
bt_sym = NULL;
}
if (!bt_sym || !*bt_sym) {
if (bt->len)
g_string_append_c (bt, '\n');
g_string_append (bt, btparts[ii]);
continue;
}
str = addr_lookup (btptr, &file_path, &lineno, bt_sym);
if (!str) {
str = btparts[ii];
file_path = NULL;
lineno = -1;
}
if (!str)
continue;
any_changed = TRUE;
if (bt->len)
g_string_append (bt, "\n\t by ");
g_string_append (bt, str);
if (str != btparts[ii])
g_string_append (bt, "()");
if (file_path && lineno > 0) {
const gchar *lastsep = strrchr (file_path, G_DIR_SEPARATOR);
g_string_append_printf (bt, " at %s:%d", lastsep ? lastsep + 1 : file_path, lineno);
}
}
g_strfreev (btparts);
if (bt->len != 0 && any_changed)
g_string_insert (bt, 0, "\t at ");
#endif /* HAVE_BACKTRACE_SYMBOLS */
}
static GString *
get_current_backtrace (void)
{
#ifdef HAVE_BACKTRACE_SYMBOLS
#define MAX_BT_DEPTH 50
gint nptrs, ii;
gpointer bt[MAX_BT_DEPTH + 1];
gchar **bt_syms;
GString *bt_str;
nptrs = backtrace (bt, MAX_BT_DEPTH + 1);
if (nptrs <= 2)
return NULL;
bt_syms = backtrace_symbols (bt, nptrs);
if (!bt_syms)
return NULL;
bt_str = g_string_new ("");
for (ii = 2; ii < nptrs; ii++) {
if (bt_str->len)
g_string_append_c (bt_str, '\n');
g_string_append_printf (bt_str, "%p\t%s", bt[ii], bt_syms[ii]);
}
g_free (bt_syms);
if (bt_str->len == 0) {
g_string_free (bt_str, TRUE);
bt_str = NULL;
}
return bt_str;
#undef MAX_BT_DEPTH
#else /* HAVE_BACKTRACE_SYMBOLS */
return NULL;
#endif /* HAVE_BACKTRACE_SYMBOLS */
}
static void
dump_left_at_exit_cb (void)
{
dump_tracked_ptrs (TRUE);
G_LOCK (ptr_tracker);
g_clear_pointer (&ptr_tracker, g_hash_table_destroy);
G_UNLOCK (ptr_tracker);
}
/**
* camel_pointer_tracker_track_with_info:
* @ptr: pointer to add to the pointer tracker
* @info: info to print in tracker summary
*
* Adds pointer to the pointer tracker, with associated information,
* which is printed in summary of pointer tracker printed by
* camel_pointer_tracker_dump(). For convenience can be used
* camel_pointer_tracker_track(), which adds place of the caller
* as @info. Added pointer should be removed with pair function
* camel_pointer_tracker_untrack().
*
* Since: 3.6
**/
void
camel_pointer_tracker_track_with_info (gpointer ptr,
const gchar *info)
{
struct pt_data *ptd;
g_return_if_fail (ptr != NULL);
G_LOCK (ptr_tracker);
if (!ptr_tracker) {
ptr_tracker = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, free_pt_data);
atexit (dump_left_at_exit_cb);
}
ptd = g_new0 (struct pt_data, 1);
ptd->ptr = ptr;
ptd->info = g_strdup (info);
ptd->backtrace = get_current_backtrace ();
g_hash_table_insert (ptr_tracker, ptr, ptd);
G_UNLOCK (ptr_tracker);
}
/**
* camel_pointer_tracker_untrack:
* @ptr: pointer to remove from the tracker
*
* Removes pointer from the pointer tracker. It's an error to try
* to remove pointer which was not added to the tracker by
* camel_pointer_tracker_track() or camel_pointer_tracker_track_with_info(),
* or a pointer which was already removed.
*
* Since: 3.6
**/
void
camel_pointer_tracker_untrack (gpointer ptr)
{
g_return_if_fail (ptr != NULL);
G_LOCK (ptr_tracker);
if (!ptr_tracker)
g_printerr ("Pointer tracker not initialized, thus cannot remove %p\n", ptr);
else if (!g_hash_table_lookup (ptr_tracker, ptr))
g_printerr ("Pointer %p is not tracked\n", ptr);
else
g_hash_table_remove (ptr_tracker, ptr);
G_UNLOCK (ptr_tracker);
}
/**
* camel_pointer_tracker_dump:
*
* Prints information about currently stored pointers
* in the pointer tracker. This is called automatically
* on application exit if camel_pointer_tracker_track() or
* camel_pointer_tracker_track_with_info() was called.
*
* Note: If the library is configured with --enable-backtraces,
* then also backtraces where the pointer was added is printed
* in the summary.
*
* Since: 3.6
**/
void
camel_pointer_tracker_dump (void)
{
dump_tracked_ptrs (FALSE);
}
/**
* camel_debug_get_backtrace:
*
* Gets current backtrace leading to this function call and demangles it.
*
* Returns: (nullable): Current backtrace, or %NULL, if cannot determine it.
*
* Note: Getting backtraces only works if the library was
* configured with --enable-backtraces.
*
* See also camel_debug_get_raw_backtrace()
*
* Since: 3.12
**/
GString *
camel_debug_get_backtrace (void)
{
GString *bt;
bt = get_current_backtrace ();
if (!bt)
return NULL;
demangle_bt (bt);
return bt;
}
/**
* camel_debug_get_raw_backtrace:
*
* Gets current raw backtrace leading to this function call.
* This is quicker than camel_debug_get_backtrace(), because it
* doesn't demangle the backtrace. To demangle it (replace addresses
* with actual function calls and eventually line numbers, if
* available) call camel_debug_demangle_backtrace().
*
* Returns: (nullable): Current raw backtrace, or %NULL, if cannot determine it.
*
* Note: Getting backtraces only works if the library was
* configured with --enable-backtraces.
*
* See also camel_debug_get_backtrace()
*
* Since: 3.30
**/
GString *
camel_debug_get_raw_backtrace (void)
{
return get_current_backtrace ();
}
/**
* camel_debug_demangle_backtrace:
* @bt: (inout) (nullable): a #GString with a raw backtrace, or %NULL
*
* Demangles @bt, possibly got from camel_debug_get_raw_backtrace(), by
* replacing addresses with actual function calls and eventually line numbers, if
* available. It modifies lines of @bt, but skips those it cannot parse.
*
* Note: Getting backtraces only works if the library was
* configured with --enable-backtraces.
*
* See also camel_debug_get_raw_backtrace()
*
* Since: 3.30
**/
void
camel_debug_demangle_backtrace (GString *bt)
{
if (bt)
demangle_bt (bt);
}
G_LOCK_DEFINE_STATIC (ref_unref_backtraces);
static GQueue *ref_unref_backtraces = NULL;
static guint total_ref_unref_backtraces = 0;
typedef struct _BacktraceLine
{
gchar *function;
gchar *file;
gint lineno;
} BacktraceLine;
static void
backtrace_line_free (gpointer ptr)
{
BacktraceLine *btline = ptr;
if (btline) {
g_free (btline->function);
g_free (btline->file);
g_free (btline);
}
}
typedef enum {
BACKTRACE_TYPE_OTHER,
BACKTRACE_TYPE_REF,
BACKTRACE_TYPE_UNREF
} BacktraceType;
typedef struct _Backtrace
{
BacktraceType type;
guint object_ref_count;
GSList *lines; /* BacktraceLine */
} Backtrace;
static void
backtrace_free (gpointer ptr)
{
Backtrace *bt = ptr;
if (bt) {
g_slist_free_full (bt->lines, backtrace_line_free);
g_free (bt);
}
}
static BacktraceLine *
parse_backtrace_line (const gchar *line)
{
gchar **parts;
gint ii, lineno = 0;
gchar *function = NULL, *filename = NULL;
if (!line)
return NULL;
while (*line == ' ' || *line == '\t')
line++;
parts = g_strsplit (line, " ", -1);
if (!parts || !*parts) {
g_strfreev (parts);
return NULL;
}
for (ii = 0; parts[ii]; ii++) {
const gchar *part = parts[ii];
if (!*part)
continue;
if (ii == 1) {
function = g_strdup (part);
} else if (ii == 3 && function) {
gchar **file;
file = g_strsplit (part, ":", -1);
if (file && file[0] && file[1]) {
filename = g_strdup (file[0]);
lineno = g_ascii_strtoll (file[1], NULL, 10);
} else {
filename = g_strdup (part);
}
g_strfreev (file);
}
}
g_strfreev (parts);
if (function) {
BacktraceLine *btline;
btline = g_new0 (BacktraceLine, 1);
btline->function = function;
btline->file = filename;
btline->lineno = lineno;
return btline;
}
g_free (function);
g_free (filename);
return NULL;
}
static Backtrace *
parse_backtrace (const GString *backtrace,
guint object_ref_count)
{
Backtrace *bt;
gchar **btlines;
gint ii;
if (!backtrace)
return NULL;
btlines = g_strsplit (backtrace->str, "\n", -1);
if (!btlines || !*btlines) {
g_strfreev (btlines);
return NULL;
}
bt = g_new0 (Backtrace, 1);
bt->object_ref_count = object_ref_count;
for (ii = 0; btlines[ii]; ii++) {
if (ii >= 1) {
BacktraceLine *btline = parse_backtrace_line (btlines[ii]);
if (!btline)
continue;
bt->lines = g_slist_prepend (bt->lines, btline);
if (ii == 1) {
if (g_strcmp0 (btline->function, "g_object_ref()") == 0) {
bt->type = BACKTRACE_TYPE_REF;
} else if (g_strcmp0 (btline->function, "g_object_unref()") == 0) {
bt->type = BACKTRACE_TYPE_UNREF;
} else {
bt->type = BACKTRACE_TYPE_OTHER;
}
}
}
}
g_strfreev (btlines);
bt->lines = g_slist_reverse (bt->lines);
return bt;
}
static void
print_backtrace (Backtrace *bt,
gint index)
{
GSList *link;
if (!bt)
return;
g_print (" Backtrace[%d] %s %d~>%d:\n", index,
bt->type == BACKTRACE_TYPE_REF ? "ref" :
bt->type == BACKTRACE_TYPE_UNREF ? "unref" : "other",
bt->object_ref_count, bt->object_ref_count + (bt->type == BACKTRACE_TYPE_REF ? 1 : bt->type == BACKTRACE_TYPE_UNREF ? -1 : 0));
for (link = bt->lines; link; link = g_slist_next (link)) {
BacktraceLine *btline = link->data;
g_print (" %s %s", link == bt->lines ? "at" : "by", btline->function);
if (btline->file) {
if (btline->lineno > 0) {
g_print (" at %s:%d\n", btline->file, btline->lineno);
} else {
g_print (" at %s\n", btline->file);
}
} else {
g_print ("\n");
}
}
g_print ("\n");
}
static gboolean
backtrace_matches (const Backtrace *match_bt,
const Backtrace *find_bt,
gint lines_tolerance)
{
GSList *mlink, *flink;
gint lines_matched = 0;
gint lt, ii;
if (!match_bt || !find_bt || !find_bt->lines || !match_bt->lines)
return FALSE;
flink = find_bt->lines;
mlink = NULL;
lt = lines_tolerance;
do {
gboolean found = FALSE;
BacktraceLine *fline = flink->data;
if (!fline)
return FALSE;
for (mlink = match_bt->lines, ii = 0; mlink && ii <= lines_tolerance; mlink = g_slist_next (mlink), ii++) {
BacktraceLine *mline = mlink->data;
if (!mline)
return FALSE;
found = g_strcmp0 (fline->function, mline->function) == 0;
if (found)
break;
}
if (found)
break;
lt--;
if (lt >= 0)
flink = g_slist_next (flink);
else
flink = NULL;
} while (flink);
if (!flink)
return FALSE;
for (mlink = g_slist_next (mlink), flink = g_slist_next (flink);
mlink && flink;
mlink = g_slist_next (mlink), flink = g_slist_next (flink)) {
BacktraceLine *mline, *fline;
mline = mlink->data;
fline = flink->data;
if (!mline || !fline)
break;
if (g_strcmp0 (mline->function, fline->function) != 0 ||
g_strcmp0 (mline->file, fline->file) != 0 ||
mline->lineno != fline->lineno) {
break;
}
lines_matched++;
}
return (!mlink && !flink) || (lines_matched > 40 && (!mlink || !flink));
}
static gboolean
backtrace_matches_ref (const Backtrace *match_bt,
const Backtrace *find_bt,
gint lines_tolerance)
{
if (!match_bt || match_bt->type != BACKTRACE_TYPE_REF)
return FALSE;
return backtrace_matches (match_bt, find_bt, lines_tolerance);
}
static gboolean
remove_matching_ref_backtrace (GQueue *backtraces,
const Backtrace *unref_backtrace)
{
GList *link;
gint up_bts = 2, up_lines = 1;
g_return_val_if_fail (backtraces != NULL, FALSE);
g_return_val_if_fail (unref_backtrace != NULL, FALSE);
g_return_val_if_fail (unref_backtrace->type != BACKTRACE_TYPE_REF, FALSE);
if (unref_backtrace->lines && unref_backtrace->lines->next && unref_backtrace->lines->next->data) {
BacktraceLine *btline = unref_backtrace->lines->next->data;
if (g_strcmp0 ("g_value_object_free_value()", btline->function) == 0)
up_lines = 5;
else if (g_strcmp0 ("g_object_notify()", btline->function) == 0)
up_bts = 5;
}
for (link = g_queue_peek_tail_link (backtraces); link && up_bts > 0; link = g_list_previous (link), up_bts--) {
Backtrace *bt = link->data;
gint inc_up_lines = 0;
if (!bt || bt->type != BACKTRACE_TYPE_REF)
continue;
if (bt->lines && bt->lines->next && bt->lines->next->data) {
BacktraceLine *btline = bt->lines->next->data;
if (g_strcmp0 ("g_weak_ref_get()", btline->function) == 0)
inc_up_lines = 2;
}
if (backtrace_matches_ref (bt, unref_backtrace, up_lines + inc_up_lines)) {
g_queue_delete_link (backtraces, link);
backtrace_free (bt);
return TRUE;
}
}
return FALSE;
}
static void
dump_ref_unref_backtraces (gboolean is_at_exit)
{
G_LOCK (ref_unref_backtraces);
if (ref_unref_backtraces) {
g_print ("\n----------------------------------------------------------\n");
if (g_queue_get_length (ref_unref_backtraces) == 0) {
g_print (" All ref/unref backtraces were properly matched\n");
} else {
guint count = g_queue_get_length (ref_unref_backtraces), refs = 0, unrefs = 0, others = 0;
GList *link;
for (link = g_queue_peek_head_link (ref_unref_backtraces); link; link = g_list_next (link)) {
Backtrace *bt = link->data;
if (!bt)
continue;
if (bt->type == BACKTRACE_TYPE_REF)
refs++;
else if (bt->type == BACKTRACE_TYPE_UNREF)
unrefs++;
else
others++;
}
g_print (" Left %u (ref(%u)/unref(%u)/other(%u)) backtraces of %u pushed total:\n", count, refs, unrefs, others, total_ref_unref_backtraces);
for (count = 0, link = g_queue_peek_head_link (ref_unref_backtraces); link; link = g_list_next (link), count++) {
Backtrace *bt = link->data;
if (!bt)
continue;
print_backtrace (bt, count);
}
}
g_print ("----------------------------------------------------------\n");
} else if (!is_at_exit) {
g_print ("\n----------------------------------------------------------\n");
g_print (" Did not receive any ref/unref backtraces yet\n");
g_print ("----------------------------------------------------------\n");
}
G_UNLOCK (ref_unref_backtraces);
}
static void
dump_left_ref_unref_backtraces_at_exit_cb (void)
{
dump_ref_unref_backtraces (TRUE);
G_LOCK (ref_unref_backtraces);
if (ref_unref_backtraces) {
g_queue_free_full (ref_unref_backtraces, backtrace_free);
ref_unref_backtraces = NULL;
}
G_UNLOCK (ref_unref_backtraces);
}
/**
* camel_debug_ref_unref_push_backtrace:
* @backtrace: a backtrace to push, taken from camel_debug_get_backtrace()
* @object_ref_count: the current object reference count when the push is done
*
* Adds this backtrace into the set of backtraces related to some object
* reference counting issues debugging. This is usually called inside g_object_ref()
* and g_object_unref(). If the backtrace corresponds to a g_object_unref()
* call, and a corresponding g_object_ref() backtrace is found in the current list,
* then the previous backtrace is removed and this one is skipped.
*
* Any left backtraces in the list are printed at the application end.
*
* A convenient function camel_debug_ref_unref_push_backtrace_for_object()
* is provided too.
*
* Since: 3.20
**/
void
camel_debug_ref_unref_push_backtrace (const GString *backtrace,
guint object_ref_count)
{
Backtrace *bt;
g_return_if_fail (backtrace != NULL);
G_LOCK (ref_unref_backtraces);
total_ref_unref_backtraces++;
bt = parse_backtrace (backtrace, object_ref_count);
if (!bt) {
G_UNLOCK (ref_unref_backtraces);
g_warn_if_fail (bt != NULL);
return;
}
if (!ref_unref_backtraces) {
ref_unref_backtraces = g_queue_new ();
atexit (dump_left_ref_unref_backtraces_at_exit_cb);
}
if (bt->type != BACKTRACE_TYPE_UNREF || !remove_matching_ref_backtrace (ref_unref_backtraces, bt)) {
g_queue_push_tail (ref_unref_backtraces, bt);
} else {
backtrace_free (bt);
}
G_UNLOCK (ref_unref_backtraces);
}
/**
* camel_debug_ref_unref_push_backtrace_for_object:
* @_object: a #GObject, for which add the backtrace
*
* Gets current backtrace of this call and adds it to the list
* of backtraces with camel_debug_ref_unref_push_backtrace().
*
* Usual usage would be, once GNOME bug 758358 is applied to the GLib sources,
* or a patched GLib is used, to call this function in an object init() function,
* like this:
*
* static void
* my_object_init (MyObject *obj)
* {
* camel_debug_ref_unref_push_backtrace_for_object (obj);
* g_track_object_ref_unref (obj, (GFunc) camel_debug_ref_unref_push_backtrace_for_object, NULL);
* }
*
* Note that the g_track_object_ref_unref() can track only one pointer, thus make
* sure you track the right one (add some logic if multiple objects are created at once).
*
* Since: 3.20
**/
void
camel_debug_ref_unref_push_backtrace_for_object (gpointer _object)
{
GString *backtrace;
GObject *object;
g_return_if_fail (G_IS_OBJECT (_object));
object = G_OBJECT (_object);
backtrace = camel_debug_get_backtrace ();
if (backtrace) {
camel_debug_ref_unref_push_backtrace (backtrace, object->ref_count);
g_string_free (backtrace, TRUE);
}
}
/**
* camel_debug_ref_unref_dump_backtraces:
*
* Prints current backtraces stored with camel_debug_ref_unref_push_backtrace()
* or with camel_debug_ref_unref_push_backtrace_for_object().
*
* It's usually not needed to use this function, as the left backtraces, if any,
* are printed at the end of the application.
*
* Since: 3.20
**/
void
camel_debug_ref_unref_dump_backtraces (void)
{
dump_ref_unref_backtraces (FALSE);
}
|