File: platform_util.cpp

package info (click to toggle)
megaglest 3.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,844 kB
  • ctags: 18,191
  • sloc: cpp: 144,280; ansic: 11,861; sh: 3,233; perl: 1,904; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (349 lines) | stat: -rw-r--r-- 9,887 bytes parent folder | download | duplicates (5)
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
// ==============================================================
//This file is part of Glest Shared Library (www.glest.org)
//Copyright (C) 2005 Matthias Braun <matze@braunis.de>

//You can redistribute this code 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.
#include "platform_util.h"
#include <iostream>
#include <string.h>
#include <unistd.h>
#include "util.h"
#include "conversion.h"

// For gcc backtrace on crash!
#if defined(HAS_GCC_BACKTRACE)

#include <execinfo.h>
#include <cxxabi.h>
#include <signal.h>

#endif

#include <stdexcept>
#include "leak_dumper.h"

using namespace Shared::Util;
using namespace std;

namespace Shared{ namespace Platform{

string PlatformExceptionHandler::application_binary="";
bool PlatformExceptionHandler::disableBacktrace = false;

const char * getDialogCommand() {

/*
	if (::system(NULL)) {
	    if (::system("which gdialog") == 0)
		    return "gdialog";
	    else if (::system("which kdialog") == 0)
		    return "kdialog";
	}
*/
	FILE *file;
	char file_string [100];

	file = popen("which zenity","r");
	//printf("File #1 [%p]\n",file);
	if (file != NULL) {
		if (fgets(file_string, 100, file) != NULL ) {
			pclose(file);
			return "zenity";
		}
		else {
			pclose(file);
		}
	}

	file = popen("which kdialog","r");
	//printf("File #2 [%p]\n",file);
	if (file != NULL) {
		if (fgets(file_string, 100, file) != NULL ) {
			pclose(file);
			return "kdialog";
		}
		else {
			pclose(file);
		}
	}

	file = popen("which yad","r");
	//printf("File #3 [%p]\n",file);
	if (file != NULL) {
		if (fgets(file_string, 100, file) != NULL ) {
			pclose(file);
			return "yad";
		}
		else {
			pclose(file);
		}
	}

	file = popen("which gdialog","r");
	//printf("File #4 [%p]\n",file);
	if (file != NULL) {
		if (fgets(file_string, 100, file) != NULL ) {
			pclose(file);
			return "gdialog";
		}
		else {
			pclose(file);
		}
	}

	return NULL;
}

bool showMessage(std::string warning,string writepath) {
	bool guiMessage = false;
	const char * dialogCommand = getDialogCommand();
	if (dialogCommand) {
		std::string command = dialogCommand;

		string text_file = writepath + "/mg_dialog_text.txt";
		{
			FILE *fp = fopen(text_file.c_str(),"wt");
			if (fp != NULL) {
				fputs(warning.c_str(),fp);
				fclose(fp);
			}
		}

		//command += " --title \"Error\" --msgbox \"`printf \"" + warning + "\"`\"";
		command += " --title \"Error\" ";

		if (!strcmp(dialogCommand, "kdialog")) {
		    command += "--textbox " + text_file + " 640 360";
		}
		else if (!strcmp(dialogCommand, "yad")) {
		    command += "--text-info --center --wrap --width 640 --height 360 --filename=" + text_file;
		}
		else {
		    command += "--text-info --width 640 --height 360 --filename=" + text_file;
		}

		//printf("\n\n\nzenity command [%s]\n\n\n",command.c_str());

		FILE *fp = popen(command.c_str(),"r");
		if (fp != 0) {
			guiMessage = true;
			pclose(fp);
		}
	}

	return guiMessage;
}

void message(string message, bool isNonGraphicalModeEnabled,string writepath) {
	std::cerr << "\n\n\n";
	std::cerr << "******************************************************\n";
	std::cerr << "    " << message << "\n";
	std::cerr << "******************************************************\n";
	std::cerr << "\n\n\n";

	if(isNonGraphicalModeEnabled == false) {
		showMessage(message,writepath);
	}
}

void exceptionMessage(const exception &excp) {
	std::cerr << "Exception: " << excp.what() << std::endl;
}

#if defined(HAS_GCC_BACKTRACE)
static int getFileAndLine(char *function, void *address, char *file, size_t flen) {
        int line=-1;
        if(PlatformExceptionHandler::application_binary != "") {
			const int maxbufSize = 8096;
			char buf[maxbufSize+1]="";

			// prepare command to be executed
			// our program need to be passed after the -e parameter
#if __APPLE_CC__
			snprintf(buf, 8096,"xcrun atos -v -o %s %p",PlatformExceptionHandler::application_binary.c_str(),address);
            fprintf(stderr,"> %s\n",buf);
#else
			snprintf(buf, 8096,"addr2line -C -e %s -f -i %p",PlatformExceptionHandler::application_binary.c_str(),address);
#endif
			FILE* f = popen (buf, "r");
			if (f == NULL) {
				perror (buf);
				return 0;
			}
#if __APPLE_CC__
            //### TODO Will: still working this out
            int len = fread(buf,1,maxbufSize,f);
            pclose(f);
            buf[len] = 0;
            fprintf(stderr,"< %s",buf);
            return -1;
#endif
            for(;function != NULL && function[0] != '\0';) {
                // get function name
                char *ret = fgets (buf, maxbufSize, f);
                if(ret == NULL) {
                    pclose(f);
                    return line;
                }
                //printf("Looking for [%s] Found [%s]\n",function,ret);
                if(strstr(ret,function) != NULL) {
                    break;
                }
                // get file and line
                ret = fgets (buf, maxbufSize, f);
                if(ret == NULL) {
                    pclose(f);
                    return line;
                }

                if(strlen(buf) > 0 && buf[0] != '?') {
                    //int l;
                    char *p = buf;
                    // file name is until ':'
                    while(*p != 0 && *p != ':') {
                        p++;
                    }

                    *p++ = 0;
                    // after file name follows line number
                    strcpy (file , buf);
                    sscanf (p,"%10d", &line);
                }
                else {
                    strcpy (file,"unknown");
                    line = 0;
                }
            }

			// get file and line
			char *ret = fgets (buf, maxbufSize, f);
			if(ret == NULL) {
				pclose(f);
				return line;
			}

			if(strlen(buf) > 0 && buf[0] != '?') {
				//int l;
				char *p = buf;

				// file name is until ':'
				while(*p != 0 && *p != ':') {
					p++;
				}

				*p++ = 0;
				// after file name follows line number
				strcpy (file , buf);
				sscanf (p,"%10d", &line);
			}
			else {
				strcpy (file,"unknown");
				line = 0;
			}
			pclose(f);
        }

        return line;
}
#endif

string PlatformExceptionHandler::getStackTrace() {
	string errMsg = "\nStack Trace:\n";
	 if(PlatformExceptionHandler::disableBacktrace == true) {
		 errMsg += "disabled...";
		 return errMsg;
	 }

#if defined(HAS_GCC_BACKTRACE)
//        if(disableBacktrace == false && sdl_quitCalled == false) {
        //errMsg = "\nStack Trace:\n";
        //errMsg += "To find line #'s use:\n";
        //errMsg += "readelf --debug-dump=decodedline %s | egrep 0xaddress-of-stack\n";

        const size_t max_depth = 25;
        void *stack_addrs[max_depth];
        size_t stack_depth = backtrace(stack_addrs, max_depth);
        char **stack_strings = backtrace_symbols(stack_addrs, stack_depth);
        //for (size_t i = 1; i < stack_depth; i++) {
        //    errMsg += string(stack_strings[i]) + "\n";
        //}

        if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

        char szBuf[8096]="";
        string strBuf = "";
        for(size_t i = 1; i < stack_depth; i++) {
            void *lineAddress = stack_addrs[i]; //getStackAddress(stackIndex);

            size_t sz = 8096; // just a guess, template names will go much wider
            char *function = static_cast<char *>(malloc(sz));
            function[0] = '\0';
            char *begin = 0;
            char *end = 0;

            // find the parentheses and address offset surrounding the mangled name
            for (char *j = stack_strings[i]; *j; ++j) {
                if (*j == '(') {
                    begin = j;
                }
                else if (*j == '+') {
                    end = j;
                }
            }
            if (begin && end) {
                *begin++ = '\0';
                *end = '\0';
                // found our mangled name, now in [begin, end)

                int status;
                char *ret = abi::__cxa_demangle(begin, function, &sz, &status);
                if (ret) {
                    // return value may be a realloc() of the input
                    function = ret;
                }
                else {
                    // demangling failed, just pretend it's a C function with no args
                    strncpy(function, begin, sz);
                    strncat(function, "()", sz);
                    function[sz-1] = '\0';
                }
                //fprintf(out, "    %s:%s\n", stack.strings[i], function);

                strBuf = string(stack_strings[i]) + ":" + string(function);
                snprintf(szBuf,8096,"address [%p]",lineAddress);
                strBuf += szBuf;
            }
            else {
                // didn't find the mangled name, just print the whole line
                //fprintf(out, "    %s\n", stack.strings[i]);
            	strBuf = stack_strings[i];
                snprintf(szBuf,8096,"address [%p]",lineAddress);
                strBuf += szBuf;
            }

            //errMsg += string(szBuf);
            errMsg += strBuf;
            char file[8096]="";
            int line = getFileAndLine(function, lineAddress, file, 8096);
            if(line >= 0) {
                errMsg += " line: " + intToStr(line);
            }
            errMsg += "\n";

            free(function);
        }

        free(stack_strings); // malloc()ed by backtrace_symbols
#endif

	return errMsg;
}

megaglest_runtime_error::megaglest_runtime_error(const string& __arg,bool noStackTrace)
	: std::runtime_error(noStackTrace == false ? __arg + PlatformExceptionHandler::getStackTrace() : __arg), noStackTrace(noStackTrace) {
}

}}//end namespace