2015-11-11 09:43:18 -04:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2019-06-10 23:15:27 -03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2015-11-11 09:43:18 -04:00
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-11-19 23:24:56 -04:00
|
|
|
#include <AP_HAL/system.h>
|
|
|
|
|
2015-11-11 09:43:18 -04:00
|
|
|
#include "Scheduler.h"
|
2023-12-20 01:24:27 -04:00
|
|
|
#include <AP_Math/div1000.h>
|
2015-11-11 09:43:18 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
using HALSITL::Scheduler;
|
2015-11-11 09:43:18 -04:00
|
|
|
|
2015-11-19 23:24:56 -04:00
|
|
|
namespace AP_HAL {
|
|
|
|
|
2015-11-11 09:43:18 -04:00
|
|
|
static struct {
|
2024-02-04 23:30:09 -04:00
|
|
|
uint64_t start_time_ns;
|
2015-11-11 09:43:18 -04:00
|
|
|
} state;
|
|
|
|
|
2024-02-04 23:30:09 -04:00
|
|
|
static uint64_t ts_to_nsec(struct timespec &ts)
|
|
|
|
{
|
|
|
|
return ts.tv_sec*1000000000ULL + ts.tv_nsec;
|
|
|
|
}
|
|
|
|
|
2015-11-19 23:24:56 -04:00
|
|
|
void init()
|
|
|
|
{
|
2024-02-04 23:30:09 -04:00
|
|
|
struct timespec ts {};
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
state.start_time_ns = ts_to_nsec(ts);
|
2015-11-11 09:43:18 -04:00
|
|
|
}
|
|
|
|
|
2021-10-02 20:48:12 -03:00
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(CYGWIN_BUILD)
|
|
|
|
void panic(const char *errormsg, ...)
|
|
|
|
#else
|
2021-08-20 03:05:43 -03:00
|
|
|
void WEAK panic(const char *errormsg, ...)
|
2021-10-02 20:48:12 -03:00
|
|
|
#endif
|
2015-11-11 09:43:18 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2016-11-17 02:06:22 -04:00
|
|
|
fflush(stdout);
|
2020-07-24 00:11:39 -03:00
|
|
|
printf("PANIC: ");
|
2015-11-11 09:43:18 -04:00
|
|
|
va_start(ap, errormsg);
|
|
|
|
vprintf(errormsg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
printf("\n");
|
|
|
|
|
2019-06-10 23:15:55 -03:00
|
|
|
dump_stack_trace();
|
2021-07-06 02:32:25 -03:00
|
|
|
dump_core_file();
|
2019-06-10 23:15:55 -03:00
|
|
|
|
2019-06-07 05:43:42 -03:00
|
|
|
if (getenv("SITL_PANIC_EXIT")) {
|
|
|
|
// this is used on the autotest server to prevent us waiting
|
|
|
|
// 10 hours for a timeout
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-11-11 09:43:18 -04:00
|
|
|
for(;;);
|
|
|
|
}
|
|
|
|
|
2019-06-10 23:15:27 -03:00
|
|
|
// partly flogged from: https://github.com/tridge/junkcode/blob/master/segv_handler/segv_handler.c
|
2022-05-05 05:53:47 -03:00
|
|
|
static void run_command_on_ownpid(const char *commandname)
|
2019-06-10 23:15:27 -03:00
|
|
|
{
|
|
|
|
// find dumpstack command:
|
2021-07-06 02:32:25 -03:00
|
|
|
const char *command_filepath = commandname; // if we can't find it trust in PATH
|
2019-06-10 23:15:27 -03:00
|
|
|
struct stat statbuf;
|
|
|
|
const char *paths[] {
|
2021-07-06 02:32:25 -03:00
|
|
|
"Tools/scripts/%s",
|
|
|
|
"APM/Tools/scripts/%s", // for autotest server
|
|
|
|
"../Tools/scripts/%s", // when run from e.g. ArduCopter subdirectory
|
2019-06-10 23:15:27 -03:00
|
|
|
};
|
2021-07-06 02:32:25 -03:00
|
|
|
char buffer[60];
|
2019-06-10 23:15:27 -03:00
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(paths); i++) {
|
2021-07-06 02:32:25 -03:00
|
|
|
// form up a filepath from each path and commandname; if it
|
|
|
|
// exists, use it
|
|
|
|
snprintf(buffer, sizeof(buffer), paths[i], commandname);
|
|
|
|
if (::stat(buffer, &statbuf) != -1) {
|
|
|
|
command_filepath = buffer;
|
2019-06-10 23:15:27 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char progname[100];
|
2021-07-06 02:32:25 -03:00
|
|
|
int n = readlink("/proc/self/exe", progname, sizeof(progname)-1);
|
2019-07-05 01:10:11 -03:00
|
|
|
if (n == -1) {
|
|
|
|
strncpy(progname, "unknown", sizeof(progname));
|
|
|
|
n = strlen(progname);
|
|
|
|
}
|
2019-06-10 23:15:27 -03:00
|
|
|
progname[n] = 0;
|
|
|
|
|
2021-07-06 02:32:25 -03:00
|
|
|
char *p = strrchr(progname, '/');
|
2020-01-04 07:39:28 -04:00
|
|
|
if (p != nullptr) {
|
|
|
|
*p = 0;
|
|
|
|
} else {
|
|
|
|
p = progname;
|
|
|
|
}
|
2019-06-10 23:15:27 -03:00
|
|
|
|
2021-07-06 02:32:25 -03:00
|
|
|
char output_filepath[80];
|
2019-06-10 23:15:27 -03:00
|
|
|
snprintf(output_filepath,
|
|
|
|
ARRAY_SIZE(output_filepath),
|
2021-07-06 02:32:25 -03:00
|
|
|
"%s_%s.%d.out",
|
|
|
|
commandname,
|
2019-06-10 23:15:27 -03:00
|
|
|
p+1,
|
|
|
|
(int)getpid());
|
2021-07-06 02:32:25 -03:00
|
|
|
char cmd[200];
|
2019-06-10 23:15:27 -03:00
|
|
|
snprintf(cmd,
|
|
|
|
sizeof(cmd),
|
|
|
|
"sh %s %d >%s 2>&1",
|
2021-07-06 02:32:25 -03:00
|
|
|
command_filepath,
|
2019-06-10 23:15:27 -03:00
|
|
|
(int)getpid(),
|
|
|
|
output_filepath);
|
|
|
|
fprintf(stderr, "Running: %s\n", cmd);
|
|
|
|
|
|
|
|
if (system(cmd)) {
|
|
|
|
fprintf(stderr, "Failed\n");
|
|
|
|
return;
|
|
|
|
}
|
2021-07-06 02:32:25 -03:00
|
|
|
fprintf(stderr, "%s has been run. Output was:\n", commandname);
|
|
|
|
fprintf(stderr, "-------------- begin %s output ----------------\n", commandname);
|
2019-06-10 23:15:27 -03:00
|
|
|
// print the trace on stderr:
|
|
|
|
int fd = open(output_filepath, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Failed to open stack dump filepath: %m");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char buf[1024]; // let's hope we're not here because we ran out of stack
|
|
|
|
while (true) {
|
|
|
|
const ssize_t ret = read(fd, buf, ARRAY_SIZE(buf));
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "Read error: %m");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (write(2, buf, ret) != ret) {
|
|
|
|
// *sigh*
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 02:32:25 -03:00
|
|
|
fprintf(stderr, "-------------- end %s output ----------------\n", commandname);
|
2019-06-10 23:15:27 -03:00
|
|
|
close(fd);
|
|
|
|
}
|
2021-07-06 02:32:25 -03:00
|
|
|
void dump_stack_trace()
|
|
|
|
{
|
|
|
|
run_command_on_ownpid("dumpstack.sh");
|
|
|
|
}
|
|
|
|
void dump_core_file()
|
|
|
|
{
|
|
|
|
run_command_on_ownpid("dumpcore.sh");
|
|
|
|
}
|
2019-06-10 23:15:27 -03:00
|
|
|
|
2015-11-11 09:43:18 -04:00
|
|
|
uint32_t micros()
|
|
|
|
{
|
|
|
|
return micros64() & 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t millis()
|
|
|
|
{
|
|
|
|
return millis64() & 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
2018-12-10 18:44:05 -04:00
|
|
|
/*
|
|
|
|
we define a millis16() here to avoid an issue with sitl builds in cygwin
|
|
|
|
*/
|
|
|
|
uint16_t millis16()
|
|
|
|
{
|
|
|
|
return millis64() & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2015-11-11 09:43:18 -04:00
|
|
|
uint64_t micros64()
|
|
|
|
{
|
2016-01-10 02:23:32 -04:00
|
|
|
const HALSITL::Scheduler* scheduler = HALSITL::Scheduler::from(hal.scheduler);
|
2015-11-11 09:43:18 -04:00
|
|
|
uint64_t stopped_usec = scheduler->stopped_clock_usec();
|
|
|
|
if (stopped_usec) {
|
|
|
|
return stopped_usec;
|
|
|
|
}
|
|
|
|
|
2024-02-04 23:30:09 -04:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return uint64_div1000(ts_to_nsec(ts) - state.start_time_ns);
|
2015-11-11 09:43:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t millis64()
|
|
|
|
{
|
2023-12-20 01:24:27 -04:00
|
|
|
return uint64_div1000(micros64());
|
2015-11-19 23:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AP_HAL
|