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