
Stack traces on ESP8266: a GDB server stub - ithkuil
https://blog.cesanta.com/esp8266-gdb
======
StavrosK
Slightly offtopic, but I wanted a nice breakout board for the ESP8266 and
couldn't find one, so I made one:

[https://github.com/skorokithakis/tiny-
ESP8266-breakout](https://github.com/skorokithakis/tiny-ESP8266-breakout)

If you want to just order one, here's a paneled PCB of the earlier versions
(the only difference is pretty much the lack of a 3v3 out, plus the traces
aren't as pretty):
[http://dirtypcbs.com/view.php?share=15302&accesskey=](http://dirtypcbs.com/view.php?share=15302&accesskey=)

Technical details: There exist some breakouts like this one[1], but they're
crap, stay away from them. They don't include all the necessary
pullups/pulldowns for the ESP to start running code, they don't include a
capacitor and they don't leave room on the breadboard for connectors, so
they're useless. My PCB fixes all these problems by adding 0805 10k Ω
resistors, a capacitor and by being less than 2.5cm in each dimension.

[1]: [http://www.ebay.com/itm/ESP8266-ESP-12-WIFI-UART-Serial-
Modu...](http://www.ebay.com/itm/ESP8266-ESP-12-WIFI-UART-Serial-Module-and-
Breakout-Board-for-Arduino-
TW-/151696756353?hash=item2351d4ce81:g:-dAAAOSwl8NVZ9nQ)

~~~
nfriedly
Nice! I did the same thing for ESP-01, only with stripe board so that I could
make it myself: [http://www.nfriedly.com/techblog/2015/07/build-a-diy-
esp8266...](http://www.nfriedly.com/techblog/2015/07/build-a-diy-esp8266ex-
esp-01-dev-test-programming-board/)

~~~
StavrosK
That looks fun! You can probably make it a bit smaller if you use one of those
stripe boards that have pins connected in threes, rather than the full length
of the board.

~~~
nfriedly
Yea, definitely. The part selection mostly came down to what I had on-hand
from previous projects.

------
moyix
If you can get a gdb stub onto an embedded system, you can do a lot of cool
stuff, up to and including running its firmware in an emulator and forwarding
I/O accesses to the real hardware (!), which gives you basically unlimited
visibility into the code. You can see Jonas Zaddach et al.'s Avatar paper
(PDF:
[http://www.s3.eurecom.fr/docs/ndss14_zaddach.pdf](http://www.s3.eurecom.fr/docs/ndss14_zaddach.pdf)
, code:
[http://www.s3.eurecom.fr/tools/avatar/](http://www.s3.eurecom.fr/tools/avatar/)
) for more details.

------
AstroJetson
I do most of my work in Lua. So the GDB traces will be to the major Lua
routines. I'm going to assume the ports of microPython will have the same
problem. Any suggestions for those implementations?

~~~
devbug
Immediate thoughts go to `debug.traceback` (via xpcall). You're on your own
when it comes to Lua<=>C boundaries, however. Now, if you're talking about
_manually_ walking the stack... this will point you in the right direction:
[http://zeuxcg.org/2010/11/07/lua-callstack-with-c-
debugger/](http://zeuxcg.org/2010/11/07/lua-callstack-with-c-debugger/)

~~~
AstroJetson
Thanks for the hints. I'll need to get to the lab to see how well they work in
the ESP8266 environment, that would be a huge bonus win.

~~~
devbug
Happy to help.

To expand upon my 4am ramblings:

There's two ways your code is going to fail, (a) Your Lua is going to die in a
pcall (or friends); or (b) Your C/C++ (or similar) code has its front fall
off.

In the former case, you just wrap with an xpcall and append your Lua callstack
to your C++ callstack. Nothing fancy. You can of course go further, using
lua_Debug to get the value of upvals and all that jazz.

However, in the latter case you have no guarantee that your application is in
a sane state; you have no (solid) assumption that you can go the lua_Debug
route, say from a segfault handler. You'll have to walk the callstack, and
when you hit lua_whatever, prod internal state.

Both methods are really helpful. Say you're writing a game engine, knowing how
to reproduce a catastrophic failure with a small Lua snippet, closes the
feedback loop and make debugging a hell of a lot easier.

There's definitely a product and SaaS around this (much more expanded), with a
Gitlab or Sentry business model.

