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

tdecore

  • tdecore
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 "tdeapplication.h"
30 #include "tdeglobal.h"
31 #include "kinstance.h"
32 #include "kstandarddirs.h"
33 
34 #include <tqmessagebox.h>
35 #include <tdelocale.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 <tdeconfig.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 tdelibs
77 #define PACKAGE_VERSION TDE_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 ~TDEApplication
115  TDEGlobal::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  tqWarning("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  tqWarning("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  TDEConfig *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 ~TDEApplication
209  TDEGlobal::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 && TDEGlobal::_instance )
219  {
220  kDebug_data->config = new TDEConfig("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 ( TDEGlobal::_instance )
226  kDebug_data->aAreaName = TDEGlobal::instance()->instanceName();
227  }
228 
229  if ( kDebug_data->oldarea != nArea ) {
230  kDebug_data->oldarea = nArea;
231  if( TDEGlobal::_instance ) {
232  if ( nArea > 0 ) {
233  kDebug_data->aAreaName = getDescrFromNum(nArea);
234  }
235  if ( nArea == 0 || kDebug_data->aAreaName.isEmpty() ) {
236  kDebug_data->aAreaName = TDEGlobal::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  // TDEApplication 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, TQDateTime::currentDateTime().toString("[yyyy/MM/dd hh:mm:ss.zzz] [").ascii(), BUFSIZE );
302  strlcat( buf, kDebug_data->aAreaName.data(), BUFSIZE );
303  strlcat( buf, "] ", BUFSIZE );
304  strlcat( buf, data, BUFSIZE );
305  }
306  else {
307  strlcpy( buf, TQDateTime::currentDateTime().toString("[yyyy/MM/dd hh:mm:ss.zzz] ").ascii(), BUFSIZE );
308  strlcat( buf, data, BUFSIZE );
309  }
310 
311  // Output
312  switch( nOutput )
313  {
314  case 0: // File
315  {
316  const char* aKey;
317  switch( nLevel )
318  {
319  case KDEBUG_INFO:
320  aKey = "InfoFilename";
321  break;
322  case KDEBUG_WARN:
323  aKey = "WarnFilename";
324  break;
325  case KDEBUG_FATAL:
326  aKey = "FatalFilename";
327  break;
328  case KDEBUG_ERROR:
329  default:
330  aKey = "ErrorFilename";
331  break;
332  }
333  TQFile aOutputFile( kDebug_data->config->readPathEntry(aKey, "kdebug.dbg") );
334  aOutputFile.open( (TQIODevice_OpenModeFlag)((int)IO_WriteOnly | (int)IO_Append | (int)IO_Raw) );
335  aOutputFile.writeBlock( buf, strlen( buf ) );
336  aOutputFile.close();
337  break;
338  }
339  case 1: // Message Box
340  {
341  // Since we are in tdecore here, we cannot use KMsgBox and use
342  // TQMessageBox instead
343  if ( !kDebug_data->aAreaName.isEmpty() )
344  aCaption += TQString("(%1)").arg( QString(kDebug_data->aAreaName) );
345  TQMessageBox::warning( 0L, aCaption, data, i18n("&OK") );
346  break;
347  }
348  case 2: // Shell
349  {
350  if (write( 2, buf, strlen( buf ) ) < 0) { //fputs( buf, stderr );
351  // ERROR
352  }
353  break;
354  }
355  case 3: // syslog
356  {
357  syslog( nPriority, "%s", buf);
358  break;
359  }
360  }
361 
362  // check if we should abort
363  if( ( nLevel == KDEBUG_FATAL )
364  && ( !kDebug_data->config || kDebug_data->config->readNumEntry( "AbortFatal", 1 ) ) )
365  abort();
366 }
367 
368 kdbgstream& perror( kdbgstream &s) { return s << TQString(TQString::fromLocal8Bit(strerror(errno))); }
369 kdbgstream kdDebug(int area) { return kdbgstream(area, KDEBUG_INFO); }
370 kdbgstream kdDebug(bool cond, int area) { if (cond) return kdbgstream(area, KDEBUG_INFO); else return kdbgstream(0, 0, false); }
371 
372 kdbgstream kdError(int area) { return kdbgstream("ERROR: ", area, KDEBUG_ERROR); }
373 kdbgstream kdError(bool cond, int area) { if (cond) return kdbgstream("ERROR: ", area, KDEBUG_ERROR); else return kdbgstream(0,0,false); }
374 kdbgstream kdWarning(int area) { return kdbgstream("WARNING: ", area, KDEBUG_WARN); }
375 kdbgstream kdWarning(bool cond, int area) { if (cond) return kdbgstream("WARNING: ", area, KDEBUG_WARN); else return kdbgstream(0,0,false); }
376 kdbgstream kdFatal(int area) { return kdbgstream("FATAL: ", area, KDEBUG_FATAL); }
377 kdbgstream kdFatal(bool cond, int area) { if (cond) return kdbgstream("FATAL: ", area, KDEBUG_FATAL); else return kdbgstream(0,0,false); }
378 
379 kdbgstream::kdbgstream(kdbgstream &str)
380  : output(str.output), area(str.area), level(str.level), print(str.print)
381 {
382  str.output.truncate(0);
383 }
384 
385 void kdbgstream::flush() {
386  if (output.isEmpty() || !print)
387  return;
388  kDebugBackend( level, area, output.local8Bit().data() );
389  output = TQString::null;
390 }
391 
392 kdbgstream &kdbgstream::form(const char *format, ...)
393 {
394  char buf[4096];
395  va_list arguments;
396  va_start( arguments, format );
397  vsnprintf( buf, sizeof(buf), format, arguments );
398  va_end(arguments);
399  *this << buf;
400  return *this;
401 }
402 
403 kdbgstream::~kdbgstream() {
404  if (!output.isEmpty()) {
405  fprintf(stderr, "ASSERT: debug output not ended with \\n\n");
406  TQString backtrace = kdBacktrace();
407  if (backtrace.ascii() != NULL) {
408  fprintf(stderr, "%s", backtrace.latin1());
409  }
410  *this << '\n';
411  }
412 }
413 
414 kdbgstream& kdbgstream::operator<< (char ch)
415 {
416  if (!print) return *this;
417  if (!isprint(ch))
418  output += "\\x" + TQString::number( static_cast<uint>( ch ), 16 ).rightJustify(2, '0');
419  else {
420  output += ch;
421  if (ch == '\n') flush();
422  }
423  return *this;
424 }
425 
426 kdbgstream& kdbgstream::operator<< (TQChar ch)
427 {
428  if (!print) return *this;
429  if (!ch.isPrint())
430  output += "\\x" + TQString::number( ch.unicode(), 16 ).rightJustify(2, '0');
431  else {
432  output += ch;
433  if (ch == QChar('\n')) flush();
434  }
435  return *this;
436 }
437 
438 kdbgstream& kdbgstream::operator<< (TQWidget* widget)
439 {
440  return *this << const_cast< const TQWidget* >( widget );
441 }
442 
443 kdbgstream& kdbgstream::operator<< (const TQWidget* widget)
444 {
445  TQString string, temp;
446  // -----
447  if(widget==0)
448  {
449  string=(TQString)"[Null pointer]";
450  } else {
451  temp.setNum((ulong)widget, 16);
452  string=(TQString)"["+widget->className()+" pointer "
453  + "(0x" + temp + ")";
454  if(widget->name(0)==0)
455  {
456  string += " to unnamed widget, ";
457  } else {
458  string += (TQString)" to widget " + widget->name() + ", ";
459  }
460  string += "geometry="
461  + TQString().setNum(widget->width())
462  + "x"+TQString().setNum(widget->height())
463  + "+"+TQString().setNum(widget->x())
464  + "+"+TQString().setNum(widget->y())
465  + "]";
466  }
467  if (!print)
468  {
469  return *this;
470  }
471  output += string;
472  if (output.at(output.length() -1 ) == QChar('\n'))
473  {
474  flush();
475  }
476  return *this;
477 }
478 /*
479  * either use 'output' directly and do the flush if needed
480  * or use the TQString operator which calls the char* operator
481  *
482  */
483 kdbgstream& kdbgstream::operator<<( const TQDateTime& time) {
484  *this << time.toString();
485  return *this;
486 }
487 kdbgstream& kdbgstream::operator<<( const TQDate& date) {
488  *this << TQString(date.toString());
489 
490  return *this;
491 }
492 kdbgstream& kdbgstream::operator<<( const TQTime& time ) {
493  *this << TQString(time.toString());
494  return *this;
495 }
496 kdbgstream& kdbgstream::operator<<( const TQPoint& p ) {
497  *this << "(" << p.x() << ", " << p.y() << ")";
498  return *this;
499 }
500 kdbgstream& kdbgstream::operator<<( const TQSize& s ) {
501  *this << "[" << s.width() << "x" << s.height() << "]";
502  return *this;
503 }
504 kdbgstream& kdbgstream::operator<<( const TQRect& r ) {
505  *this << "[" << r.x() << "," << r.y() << " - " << r.width() << "x" << r.height() << "]";
506  return *this;
507 }
508 kdbgstream& kdbgstream::operator<<( const TQRegion& reg ) {
509  *this<< "[ ";
510 
511  TQMemArray<TQRect>rs=reg.rects();
512  for (uint i=0;i<rs.size();++i)
513  *this << TQString(TQString("[%1,%2 - %3x%4] ").arg(rs[i].x()).arg(rs[i].y()).arg(rs[i].width()).arg(rs[i].height() )) ;
514 
515  *this <<"]";
516  return *this;
517 }
518 kdbgstream& kdbgstream::operator<<( const KURL& u ) {
519  *this << u.prettyURL();
520  return *this;
521 }
522 kdbgstream& kdbgstream::operator<<( const TQStringList& l ) {
523  *this << "(";
524  *this << l.join(",");
525  *this << ")";
526 
527  return *this;
528 }
529 kdbgstream& kdbgstream::operator<<( const TQColor& c ) {
530  if ( c.isValid() )
531  *this << TQString(c.name());
532  else
533  *this << "(invalid/default)";
534  return *this;
535 }
536 kdbgstream& kdbgstream::operator<<( const TQPen& p ) {
537  static const char* const s_penStyles[] = {
538  "NoPen", "SolidLine", "DashLine", "DotLine", "DashDotLine",
539  "DashDotDotLine" };
540  static const char* const s_capStyles[] = {
541  "FlatCap", "SquareCap", "RoundCap" };
542  *this << "[ style:";
543  *this << s_penStyles[ p.style() ];
544  *this << " width:";
545  *this << p.width();
546  *this << " color:";
547  if ( p.color().isValid() )
548  *this << TQString(p.color().name());
549  else
550  *this <<"(invalid/default)";
551  if ( p.width() > 0 ) // cap style doesn't matter, otherwise
552  {
553  *this << " capstyle:";
554  *this << s_capStyles[ p.capStyle() >> 4 ];
555  // join style omitted
556  }
557  *this <<" ]";
558  return *this;
559 }
560 kdbgstream& kdbgstream::operator<<( const TQBrush& b) {
561  static const char* const s_brushStyles[] = {
562  "NoBrush", "SolidPattern", "Dense1Pattern", "Dense2Pattern", "Dense3Pattern",
563  "Dense4Pattern", "Dense5Pattern", "Dense6Pattern", "Dense7Pattern",
564  "HorPattern", "VerPattern", "CrossPattern", "BDiagPattern", "FDiagPattern",
565  "DiagCrossPattern" };
566 
567  *this <<"[ style: ";
568  *this <<s_brushStyles[ b.style() ];
569  *this <<" color: ";
570  // can't use operator<<(str, b.color()) because that terminates a kdbgstream (flushes)
571  if ( b.color().isValid() )
572  *this << TQString(b.color().name()) ;
573  else
574  *this <<"(invalid/default)";
575  if ( b.pixmap() )
576  *this <<" has a pixmap";
577  *this <<" ]";
578  return *this;
579 }
580 
581 kdbgstream& kdbgstream::operator<<( const TQVariant& v) {
582  *this << "[variant: ";
583  *this << v.typeName();
584  // For now we just attempt a conversion to string.
585  // Feel free to switch(v.type()) and improve the output.
586  *this << " toString=";
587  *this << v.toString();
588  *this << "]";
589  return *this;
590 }
591 
592 kdbgstream& kdbgstream::operator<<( const TQByteArray& data) {
593  if (!print) return *this;
594  output += '[';
595  unsigned int i = 0;
596  unsigned int sz = TQMIN( data.size(), 64 );
597  for ( ; i < sz ; ++i ) {
598  output += TQString::number( (unsigned char) data[i], 16 ).rightJustify(2, '0');
599  if ( i < sz )
600  output += ' ';
601  }
602  if ( sz < data.size() )
603  output += "...";
604  output += ']';
605  return *this;
606 }
607 
608 #ifdef HAVE_BACKTRACE
609 struct BacktraceFunctionInfo {
610  const void *addr; //< the address of function returned by backtrace()
611  const char* fileName; //< the file of binary owning the function (e.g. shared library or current header)
612  const void *base; //< the base address there the binary is loaded to
613  uintptr_t offset; //< offset of the function in binary (base - address)
614  TQString functionName; //< mangled name of function
615  TQString prettyName; //< demangled name of function
616  TQString sourceName; //< name of source file function declared in
617  unsigned sourceLine; //< line where function defined
618 };
619 
620 #ifdef WITH_LIBBFD
621 
622 // load symbol table from file
623 asymbol** bfdLoadSymtab (bfd *abfd) {
624  long symCount; // count of entries in symbol table
625  long symtab_sz; // size of the table
626  asymbol** rv;
627  bfd_boolean dynamic = FALSE;
628 
629  // make shure the file has symbol table
630  if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0){
631  return 0;
632  }
633 
634  // determin the amount of space we'll need to store the table
635  symtab_sz = bfd_get_symtab_upper_bound (abfd);
636  if (symtab_sz == 0) {
637  symtab_sz = bfd_get_dynamic_symtab_upper_bound (abfd);
638  dynamic = TRUE;
639  }
640  if (symtab_sz < 0) {
641  return 0;
642  }
643 
644  // allocate memory
645  rv = (asymbol **) malloc(symtab_sz); // dunno, why not malloc
646  if ( !rv ) {
647  return 0;
648  }
649 
650  // actually load the table
651  if (dynamic) {
652  symCount = bfd_canonicalize_dynamic_symtab (abfd, rv);
653  } else {
654  symCount = bfd_canonicalize_symtab (abfd, rv);
655  }
656 
657  if (symCount < 0) {
658  if (rv) {
659  free(rv);
660  }
661  return 0;
662  }
663 
664  return rv;
665 }
666 
667 void bfdFillAdditionalFunctionsInfo(BacktraceFunctionInfo &func) {
668  static bool inited=0;
669  if (!inited) {
670  bfd_init();
671  inited=1;
672  }
673 
674  bfd *abfd = bfd_openr(func.fileName, 0); // a bfd object
675  if( !abfd ) {
676  return;
677  }
678 
679  // check format of the object
680  if( !bfd_check_format(abfd, bfd_object) ) {
681  bfd_close(abfd);
682  return;
683  }
684 
685  // load symbol table
686  asymbol **syms= bfdLoadSymtab(abfd);
687  if(!syms) {
688  bfd_close(abfd);
689  return;
690  }
691 
692  // found source file and line for given address
693  for (asection *sect = abfd->sections; sect != NULL; sect = sect->next) {
694 
695  if (bfd_get_section_flags(abfd, sect) & SEC_ALLOC) {
696  bfd_vma sectStart = bfd_get_section_vma(abfd, sect);
697  bfd_vma sectEnd = sectStart + bfd_section_size(abfd, sect);
698  if (sectStart <= func.offset && func.offset < sectEnd) {
699  bfd_vma sectOffset = func.offset - sectStart;
700  const char* functionName;
701  const char* sourceName;
702  unsigned sourceLine;
703  if (bfd_find_nearest_line(abfd, sect, syms, sectOffset,
704  &sourceName, &functionName, &sourceLine))
705  {
706  func.sourceName = sourceName;
707  func.sourceLine = sourceLine;
708  if(func.functionName.isEmpty()) {
709  func.functionName = TQString::fromAscii(functionName);
710  }
711  break;
712  }
713  }
714  }
715  }
716 #ifdef HAVE_DEMANGLE_H
717  if(func.prettyName.isEmpty() && !func.functionName.isEmpty()) {
718  char *demangled = bfd_demangle(abfd, func.functionName.ascii(), DMGL_AUTO | DMGL_PARAMS);
719  if (demangled) {
720  func.prettyName = demangled;
721  free(demangled);
722  }
723  }
724 #endif // HAVE_DEMANGLE_H
725 
726  if( syms ) {
727  free(syms);
728  }
729  bfd_close(abfd);
730 }
731 
732 #endif // WITH_LIBBFD
733 
734 void fillAdditionalFunctionsInfo(BacktraceFunctionInfo &func) {
735 #ifdef WITH_LIBBFD
736  bfdFillAdditionalFunctionsInfo(func);
737 #endif // WITH_LIBBFD
738 
739 #ifdef HAVE_ABI_CXA_DEMANGLE
740  if(func.prettyName.isEmpty() && !func.functionName.isEmpty()) {
741  int status=0;
742  char *demangled = abi::__cxa_demangle(func.functionName.ascii(), 0, 0, &status);
743  if (demangled) {
744  func.prettyName = demangled;
745  free(demangled);
746  }
747  }
748 #endif // HAVE_ABI_CXA_DEMANGLE
749 
750 }
751 
752 TQString formatBacktrace(void *addr) {
753  TQString rv;
754  BacktraceFunctionInfo func;
755  func.addr = addr;
756 
757  // NOTE: if somebody would compile for some non-linux-glibc platform
758  // check if dladdr function is avalible there
759  Dl_info info;
760  dladdr(func.addr, &info); // obtain information about the function.
761 
762  func.fileName = info.dli_fname;
763  func.base = info.dli_fbase;
764  func.offset = (uintptr_t)func.addr - (uintptr_t)func.base;
765  func.functionName = TQString::fromAscii(info.dli_sname);
766  func.sourceLine = 0;
767 
768  fillAdditionalFunctionsInfo(func);
769 
770  rv.sprintf("0x%0*lx", (int) sizeof(void*)*2, (uintptr_t) func.addr);
771 
772  rv += " in ";
773  if (!func.prettyName.isEmpty()) {
774  rv += func.prettyName;
775  } else if (!func.functionName.isEmpty()) {
776  rv += func.functionName;
777  } else {
778  rv += "??";
779  }
780 
781  if (!func.sourceName.isEmpty()) {
782  rv += " in ";
783  rv += func.sourceName;
784  rv += ":";
785  rv += func.sourceLine ? TQString::number(func.sourceLine) : "??";
786  } else if (func.fileName && func.fileName[0]) {
787  rv += TQString().sprintf(" from %s:0x%08lx",func.fileName, func.offset);
788  } else {
789  rv += " from ??";
790  }
791 
792  return rv;
793 }
794 #endif // HAVE_BACKTRACE
795 
796 
797 TQString kdBacktrace(int levels)
798 {
799  TQString rv;
800 #ifdef HAVE_BACKTRACE
801  if (levels < 0 || levels > 256 ) {
802  levels = 256;
803  }
804 
805  rv = "[\n";
806 
807  if (levels) {
808 #ifdef HAVE_ALLOCA
809  void** trace = (void**)alloca(levels * sizeof(void*));
810 #else // HAVE_ALLOCA
811  void* trace[256];
812 #endif // HAVE_ALLOCA
813  levels = backtrace(trace, levels);
814 
815  if (levels) {
816  for (int i = 0; i < levels; ++i) {
817  rv += QString().sprintf("#%-2d ", i);
818  rv += formatBacktrace(trace[i]);
819  rv += '\n';
820  }
821  } else {
822  rv += "backtrace() failed\n";
823  }
824  }
825 
826  rv += "]\n";
827 #endif // HAVE_BACKTRACE
828  return rv;
829 }
830 
831 // Keep for ABI compatability for some time
832 // FIXME remove this (2013-08-18, 18:09, Fat-Zer)
833 TQString kdBacktrace()
834 {
835  return kdBacktrace(-1 /*all*/);
836 }
837 
838 void kdBacktraceFD(int fd) {
839 #ifdef HAVE_BACKTRACE
840  void *trace[256];
841  int levels;
842 
843  levels = backtrace(trace, 256);
844  if (levels) {
845  backtrace_symbols_fd(trace, levels, fd);
846  }
847 #endif // HAVE_BACKTRACE
848 }
849 void kdClearDebugConfig()
850 {
851  if (kDebug_data) {
852  delete kDebug_data->config;
853  kDebug_data->config = 0;
854  }
855 }
856 
857 
858 // Needed for --enable-final
859 #ifdef NDEBUG
860 #define kdDebug kndDebug
861 #endif
TDEConfig
Access KDE Configuration entries.
Definition: tdeconfig.h:43
KURL
Represents and parses a URL.
Definition: kurl.h:127
TDEGlobal::kdBacktrace
TQString kdBacktrace(int levels=-1)
Definition: kdebug.cpp:797
TDEStdAccel::key
int key(StdAccel id)
Definition: tdestdaccel.cpp:383
kdbgstream::flush
void flush()
Flushes the output.
Definition: kdebug.cpp:385
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
TDEGlobal::kdError
kdbgstream kdError(int area=0)
Definition: kdebug.cpp:372
kdbgstream
kdbgstream is a text stream that allows you to print debug messages.
Definition: kdebug.h:80
TDEGlobal::kdDebug
kdbgstream kdDebug(int area=0)
Definition: kdebug.cpp:369
TDELocale::i18n
TQString i18n(const char *text)
Definition: tdelocale.cpp:1976
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to TDEGlobal.
Definition: kstaticdeleter.h:85
tdelocale.h
TDEGlobal::instance
static TDEInstance * instance()
Returns the global instance.
Definition: tdeglobal.cpp:102
TDEGlobal::kdWarning
kdbgstream kdWarning(int area=0)
Definition: kdebug.cpp:374
KDebugDCOPIface
DCOP interface to KDebug.
Definition: kdebugdcopiface.h:30
kdbgstream::form
kdbgstream & form(const char *format,...)
Prints the string format which can contain printf-style formatted values.
Definition: kdebug.cpp:392
TDEGlobal::kdBacktraceFD
void kdBacktraceFD(int fd=2)
Definition: kdebug.cpp:838
TDEInstance::instanceName
TQCString instanceName() const
Returns the name of the instance.
Definition: kinstance.cpp:320
TDEGlobal::kdFatal
kdbgstream kdFatal(int area=0)
Definition: kdebug.cpp:376
TDEGlobal::kdClearDebugConfig
void kdClearDebugConfig()
Definition: kdebug.cpp:849
TDEStandardDirs::locate
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
Definition: kstandarddirs.cpp:1689
kdbgstream::operator<<
kdbgstream & operator<<(bool i)
Prints the given value.
Definition: kdebug.h:99
TDEGlobal::unregisterStaticDeleter
static void unregisterStaticDeleter(KStaticDeleterBase *d)
Unregisters a static deleter.
Definition: tdeglobal.cpp:198
KURL::prettyURL
TQString prettyURL(int _trailing=0) const
Returns the URL as string in human-friendly format.
Definition: kurl.cpp:1559

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.