Merge commit 'dm/experimental' into vuplus_experimental
[vuplus_dvbapp] / main / bsod.cpp
1 #include <string.h>
2 #include <signal.h>
3 #include <asm/ptrace.h>
4
5 #include <lib/base/eerror.h>
6 #include <lib/base/smartptr.h>
7 #include <lib/base/nconfig.h>
8 #include <lib/gdi/grc.h>
9 #include <lib/gdi/gfbdc.h>
10 #ifdef WITH_SDL
11 #include <lib/gdi/sdl.h>
12 #endif
13
14 #include "version.h"
15
16 /************************************************/
17
18 #ifdef BUILD_VUPLUS /* ikseong  */
19 #define CRASH_EMAILADDR "crashlog"
20 #else
21 #define CRASH_EMAILADDR "crashlog@dream-multimedia-tv.de"
22 #endif
23 #define STDBUFFER_SIZE 512
24 #define RINGBUFFER_SIZE 16384
25 static char ringbuffer[RINGBUFFER_SIZE];
26 static int ringbuffer_head;
27
28 static void addToLogbuffer(const char *data, int len)
29 {
30         while (len)
31         {
32                 int remaining = RINGBUFFER_SIZE - ringbuffer_head;
33         
34                 if (remaining > len)
35                         remaining = len;
36         
37                 memcpy(ringbuffer + ringbuffer_head, data, remaining);
38                 len -= remaining;
39                 data += remaining;
40                 ringbuffer_head += remaining;
41                 if (ringbuffer_head >= RINGBUFFER_SIZE)
42                         ringbuffer_head = 0;
43         }
44 }
45
46 static std::string getLogBuffer()
47 {
48         int begin = ringbuffer_head;
49         while (ringbuffer[begin] == 0)
50         {
51                 ++begin;
52                 if (begin == RINGBUFFER_SIZE)
53                         begin = 0;
54                 if (begin == ringbuffer_head)
55                         return "";
56         }
57         if (begin < ringbuffer_head)
58                 return std::string(ringbuffer + begin, ringbuffer_head - begin);
59         else
60         {
61                 return std::string(ringbuffer + begin, RINGBUFFER_SIZE - begin) + std::string(ringbuffer, ringbuffer_head);
62         }
63 }
64
65 static void addToLogbuffer(int level, const std::string &log)
66 {
67         addToLogbuffer(log.c_str(), log.size());
68 }
69
70 static std::string getConfigFileValue(const char *entry)
71 {
72         std::string configfile = "/etc/enigma2/settings";
73         std::string configvalue;
74         if (entry)
75         {
76                 ePythonConfigQuery::getConfigValue(entry, configvalue);
77                 if (configvalue != "") //we get at least the default value if python is still alive
78                 {
79                         return configvalue;
80                 }
81                 else // get value from enigma2 settings file
82                 {
83                         FILE *f = fopen(configfile.c_str(), "r");
84                         if (!f)
85                         {
86                                 return "Error";
87                         }
88                         while (1)
89                         {
90                                 char line[1024];
91                                 if (!fgets(line, 1024, f))
92                                         break;
93                                 if (!strncmp(line, entry, strlen(entry) ))
94                                 {
95                                         if (strlen(line) && line[strlen(line)-1] == '\r')
96                                                 line[strlen(line)-1] = 0;
97                                         if (strlen(line) && line[strlen(line)-1] == '\n')
98                                                 line[strlen(line)-1] = 0;
99                                         std::string tmp = line;
100                                         int posEqual = tmp.find("=", 0);
101                                         configvalue = tmp.substr(posEqual+1);
102                                 }
103                         }
104                         fclose(f);
105                         return configvalue;
106                 }
107         }
108 }
109
110 static std::string getFileContent(const char *file)
111 {
112         std::string filecontent;
113
114         if (file)
115         {
116                 FILE *f = fopen(file, "r");
117                 if (!f)
118                 {
119                         return "Error";
120                 }
121                 while (1)
122                 {
123                         char line[1024];
124                         if (!fgets(line, 1024, f))
125                                 break;
126                         std::string tmp = line;
127                         std::string password;
128                         int pwdpos = tmp.find(".password=", 0);
129                         if( pwdpos != std::string::npos)
130                         {
131                                 filecontent += tmp.substr(0,pwdpos +10);
132                                 for ( int pos = pwdpos +10; pos < tmp.length()-1; ++pos )
133                                 {
134                                         filecontent += "X";
135                                 }
136                                 filecontent += "\n";
137                         }
138                         else {
139                                 filecontent += line;
140                         }
141                 }
142                 fclose(f);
143         }
144         return filecontent;
145 }
146
147 static std::string execCommand(char* cmd) {
148         FILE* pipe = popen(cmd, "r");
149         if (!pipe)
150                 return "Error";
151         char buffer[STDBUFFER_SIZE];
152         std::string result = "";
153         while(!feof(pipe))
154         {
155                 if(!fgets(buffer,STDBUFFER_SIZE, pipe))
156                         break;
157                 result += buffer;
158         }
159         pclose(pipe);
160         return result;
161 }
162
163 extern std::string execCommand();
164 extern std::string getConfigFileValue();
165 extern std::string getFileContent();
166 extern std::string getLogBuffer();
167
168 #define INFOFILE "/maintainer.info"
169
170 void bsodFatal(const char *component)
171 {
172         char logfile[128];
173         sprintf(logfile, "/media/hdd/enigma2_crash_%u.log", (unsigned int)time(0));
174         FILE *f = fopen(logfile, "wb");
175         
176         std::string lines = getLogBuffer();
177         
178                 /* find python-tracebacks, and extract "  File "-strings */
179         size_t start = 0;
180         
181         char crash_emailaddr[256] = CRASH_EMAILADDR;
182         char crash_component[256] = "enigma2";
183
184         if (component)
185                 snprintf(crash_component, 256, component);
186         else
187         {
188                 while ((start = lines.find("\n  File \"", start)) != std::string::npos)
189                 {
190                         start += 9;
191                         size_t end = lines.find("\"", start);
192                         if (end == std::string::npos)
193                                 break;
194                         end = lines.rfind("/", end);
195                                 /* skip a potential prefix to the path */
196                         unsigned int path_prefix = lines.find("/usr/", start);
197                         if (path_prefix != std::string::npos && path_prefix < end)
198                                 start = path_prefix;
199
200                         if (end == std::string::npos)
201                                 break;
202                         if (end - start >= (256 - strlen(INFOFILE)))
203                                 continue;
204                         char filename[256];
205                         snprintf(filename, 256, "%s%s", lines.substr(start, end - start).c_str(), INFOFILE);
206                         FILE *cf = fopen(filename, "r");
207                         if (cf)
208                         {
209                                 fgets(crash_emailaddr, sizeof crash_emailaddr, cf);
210                                 if (*crash_emailaddr && crash_emailaddr[strlen(crash_emailaddr)-1] == '\n')
211                                         crash_emailaddr[strlen(crash_emailaddr)-1] = 0;
212
213                                 fgets(crash_component, sizeof crash_component, cf);
214                                 if (*crash_component && crash_component[strlen(crash_component)-1] == '\n')
215                                         crash_component[strlen(crash_component)-1] = 0;
216                                 fclose(cf);
217                         }
218                 }
219         }
220
221         if (f)
222         {
223                 time_t t = time(0);
224                 char crashtime[STDBUFFER_SIZE];
225                 sprintf(crashtime, "%s",ctime(&t));
226                 if (strlen(crashtime) && crashtime[strlen(crashtime)-1] == '\n')
227                                 crashtime[strlen(crashtime)-1] = 0;
228                 fprintf(f, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n<opendreambox>\n");
229                 fprintf(f, "\t<enigma2>\n");
230                 fprintf(f, "\t\t<crashdate>%s</crashdate>\n", crashtime);
231 #ifdef ENIGMA2_CHECKOUT_TAG
232                 fprintf(f, "\t\t<checkouttag>" ENIGMA2_CHECKOUT_TAG "</checkouttag>\n");
233 #else
234                 fprintf(f, "\t\t<compiledate>" __DATE__ "</compiledate>\n");
235 #endif
236 #ifdef ENIGMA2_CHECKOUT_ROOT
237                 fprintf(f, "\t\t<checkoutroot>" ENIGMA2_CHECKOUT_ROOT "</checkoutroot>\n");
238 #endif
239                 fprintf(f, "\t\t<contactemail>%s</contactemail>\n", crash_emailaddr);
240                 fprintf(f, "\t\t<!-- Please email this crashlog to above address -->\n");
241                 fprintf(f, "\t</enigma2>\n");
242
243                 fprintf(f, "\t<image>\n");
244                 std::string model = getFileContent("/proc/stb/info/model");
245                 if (model != "Error")
246                 {
247                         char modelname[STDBUFFER_SIZE];
248                         sprintf(modelname, "%s",model.c_str());
249                         if (strlen(modelname) && modelname[strlen(modelname)-1] == '\n')
250                                 modelname[strlen(modelname)-1] = 0;
251                         fprintf(f, "\t\t<dreamboxmodel>%s</dreamboxmodel>\n", modelname);
252                 }
253                 std::string kernel = getFileContent("/proc/cmdline");
254                 if (kernel != "Error")
255                 {
256                         char kernelcmd[STDBUFFER_SIZE];
257                         sprintf(kernelcmd, "%s",kernel.c_str());
258                         if (strlen(kernelcmd) && kernelcmd[strlen(kernelcmd)-1] == '\n')
259                                 kernelcmd[strlen(kernelcmd)-1] = 0;
260                         fprintf(f, "\t\t<kernelcmdline>%s</kernelcmdline>\n", kernelcmd);
261                 }
262                 std::string sendAnonCrashlog = getConfigFileValue("config.plugins.crashlogautosubmit.sendAnonCrashlog");
263                 if (sendAnonCrashlog == "False" || sendAnonCrashlog == "false") // defaults to true... default anonymized crashlogs
264                 {
265                         std::string ca = getFileContent("/proc/stb/info/ca");
266                         if (ca != "Error")
267                         {
268                                 char dreamboxca[STDBUFFER_SIZE];
269                                 sprintf(dreamboxca, "%s",ca.c_str());
270                                 if (strlen(dreamboxca) && dreamboxca[strlen(dreamboxca)-1] == '\n')
271                                         dreamboxca[strlen(dreamboxca)-1] = 0;
272                                 fprintf(f, "\t\t<dreamboxca>\n\t\t<![CDATA[\n%s\n\t\t]]>\n\t\t</dreamboxca>\n", dreamboxca);
273                         }
274                         std::string settings = getFileContent("/etc/enigma2/settings");
275                         if (settings != "Error")
276                         {
277                                 fprintf(f, "\t\t<enigma2settings>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2settings>\n", settings.c_str());
278                         }
279                 }
280                 std::string addNetwork = getConfigFileValue("config.plugins.crashlogautosubmit.addNetwork");
281                 if (addNetwork == "True" || addNetwork == "true")
282                 {
283                         std::string nwinterfaces = getFileContent("/etc/network/interfaces");
284                         if (nwinterfaces != "Error")
285                         {
286                                 fprintf(f, "\t\t<networkinterfaces>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</networkinterfaces>\n", nwinterfaces.c_str());
287                         }
288                         std::string dns = getFileContent("/etc/resolv.conf");
289                         if (dns != "Error")
290                         {
291                                 fprintf(f, "\t\t<dns>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</dns>\n", dns.c_str());
292                         }
293                         std::string defaultgw = getFileContent("/etc/default_gw");
294                         if (defaultgw != "Error")
295                         {
296                                 char gateway[STDBUFFER_SIZE];
297                                 sprintf(gateway, "%s",defaultgw.c_str());
298                                 if (strlen(gateway) && gateway[strlen(gateway)-1] == '\n')
299                                         gateway[strlen(gateway)-1] = 0;
300                                 fprintf(f, "\t\t<defaultgateway>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</defaultgateway>\n", gateway);
301                         }
302                 }
303                 std::string addWlan = getConfigFileValue("config.plugins.crashlogautosubmit.addWlan");
304                 if (addWlan == "True" || addWlan == "true")
305                 {
306                         std::string wpasupplicant = getFileContent("/etc/wpa_supplicant.conf");
307                         if (wpasupplicant != "Error")
308                         {
309                                 fprintf(f, "\t\t<wpasupplicant>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</wpasupplicant>\n", wpasupplicant.c_str());
310                         }
311                 }
312                 std::string imageversion = getFileContent("/etc/image-version");
313                 if (imageversion != "Error")
314                 {
315                         fprintf(f, "\t\t<imageversion>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</imageversion>\n", imageversion.c_str());
316                 }
317                 std::string imageissue = getFileContent("/etc/issue.net");
318                 if (imageissue != "Error")
319                 {
320                         fprintf(f, "\t\t<imageissue>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</imageissue>\n", imageissue.c_str());
321                 }
322                 fprintf(f, "\t</image>\n");
323
324                 fprintf(f, "\t<software>\n");
325                 std::string installedplugins = execCommand("ipkg list_installed | grep enigma2");
326                 fprintf(f, "\t\t<enigma2software>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2software>\n", installedplugins.c_str());
327                 std::string dreambox = execCommand("ipkg list_installed | grep dream");
328                 fprintf(f, "\t\t<dreamboxsoftware>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</dreamboxsoftware>\n", dreambox.c_str());
329                 std::string gstreamer = execCommand("ipkg list_installed | grep gst");
330                 fprintf(f, "\t\t<gstreamersoftware>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</gstreamersoftware>\n", gstreamer.c_str());
331                 fprintf(f, "\t</software>\n");
332
333                 fprintf(f, "\t<crashlogs>\n");
334                 std::string buffer = getLogBuffer();
335                 fprintf(f, "\t\t<enigma2crashlog>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</enigma2crashlog>\n", buffer.c_str());
336                 std::string pythonmd5 = execCommand("find /usr/lib/enigma2/python/ -name \"*.py\" | xargs md5sum");
337                 fprintf(f, "\t\t<pythonMD5sum>\n\t\t<![CDATA[\n%s\t\t]]>\n\t\t</pythonMD5sum>\n", pythonmd5.c_str());
338                 fprintf(f, "\t</crashlogs>\n");
339
340                 fprintf(f, "\n</opendreambox>\n");
341                 fclose(f);
342                 
343         }
344         
345 #ifdef WITH_SDL
346         ePtr<gSDLDC> my_dc;
347         gSDLDC::getInstance(my_dc);
348 #else
349         ePtr<gFBDC> my_dc;
350         gFBDC::getInstance(my_dc);
351 #endif
352         
353         {
354                 gPainter p(my_dc);
355                 p.resetOffset();
356                 p.resetClip(eRect(ePoint(0, 0), my_dc->size()));
357 #ifdef ENIGMA2_CHECKOUT_TAG
358                 if (ENIGMA2_CHECKOUT_TAG[0] == 'T') /* tagged checkout (release) */
359                         p.setBackgroundColor(gRGB(0x0000C0));
360                 else if (ENIGMA2_CHECKOUT_TAG[0] == 'D') /* dated checkout (daily experimental build) */
361                 {
362                         srand(time(0));
363                         int r = rand();
364                         unsigned int col = 0;
365                         if (r & 1)
366                                 col |= 0x800000;
367                         if (r & 2)
368                                 col |= 0x008000;
369                         if (r & 4)
370                                 col |= 0x0000c0;
371                         p.setBackgroundColor(gRGB(col));
372                 }
373 #else
374                         p.setBackgroundColor(gRGB(0x008000));
375 #endif
376
377                 p.setForegroundColor(gRGB(0xFFFFFF));
378         
379                 ePtr<gFont> font = new gFont("Regular", 20);
380                 p.setFont(font);
381                 p.clear();
382         
383                 eRect usable_area = eRect(100, 70, my_dc->size().width() - 150, 100);
384                 
385                 char text[512];
386 #ifdef BUILD_VUPLUS /* ikseong  */
387                 snprintf(text, 512, "We are really sorry. Your STB encountered "
388 #else
389                 snprintf(text, 512, "We are really sorry. Your Dreambox encountered "
390 #endif
391                         "a software problem, and needs to be restarted. "
392                         "Please send the logfile created in /hdd/ to %s.\n"
393 #ifdef BUILD_VUPLUS /* ikseong  */
394                         "Your STB restarts in 10 seconds!\n"
395 #else
396                         "Your Dreambox restarts in 10 seconds!\n"
397 #endif
398                         "Component: %s",
399                         crash_emailaddr, crash_component);
400         
401                 p.renderText(usable_area, text, gPainter::RT_WRAP|gPainter::RT_HALIGN_LEFT);
402         
403                 usable_area = eRect(100, 170, my_dc->size().width() - 180, my_dc->size().height() - 20);
404         
405                 int i;
406         
407                 size_t start = std::string::npos + 1;
408                 for (i=0; i<20; ++i)
409                 {
410                         start = lines.rfind('\n', start - 1);
411                         if (start == std::string::npos)
412                         {
413                                 start = 0;
414                                 break;
415                         }
416                 }
417         
418                 font = new gFont("Regular", 14);
419                 p.setFont(font);
420         
421                 p.renderText(usable_area, 
422                         lines.substr(start), gPainter::RT_HALIGN_LEFT);
423                 sleep(10);
424         }
425
426         raise(SIGKILL);
427 }
428
429 #if defined(__MIPSEL__)
430 void oops(const mcontext_t &context, int dumpcode)
431 {
432         eDebug("PC: %08lx", (unsigned long)context.pc);
433         int i;
434         for (i=0; i<32; ++i)
435         {
436                 eDebugNoNewLine(" %08x", (int)context.gregs[i]);
437                 if ((i&3) == 3)
438                         eDebug("");
439         }
440                 /* this is temporary debug stuff. */
441         if (dumpcode && ((unsigned long)context.pc) > 0x10000) /* not a zero pointer */
442         {
443                 eDebug("As a final action, i will try to dump a bit of code.");
444                 eDebug("I just hope that this won't crash.");
445                 int i;
446                 eDebugNoNewLine("%08lx:", (unsigned long)context.pc);
447                 for (i=0; i<0x20; ++i)
448                         eDebugNoNewLine(" %02x", ((unsigned char*)context.pc)[i]);
449                 eDebug(" (end)");
450         }
451 }
452 #else
453 #warning "no oops support!"
454 #define NO_OOPS_SUPPORT
455 #endif
456
457 void handleFatalSignal(int signum, siginfo_t *si, void *ctx)
458 {
459         ucontext_t *uc = (ucontext_t*)ctx;
460
461 #ifndef NO_OOPS_SUPPORT
462         oops(uc->uc_mcontext, signum == SIGSEGV || signum == SIGABRT);
463 #endif
464         eDebug("-------");
465         bsodFatal("enigma2, signal");
466 }
467
468 void bsodCatchSignals()
469 {
470         struct sigaction act;
471         act.sa_handler = SIG_DFL;
472         act.sa_sigaction = handleFatalSignal;
473         act.sa_flags = SA_RESTART | SA_SIGINFO;
474         if (sigemptyset(&act.sa_mask) == -1)
475                 perror("sigemptyset");
476         
477                 /* start handling segfaults etc. */
478         sigaction(SIGSEGV, &act, 0);
479         sigaction(SIGILL, &act, 0);
480         sigaction(SIGBUS, &act, 0);
481         sigaction(SIGABRT, &act, 0);
482 }
483
484 void bsodLogInit()
485 {
486         logOutput.connect(addToLogbuffer);
487 }