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
|
/* This file is part of the KDE libraries
Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
2002 Holger Freyther (freyther@kde.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kdebug.h"
#ifdef NDEBUG
#undef kdDebug
#endif
#include "kdebugdcopiface.h"
#include "kapplication.h"
#include "kglobal.h"
#include "kinstance.h"
#include "kstandarddirs.h"
#include <tqmessagebox.h>
#include <klocale.h>
#include <tqfile.h>
#include <tqintdict.h>
#include <tqstring.h>
#include <tqdatetime.h>
#include <tqpoint.h>
#include <tqrect.h>
#include <tqregion.h>
#include <tqstringlist.h>
#include <tqpen.h>
#include <tqbrush.h>
#include <tqsize.h>
#include <kurl.h>
#include <stdlib.h> // abort
#include <unistd.h> // getpid
#include <stdarg.h> // vararg stuff
#include <ctype.h> // isprint
#include <syslog.h>
#include <errno.h>
#include <cstring>
#include <kconfig.h>
#include "kstaticdeleter.h"
#include <config.h>
#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#ifdef HAVE_ABI_CXA_DEMANGLE
#include <cxxabi.h>
#endif
#include <link.h>
#ifdef WITH_LIBBFD
/* newer versions of libbfd require some autotools-specific macros to be defined */
/* see binutils Bug 14243 and 14072 */
#define PACKAGE kdelibs
#define PACKAGE_VERSION KDE_VERSION
#include <bfd.h>
#ifdef HAVE_DEMANGLE_H
#include <demangle.h>
#endif // HAVE_DEMANGLE_H
#endif // WITH_LIBBFD
#endif // HAVE_BACKTRACE
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif // HAVE_ALLOCA_H
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif // HAVE_STDINT_H
class KDebugEntry;
class KDebugEntry
{
public:
KDebugEntry (int n, const TQCString& d) {number=n; descr=d;}
unsigned int number;
TQCString descr;
};
static TQIntDict<KDebugEntry> *KDebugCache;
static KStaticDeleter< TQIntDict<KDebugEntry> > kdd;
static TQCString getDescrFromNum(unsigned int _num)
{
if (!KDebugCache) {
kdd.setObject(KDebugCache, new TQIntDict<KDebugEntry>( 601 ));
// Do not call this deleter from ~KApplication
KGlobal::unregisterStaticDeleter(&kdd);
KDebugCache->setAutoDelete(true);
}
KDebugEntry *ent = KDebugCache->find( _num );
if ( ent )
return ent->descr;
if ( !KDebugCache->isEmpty() ) // areas already loaded
return TQCString();
TQString filename(locate("config","kdebug.areas"));
if (filename.isEmpty())
return TQCString();
TQFile file(filename);
if (!file.open(IO_ReadOnly)) {
qWarning("Couldn't open %s", filename.local8Bit().data());
file.close();
return TQCString();
}
uint lineNumber=0;
TQCString line(1024);
int len;
while (( len = file.readLine(line.data(),line.size()-1) ) > 0) {
int i=0;
++lineNumber;
while (line[i] && line[i] <= ' ')
i++;
unsigned char ch=line[i];
if ( !ch || ch =='#' || ch =='\n')
continue; // We have an eof, a comment or an empty line
if (ch < '0' && ch > '9') {
qWarning("Syntax error: no number (line %u)",lineNumber);
continue;
}
const int numStart=i;
do {
ch=line[++i];
} while ( ch >= '0' && ch <= '9');
const TQ_ULONG number =line.mid(numStart,i).toULong();
while (line[i] && line[i] <= ' ')
i++;
KDebugCache->insert(number, new KDebugEntry(number, line.mid(i, len-i-1)));
}
file.close();
ent = KDebugCache->find( _num );
if ( ent )
return ent->descr;
return TQCString();
}
enum DebugLevels {
KDEBUG_INFO= 0,
KDEBUG_WARN= 1,
KDEBUG_ERROR= 2,
KDEBUG_FATAL= 3
};
struct kDebugPrivate {
kDebugPrivate() :
oldarea(0), config(0) { }
~kDebugPrivate() { delete config; }
TQCString aAreaName;
unsigned int oldarea;
KConfig *config;
};
static kDebugPrivate *kDebug_data = 0;
static KStaticDeleter<kDebugPrivate> pcd;
static KStaticDeleter<KDebugDCOPIface> dcopsd;
static KDebugDCOPIface* kDebugDCOPIface = 0;
static void kDebugBackend( unsigned short nLevel, unsigned int nArea, const char *data)
{
if ( !kDebug_data )
{
pcd.setObject(kDebug_data, new kDebugPrivate());
// Do not call this deleter from ~KApplication
KGlobal::unregisterStaticDeleter(&pcd);
// create the dcop interface if it has not been created yet
if (!kDebugDCOPIface)
{
kDebugDCOPIface = dcopsd.setObject(kDebugDCOPIface, new KDebugDCOPIface);
}
}
if (!kDebug_data->config && KGlobal::_instance )
{
kDebug_data->config = new KConfig("kdebugrc", false, false);
kDebug_data->config->setGroup("0");
//AB: this is necessary here, otherwise all output with area 0 won't be
//prefixed with anything, unless something with area != 0 is called before
if ( KGlobal::_instance )
kDebug_data->aAreaName = KGlobal::instance()->instanceName();
}
if ( kDebug_data->oldarea != nArea ) {
kDebug_data->oldarea = nArea;
if( KGlobal::_instance ) {
if ( nArea > 0 ) {
kDebug_data->aAreaName = getDescrFromNum(nArea);
}
if ( nArea == 0 || kDebug_data->aAreaName.isEmpty() ) {
kDebug_data->aAreaName = KGlobal::instance()->instanceName();
}
}
}
int nPriority = 0;
TQString aCaption;
/* Determine output */
TQString key;
switch( nLevel )
{
case KDEBUG_INFO:
key = "InfoOutput";
aCaption = "Info";
nPriority = LOG_INFO;
break;
case KDEBUG_WARN:
key = "WarnOutput";
aCaption = "Warning";
nPriority = LOG_WARNING;
break;
case KDEBUG_FATAL:
key = "FatalOutput";
aCaption = "Fatal Error";
nPriority = LOG_CRIT;
break;
case KDEBUG_ERROR:
default:
/* Programmer error, use "Error" as default */
key = "ErrorOutput";
aCaption = "Error";
nPriority = LOG_ERR;
break;
}
short nOutput = -1;
if ( kDebug_data->config ) {
kDebug_data->config->setGroup( TQString::number(static_cast<int>(nArea)) );
nOutput = kDebug_data->config->readNumEntry(key, -1);
if( nOutput == -1 ) {
kDebug_data->config->setGroup( TQString::fromAscii("Default") );
nOutput = kDebug_data->config->readNumEntry(key, -1);
}
}
// if no output mode is specified default to no stderr output
// NOTE: don't set this to 4 (no output) because in that case you won't be
// able to get any output from applications which don't create
// KApplication objects.
if ( nOutput == -1 ) {
nOutput = 2;
}
// If the application doesn't have a TQApplication object it can't use
// a messagebox, as well as in case of GUI is disabled.
if ( nOutput == 1 && ( !kapp || !kapp->guiEnabled()) ) {
nOutput = 2;
} else if ( nOutput == 4 && nLevel != KDEBUG_FATAL ) {
return;
}
const int BUFSIZE = 4096;
char buf[BUFSIZE];
if ( !kDebug_data->aAreaName.isEmpty() ) {
strlcpy( buf, kDebug_data->aAreaName.data(), BUFSIZE );
strlcat( buf, ": ", BUFSIZE );
strlcat( buf, data, BUFSIZE );
}
else
strlcpy( buf, data, BUFSIZE );
// Output
switch( nOutput )
{
case 0: // File
{
const char* aKey;
switch( nLevel )
{
case KDEBUG_INFO:
aKey = "InfoFilename";
break;
case KDEBUG_WARN:
aKey = "WarnFilename";
break;
case KDEBUG_FATAL:
aKey = "FatalFilename";
break;
case KDEBUG_ERROR:
default:
aKey = "ErrorFilename";
break;
}
TQFile aOutputFile( kDebug_data->config->readPathEntry(aKey, "kdebug.dbg") );
aOutputFile.open( (TQIODevice_OpenModeFlag)((int)IO_WriteOnly | (int)IO_Append | (int)IO_Raw) );
aOutputFile.writeBlock( buf, strlen( buf ) );
aOutputFile.close();
break;
}
case 1: // Message Box
{
// Since we are in kdecore here, we cannot use KMsgBox and use
// TQMessageBox instead
if ( !kDebug_data->aAreaName.isEmpty() )
aCaption += TQString("(%1)").arg( QString(kDebug_data->aAreaName) );
TQMessageBox::warning( 0L, aCaption, data, i18n("&OK") );
break;
}
case 2: // Shell
{
write( 2, buf, strlen( buf ) ); //fputs( buf, stderr );
break;
}
case 3: // syslog
{
syslog( nPriority, "%s", buf);
break;
}
}
// check if we should abort
if( ( nLevel == KDEBUG_FATAL )
&& ( !kDebug_data->config || kDebug_data->config->readNumEntry( "AbortFatal", 1 ) ) )
abort();
}
kdbgstream &perror( kdbgstream &s) { return s << TQString(TQString::fromLocal8Bit(strerror(errno))); }
kdbgstream kdDebug(int area) { return kdbgstream(area, KDEBUG_INFO); }
kdbgstream kdDebug(bool cond, int area) { if (cond) return kdbgstream(area, KDEBUG_INFO); else return kdbgstream(0, 0, false); }
kdbgstream kdError(int area) { return kdbgstream("ERROR: ", area, KDEBUG_ERROR); }
kdbgstream kdError(bool cond, int area) { if (cond) return kdbgstream("ERROR: ", area, KDEBUG_ERROR); else return kdbgstream(0,0,false); }
kdbgstream kdWarning(int area) { return kdbgstream("WARNING: ", area, KDEBUG_WARN); }
kdbgstream kdWarning(bool cond, int area) { if (cond) return kdbgstream("WARNING: ", area, KDEBUG_WARN); else return kdbgstream(0,0,false); }
kdbgstream kdFatal(int area) { return kdbgstream("FATAL: ", area, KDEBUG_FATAL); }
kdbgstream kdFatal(bool cond, int area) { if (cond) return kdbgstream("FATAL: ", area, KDEBUG_FATAL); else return kdbgstream(0,0,false); }
kdbgstream::kdbgstream(kdbgstream &str)
: output(str.output), area(str.area), level(str.level), print(str.print)
{
str.output.truncate(0);
}
void kdbgstream::flush() {
if (output.isEmpty() || !print)
return;
kDebugBackend( level, area, output.local8Bit().data() );
output = TQString::null;
}
kdbgstream &kdbgstream::form(const char *format, ...)
{
char buf[4096];
va_list arguments;
va_start( arguments, format );
vsnprintf( buf, sizeof(buf), format, arguments );
va_end(arguments);
*this << buf;
return *this;
}
kdbgstream::~kdbgstream() {
if (!output.isEmpty()) {
fprintf(stderr, "ASSERT: debug output not ended with \\n\n");
TQString backtrace = kdBacktrace();
if (backtrace.ascii() != NULL) {
fprintf(stderr, "%s", backtrace.latin1());
}
*this << "\n";
}
}
kdbgstream& kdbgstream::operator << (char ch)
{
if (!print) return *this;
if (!isprint(ch))
output += "\\x" + TQString::number( static_cast<uint>( ch ), 16 ).rightJustify(2, '0');
else {
output += ch;
if (ch == '\n') flush();
}
return *this;
}
kdbgstream& kdbgstream::operator << (TQChar ch)
{
if (!print) return *this;
if (!ch.isPrint())
output += "\\x" + TQString::number( ch.unicode(), 16 ).rightJustify(2, '0');
else {
output += ch;
if (ch == QChar('\n')) flush();
}
return *this;
}
kdbgstream& kdbgstream::operator << (TQWidget* widget)
{
return *this << const_cast< const TQWidget* >( widget );
}
kdbgstream& kdbgstream::operator << (const TQWidget* widget)
{
TQString string, temp;
// -----
if(widget==0)
{
string=(TQString)"[Null pointer]";
} else {
temp.setNum((ulong)widget, 16);
string=(TQString)"["+widget->className()+" pointer "
+ "(0x" + temp + ")";
if(widget->name(0)==0)
{
string += " to unnamed widget, ";
} else {
string += (TQString)" to widget " + widget->name() + ", ";
}
string += "geometry="
+ TQString().setNum(widget->width())
+ "x"+TQString().setNum(widget->height())
+ "+"+TQString().setNum(widget->x())
+ "+"+TQString().setNum(widget->y())
+ "]";
}
if (!print)
{
return *this;
}
output += string;
if (output.at(output.length() -1 ) == QChar('\n'))
{
flush();
}
return *this;
}
/*
* either use 'output' directly and do the flush if needed
* or use the TQString operator which calls the char* operator
*
*/
kdbgstream& kdbgstream::operator<<( const TQDateTime& time) {
*this << time.toString();
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQDate& date) {
*this << TQString(date.toString());
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQTime& time ) {
*this << TQString(time.toString());
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQPoint& p ) {
*this << "(" << p.x() << ", " << p.y() << ")";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQSize& s ) {
*this << "[" << s.width() << "x" << s.height() << "]";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQRect& r ) {
*this << "[" << r.x() << "," << r.y() << " - " << r.width() << "x" << r.height() << "]";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQRegion& reg ) {
*this<< "[ ";
TQMemArray<TQRect>rs=reg.rects();
for (uint i=0;i<rs.size();++i)
*this << TQString(TQString("[%1,%2 - %3x%4] ").arg(rs[i].x()).arg(rs[i].y()).arg(rs[i].width()).arg(rs[i].height() )) ;
*this <<"]";
return *this;
}
kdbgstream& kdbgstream::operator<<( const KURL& u ) {
*this << u.prettyURL();
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQStringList& l ) {
*this << "(";
*this << l.join(",");
*this << ")";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQColor& c ) {
if ( c.isValid() )
*this << TQString(c.name());
else
*this << "(invalid/default)";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQPen& p ) {
static const char* const s_penStyles[] = {
"NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine",
"DashDotDotLine" };
static const char* const s_capStyles[] = {
"FlatCap", "SquareCap", "RoundCap" };
*this << "[ style:";
*this << s_penStyles[ p.style() ];
*this << " width:";
*this << p.width();
*this << " color:";
if ( p.color().isValid() )
*this << TQString(p.color().name());
else
*this <<"(invalid/default)";
if ( p.width() > 0 ) // cap style doesn't matter, otherwise
{
*this << " capstyle:";
*this << s_capStyles[ p.capStyle() >> 4 ];
// join style omitted
}
*this <<" ]";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQBrush& b) {
static const char* const s_brushStyles[] = {
"NoBrush", "SolidPattern", "Dense1Pattern", "Dense2Pattern", "Dense3Pattern",
"Dense4Pattern", "Dense5Pattern", "Dense6Pattern", "Dense7Pattern",
"HorPattern", "VerPattern", "CrossPattern", "BDiagPattern", "FDiagPattern",
"DiagCrossPattern" };
*this <<"[ style: ";
*this <<s_brushStyles[ b.style() ];
*this <<" color: ";
// can't use operator<<(str, b.color()) because that terminates a kdbgstream (flushes)
if ( b.color().isValid() )
*this << TQString(b.color().name()) ;
else
*this <<"(invalid/default)";
if ( b.pixmap() )
*this <<" has a pixmap";
*this <<" ]";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQVariant& v) {
*this << "[variant: ";
*this << v.typeName();
// For now we just attempt a conversion to string.
// Feel free to switch(v.type()) and improve the output.
*this << " toString=";
*this << v.toString();
*this << "]";
return *this;
}
kdbgstream& kdbgstream::operator<<( const TQByteArray& data) {
if (!print) return *this;
output += '[';
unsigned int i = 0;
unsigned int sz = QMIN( data.size(), 64 );
for ( ; i < sz ; ++i ) {
output += TQString::number( (unsigned char) data[i], 16 ).rightJustify(2, '0');
if ( i < sz )
output += ' ';
}
if ( sz < data.size() )
output += "...";
output += ']';
return *this;
}
#ifdef HAVE_BACKTRACE
struct BacktraceFunctionInfo {
const void *addr; //< the address of function returned by backtrace()
const char* fileName; //< the file of binary owning the function (e.g. shared library or current header)
const void *base; //< the base address there the binary is loaded to
uintptr_t offset; //< offset of the function in binary (base - address)
TQString functionName; //< mangled name of function
TQString prettyName; //< demangled name of function
TQString sourceName; //< name of source file function declared in
unsigned sourceLine; //< line where function defined
};
#ifdef WITH_LIBBFD
// load symbol table from file
asymbol** bfdLoadSymtab (bfd *abfd) {
long symCount; // count of entries in symbol table
long symtab_sz; // size of the table
asymbol** rv;
bfd_boolean dynamic = FALSE;
// make shure the file has symbol table
if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0){
return 0;
}
// determin the amount of space we'll need to store the table
symtab_sz = bfd_get_symtab_upper_bound (abfd);
if (symtab_sz == 0) {
symtab_sz = bfd_get_dynamic_symtab_upper_bound (abfd);
dynamic = TRUE;
}
if (symtab_sz < 0) {
return 0;
}
// allocate memory
rv = (asymbol **) malloc(symtab_sz); // dunno, why not malloc
if ( !rv ) {
return 0;
}
// actually load the table
if (dynamic) {
symCount = bfd_canonicalize_dynamic_symtab (abfd, rv);
} else {
symCount = bfd_canonicalize_symtab (abfd, rv);
}
if (symCount < 0) {
if (rv) {
free(rv);
}
return 0;
}
return rv;
}
void bfdFillAdditionalFunctionsInfo(BacktraceFunctionInfo &func) {
static bool inited=0;
if (!inited) {
bfd_init();
inited=1;
}
bfd *abfd = bfd_openr(func.fileName, 0); // a bfd object
if( !abfd ) {
return;
}
// check format of the object
if( !bfd_check_format(abfd, bfd_object) ) {
bfd_close(abfd);
return;
}
// load symbol table
asymbol **syms= bfdLoadSymtab(abfd);
if(!syms) {
bfd_close(abfd);
return;
}
// found source file and line for given address
for (asection *sect = abfd->sections; sect != NULL; sect = sect->next) {
if (bfd_get_section_flags(abfd, sect) & SEC_ALLOC) {
bfd_vma sectStart = bfd_get_section_vma(abfd, sect);
bfd_vma sectEnd = sectStart + bfd_section_size(abfd, sect);
if (sectStart <= func.offset && func.offset < sectEnd) {
bfd_vma sectOffset = func.offset - sectStart;
const char* functionName;
const char* sourceName;
unsigned sourceLine;
if (bfd_find_nearest_line(abfd, sect, syms, sectOffset,
&sourceName, &functionName, &sourceLine))
{
func.sourceName = sourceName;
func.sourceLine = sourceLine;
if(func.functionName.isEmpty()) {
func.functionName = TQString::fromAscii(functionName);
}
break;
}
}
}
}
#ifdef HAVE_DEMANGLE_H
if(func.prettyName.isEmpty() && !func.functionName.isEmpty()) {
char *demangled = bfd_demangle(abfd, func.functionName.ascii(), DMGL_AUTO | DMGL_PARAMS);
if (demangled) {
func.prettyName = demangled;
free(demangled);
}
}
#endif // HAVE_DEMANGLE_H
if( syms ) {
free(syms);
}
bfd_close(abfd);
}
#endif // WITH_LIBBFD
void fillAdditionalFunctionsInfo(BacktraceFunctionInfo &func) {
#ifdef WITH_LIBBFD
bfdFillAdditionalFunctionsInfo(func);
#endif // WITH_LIBBFD
#ifdef HAVE_ABI_CXA_DEMANGLE
if(func.prettyName.isEmpty() && !func.functionName.isEmpty()) {
int status=0;
char *demangled = abi::__cxa_demangle(func.functionName.ascii(), 0, 0, &status);
if (demangled) {
func.prettyName = demangled;
free(demangled);
}
}
#endif // HAVE_ABI_CXA_DEMANGLE
}
TQString formatBacktrace(void *addr) {
TQString rv;
BacktraceFunctionInfo func;
func.addr = addr;
// NOTE: if somebody would compile for some non-linux-glibc platform
// check if dladdr function is avalible there
Dl_info info;
dladdr(func.addr, &info); // obtain information about the function.
func.fileName = info.dli_fname;
func.base = info.dli_fbase;
func.offset = (uintptr_t)func.addr - (uintptr_t)func.base;
func.functionName = TQString::fromAscii(info.dli_sname);
func.sourceLine = 0;
fillAdditionalFunctionsInfo(func);
rv.sprintf("0x%0*lx", (int) sizeof(void*)*2, (uintptr_t) func.addr);
rv += " in ";
if (!func.prettyName.isEmpty()) {
rv += func.prettyName;
} else if (!func.functionName.isEmpty()) {
rv += func.functionName;
} else {
rv += "??";
}
if (!func.sourceName.isEmpty()) {
rv += " in ";
rv += func.sourceName;
rv += ":";
rv += func.sourceLine ? TQString::number(func.sourceLine) : "??";
} else if (func.fileName && func.fileName[0]) {
rv += TQString().sprintf(" from %s:0x%08lx",func.fileName, func.offset);
} else {
rv += " from ??";
}
return rv;
}
#endif // HAVE_BACKTRACE
TQString kdBacktrace(int levels)
{
TQString rv;
#ifdef HAVE_BACKTRACE
if (levels < 0 || levels > 256 ) {
levels = 256;
}
rv = "[\n";
if (levels) {
#ifdef HAVE_ALLOCA
void** trace = (void**)alloca(levels * sizeof(void*));
#else // HAVE_ALLOCA
void* trace[256];
#endif // HAVE_ALLOCA
levels = backtrace(trace, levels);
if (levels) {
for (int i = 0; i < levels; ++i) {
rv += QString().sprintf("#%-2d ", i);
rv += formatBacktrace(trace[i]);
rv += '\n';
}
} else {
rv += "backtrace() failed\n";
}
}
rv += "]\n";
#endif // HAVE_BACKTRACE
return rv;
}
// Keep for ABI compatability for some time
// FIXME remove this (2013-08-18, 18:09, Fat-Zer)
TQString kdBacktrace()
{
return kdBacktrace(-1 /*all*/);
}
void kdBacktraceFD(int fd) {
#ifdef HAVE_BACKTRACE
void *trace[256];
int levels;
levels = backtrace(trace, 256);
if (levels) {
backtrace_symbols_fd(trace, levels, fd);
}
#endif // HAVE_BACKTRACE
}
void kdClearDebugConfig()
{
if (kDebug_data) {
delete kDebug_data->config;
kDebug_data->config = 0;
}
}
// Needed for --enable-final
#ifdef NDEBUG
#define kdDebug kndDebug
#endif
|