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
//
// This file is part of the aMule Project.
//
// Copyright (c) 2005-2011 Mikkel Schubert ( xaignar@users.sourceforge.net )
// Copyright (c) 2005-2011 aMule Team ( admin@amule.org / http://www.amule.org )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
//

#include <cstdlib>			// Needed for std::abort()

#include "config.h"			// Needed for HAVE_CXXABI and HAVE_EXECINFO

#include "MuleDebug.h"			// Interface declaration
#include "StringFunctions.h"		// Needed for unicode2char
#include "Format.h"			// Needed for CFormat

#ifdef HAVE_EXECINFO
#	include <execinfo.h>
#	include <wx/utils.h>		// Needed for wxArrayString
#	ifndef HAVE_BFD
#		include <wx/thread.h>	// Needed for wxThread
#	endif
#endif

#ifdef HAVE_CXXABI
#	ifdef HAVE_TYPEINFO
#		include <typeinfo>	// Needed for some MacOSX versions with broken system headers
#	endif
#	include <cxxabi.h>
#endif


#if wxUSE_STACKWALKER && defined(__WINDOWS__)
	#include <wx/stackwalk.h> // Do_not_auto_remove
#elif defined(HAVE_BFD)
	#include <ansidecl.h> // Do_not_auto_remove
	#include <bfd.h> // Do_not_auto_remove
#endif

#include <vector>


/**
 * This functions displays a verbose description of
 * any unhandled exceptions that occur and then
 * terminate the program by raising SIGABRT.
 */
void OnUnhandledException()
{
	// Revert to the original exception handler, to avoid
	// infinate recursion, in case something goes wrong in
	// this function.
	std::set_terminate(std::abort);

#ifdef HAVE_CXXABI
	std::type_info *t = __cxxabiv1::__cxa_current_exception_type();
	FILE* output = stderr;
#else
	FILE* output = stdout;
	bool t = true;<--- Assignment 't=true', assigned value is 1
#endif
	if (t) {<--- Condition 't' is always true
		int status = -1;<--- Assignment 'status=-1', assigned value is -1
		char *dem = 0;
#ifdef HAVE_CXXABI
		// Note that "name" is the mangled name.
		char const *name = t->name();

		dem = __cxxabiv1::__cxa_demangle(name, 0, 0, &status);
#else
		const char* name = "Unknown";
#endif
		fprintf(output, "\nTerminated after throwing an instance of '%s'\n", (status ? name : dem));<--- Condition 'status' is always true
		free(dem);

		try {
			throw;<--- Rethrowing current exception with 'throw;', it seems there is no current exception to rethrow. If there is no current exception this calls std::terminate(). More: https://isocpp.org/wiki/faq/exceptions#throw-without-an-object
		} catch (const std::exception& e) {
			fprintf(output, "\twhat(): %s\n", e.what());
		} catch (const CMuleException& e) {
			fprintf(output, "\twhat(): %s\n", (const char*)unicode2char(e.what()));
		} catch (const wxString& e) {
			fprintf(output, "\twhat(): %s\n", (const char*)unicode2char(e));
		} catch (...) {
			// Unable to retrieve cause of exception
		}

		fprintf(output, "\tbacktrace:\n%s\n", (const char*)unicode2char(get_backtrace(1)));
	}
	std::abort();
}


void InstallMuleExceptionHandler()
{
	std::set_terminate(OnUnhandledException);
}


// Make it 1 for getting the file path also
#define TOO_VERBOSE_BACKTRACE 0

#if wxUSE_STACKWALKER && defined(__WINDOWS__)

// Derived class to define the actions to be done on frame print.
// I was tempted to name it MuleSkyWalker
class MuleStackWalker : public wxStackWalker
{
public:
	MuleStackWalker() {};
	~MuleStackWalker() {};

	void OnStackFrame(const wxStackFrame& frame)
	{
		wxString btLine = CFormat(wxT("[%u] ")) % frame.GetLevel();
		wxString filename = frame.GetName();

		if (!filename.IsEmpty()) {
			btLine += filename + wxT(" (") +
#if TOO_VERBOSE_BACKTRACE
			        frame.GetModule()
#else
				frame.GetModule().AfterLast(wxT('/'))
#endif
			        + wxT(")");
		} else {
			btLine += CFormat(wxT("%p")) % frame.GetAddress();
		}

		if (frame.HasSourceLocation()) {
			btLine += wxT(" at ") +
#if TOO_VERBOSE_BACKTRACE
			        frame.GetFileName()
#else
				frame.GetFileName().AfterLast(wxT('/'))
#endif
			        + CFormat(wxT(":%u")) % frame.GetLine();
		} else {
			btLine += wxT(" (Unknown file/line)");
		}

		//! Contains the entire backtrace
		m_trace += btLine + wxT("\n");
	}

