brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #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)). |
| 12 | extern "C" void abort() __attribute__((noreturn)); |
brians | 8fcd875 | 2013-03-16 21:30:54 +0000 | [diff] [blame] | 13 | #include <usrLib.h> |
| 14 | #include <dbgLib.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | #endif |
| 16 | |
| 17 | #include <string> |
| 18 | |
| 19 | #include "aos/aos_stdint.h" |
| 20 | |
| 21 | namespace aos { |
| 22 | |
| 23 | void Die(const char *format, ...) { |
| 24 | va_list args; |
| 25 | va_start(args, format); |
| 26 | VDie(format, args); |
Brian Silverman | 5b61e46 | 2013-03-16 16:47:23 -0700 | [diff] [blame] | 27 | // va_end(args) // not because VDie never returns |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | namespace { |
| 31 | // Calculates the filename to dump the message into. |
| 32 | const std::string GetFilename() { |
| 33 | #ifdef __VXWORKS__ |
| 34 | const char *name = taskName(0); // get the name of this task |
| 35 | if (name == NULL) name = "<unknown>"; |
| 36 | const std::string first_part = "/aos_fatal_error."; |
| 37 | return first_part + std::string(name); |
| 38 | #else |
| 39 | char *filename; |
| 40 | if (asprintf(&filename, "/tmp/aos_fatal_error.%jd", |
| 41 | static_cast<intmax_t>(getpid())) > 0) { |
| 42 | std::string r(filename); |
| 43 | free(filename); |
| 44 | return r; |
| 45 | } else { |
| 46 | fprintf(stderr, "aos fatal: asprintf(%p, \"thingie with %%jd\", %jd)" |
| 47 | " failed with %d (%s)\n", &filename, |
| 48 | static_cast<intmax_t>(getpid()), errno, strerror(errno)); |
| 49 | return std::string(); |
| 50 | } |
| 51 | #endif |
| 52 | } |
| 53 | } // namespace |
brians | 6ed722d | 2013-02-12 03:19:55 +0000 | [diff] [blame] | 54 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | void VDie(const char *format, va_list args_in) { |
Brian Silverman | 5b61e46 | 2013-03-16 16:47:23 -0700 | [diff] [blame] | 56 | // We don't bother va_ending either of these because we're going nowhere and |
| 57 | // vxworks has some weird bugs that sometimes show up... |
| 58 | va_list args1, args2; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 59 | |
| 60 | fputs("aos fatal: ERROR!! details following\n", stderr); |
Brian Silverman | 5b61e46 | 2013-03-16 16:47:23 -0700 | [diff] [blame] | 61 | va_copy(args1, args_in); |
| 62 | vfprintf(stderr, format, args1); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | fputs("aos fatal: ERROR!! see stderr for details\n", stdout); |
| 64 | |
| 65 | const std::string filename = GetFilename(); |
| 66 | if (!filename.empty()) { |
| 67 | FILE *error_file = fopen(filename.c_str(), "w"); |
| 68 | if (error_file != NULL) { |
Brian Silverman | 5b61e46 | 2013-03-16 16:47:23 -0700 | [diff] [blame] | 69 | va_copy(args2, args_in); |
| 70 | vfprintf(error_file, format, args2); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 71 | fclose(error_file); |
| 72 | } else { |
| 73 | fprintf(stderr, "aos fatal: fopen('%s', \"w\") failed with %d (%s)\n", |
| 74 | filename.c_str(), errno, strerror(errno)); |
| 75 | } |
| 76 | } |
| 77 | |
brians | 8fcd875 | 2013-03-16 21:30:54 +0000 | [diff] [blame] | 78 | #ifdef __VXWORKS__ |
| 79 | printf("I am 0x%x suspending for debugging purposes.\n", taskIdSelf()); |
| 80 | printf("\t`tt 0x%x` will give you a stack trace.\n", taskIdSelf()); |
| 81 | fputs("\t`lkAddr` will reverse lookup a symbol for you.\n", stdout); |
| 82 | fputs("\t`dbgHelp` and `help` have some useful commands in them.\n", stdout); |
| 83 | taskSuspend(0); |
| 84 | printf("You weren't supposed to resume 0x%x!!. Going to really die now.\n", |
| 85 | taskIdSelf()); |
| 86 | #endif |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 87 | abort(); |
| 88 | } |
| 89 | |
| 90 | } // namespace aos |