blob: 203c65423992d86b4deba58f6372de4333bf6e41 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/die.h"
2
3#include <stdlib.h>
4#include <stdio.h>
5#include <errno.h>
6#include <sys/types.h>
7#include <unistd.h>
8#include <string.h>
9#ifdef __VXWORKS__
10#include <taskLib.h>
11// Have to re-declare it with __attribute__((noreturn)).
12extern "C" void abort() __attribute__((noreturn));
brians8fcd8752013-03-16 21:30:54 +000013#include <usrLib.h>
14#include <dbgLib.h>
brians343bc112013-02-10 01:53:46 +000015#endif
16
17#include <string>
18
19#include "aos/aos_stdint.h"
20
21namespace aos {
22
23void Die(const char *format, ...) {
24 va_list args;
25 va_start(args, format);
26 VDie(format, args);
27}
28
29namespace {
30// Calculates the filename to dump the message into.
31const std::string GetFilename() {
32#ifdef __VXWORKS__
33 const char *name = taskName(0); // get the name of this task
34 if (name == NULL) name = "<unknown>";
35 const std::string first_part = "/aos_fatal_error.";
36 return first_part + std::string(name);
37#else
38 char *filename;
39 if (asprintf(&filename, "/tmp/aos_fatal_error.%jd",
40 static_cast<intmax_t>(getpid())) > 0) {
41 std::string r(filename);
42 free(filename);
43 return r;
44 } else {
45 fprintf(stderr, "aos fatal: asprintf(%p, \"thingie with %%jd\", %jd)"
46 " failed with %d (%s)\n", &filename,
47 static_cast<intmax_t>(getpid()), errno, strerror(errno));
48 return std::string();
49 }
50#endif
51}
52} // namespace
brians6ed722d2013-02-12 03:19:55 +000053
brians343bc112013-02-10 01:53:46 +000054void VDie(const char *format, va_list args_in) {
55 va_list args;
56
57 fputs("aos fatal: ERROR!! details following\n", stderr);
58 va_copy(args, args_in);
59 vfprintf(stderr, format, args);
60 va_end(args);
61 fputs("aos fatal: ERROR!! see stderr for details\n", stdout);
62
63 const std::string filename = GetFilename();
64 if (!filename.empty()) {
65 FILE *error_file = fopen(filename.c_str(), "w");
66 if (error_file != NULL) {
67 va_copy(args, args_in);
68 vfprintf(error_file, format, args);
69 va_end(args);
70 fclose(error_file);
71 } else {
72 fprintf(stderr, "aos fatal: fopen('%s', \"w\") failed with %d (%s)\n",
73 filename.c_str(), errno, strerror(errno));
74 }
75 }
76
brians8fcd8752013-03-16 21:30:54 +000077#ifdef __VXWORKS__
78 printf("I am 0x%x suspending for debugging purposes.\n", taskIdSelf());
79 printf("\t`tt 0x%x` will give you a stack trace.\n", taskIdSelf());
80 fputs("\t`lkAddr` will reverse lookup a symbol for you.\n", stdout);
81 fputs("\t`dbgHelp` and `help` have some useful commands in them.\n", stdout);
82 taskSuspend(0);
83 printf("You weren't supposed to resume 0x%x!!. Going to really die now.\n",
84 taskIdSelf());
85#endif
brians343bc112013-02-10 01:53:46 +000086 abort();
87}
88
89} // namespace aos