	wxString m_trace;
};


wxString get_backtrace(unsigned n)
{
	MuleStackWalker walker; // Texas ranger?
	walker.Walk(n); // Skip this one and Walk() also!

	return walker.m_trace;
}

#elif defined(__LINUX__)

#ifdef HAVE_BFD

static bfd* s_abfd;
static asymbol** s_symbol_list;
static bool s_have_backtrace_symbols = false;
static const char* s_file_name;
static const char* s_function_name;
static unsigned int s_line_number;
static int s_found;


/*
* read all symbols in the executable into an array
* and return the pointer to the array in symbol_list.
* Also return the number of actual symbols read
* If there's any error, return -1
*/
static int get_backtrace_symbols(bfd *abfd, asymbol ***symbol_list_ptr)
{
	int vectorsize = bfd_get_symtab_upper_bound(abfd);

	if (vectorsize < 0) {
		fprintf (stderr, "Error while getting vector size for backtrace symbols : %s",
			bfd_errmsg(bfd_get_error()));
		return -1;
	}

	if (vectorsize == 0) {
		fprintf (stderr, "Error while getting backtrace symbols : No symbols (%s)",
			bfd_errmsg(bfd_get_error()));
		return -1;
	}

	*symbol_list_ptr = (asymbol**)malloc(vectorsize);

	if (*symbol_list_ptr == NULL) {
		fprintf (stderr, "Error while getting backtrace symbols : Cannot allocate memory");
		return -1;
	}

	vectorsize = bfd_canonicalize_symtab(abfd, *symbol_list_ptr);

	if (vectorsize < 0) {
		fprintf(stderr, "Error while getting symbol table : %s",
			bfd_errmsg(bfd_get_error()));
		return -1;
	}

	return vectorsize;
}


/*
* print file, line and function information for address
* The info is actually set into global variables. This
* function is called from the iterator bfd_map_over_sections
*
*/
void init_backtrace_info()
{
	bfd_init();
	s_abfd = bfd_openr("/proc/self/exe", NULL);

	if (s_abfd == NULL) {
		fprintf(stderr, "Error while opening file for backtrace symbols : %s",
			bfd_errmsg(bfd_get_error()));
		return;
	}

	if (!(bfd_check_format_matches(s_abfd, bfd_object, NULL))) {
		fprintf (stderr, "Error while init. backtrace symbols : %s",
			bfd_errmsg (bfd_get_error ()));
		bfd_close(s_abfd);
		return;
	}

	s_have_backtrace_symbols = (get_backtrace_symbols(s_abfd, &s_symbol_list) > 0);
}


void get_file_line_info(bfd *abfd, asection *section, void* _address)
{
	wxASSERT(s_symbol_list);

	if (s_found) {
		return;
	}

	if ((section->flags & SEC_ALLOC) == 0) {
		return;
	}

	bfd_vma vma = section->vma;

	unsigned long address = (unsigned long)_address;
	if (address < vma) {
		return;
	}

	bfd_size_type size = section->size;
	if (address > (vma + size)) {
		return;
	}

	s_found =  bfd_find_nearest_line(abfd, section, s_symbol_list,
		address - vma, &s_file_name, &s_function_name, &s_line_number);
}

#endif // HAVE_BFD

static wxString demangle(const wxString& function)
{
#ifdef HAVE_CXXABI
	wxString result;

	if (function.Mid(0,2) == wxT("_Z")) {
		int status;
		char *demangled = abi::__cxa_demangle(function.mb_str(), NULL, NULL, &status);

		if (!status) {
			result = wxConvCurrent->cMB2WX(demangled);
		}

		if (demangled) {
			free(demangled);
		}
	}

	return result;
#else
	return wxEmptyString;
#endif
}


