Debugging c code tips? #7654
Replies: 1 comment
-
Posted at 2023-12-25 by user156811 I did see this. https://www.espruino.com/AdvancedDebug Posted at 2023-12-26 by user156811 So i see there are debugging flags. So there's a USE_DEBUGGER and a just a standard DEBUG flag. So i'm guessing USE_DEBUGGER is hardware? Unless USE_DEBUGGER is the flag to enable the javascript debugger? Posted at 2023-12-27 by user156811 So to get GDB working i need to hardcode the file and the arm directory. make -j BOARD=P8-SDK12-SPIFLASH PROJECTNAME=./bin/espruino_2v19.88_p8_SDK12_SD30_SPIFLASH -j8 flash gdb
## ARM.make
./gcc-arm-none-eabi-8-2018-q4-major/bin/arm-none-eabi-gdb -x gdbinit Posted at 2023-12-27 by user156811 So now it's complaining about that it can't connect to remote host? Posted at 2023-12-27 by user156811 I'm confused as to why it's trying to do a remote session? Posted at 2023-12-28 by @fanoush
gdb runs on PC and the debugged code runs on ARM device so it needs to be a bit complicated, there is remote GDB server running somewhere. It can be OpenOCD running on your PC translating gdb commands to SWD/JTAG commands. It can be Segger GDB server for J-Link, it can be even running inside your debug probe for speed (Blackmagic probe has one). As for debugging Espruino I did it few times but there is issue with nrf52 with BLE stack running - you cannot stop and continue as it breaks Bluetooth timing and device reboots. With bluetooth turned off ( at least I used command line gdb with the elf file which is produced in each build, no extra compile flags are needed. I think the elf file produced by gcc should also work in any IDE but don't have any guide, try Google. EDIT: see also this post https://forum.espruino.com/conversations/369539/#16249545 Posted at 2023-12-28 by user156811 Thanks for the constantly good advice @fanoush! I'll give it a go and post my results. Posted at 2024-01-17 by user156811 Ok so i got something now. Things i had to do
Posted at 2024-01-17 by user156811 Just for people new to this. First start your server
If its working you should see something like Waiting for GDB connection Then do your
Then gdb will get ready to be started and then you just hit enter to start it or q to quite. This should build your program and then run the arm gdb client. If there's problems with with GDB for some reason the client side. Posted at 2024-01-19 by user156811 Progress being made. helpful links So after your running gdb is connect to your server you need to
Which flashes the code onto the device. From there you can do your next and continue commands. You can also edit your ~/.gdbinit file so you don't have to hit enter every time. next problemSo i have testing the above stuff on a nRF52840DK which has lots of flash. But the current micro is a nRF52832 with barely any. So my plan was to add the debug symbols to just the specific files i care about. So this compiles but doesn't seem to add any size to the code base. So thats the next thing to figure out. trying to add special debug symbols
Posted at 2024-01-19 by @fanoush
AFAIK there is no issue with debug symbols, you have all symbols even without DEBUG=1, they are in elf file and kept on PC side, not part of binary in device. What DEBUG=1 does is turning off optimizations so the binary is much larger. RELEASE=1 turns off extra assert checks (also not needed for debugging). You should be able to debug even without DEBUG=1 and with RELEASE=1. What can happen is that some code is optimized out so the debugger will not match all lines 1:1 when the code is not there, however mostly it works just fine. BTW, you can also run make BOARD=... lst to generate .lst text file with C and assembly language mix and all symbols so you can figure out how the code really looks if you get lost when debugging optimized code. Posted at 2024-01-19 by @gfwilliams As @fanoush says - but if you do want to use the unoptimised bdebug dinary on an nRF52 you can maybe look at removing features from the BOARD.py file you're not using, for instance: https://github.com/espruino/Espruino/blob/master/boards/NRF52832DK.py#L34-L37 you can even use the Posted at 2024-01-20 by user156811 @gfwilliams @fanoush - Thanks for clearing this up for me. I think i was getting confused because i see this when i start up GDB. Which i'm guessing is because when you first load up gdb it doesn't load the file. Its currently commented out for some reason. Posted at 2024-01-20 by user156811 [Solution] is on #18 - but i'll keep these note for future reference. Right now i'm trying to figure out why the program completely crashes when i first run it? This is what happens when i try to debug my Pinetime. When i do my first step it just crashes. When i don't debug it, it the build works just fine?
Posted at 2024-01-20 by user156811 I still have to actually understand more about this. But the registers look wrong from the start?
Posted at 2024-01-20 by user156811 So i have a pine time and a nRF528340 dev board that i'm working with currently. I can seem to debug the nRF528340DK perfectly. But the pinetime just seems to not want to work from the start. From the the two build one thing that stand out to me is this line at the end. This overlap=replace doesn't seem to need to be set in the normal devboard. FIXME - had to set --overlap=replace Posted at 2024-01-20 by user156811 OK! Figured it out!!!!!!!!!!!!!!!!!So because of the FIXME line were it merges all the files. If you load the elf file it doesn't work. So the solution seems to be to load hex file! So now i can finally step through the code on the pinetime!
Posted at 2024-01-21 by user156811
So now i think i'm at this step. When i'm stepping through the program from the very start. I just don't know were to stick this NRF.sleep() call? Posted at 2024-01-21 by user156811 current stack trace
Posted at 2024-01-21 by @fanoush
well if you need to debug startup then I don't know about such way, in theory we could add some option to not start advertising automatically. You can also compile bluetooth module out for debugging startup. In other cases just run it fully, then execute NRF.sleep() in console or NRF.sleep() /wake() mapped to something like button click and then set breakpoints as you wish when it is sleeping. it is easier to do this with console over serial (or usb with that 52840 devboard), then you can type it directly as needed BTW som more info here https://devzone.nordicsemi.com/f/nordic-q-a/9622/application-debug-with-softdevice Posted at 2024-01-21 by user156811 Yes so i need to debug the initialization of all the hardware. But whats interesting is that it seems to fail just because its in GDB. without hitting any breakpoints and restarting? For example. i startup GDB and hit continue and it breaks? But if i just flash the software normally it works fine? A lot more hurtles then i was expecting to step through the software :/ I think i'm just going to try and comment that line out and see it will work just without bluetooth. Just so i can make some forward progress. Then i'll try and figuring out the above link you sent me. Right now thats slightly more in the weeds then i want to be at. Posted at 2024-01-22 by @fanoush
There should be no difference until you hit first breakpoint. I think I never used gdb to actually flash firmware to the device. I just do it the normal way first, then run gdb and attach elf file with 'file' command and use run command (?), I forgot exact way, I need to check. I use openocd for gdb server so I can cheat a bit by having both gdb attached to port 3333 and also normal console to port 4444, there I can reset device to start again without gdb knowing. Posted at 2024-01-22 by user156811 A normal console being a serial console? Do you remember what command you used to reset it? Posted at 2024-01-22 by @fanoush I mean normal openocd console running on port 4444 , you connect to it e.g. via telnet command (telnet localhost 4444) https://openocd.org/doc/html/General-Commands.html , you flash hex file via 'program' command, reset via 'reset run' or 'reset halt'. As for espruino serial console with interpreter, yes I have it connected too - I mostly use use CMSIS-DAP debug dongles with SWD and UART pins connected to the device. CMSIS-DAP is nice because it needs no drivers and work in linux and windows with openocd out of box. Also you can turn almost any ARM board into CMIS-DAP dongle - easiest is probably the raspberry pico. Posted at 2024-01-22 by user156811 I can agree with that. I'm currently using my pico as a program because i found it easy to set up and more reliable then my fake JTAG programmer. Posted at 2024-01-22 by @gfwilliams I'm surprised you hit issues with On the debug front, it's a nightmare with nRF52 because the softdevice tries to restart if it realises the CPU is halted for much time. There is a Posted at 2024-01-22 by @fanoush
Well that is useful if one has watchdog started but is not so much related to softdevice Found the PRIMASK stuff, discussed here https://devzone.nordicsemi.com/f/nordic-q-a/477/can-i-debug-ble-program/2498 with some gdb hooks EDIT: oh i see now that it is already there in Posted at 2024-01-23 by user156811
overlap=METHOD That does sound very bad :/ I should probably look into that. Posted at 2024-01-23 by user156811 So commenting out a few parts i managed to get to jsiInit in the main.c file. I'm still trying to figure this out, but this isn't were it laod the banglejs_storage is it? Currently i'm working on modifying the bangle code project. Posted at 2024-01-23 by @gfwilliams jsiInit calls jsiSemiInit which in turn does load from flash... I think Posted at 2024-01-25 by user156811 Ok so i don't think i'm running into a "error" --overlap. It seems like it part of the build script? So there seems to be two types of defines
And in my python board file i have 'bootloader' : 1. This one is sets USE_BOOTLOADER to true. As for BOOTLOADER
file bin/bootloader_espruino_2v19....elf
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2023-12-25 by user156811
If i'm making changes in the bangleJS portion of the code. What options do i have for debugging the code. I have a nordic NRF 52840 development kit and a custom display i'm using. So it has a segger programmer attached to it over usb.
I'm sorry for the very basic question.
Beta Was this translation helpful? Give feedback.
All reactions