gdbstub.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * This module, when enabled with the LUA_USE_MODULES_GDBSTUB define causes
  3. * the gdbstub code to be included and enabled to handle all fatal exceptions.
  4. * This allows you to use the lx106 gdb to catch the exception and then poke
  5. * around. You can continue from a break, but attempting to continue from an
  6. * exception usually fails.
  7. *
  8. * This should not be included in production builds as any exception will
  9. * put the nodemcu into a state where it is waiting for serial input and it has
  10. * the watchdog disabled. Good for debugging. Bad for unattended operation!
  11. *
  12. * See the docs for more information.
  13. *
  14. * Philip Gladstone, N1DQ
  15. */
  16. #include "module.h"
  17. #include "lauxlib.h"
  18. #include "platform.h"
  19. #include <stdint.h>
  20. #include "user_interface.h"
  21. #include "../esp-gdbstub/gdbstub.h"
  22. static int init_done = 0;
  23. static int lgdbstub_open(lua_State *L);
  24. // gdbstub.brk() init gdb if nec and execute a break instructiont to entry gdb
  25. static int lgdbstub_break(lua_State *L) {
  26. lgdbstub_open(L);
  27. asm("break 0,0" ::);
  28. return 0;
  29. }
  30. // as for break but also redirect output to the debugger.
  31. static int lgdbstub_pbreak(lua_State *L) {
  32. lgdbstub_open(L);
  33. gdbstub_redirect_output(1);
  34. asm("break 0,0" ::);
  35. return 0;
  36. }
  37. // gdbstub.gdboutput(1) switches the output to gdb format so that gdb can display it
  38. static int lgdbstub_gdboutput(lua_State *L) {
  39. gdbstub_redirect_output(lua_toboolean(L, 1));
  40. return 0;
  41. }
  42. static int lgdbstub_open(lua_State *L) {
  43. if (init_done)
  44. return 0;
  45. gdbstub_init();
  46. init_done = 1;
  47. return 0;
  48. }
  49. // Module function map
  50. LROT_BEGIN(gdbstub, NULL, 0)
  51. LROT_FUNCENTRY( brk, lgdbstub_break )
  52. LROT_FUNCENTRY( pbrk, lgdbstub_pbreak )
  53. LROT_FUNCENTRY( gdboutput, lgdbstub_gdboutput )
  54. LROT_FUNCENTRY( open, lgdbstub_open )
  55. LROT_END(gdbstub, NULL, 0)
  56. NODEMCU_MODULE(GDBSTUB, "gdbstub", gdbstub, NULL);