// Print a stack backtrace if available
wxString get_backtrace(unsigned n)
{
#ifdef HAVE_EXECINFO
	// (stkn) create backtrace
	void *bt_array[100];	// 100 should be enough ?!?
	char **bt_strings;
	int num_entries;

	if ((num_entries = backtrace(bt_array, 100)) < 0) {
		fprintf(stderr, "* Could not generate backtrace\n");
		return wxEmptyString;
	}

	if ((bt_strings = backtrace_symbols(bt_array, num_entries)) == NULL) {
		fprintf(stderr, "* Could not get symbol names for backtrace\n");
		return wxEmptyString;
	}

	std::vector<wxString> libname(num_entries);
	std::vector<wxString> funcname(num_entries);
	std::vector<wxString> address(num_entries);
	wxString AllAddresses;

	for (int i = 0; i < num_entries; ++i) {
		wxString wxBtString = wxConvCurrent->cMB2WX(bt_strings[i]);
		int posLPar = wxBtString.Find(wxT('('));
		int posRPar = wxBtString.Find(wxT(')'));
		int posLBra = wxBtString.Find(wxT('['));
		int posRBra = wxBtString.Find(wxT(']'));
		bool hasFunction = true;
		if (posLPar == -1 || posRPar == -1) {
			if (posLBra == -1 || posRBra == -1) {
				/* It is important to have exactly num_entries
				* addresses in AllAddresses */
				AllAddresses += wxT("0x0000000 ");
				continue;
			}
			posLPar = posLBra;
			hasFunction = false;
		}
		/* Library name */
		int len = posLPar;
		libname[i] = wxBtString.Mid(0, len);
		/* Function name */
		if (hasFunction) {
			int posPlus = wxBtString.Find(wxT('+'), true);
			if (posPlus == -1)
				posPlus = posRPar;
			len = posPlus - posLPar - 1;
			funcname[i] = wxBtString.Mid(posLPar + 1, len);
			wxString demangled = demangle(funcname[i]);
			if (!demangled.IsEmpty()) {
				funcname[i] = demangled;
			}
		}
		/* Address */
		if ( posLBra == -1 || posRBra == -1) {
			address[i] = wxT("0x0000000");
		} else {
			len = posRBra - posLBra - 1;
			address[i] = wxBtString.Mid(posLBra + 1, len);
			AllAddresses += address[i] + wxT(" ");
		}
	}
	free(bt_strings);

	/* Get line numbers from addresses */
	wxArrayString out;
	bool hasLineNumberInfo = false;

#ifdef HAVE_BFD
	if (!s_have_backtrace_symbols) {
		init_backtrace_info();
		wxASSERT(s_have_backtrace_symbols);
	}

	for (int i = 0; i < num_entries; ++i) {
		s_file_name = NULL;
		s_function_name = NULL;
		s_line_number = 0;
		s_found = false ;

		unsigned long addr;
		address[i].ToULong(&addr,0); // As it's "0x" prepended, wx will read it as base 16. Hopefully.

		bfd_map_over_sections(s_abfd, get_file_line_info, (void*)addr);

		if (s_found) {
			wxString function = wxConvCurrent->cMB2WX(s_function_name);
			wxString demangled = demangle(function);
			if (!demangled.IsEmpty()) {
				function = demangled;
				funcname[i] = demangled;
			}
			out.Insert(wxConvCurrent->cMB2WX(s_function_name),i*2);
			out.Insert(CFormat(wxT("%s:%u")) % wxString(wxConvCurrent->cMB2WX(s_file_name)) % s_line_number, i*2+1);
		} else {
			out.Insert(wxT("??"),i*2);
			out.Insert(wxT("??"),i*2+1);
		}
	}

	hasLineNumberInfo = true;<--- Assignment 'hasLineNumberInfo=true', assigned value is 1<--- Assignment 'hasLineNumberInfo=true', assigned value is 1<--- Assignment 'hasLineNumberInfo=true', assigned value is 1

#else	/* !HAVE_BFD */
	if (wxThread::IsMain()) {
		wxString command;
		command << wxT("addr2line -C -f -s -e /proc/") <<
		getpid() << wxT("/exe ") << AllAddresses;
		// The output of the command is this wxArrayString, in which
		// the even elements are the function names, and the odd elements
		// are the line numbers.

		hasLineNumberInfo = wxExecute(command, out) != -1;
	}

#endif	/* HAVE_BFD / !HAVE_BFD */

	wxString trace;
	// Remove 'n+1' first entries (+1 because of this function)
	for (int i = n+1; i < num_entries; ++i) {
		/* If we have no function name, use the result from addr2line */
		if (funcname[i].IsEmpty()) {
			if (hasLineNumberInfo) {<--- Condition 'hasLineNumberInfo' is always true
				funcname[i] = out[2*i];
			} else {
				funcname[i] = wxT("??");
			}
		}
		wxString btLine;
		btLine << wxT("[") << i << wxT("] ") << funcname[i] << wxT(" in ");
		/* If addr2line did not find a line number, use bt_string */
		if (!hasLineNumberInfo || out[2*i+1].Mid(0,2) == wxT("??")) {<--- Condition '!hasLineNumberInfo' is always false<--- Assuming that condition '!hasLineNumberInfo' is not redundant
			btLine += libname[i] + wxT("[") + address[i] + wxT("]");
		} else if (hasLineNumberInfo) {<--- Condition 'hasLineNumberInfo' is always true<--- Condition 'hasLineNumberInfo' is always true
#if TOO_VERBOSE_BACKTRACE
			btLine += out[2*i+1];
#else
			btLine += out[2*i+1].AfterLast(wxT('/'));
#endif
		} else {
			btLine += libname[i];
		}

		trace += btLine + wxT("\n");
	}

	return trace;
#else /* !HAVE_EXECINFO */
	fprintf(stderr, "--== cannot generate backtrace ==--\n\n");
	return wxEmptyString;
#endif /* HAVE_EXECINFO */
}

