• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdecore
 

kdecore

  • kdecore
kdebug.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
3  2002 Holger Freyther (freyther@kde.org)
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kdebug.h"
22 
23 #ifdef NDEBUG
24 #undef kdDebug
25 #endif
26 
27 #include "kdebugdcopiface.h"
28 
29 #include "kapplication.h"
30 #include "kglobal.h"
31 #include "kinstance.h"
32 #include "kstandarddirs.h"
33 
34 #include <tqmessagebox.h>
35 #include <klocale.h>
36 #include <tqfile.h>
37 #include <tqintdict.h>
38 #include <tqstring.h>
39 #include <tqdatetime.h>
40 #include <tqpoint.h>
41 #include <tqrect.h>
42 #include <tqregion.h>
43 #include <tqstringlist.h>
44 #include <tqpen.h>
45 #include <tqbrush.h>
46 #include <tqsize.h>
47 
48 #include <kurl.h>
49 
50 #include <stdlib.h> // abort
51 #include <unistd.h> // getpid
52 #include <stdarg.h> // vararg stuff
53 #include <ctype.h> // isprint
54 #include <syslog.h>
55 #include <errno.h>
56 #include <cstring>
57 #include <kconfig.h>
58 #include "kstaticdeleter.h"
59 #include <config.h>
60 
61 #ifdef HAVE_BACKTRACE
62 #include BACKTRACE_H
63 
64 #ifdef HAVE_DLFCN_H
65 #include <dlfcn.h>
66 #endif
67 
68 #ifdef HAVE_ABI_CXA_DEMANGLE
69 #include <cxxabi.h>
70 #endif
71 
72 #include <link.h>
73 #ifdef WITH_LIBBFD
74 /* newer versions of libbfd require some autotools-specific macros to be defined */
75 /* see binutils Bug 14243 and 14072 */
76 #define PACKAGE kdelibs
77 #define PACKAGE_VERSION KDE_VERSION
78 
79 #include <bfd.h>
80 
81 #ifdef HAVE_DEMANGLE_H
82 #include <demangle.h>
83 #endif // HAVE_DEMANGLE_H
84 #endif // WITH_LIBBFD
85 
86 #endif // HAVE_BACKTRACE
87 
88 #ifdef HAVE_ALLOCA_H
89 #include <alloca.h>
90 #endif // HAVE_ALLOCA_H
91 
92 #ifdef HAVE_STDINT_H
93 #include <stdint.h>
94 #endif // HAVE_STDINT_H
95 
96 class KDebugEntry;
97 
98 class KDebugEntry
99 {
100 public:
101  KDebugEntry (int n, const TQCString& d) {number=n; descr=d;}
102  unsigned int number;
103  TQCString descr;
104 };
105 
106 static TQIntDict<KDebugEntry> *KDebugCache;
107 
108 static KStaticDeleter< TQIntDict<KDebugEntry> > kdd;
109 
110 static TQCString getDescrFromNum(unsigned int _num)
111 {
112  if (!KDebugCache) {
113  kdd.setObject(KDebugCache, new TQIntDict<KDebugEntry>( 601 ));
114  // Do not call this deleter from ~KApplication
115  KGlobal::unregisterStaticDeleter(&kdd);
116  KDebugCache->setAutoDelete(true);
117  }
118 
119  KDebugEntry *ent = KDebugCache->find( _num );
120  if ( ent )
121  return ent->descr;
122 
123  if ( !KDebugCache->isEmpty() ) // areas already loaded
124  return TQCString();
125 
126  TQString filename(locate("config","kdebug.areas"));
127  if (filename.isEmpty())
128  return TQCString();
129 
130  TQFile file(filename);
131  if (!file.open(IO_ReadOnly)) {
132  qWarning("Couldn't open %s", filename.local8Bit().data());
133  file.close();
134  return TQCString();
135  }
136 
137  uint lineNumber=0;
138  TQCString line(1024);
139  int len;
140 
141  while (( len = file.readLine(line.data(),line.size()-1) ) > 0) {
142  int i=0;
143  ++lineNumber;
144 
145  while (line[i] && line[i] <= ' ')
146  i++;
147 
148  unsigned char ch=line[i];
149 
150  if ( !ch || ch =='#' || ch =='\n')
151  continue; // We have an eof, a comment or an empty line
152 
153  if (ch < '0' && ch > '9') {
154  qWarning("Syntax error: no number (line %u)",lineNumber);
155  continue;
156  }
157 
158  const int numStart=i;
159  do {
160  ch=line[++i];
161  } while ( ch >= '0' && ch <= '9');
162 
163  const TQ_ULONG number =line.mid(numStart,i).toULong();
164 
165  while (line[i] && line[i] <= ' ')
166  i++;
167 
168  KDebugCache->insert(number, new KDebugEntry(number, line.mid(i, len-i-1)));
169  }
170  file.close();
171 
172  ent = KDebugCache->find( _num );
173  if ( ent )
174  return ent->descr;
175 
176  return TQCString();
177 }
178 
179 enum DebugLevels {
180  KDEBUG_INFO= 0,
181  KDEBUG_WARN= 1,
182  KDEBUG_ERROR= 2,
183  KDEBUG_FATAL= 3
184 };
185 
186 
187 struct kDebugPrivate {
188  kDebugPrivate() :
189  oldarea(0), config(0) { }
190 
191  ~kDebugPrivate() { delete config; }
192 
193  TQCString aAreaName;
194  unsigned int oldarea;
195  KConfig *config;
196 };
197 
198 static kDebugPrivate *kDebug_data = 0;
199 static KStaticDeleter<kDebugPrivate> pcd;
200 static KStaticDeleter<KDebugDCOPIface> dcopsd;
201 static KDebugDCOPIface* kDebugDCOPIface = 0;
202 
203 static void kDebugBackend( unsigned short nLevel, unsigned int nArea, const char *data)
204 {
205  if ( !kDebug_data )
206  {
207  pcd.setObject(kDebug_data, new kDebugPrivate());
208  // Do not call this deleter from ~KApplication
209  KGlobal::unregisterStaticDeleter(&pcd);
210 
211  // create the dcop interface if it has not been created yet
212  if (!kDebugDCOPIface)
213  {
214  kDebugDCOPIface = dcopsd.setObject(kDebugDCOPIface, new KDebugDCOPIface);
215  }
216  }
217 
218  if (!kDebug_data->config && KGlobal::_instance )
219  {
220  kDebug_data->config = new KConfig("kdebugrc", false, false);
221  kDebug_data->config->setGroup("0");
222 
223  //AB: this is necessary here, otherwise all output with area 0 won't be
224  //prefixed with anything, unless something with area != 0 is called before
225  if ( KGlobal::_instance )
226  kDebug_data->aAreaName = KGlobal::instance()->instanceName();
227  }
228 
229  if ( kDebug_data->oldarea != nArea ) {
230  kDebug_data->oldarea = nArea;
231  if( KGlobal::_instance ) {
232  if ( nArea > 0 ) {
233  kDebug_data->aAreaName = getDescrFromNum(nArea);
234  }
235  if ( nArea == 0 || kDebug_data->aAreaName.isEmpty() ) {
236  kDebug_data->aAreaName = KGlobal::instance()->instanceName();
237  }
238  }
239  }
240 
241  int nPriority = 0;
242  TQString aCaption;
243 
244  /* Determine output */
245 
246  TQString key;
247  switch( nLevel )
248  {
249  case KDEBUG_INFO:
250  key = "InfoOutput";
251  aCaption = "Info";
252  nPriority = LOG_INFO;
253  break;
254  case KDEBUG_WARN:
255  key = "WarnOutput";
256  aCaption = "Warning";
257  nPriority = LOG_WARNING;
258  break;
259  case KDEBUG_FATAL:
260  key = "FatalOutput";
261  aCaption = "Fatal Error";
262  nPriority = LOG_CRIT;
263  break;
264  case KDEBUG_ERROR:
265  default:
266  /* Programmer error, use "Error" as default */
267  key = "ErrorOutput";
268  aCaption = "Error";
269  nPriority = LOG_ERR;
270  break;
271  }
272 
273  short nOutput = -1;
274  if ( kDebug_data->config ) {
275  kDebug_data->config->setGroup( TQString::number(static_cast<int>(nArea)) );
276  nOutput = kDebug_data->config->readNumEntry(key, -1);
277  if( nOutput == -1 ) {
278  kDebug_data->config->setGroup( TQString::fromAscii("Default") );
279  nOutput = kDebug_data->config->readNumEntry(key, -1);
280  }
281  }
282  // if no output mode is specified default to no stderr output
283  // NOTE: don't set this to 4 (no output) because in that case you won't be
284  // able to get any output from applications which don't create
285  // KApplication objects.
286  if ( nOutput == -1 ) {
287  nOutput = 2;
288  }
289 
290  // If the application doesn't have a TQApplication object it can't use
291  // a messagebox, as well as in case of GUI is disabled.
292  if ( nOutput == 1 && ( !kapp || !kapp->guiEnabled()) ) {
293  nOutput = 2;
294  } else if ( nOutput == 4 && nLevel != KDEBUG_FATAL ) {
295  return;
296  }
297 
298  const int BUFSIZE = 4096;
299  char buf[BUFSIZE];
300  if ( !kDebug_data->aAreaName.isEmpty() ) {
301  strlcpy( buf, kDebug_data->aAreaName.data(), BUFSIZE );
302  strlcat( buf, ": ", BUFSIZE );
303  strlcat( buf, data, BUFSIZE );
304  }
305  else
306  strlcpy( buf, data, BUFSIZE );
307 
308 
309  // Output
310  switch( nOutput )
311  {
312  case 0: // File
313  {
314  const char* aKey;
315  switch( nLevel )
316  {
317  case KDEBUG_INFO:
318  aKey = "InfoFilename";
319  break;
320  case KDEBUG_WARN:
321  aKey = "WarnFilename";
322  break;
323  case KDEBUG_FATAL:
324  aKey = "FatalFilename";
325  break;
326  case KDEBUG_ERROR:
327  default:
328  aKey = "ErrorFilename";
329  break;
330  }
331  TQFile aOutputFile( kDebug_data->config->readPathEntry(aKey, "kdebug.dbg") );
332  aOutputFile.open( (TQIODevice_OpenModeFlag)((int)IO_WriteOnly | (int)IO_Append | (int)IO_Raw) );
333  aOutputFile.writeBlock( buf, strlen( buf ) );
334  aOutputFile.close();
335  break;
336  }
337  case 1: // Message Box
338  {
339  // Since we are in kdecore here, we cannot use KMsgBox and use
340  // TQMessageBox instead
341  if ( !kDebug_data->aAreaName.isEmpty() )
342  aCaption += TQString("(%1)").arg( QString(kDebug_data->aAreaName) );
343  TQMessageBox::warning( 0L, aCaption, data, i18n("&OK") );
344  break;
345  }
346  case 2: // Shell
347  {
348  write( 2, buf, strlen( buf ) ); //fputs( buf, stderr );
349  break;
350  }
351  case 3: // syslog
352  {
353  syslog( nPriority, "%s", buf);
354  break;
355  }
356  }
357 
358  // check if we should abort
359  if( ( nLevel == KDEBUG_FATAL )
360  && ( !kDebug_data->config || kDebug_data->config->readNumEntry( "AbortFatal", 1 ) ) )
361  abort();
362 }
363 
364 kdbgstream &perror( kdbgstream &s) { return s << TQString(TQString::fromLocal8Bit(strerror(errno))); }
365 kdbgstream kdDebug(int area) { return kdbgstream(area, KDEBUG_INFO); }
366 kdbgstream kdDebug(bool cond, int area) { if (cond) return kdbgstream(area, KDEBUG_INFO); else return kdbgstream(0, 0, false); }
367 
368 kdbgstream kdError(int area) { return kdbgstream("ERROR: ", area, KDEBUG_ERROR); }
369 kdbgstream kdError(bool cond, int area) { if (cond) return kdbgstream("ERROR: ", area, KDEBUG_ERROR); else return kdbgstream(0,0,false); }
370 kdbgstream kdWarning(int area) { return kdbgstream("WARNING: ", area, KDEBUG_WARN); }
371 kdbgstream kdWarning(bool cond, int area) { if (cond) return kdbgstream("WARNING: ", area, KDEBUG_WARN); else return kdbgstream(0,0,false); }
372 kdbgstream kdFatal(int area) { return kdbgstream("FATAL: ", area, KDEBUG_FATAL); }
373 kdbgstream kdFatal(bool cond, int area) { if (cond) return kdbgstream("FATAL: ", area, KDEBUG_FATAL); else return kdbgstream(0,0,false); }
374 
375 kdbgstream::kdbgstream(kdbgstream &str)
376  : output(str.output), area(str.area), level(str.level), print(str.print)
377 {
378  str.output.truncate(0);
379 }
380 
381 void kdbgstream::flush() {
382  if (output.isEmpty() || !print)
383  return;
384  kDebugBackend( level, area, output.local8Bit().data() );
385  output = TQString::null;
386 }
387 
388 kdbgstream &kdbgstream::form(const char *format, ...)
389 {
390  char buf[4096];
391  va_list arguments;
392  va_start( arguments, format );
393  vsnprintf( buf, sizeof(buf), format, arguments );
394  va_end(arguments);
395  *this << buf;
396  return *this;
397 }
398 
399 kdbgstream::~kdbgstream() {
400  if (!output.isEmpty()) {
401  fprintf(stderr, "ASSERT: debug output not ended with \\n\n");
402  TQString backtrace = kdBacktrace();
403  if (backtrace.ascii() != NULL) {
404  fprintf(stderr, "%s", backtrace.latin1());
405  }
406  *this << "\n";
407  }
408 }
409 
410 kdbgstream& kdbgstream::operator << (char ch)
411 {
412  if (!print) return *this;
413  if (!isprint(ch))
414  output += "\\x" + TQString::number( static_cast<uint>( ch ), 16 ).rightJustify(2, '0');
415  else {
416  output += ch;
417  if (ch == '\n') flush();
418  }
419  return *this;
420 }
421 
422 kdbgstream& kdbgstream::operator << (TQChar ch)
423 {
424  if (!print) return *this;
425  if (!ch.isPrint())
426  output += "\\x" + TQString::number( ch.unicode(), 16 ).rightJustify(2, '0');
427  else {
428  output += ch;
429  if (ch == QChar('\n')) flush();
430  }
431  return *this;
432 }
433 
434 kdbgstream& kdbgstream::operator << (TQWidget* widget)
435 {
436  return *this << const_cast< const TQWidget* >( widget );
437 }
438 
439 kdbgstream& kdbgstream::operator << (const TQWidget* widget)
440 {
441  TQString string, temp;
442  // -----
443  if(widget==0)
444  {
445  string=(TQString)"[Null pointer]";
446  } else {
447  temp.setNum((ulong)widget, 16);
448  string=(TQString)"["+widget->className()+" pointer "
449  + "(0x" + temp + ")";
450  if(widget->name(0)==0)
451  {
452  string += " to unnamed widget, ";
453  } else {
454  string += (TQString)" to widget " + widget->name() + ", ";
455  }
456  string += "geometry="
457  + TQString().setNum(widget->width())
458  + "x"+TQString().setNum(widget->height())
459  + "+"+TQString().setNum(widget->x())
460  + "+"+TQString().setNum(widget->y())
461  + "]";
462  }
463  if (!print)
464  {
465  return *this;
466  }
467  output += string;
468  if (output.at(output.length() -1 ) == QChar('\n'))
469  {
470  flush();
471  }
472  return *this;
473 }
474 /*
475  * either use 'output' directly and do the flush if needed
476  * or use the TQString operator which calls the char* operator
477  *
478  */
479 kdbgstream& kdbgstream::operator<<( const TQDateTime& time) {
480  *this << time.toString();
481  return *this;
482 }
483 kdbgstream& kdbgstream::operator<<( const TQDate& date) {
484  *this << TQString(date.toString());
485 
486  return *this;
487 }
488 kdbgstream& kdbgstream::operator<<( const TQTime& time ) {
489  *this << TQString(time.toString());
490  return *this;
491 }
492 kdbgstream& kdbgstream::operator<<( const TQPoint& p ) {
493  *this << "(" << p.x() << ", " << p.y() << ")";
494  return *this;
495 }
496 kdbgstream& kdbgstream::operator<<( const TQSize& s ) {
497  *this << "[" << s.width() << "x" << s.height() << "]";
498  return *this;
499 }
500 kdbgstream& kdbgstream::operator<<( const TQRect& r ) {
501  *this << "[" << r.x() << "," << r.y() << " - " << r.width() << "x" << r.height() << "]";
502  return *this;
503 }
504 kdbgstream& kdbgstream::operator<<( const TQRegion& reg ) {
505  *this<< "[ ";
506 
507  TQMemArray<TQRect>rs=reg.rects();
508  for (uint i=0;i<rs.size();++i)
509  *this << TQString(TQString("[%1,%2 - %3x%4] ").arg(rs[i].x()).arg(rs[i].y()).arg(rs[i].width()).arg(rs[i].height() )) ;
510 
511  *this <<"]";
512  return *this;
513 }
514 kdbgstream& kdbgstream::operator<<( const KURL& u ) {
515  *this << u.prettyURL();
516  return *this;
517 }
518 kdbgstream& kdbgstream::operator<<( const TQStringList& l ) {
519  *this << "(";
520  *this << l.join(",");
521  *this << ")";
522 
523  return *this;
524 }
525 kdbgstream& kdbgstream::operator<<( const TQColor& c ) {
526  if ( c.isValid() )
527  *this << TQString(c.name());
528  else
529  *this << "(invalid/default)";
530  return *this;
531 }
532 kdbgstream& kdbgstream::operator<<( const TQPen& p ) {
533  static const char* const s_penStyles[] = {
534  "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine",
535  "DashDotDotLine" };
536  static const char* const s_capStyles[] = {
537  "FlatCap", "SquareCap", "RoundCap" };
538  *this << "[ style:";
539  *this << s_penStyles[ p.style() ];
540  *this << " width:";
541  *this << p.width();
542  *this << " color:";
543  if ( p.color().isValid() )
544  *this << TQString(p.color().name());
545  else
546  *this <<"(invalid/default)";
547  if ( p.width() > 0 ) // cap style doesn't matter, otherwise
548  {
549  *this << " capstyle:";
550  *this << s_capStyles[ p.capStyle() >> 4 ];
551  // join style omitted
552  }
553  *this <<" ]";
554  return *this;
555 }
556 kdbgstream& kdbgstream::operator<<( const TQBrush& b) {
557  static const char* const s_brushStyles[] = {
558  "NoBrush", "SolidPattern", "Dense1Pattern", "Dense2Pattern", "Dense3Pattern",
559  "Dense4Pattern", "Dense5Pattern", "Dense6Pattern", "Dense7Pattern",
560  "HorPattern", "VerPattern", "CrossPattern", "BDiagPattern", "FDiagPattern",
561  "DiagCrossPattern" };
562 
563  *this <<"[ style: ";
564  *this <<s_brushStyles[ b.style() ];
565  *this <<" color: ";
566  // can't use operator<<(str, b.color()) because that terminates a kdbgstream (flushes)
567  if ( b.color().isValid() )
568  *this << TQString(b.color().name()) ;
569  else
570  *this <<"(invalid/default)";
571  if ( b.pixmap() )
572  *this <<" has a pixmap";
573  *this <<" ]";
574  return *this;
575 }
576 
577 kdbgstream& kdbgstream::operator<<( const TQVariant& v) {
578  *this << "[variant: ";
579  *this << v.typeName();
580  // For now we just attempt a conversion to string.
581  // Feel free to switch(v.type()) and improve the output.
582  *this << " toString=";
583  *this << v.toString();
584  *this << "]";
585  return *this;
586 }
587 
588 kdbgstream& kdbgstream::operator<<( const TQByteArray& data) {
589  if (!print) return *this;
590  output += '[';
591  unsigned int i = 0;
592  unsigned int sz = QMIN( data.size(), 64 );
593  for ( ; i < sz ; ++i ) {
594  output += TQString::number( (unsigned char) data[i], 16 ).rightJustify(2, '0');
595  if ( i < sz )
596  output += ' ';
597  }
598  if ( sz < data.size() )
599  output += "...";
600  output += ']';
601  return *this;
602 }
603 
604 #ifdef HAVE_BACKTRACE
605 struct BacktraceFunctionInfo {
606  const void *addr; //< the address of function returned by backtrace()
607  const char* fileName; //< the file of binary owning the function (e.g. shared library or current header)
608  const void *base; //< the base address there the binary is loaded to
609  uintptr_t offset; //< offset of the function in binary (base - address)
610  TQString functionName; //< mangled name of function
611  TQString prettyName; //< demangled name of function
612  TQString sourceName; //< name of source file function declared in
613  unsigned sourceLine; //< line where function defined
614 };
615 
616 #ifdef WITH_LIBBFD
617 
618 // load symbol table from file
619 asymbol** bfdLoadSymtab (bfd *abfd) {
620  long symCount; // count of entries in symbol table
621  long symtab_sz; // size of the table
622  asymbol** rv;
623  bfd_boolean dynamic = FALSE;
624 
625  // make shure the file has symbol table
626  if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0){
627  return 0;
628  }
629 
630  // determin the amount of space we'll need to store the table
631  symtab_sz = bfd_get_symtab_upper_bound (abfd);
632  if (symtab_sz == 0) {
633  symtab_sz = bfd_get_dynamic_symtab_upper_bound (abfd);
634  dynamic = TRUE;
635  }
636  if (symtab_sz < 0) {
637  return 0;
638  }
639 
640  // allocate memory
641  rv = (asymbol **) malloc(symtab_sz); // dunno, why not malloc
642  if ( !rv ) {
643  return 0;
644  }
645 
646  // actually load the table
647  if (dynamic) {
648  symCount = bfd_canonicalize_dynamic_symtab (abfd, rv);
649  } else {
650  symCount = bfd_canonicalize_symtab (abfd, rv);
651  }
652 
653  if (symCount < 0) {
654  if (rv) {
655  free(rv);
656  }
657  return 0;
658  }
659 
660  return rv;
661 }
662 
663 void bfdFillAdditionalFunctionsInfo(BacktraceFunctionInfo &func) {
664  static bool inited=0;
665  if (!inited) {
666  bfd_init();
667  inited=1;
668  }
669 
670  bfd *abfd = bfd_openr(func.fileName, 0); // a bfd object
671  if( !abfd ) {
672  return;
673  }
674 
675  // check format of the object
676  if( !bfd_check_format(abfd, bfd_object) ) {
677  bfd_close(abfd);
678  return;
679  }
680 
681  // load symbol table
682  asymbol **syms= bfdLoadSymtab(abfd);
683  if(!syms) {
684  bfd_close(abfd);
685  return;
686  }
687 
688  // found source file and line for given address
689  for (asection *sect = abfd->sections; sect != NULL; sect = sect->next) {
690 
691  if (bfd_get_section_flags(abfd, sect) & SEC_ALLOC) {
692  bfd_vma sectStart = bfd_get_section_vma(abfd, sect);
693  bfd_vma sectEnd = sectStart + bfd_section_size(abfd, sect);
694  if (sectStart <= func.offset && func.offset < sectEnd) {
695  bfd_vma sectOffset = func.offset - sectStart;
696  const char* functionName;
697  const char* sourceName;
698  unsigned sourceLine;
699  if (bfd_find_nearest_line(abfd, sect, syms, sectOffset,
700  &sourceName, &functionName, &sourceLine))
701  {
702  func.sourceName = sourceName;
703  func.sourceLine = sourceLine;
704  if(func.functionName.isEmpty()) {
705  func.functionName = TQString::fromAscii(functionName);
706  }
707  break;
708  }
709  }
710  }
711  }
712 #ifdef HAVE_DEMANGLE_H
713  if(func.prettyName.isEmpty() && !func.functionName.isEmpty()) {
714  char *demangled = bfd_demangle(abfd, func.functionName.ascii(), DMGL_AUTO | DMGL_PARAMS);
715  if (demangled) {
716  func.prettyName = demangled;
717  free(demangled);
718  }
719  }
720 #endif // HAVE_DEMANGLE_H
721 
722  if( syms ) {
723  free(syms);
724  }
725  bfd_close(abfd);
726 }
727 
728 #endif // WITH_LIBBFD
729 
730 void fillAdditionalFunctionsInfo(BacktraceFunctionInfo &func) {
731 #ifdef WITH_LIBBFD
732  bfdFillAdditionalFunctionsInfo(func);
733 #endif // WITH_LIBBFD
734 
735 #ifdef HAVE_ABI_CXA_DEMANGLE
736  if(func.prettyName.isEmpty() && !func.functionName.isEmpty()) {
737  int status=0;
738  char *demangled = abi::__cxa_demangle(func.functionName.ascii(), 0, 0, &status);
739  if (demangled) {
740  func.prettyName = demangled;
741  free(demangled);
742  }
743  }
744 #endif // HAVE_ABI_CXA_DEMANGLE
745 
746 }
747 
748 TQString formatBacktrace(void *addr) {
749  TQString rv;
750  BacktraceFunctionInfo func;
751  func.addr = addr;
752 
753  // NOTE: if somebody would compile for some non-linux-glibc platform
754  // check if dladdr function is avalible there
755  Dl_info info;
756  dladdr(func.addr, &info); // obtain information about the function.
757 
758  func.fileName = info.dli_fname;
759  func.base = info.dli_fbase;
760  func.offset = (uintptr_t)func.addr - (uintptr_t)func.base;
761  func.functionName = TQString::fromAscii(info.dli_sname);
762  func.sourceLine = 0;
763 
764  fillAdditionalFunctionsInfo(func);
765 
766  rv.sprintf("0x%0*lx", (int) sizeof(void*)*2, (uintptr_t) func.addr);
767 
768  rv += " in ";
769  if (!func.prettyName.isEmpty()) {
770  rv += func.prettyName;
771  } else if (!func.functionName.isEmpty()) {
772  rv += func.functionName;
773  } else {
774  rv += "??";
775  }
776 
777  if (!func.sourceName.isEmpty()) {
778  rv += " in ";
779  rv += func.sourceName;
780  rv += ":";
781  rv += func.sourceLine ? TQString::number(func.sourceLine) : "??";
782  } else if (func.fileName && func.fileName[0]) {
783  rv += TQString().sprintf(" from %s:0x%08lx",func.fileName, func.offset);
784  } else {
785  rv += " from ??";
786  }
787 
788  return rv;
789 }
790 #endif // HAVE_BACKTRACE
791 
792 
793 TQString kdBacktrace(int levels)
794 {
795  TQString rv;
796 #ifdef HAVE_BACKTRACE
797  if (levels < 0 || levels > 256 ) {
798  levels = 256;
799  }
800 
801  rv = "[\n";
802 
803  if (levels) {
804 #ifdef HAVE_ALLOCA
805  void** trace = (void**)alloca(levels * sizeof(void*));
806 #else // HAVE_ALLOCA
807  void* trace[256];
808 #endif // HAVE_ALLOCA
809  levels = backtrace(trace, levels);
810 
811  if (levels) {
812  for (int i = 0; i < levels; ++i) {
813  rv += QString().sprintf("#%-2d ", i);
814  rv += formatBacktrace(trace[i]);
815  rv += '\n';
816  }
817  } else {
818  rv += "backtrace() failed\n";
819  }
820  }
821 
822  rv += "]\n";
823 #endif // HAVE_BACKTRACE
824  return rv;
825 }
826 
827 // Keep for ABI compatability for some time
828 // FIXME remove this (2013-08-18, 18:09, Fat-Zer)
829 TQString kdBacktrace()
830 {
831  return kdBacktrace(-1 /*all*/);
832 }
833 
834 void kdBacktraceFD(int fd) {
835 #ifdef HAVE_BACKTRACE
836  void *trace[256];
837  int levels;
838 
839  levels = backtrace(trace, 256);
840  if (levels) {
841  backtrace_symbols_fd(trace, levels, fd);
842  }
843 #endif // HAVE_BACKTRACE
844 }
845 void kdClearDebugConfig()
846 {
847  if (kDebug_data) {
848  delete kDebug_data->config;
849  kDebug_data->config = 0;
850  }
851 }
852 
853 
854 // Needed for --enable-final
855 #ifdef NDEBUG
856 #define kdDebug kndDebug
857 #endif
KURL
Represents and parses a URL.
Definition: kurl.h:127
kdbgstream::flush
void flush()
Flushes the output.
Definition: kdebug.cpp:381
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
kdbgstream
kdbgstream is a text stream that allows you to print debug messages.
Definition: kdebug.h:80
klocale.h
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to KGlobal.
Definition: kstaticdeleter.h:85
KInstance::instanceName
TQCString instanceName() const
Returns the name of the instance.
Definition: kinstance.cpp:263
KDebugDCOPIface
DCOP interface to KDebug.
Definition: kdebugdcopiface.h:30
KURL::prettyURL
TQString prettyURL(int _trailing=0) const
Returns the URL as string in human-friendly format.
Definition: kurl.cpp:1528
KStdAccel::key
int key(StdAccel id)
Definition: kstdaccel.cpp:383
kdbgstream::form
kdbgstream & form(const char *format,...)
Prints the string format which can contain printf-style formatted values.
Definition: kdebug.cpp:388
KGlobal::instance
static KInstance * instance()
Returns the global instance.
Definition: kglobal.cpp:82
KConfig
Access KDE Configuration entries.
Definition: kconfig.h:43
KGlobal::unregisterStaticDeleter
static void unregisterStaticDeleter(KStaticDeleterBase *d)
Unregisters a static deleter.
Definition: kglobal.cpp:178
kdbgstream::operator<<
kdbgstream & operator<<(bool i)
Prints the given value.
Definition: kdebug.h:99

kdecore

Skip menu "kdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.13
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |