PX4 / PX4-Autopilot

PX4 Autopilot Software
https://px4.io
BSD 3-Clause "New" or "Revised" License
8.17k stars 13.36k forks source link

Hardfault caused by printf #10229

Closed garfieldG closed 6 years ago

garfieldG commented 6 years ago

Adding printf to land detector caused hardfault :

Laup_hardfault: PANIC!!! Hard fault: 40000000 up_assert: Assertion failed at file:armv7-m/up_hardfault.c line: 171 task: hpwork up_dumpstate: sp: 200028c4 up_dumpstate: IRQ stack: up_dumpstate: base: 200028f0 up_dumpstate: size: 000002ec up_dumpstate: used: 0000020c up_stackdump: 200028c0: 08005a0f 00000003 00000000 08005a17 080059f5 080059e9 000000f0 1000152c up_stackdump: 200028e0: 10001650 0814ea25 1000168c 08004f53 1000152c 00000001 00000001 00000000 up_dumpstate: sp: 10001600 up_dumpstate: User stack: up_dumpstate: base: 10001720 up_dumpstate: size: 000006f4 up_dumpstate: used: 000002e8 up_stackdump: 10001600: 00000009 20006254 0812a9ab 20006254 1000dc30 0800ec85 0800eca5 00000009 up_stackdump: 10001620: 000000f0 7f800000 00000000 10000dfc 0814ea01 10001684 00000000 02366f66 up_stackdump: 10001640: 00000000 00000001 00000001 0800f23f 0800f255 0800f24d 0000002a 10000dfc up_stackdump: 10001660: 0814ea01 20003e4c 2001cf70 0800e313 0814ea01 10001684 2001cf60 0808e297 up_stackdump: 10001680: 0814ea01 0814e997 02366f67 00000000 0814e997 08006eab 00000000 00000000 up_stackdump: 100016a0: 4502d012 3fa00000 00000000 00000000 00000000 2001d040 20002d34 00000005 up_stackdump: 100016c0: 000000f0 00000001 00000010 20002d38 200030bc 08008935 00009158 00000000 up_stackdump: 100016e0: 00000010 00000000 00000000 20002d34 00000000 00000000 00000000 00000000 up_stackdump: 10001700: 00000000 00000000 00000000 0800807b 0800806d 08007f23 00000000 00000000 up_registerdump: R0: 02366f67 00000073 00000000 02366f67 00000000 10001650 0814ea25 1000168c up_registerdump: R8: 02366f67 00000000 00000000 00000000 0801994f 10001600 0800ea95 0801074a up_registerdump: xPSR: 41000000 BASEPRI: 000000f0 CONTROL: 00000000 up_registerdump: EXC_RETURN: ffffffe9 up_taskdump: Idle Task: PID=0 Stack Used=0 of 0 up_taskdump: hpwork: PID=1 Stack Used=744 of 1780 up_taskdump: lpwork: PID=2 Stack Used=640 of 1780 up_taskdump: init: PID=3 Stack Used=1320 of 2484 up_taskdump: mavlink_if0: PID=101 Stack Used=1296 of 2532 up_taskdump: gps: PID=38 Stack Used=1144 of 1604 up_taskdump: logger: PID=171 Stack Used=1088 of 3540 up_taskdump: log_writer_file: PID=172 Stack Used=376 of 1148 up_taskdump: mavlink_if0: PID=110 Stack Used=1696 of 2572 up_taskdump: mavlink_rcv_if0: PID=111 Stack Used=1272 of 2836 up_taskdump: ekf2: PID=147 Stack Used=4552 of 6572 up_taskdump: land_detector: PID=180 Stack Used=384 of 1148 up_taskdump: navigator: PID=149 Stack Used=840 of 1764 up_taskdump: sensors: PID=88 Stack Used=1216 of 1964 up_taskdump: commander: PID=90 Stack Used=1352 of 3212 up_taskdump: commander_low_prio: PID=91 Stack Used=552 of 2996 up_taskdump: mavlink_if1: PID=125 Stack Used=1672 of 2532 up_taskdump: mavlink_rcv_if1: PID=126 Stack Used=1184 of 2836

The printf : printf("LandDetector Mode changed at %d to %s\n",_landDetected.timestamp, landDetected?"landDetected": (freefallDetected?"freefallDetected": (maybe_landedDetected?"maybe_landedDetected": (ground_contactDetected?"ground_contactDetected":"None"))) );

Added it to master branch on top of e5ed05766dfb8bb0a6160018e6b3f32e18ad0d38 hash. This was the only change I made to the code.

bkueng commented 6 years ago

_landDetected.timestamp is an uint64_t, so the format should be %llu (or better: %" PRIu64 ").

It if still does not help, try increasing the stack size (for your config): https://github.com/PX4/Firmware/blob/master/platforms/nuttx/nuttx-configs/px4fmu-v2/nsh/defconfig#L824

But you don't really need a printf there, you can also just use the listener command.

BazookaJoe1900 commented 6 years ago

@bkueng @garfieldG I think that even in case of writing wrong printf formating the cpushould not do hardfault. using printf is widly used and it can be easy to misuse it. is there a way to avoid that bad formating, better on compile time?

LorenzMeier commented 6 years ago

If you write deep embedded code there are no fundamental guardrails. We are eliminating the use of printf in most places right now. Many safety-critical programming guidelines forbid its usage completely.

The hardfault is likely the result of a stack overflow or alike. You would need to increase the work queue stack to fix that.