#elif defined( __APPLE__ )

// According to sources, parts of this code originate at http://www.tlug.org.za/wiki/index.php/Obtaining_a_stack_trace_in_C_upon_SIGSEGV
// which doesn't exist anymore.

// Other code (stack frame list and demangle related) has been modified from code with license:

// Copyright 2007 Edd Dawson.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <string>
#include <dlfcn.h>
#include <cxxabi.h>
#include <sstream>

class stack_frame
{
public:
	stack_frame(const void * f_instruction, const std::string& f_function) : frame_instruction(f_instruction), frame_function(f_function) {};

	const void *instruction() const { return frame_instruction; }
	const std::string& function() const { return frame_function; }

private:
	const void * frame_instruction;
	const std::string frame_function;
};

std::string demangle(const char *name)
{
	int status = 0;
	char *d = 0;
	std::string ret = name;
	try {
		if ((d = abi::__cxa_demangle(name, 0, 0, &status))) {
			ret = d;
		}
	} catch(...) {  }

	std::free(d);
	return ret;
}


void fill_frames(std::list<stack_frame> &frames)
{
	try {
		void **fp = (void **) __builtin_frame_address (1);
		void *saved_pc = NULL;

		// First frame is skipped
		while (fp != NULL) {<--- Assuming that condition 'fp!=NULL' is not redundant<--- Assuming that condition 'fp!=NULL' is not redundant<--- Assuming that condition 'fp!=NULL' is not redundant
			fp = (void**)(*fp);
			if (*fp == NULL) {<--- Null pointer dereference
				break;
			}

#if defined(__i386__)
			saved_pc = fp[1];<--- Null pointer dereference
#elif defined(__ppc__)
			saved_pc = *(fp + 2);
#else
			// ?
			saved_pc = *(fp + 2);<--- Null pointer addition
#endif
			if (saved_pc) {
				Dl_info info;

				if (dladdr(saved_pc, &info)) {
					frames.push_back(stack_frame(saved_pc, demangle(info.dli_sname) + " in " + info.dli_fname));
				}
			}
		}
	} catch (...) {
		// Nothing to be done here, just leave.
	}
}

wxString get_backtrace(unsigned n)
{
	std::list<stack_frame> frames;
	fill_frames(frames);
	std::ostringstream backtrace;
	std::list<stack_frame>::iterator it = frames.begin();

	int count = 0;
	while (it != frames.end()) {
		if (count >= n) {
			backtrace << (*it).instruction() << " : " << (*it).function() << std::endl;
			++it;
		}

		++count;
	}

	return wxString(backtrace.str().c_str(), wxConvUTF8);
}

#else /* ! __APPLE__ */

wxString get_backtrace(unsigned WXUNUSED(n))
{
	return wxT("--== no BACKTRACE for your platform ==--\n\n");
}

#endif /* !__LINUX__ */

void print_backtrace(unsigned n)
{
	wxString trace = get_backtrace(n);

	// This is because the string is ansi anyway, and the conv classes are very slow
	fprintf(stderr, "%s\n", (const char*)unicode2char(trace));
}

// File_checked_for_headers