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"
|
|
|
|
|
|
|
|
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 {
|
|
|
|
struct timeval start_time;
|
|
|
|
} state;
|
|
|
|
|
2015-11-19 23:24:56 -04:00
|
|
|
void init()
|
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
gettimeofday(&state.start_time, nullptr);
|
2015-11-11 09:43:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void panic(const char *errormsg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2016-11-17 02:06:22 -04:00
|
|
|
fflush(stdout);
|
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();
|
|
|
|
|
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
|
|
|
|
void dump_stack_trace()
|
|
|
|
{
|
|
|
|
// find dumpstack command:
|
2020-02-20 05:28:01 -04:00
|
|
|
const char *dumpstack = "dumpstack.sh"; // if we can't find it trust in PATH
|
2019-06-10 23:15:27 -03:00
|
|
|
struct stat statbuf;
|
|
|
|
const char *paths[] {
|
2020-02-20 05:28:01 -04:00
|
|
|
"Tools/scripts/dumpstack.sh",
|
|
|
|
"APM/Tools/scripts/dumpstack.sh", // for autotest server
|
2019-06-10 23:15:27 -03:00
|
|
|
};
|
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(paths); i++) {
|
|
|
|
if (::stat(paths[i], &statbuf) != -1) {
|
|
|
|
dumpstack = paths[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char cmd[100];
|
|
|
|
char progname[100];
|
|
|
|
char *p;
|
|
|
|
int n;
|
|
|
|
|
2019-07-05 01:10:11 -03:00
|
|
|
n = readlink("/proc/self/exe", progname, sizeof(progname)-1);
|
|
|
|
if (n == -1) {
|
|
|
|
strncpy(progname, "unknown", sizeof(progname));
|
|
|
|
n = strlen(progname);
|
|
|
|
}
|
2019-06-10 23:15:27 -03:00
|
|
|
progname[n] = 0;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
char output_filepath[30];
|
|
|
|
snprintf(output_filepath,
|
|
|
|
ARRAY_SIZE(output_filepath),
|
2020-02-05 18:58:50 -04:00
|
|
|
"dumpstack_%s.%d.out",
|
2019-06-10 23:15:27 -03:00
|
|
|
p+1,
|
|
|
|
(int)getpid());
|
|
|
|
snprintf(cmd,
|
|
|
|
sizeof(cmd),
|
|
|
|
"sh %s %d >%s 2>&1",
|
|
|
|
dumpstack,
|
|
|
|
(int)getpid(),
|
|
|
|
output_filepath);
|
|
|
|
fprintf(stderr, "Running: %s\n", cmd);
|
|
|
|
|
|
|
|
if (system(cmd)) {
|
|
|
|
fprintf(stderr, "Failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Stack dumped\n");
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval tp;
|
2016-10-30 02:24:21 -03:00
|
|
|
gettimeofday(&tp, nullptr);
|
2015-11-11 09:43:18 -04:00
|
|
|
uint64_t ret = 1.0e6 * ((tp.tv_sec + (tp.tv_usec * 1.0e-6)) -
|
|
|
|
(state.start_time.tv_sec +
|
|
|
|
(state.start_time.tv_usec * 1.0e-6)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t millis64()
|
|
|
|
{
|
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 / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval tp;
|
2016-10-30 02:24:21 -03:00
|
|
|
gettimeofday(&tp, nullptr);
|
2015-11-11 09:43:18 -04:00
|
|
|
uint64_t ret = 1.0e3*((tp.tv_sec + (tp.tv_usec*1.0e-6)) -
|
|
|
|
(state.start_time.tv_sec +
|
|
|
|
(state.start_time.tv_usec*1.0e-6)));
|
|
|
|
return ret;
|
2015-11-19 23:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AP_HAL
|