idf_monitor.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. #!/usr/bin/env python
  2. #
  3. # esp-idf serial output monitor tool. Does some helpful things:
  4. # - Looks up hex addresses in ELF file with addr2line
  5. # - Reset ESP32 via serial RTS line (Ctrl-T Ctrl-R)
  6. # - Run "make (or idf.py) flash" (Ctrl-T Ctrl-F)
  7. # - Run "make (or idf.py) app-flash" (Ctrl-T Ctrl-A)
  8. # - If gdbstub output is detected, gdb is automatically loaded
  9. #
  10. # Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License");
  13. # you may not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS,
  20. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. #
  24. # Contains elements taken from miniterm "Very simple serial terminal" which
  25. # is part of pySerial. https://github.com/pyserial/pyserial
  26. # (C)2002-2015 Chris Liechti <cliechti@gmx.net>
  27. #
  28. # Originally released under BSD-3-Clause license.
  29. #
  30. from __future__ import print_function, division
  31. from __future__ import unicode_literals
  32. from builtins import chr
  33. from builtins import object
  34. from builtins import bytes
  35. import subprocess
  36. import argparse
  37. import codecs
  38. import re
  39. import os
  40. try:
  41. import queue
  42. except ImportError:
  43. import Queue as queue
  44. import shlex
  45. import time
  46. import sys
  47. import serial
  48. import serial.tools.miniterm as miniterm
  49. import threading
  50. import ctypes
  51. import types
  52. from distutils.version import StrictVersion
  53. key_description = miniterm.key_description
  54. # Control-key characters
  55. CTRL_A = '\x01'
  56. CTRL_B = '\x02'
  57. CTRL_F = '\x06'
  58. CTRL_H = '\x08'
  59. CTRL_R = '\x12'
  60. CTRL_T = '\x14'
  61. CTRL_Y = '\x19'
  62. CTRL_P = '\x10'
  63. CTRL_X = '\x18'
  64. CTRL_RBRACKET = '\x1d' # Ctrl+]
  65. # ANSI terminal codes (if changed, regular expressions in LineMatcher need to be udpated)
  66. ANSI_RED = '\033[1;31m'
  67. ANSI_YELLOW = '\033[0;33m'
  68. ANSI_NORMAL = '\033[0m'
  69. def color_print(message, color):
  70. """ Print a message to stderr with colored highlighting """
  71. sys.stderr.write("%s%s%s\n" % (color, message, ANSI_NORMAL))
  72. def yellow_print(message):
  73. color_print(message, ANSI_YELLOW)
  74. def red_print(message):
  75. color_print(message, ANSI_RED)
  76. __version__ = "1.1"
  77. # Tags for tuples in queues
  78. TAG_KEY = 0
  79. TAG_SERIAL = 1
  80. TAG_SERIAL_FLUSH = 2
  81. # regex matches an potential PC value (0x4xxxxxxx)
  82. MATCH_PCADDR = re.compile(r'0x4[0-9a-f]{7}', re.IGNORECASE)
  83. DEFAULT_TOOLCHAIN_PREFIX = "xtensa-esp32-elf-"
  84. DEFAULT_PRINT_FILTER = ""
  85. class StoppableThread(object):
  86. """
  87. Provide a Thread-like class which can be 'cancelled' via a subclass-provided
  88. cancellation method.
  89. Can be started and stopped multiple times.
  90. Isn't an instance of type Thread because Python Thread objects can only be run once
  91. """
  92. def __init__(self):
  93. self._thread = None
  94. @property
  95. def alive(self):
  96. """
  97. Is 'alive' whenever the internal thread object exists
  98. """
  99. return self._thread is not None
  100. def start(self):
  101. if self._thread is None:
  102. self._thread = threading.Thread(target=self._run_outer)
  103. self._thread.start()
  104. def _cancel(self):
  105. pass # override to provide cancellation functionality
  106. def run(self):
  107. pass # override for the main thread behaviour
  108. def _run_outer(self):
  109. try:
  110. self.run()
  111. finally:
  112. self._thread = None
  113. def stop(self):
  114. if self._thread is not None:
  115. old_thread = self._thread
  116. self._thread = None
  117. self._cancel()
  118. old_thread.join()
  119. class ConsoleReader(StoppableThread):
  120. """ Read input keys from the console and push them to the queue,
  121. until stopped.
  122. """
  123. def __init__(self, console, event_queue, test_mode):
  124. super(ConsoleReader, self).__init__()
  125. self.console = console
  126. self.event_queue = event_queue
  127. self.test_mode = test_mode
  128. def run(self):
  129. self.console.setup()
  130. try:
  131. while self.alive:
  132. try:
  133. if os.name == 'nt':
  134. # Windows kludge: because the console.cancel() method doesn't
  135. # seem to work to unblock getkey() on the Windows implementation.
  136. #
  137. # So we only call getkey() if we know there's a key waiting for us.
  138. import msvcrt
  139. while not msvcrt.kbhit() and self.alive:
  140. time.sleep(0.1)
  141. if not self.alive:
  142. break
  143. elif self.test_mode:
  144. # In testing mode the stdin is connected to PTY but is not used for input anything. For PTY
  145. # the canceling by fcntl.ioctl isn't working and would hang in self.console.getkey().
  146. # Therefore, we avoid calling it.
  147. while self.alive:
  148. time.sleep(0.1)
  149. break
  150. c = self.console.getkey()
  151. except KeyboardInterrupt:
  152. c = '\x03'
  153. if c is not None:
  154. self.event_queue.put((TAG_KEY, c), False)
  155. finally:
  156. self.console.cleanup()
  157. def _cancel(self):
  158. if os.name == 'posix' and not self.test_mode:
  159. # this is the way cancel() is implemented in pyserial 3.3 or newer,
  160. # older pyserial (3.1+) has cancellation implemented via 'select',
  161. # which does not work when console sends an escape sequence response
  162. #
  163. # even older pyserial (<3.1) does not have this method
  164. #
  165. # on Windows there is a different (also hacky) fix, applied above.
  166. #
  167. # note that TIOCSTI is not implemented in WSL / bash-on-Windows.
  168. # TODO: introduce some workaround to make it work there.
  169. #
  170. # Note: This would throw exception in testing mode when the stdin is connected to PTY.
  171. import fcntl
  172. import termios
  173. fcntl.ioctl(self.console.fd, termios.TIOCSTI, b'\0')
  174. class SerialReader(StoppableThread):
  175. """ Read serial data from the serial port and push to the
  176. event queue, until stopped.
  177. """
  178. def __init__(self, serial, event_queue):
  179. super(SerialReader, self).__init__()
  180. self.baud = serial.baudrate
  181. self.serial = serial
  182. self.event_queue = event_queue
  183. if not hasattr(self.serial, 'cancel_read'):
  184. # enable timeout for checking alive flag,
  185. # if cancel_read not available
  186. self.serial.timeout = 0.25
  187. def run(self):
  188. if not self.serial.is_open:
  189. self.serial.baudrate = self.baud
  190. self.serial.rts = True # Force an RTS reset on open
  191. self.serial.open()
  192. self.serial.rts = False
  193. try:
  194. while self.alive:
  195. data = self.serial.read(self.serial.in_waiting or 1)
  196. if len(data):
  197. self.event_queue.put((TAG_SERIAL, data), False)
  198. finally:
  199. self.serial.close()
  200. def _cancel(self):
  201. if hasattr(self.serial, 'cancel_read'):
  202. try:
  203. self.serial.cancel_read()
  204. except Exception:
  205. pass
  206. class LineMatcher(object):
  207. """
  208. Assembles a dictionary of filtering rules based on the --print_filter
  209. argument of idf_monitor. Then later it is used to match lines and
  210. determine whether they should be shown on screen or not.
  211. """
  212. LEVEL_N = 0
  213. LEVEL_E = 1
  214. LEVEL_W = 2
  215. LEVEL_I = 3
  216. LEVEL_D = 4
  217. LEVEL_V = 5
  218. level = {'N': LEVEL_N, 'E': LEVEL_E, 'W': LEVEL_W, 'I': LEVEL_I, 'D': LEVEL_D,
  219. 'V': LEVEL_V, '*': LEVEL_V, '': LEVEL_V}
  220. def __init__(self, print_filter):
  221. self._dict = dict()
  222. self._re = re.compile(r'^(?:\033\[[01];?[0-9]+m?)?([EWIDV]) \([0-9]+\) ([^:]+): ')
  223. items = print_filter.split()
  224. if len(items) == 0:
  225. self._dict["*"] = self.LEVEL_V # default is to print everything
  226. for f in items:
  227. s = f.split(r':')
  228. if len(s) == 1:
  229. # specifying no warning level defaults to verbose level
  230. lev = self.LEVEL_V
  231. elif len(s) == 2:
  232. if len(s[0]) == 0:
  233. raise ValueError('No tag specified in filter ' + f)
  234. try:
  235. lev = self.level[s[1].upper()]
  236. except KeyError:
  237. raise ValueError('Unknown warning level in filter ' + f)
  238. else:
  239. raise ValueError('Missing ":" in filter ' + f)
  240. self._dict[s[0]] = lev
  241. def match(self, line):
  242. try:
  243. m = self._re.search(line)
  244. if m:
  245. lev = self.level[m.group(1)]
  246. if m.group(2) in self._dict:
  247. return self._dict[m.group(2)] >= lev
  248. return self._dict.get("*", self.LEVEL_N) >= lev
  249. except (KeyError, IndexError):
  250. # Regular line written with something else than ESP_LOG*
  251. # or an empty line.
  252. pass
  253. # We need something more than "*.N" for printing.
  254. return self._dict.get("*", self.LEVEL_N) > self.LEVEL_N
  255. class SerialStopException(Exception):
  256. """
  257. This exception is used for stopping the IDF monitor in testing mode.
  258. """
  259. pass
  260. class Monitor(object):
  261. """
  262. Monitor application main class.
  263. This was originally derived from miniterm.Miniterm, but it turned out to be easier to write from scratch for this
  264. purpose.
  265. Main difference is that all event processing happens in the main thread, not the worker threads.
  266. """
  267. def __init__(self, serial_instance, elf_file, print_filter, make="make", toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX, eol="CRLF"):
  268. super(Monitor, self).__init__()
  269. self.event_queue = queue.Queue()
  270. self.console = miniterm.Console()
  271. if os.name == 'nt':
  272. sys.stderr = ANSIColorConverter(sys.stderr, decode_output=True)
  273. self.console.output = ANSIColorConverter(self.console.output)
  274. self.console.byte_output = ANSIColorConverter(self.console.byte_output)
  275. if StrictVersion(serial.VERSION) < StrictVersion('3.3.0'):
  276. # Use Console.getkey implementation from 3.3.0 (to be in sync with the ConsoleReader._cancel patch above)
  277. def getkey_patched(self):
  278. c = self.enc_stdin.read(1)
  279. if c == chr(0x7f):
  280. c = chr(8) # map the BS key (which yields DEL) to backspace
  281. return c
  282. self.console.getkey = types.MethodType(getkey_patched, self.console)
  283. socket_mode = serial_instance.port.startswith("socket://") # testing hook - data from serial can make exit the monitor
  284. self.serial = serial_instance
  285. self.console_reader = ConsoleReader(self.console, self.event_queue, socket_mode)
  286. self.serial_reader = SerialReader(self.serial, self.event_queue)
  287. self.elf_file = elf_file
  288. if not os.path.exists(make):
  289. self.make = shlex.split(make) # allow for possibility the "make" arg is a list of arguments (for idf.py)
  290. else:
  291. self.make = make
  292. self.toolchain_prefix = toolchain_prefix
  293. self.menu_key = CTRL_T
  294. self.exit_key = CTRL_RBRACKET
  295. self.translate_eol = {
  296. "CRLF": lambda c: c.replace("\n", "\r\n"),
  297. "CR": lambda c: c.replace("\n", "\r"),
  298. "LF": lambda c: c.replace("\r", "\n"),
  299. }[eol]
  300. # internal state
  301. self._pressed_menu_key = False
  302. self._last_line_part = b""
  303. self._gdb_buffer = b""
  304. self._pc_address_buffer = b""
  305. self._line_matcher = LineMatcher(print_filter)
  306. self._invoke_processing_last_line_timer = None
  307. self._force_line_print = False
  308. self._output_enabled = True
  309. self._serial_check_exit = socket_mode
  310. def invoke_processing_last_line(self):
  311. self.event_queue.put((TAG_SERIAL_FLUSH, b''), False)
  312. def main_loop(self):
  313. self.console_reader.start()
  314. self.serial_reader.start()
  315. try:
  316. while self.console_reader.alive and self.serial_reader.alive:
  317. (event_tag, data) = self.event_queue.get()
  318. if event_tag == TAG_KEY:
  319. self.handle_key(data)
  320. elif event_tag == TAG_SERIAL:
  321. self.handle_serial_input(data)
  322. if self._invoke_processing_last_line_timer is not None:
  323. self._invoke_processing_last_line_timer.cancel()
  324. self._invoke_processing_last_line_timer = threading.Timer(0.1, self.invoke_processing_last_line)
  325. self._invoke_processing_last_line_timer.start()
  326. # If no futher data is received in the next short period
  327. # of time then the _invoke_processing_last_line_timer
  328. # generates an event which will result in the finishing of
  329. # the last line. This is fix for handling lines sent
  330. # without EOL.
  331. elif event_tag == TAG_SERIAL_FLUSH:
  332. self.handle_serial_input(data, finalize_line=True)
  333. else:
  334. raise RuntimeError("Bad event data %r" % ((event_tag,data),))
  335. except SerialStopException:
  336. sys.stderr.write(ANSI_NORMAL + "Stopping condition has been received\n")
  337. finally:
  338. try:
  339. self.console_reader.stop()
  340. self.serial_reader.stop()
  341. # Cancelling _invoke_processing_last_line_timer is not
  342. # important here because receiving empty data doesn't matter.
  343. self._invoke_processing_last_line_timer = None
  344. except Exception:
  345. pass
  346. sys.stderr.write(ANSI_NORMAL + "\n")
  347. def handle_key(self, key):
  348. if self._pressed_menu_key:
  349. self.handle_menu_key(key)
  350. self._pressed_menu_key = False
  351. elif key == self.menu_key:
  352. self._pressed_menu_key = True
  353. elif key == self.exit_key:
  354. self.console_reader.stop()
  355. self.serial_reader.stop()
  356. else:
  357. try:
  358. key = self.translate_eol(key)
  359. self.serial.write(codecs.encode(key))
  360. except serial.SerialException:
  361. pass # this shouldn't happen, but sometimes port has closed in serial thread
  362. except UnicodeEncodeError:
  363. pass # this can happen if a non-ascii character was passed, ignoring
  364. def handle_serial_input(self, data, finalize_line=False):
  365. sp = data.split(b'\n')
  366. if self._last_line_part != b"":
  367. # add unprocessed part from previous "data" to the first line
  368. sp[0] = self._last_line_part + sp[0]
  369. self._last_line_part = b""
  370. if sp[-1] != b"":
  371. # last part is not a full line
  372. self._last_line_part = sp.pop()
  373. for line in sp:
  374. if line != b"":
  375. if self._serial_check_exit and line == self.exit_key.encode('latin-1'):
  376. raise SerialStopException()
  377. if self._output_enabled and (self._force_line_print or self._line_matcher.match(line.decode(errors="ignore"))):
  378. self.console.write_bytes(line + b'\n')
  379. self.handle_possible_pc_address_in_line(line)
  380. self.check_gdbstub_trigger(line)
  381. self._force_line_print = False
  382. # Now we have the last part (incomplete line) in _last_line_part. By
  383. # default we don't touch it and just wait for the arrival of the rest
  384. # of the line. But after some time when we didn't received it we need
  385. # to make a decision.
  386. if self._last_line_part != b"":
  387. if self._force_line_print or (finalize_line and self._line_matcher.match(self._last_line_part.decode(errors="ignore"))):
  388. self._force_line_print = True
  389. if self._output_enabled:
  390. self.console.write_bytes(self._last_line_part)
  391. self.handle_possible_pc_address_in_line(self._last_line_part)
  392. self.check_gdbstub_trigger(self._last_line_part)
  393. # It is possible that the incomplete line cuts in half the PC
  394. # address. A small buffer is kept and will be used the next time
  395. # handle_possible_pc_address_in_line is invoked to avoid this problem.
  396. # MATCH_PCADDR matches 10 character long addresses. Therefore, we
  397. # keep the last 9 characters.
  398. self._pc_address_buffer = self._last_line_part[-9:]
  399. # GDB sequence can be cut in half also. GDB sequence is 7
  400. # characters long, therefore, we save the last 6 characters.
  401. self._gdb_buffer = self._last_line_part[-6:]
  402. self._last_line_part = b""
  403. # else: keeping _last_line_part and it will be processed the next time
  404. # handle_serial_input is invoked
  405. def handle_possible_pc_address_in_line(self, line):
  406. line = self._pc_address_buffer + line
  407. self._pc_address_buffer = b""
  408. for m in re.finditer(MATCH_PCADDR, line.decode(errors="ignore")):
  409. self.lookup_pc_address(m.group())
  410. def handle_menu_key(self, c):
  411. if c == self.exit_key or c == self.menu_key: # send verbatim
  412. self.serial.write(codecs.encode(c))
  413. elif c in [CTRL_H, 'h', 'H', '?']:
  414. red_print(self.get_help_text())
  415. elif c == CTRL_R: # Reset device via RTS
  416. self.serial.setRTS(True)
  417. time.sleep(0.2)
  418. self.serial.setRTS(False)
  419. self.output_enable(True)
  420. elif c == CTRL_F: # Recompile & upload
  421. self.run_make("flash")
  422. elif c == CTRL_A: # Recompile & upload app only
  423. self.run_make("app-flash")
  424. elif c == CTRL_Y: # Toggle output display
  425. self.output_toggle()
  426. elif c == CTRL_P:
  427. yellow_print("Pause app (enter bootloader mode), press Ctrl-T Ctrl-R to restart")
  428. # to fast trigger pause without press menu key
  429. self.serial.setDTR(False) # IO0=HIGH
  430. self.serial.setRTS(True) # EN=LOW, chip in reset
  431. time.sleep(1.3) # timeouts taken from esptool.py, includes esp32r0 workaround. defaults: 0.1
  432. self.serial.setDTR(True) # IO0=LOW
  433. self.serial.setRTS(False) # EN=HIGH, chip out of reset
  434. time.sleep(0.45) # timeouts taken from esptool.py, includes esp32r0 workaround. defaults: 0.05
  435. self.serial.setDTR(False) # IO0=HIGH, done
  436. elif c in [CTRL_X, 'x', 'X']: # Exiting from within the menu
  437. self.console_reader.stop()
  438. self.serial_reader.stop()
  439. else:
  440. red_print('--- unknown menu character {} --'.format(key_description(c)))
  441. def get_help_text(self):
  442. return """
  443. --- idf_monitor ({version}) - ESP-IDF monitor tool
  444. --- based on miniterm from pySerial
  445. ---
  446. --- {exit:8} Exit program
  447. --- {menu:8} Menu escape key, followed by:
  448. --- Menu keys:
  449. --- {menu:7} Send the menu character itself to remote
  450. --- {exit:7} Send the exit character itself to remote
  451. --- {reset:7} Reset target board via RTS line
  452. --- {makecmd:7} Build & flash project
  453. --- {appmake:7} Build & flash app only
  454. --- {output:7} Toggle output display
  455. --- {pause:7} Reset target into bootloader to pause app via RTS line
  456. --- {menuexit:7} Exit program
  457. """.format(version=__version__,
  458. exit=key_description(self.exit_key),
  459. menu=key_description(self.menu_key),
  460. reset=key_description(CTRL_R),
  461. makecmd=key_description(CTRL_F),
  462. appmake=key_description(CTRL_A),
  463. output=key_description(CTRL_Y),
  464. pause=key_description(CTRL_P),
  465. menuexit=key_description(CTRL_X) + ' (or X)')
  466. def __enter__(self):
  467. """ Use 'with self' to temporarily disable monitoring behaviour """
  468. self.serial_reader.stop()
  469. self.console_reader.stop()
  470. def __exit__(self, *args, **kwargs):
  471. """ Use 'with self' to temporarily disable monitoring behaviour """
  472. self.console_reader.start()
  473. self.serial_reader.start()
  474. def prompt_next_action(self, reason):
  475. self.console.setup() # set up console to trap input characters
  476. try:
  477. red_print("""
  478. --- {}
  479. --- Press {} to exit monitor.
  480. --- Press {} to build & flash project.
  481. --- Press {} to build & flash app.
  482. --- Press any other key to resume monitor (resets target).""".format(reason,
  483. key_description(self.exit_key),
  484. key_description(CTRL_F),
  485. key_description(CTRL_A)))
  486. k = CTRL_T # ignore CTRL-T here, so people can muscle-memory Ctrl-T Ctrl-F, etc.
  487. while k == CTRL_T:
  488. k = self.console.getkey()
  489. finally:
  490. self.console.cleanup()
  491. if k == self.exit_key:
  492. self.event_queue.put((TAG_KEY, k))
  493. elif k in [CTRL_F, CTRL_A]:
  494. self.event_queue.put((TAG_KEY, self.menu_key))
  495. self.event_queue.put((TAG_KEY, k))
  496. def run_make(self, target):
  497. with self:
  498. if isinstance(self.make, list):
  499. popen_args = self.make + [target]
  500. else:
  501. popen_args = [self.make, target]
  502. yellow_print("Running %s..." % " ".join(popen_args))
  503. p = subprocess.Popen(popen_args)
  504. try:
  505. p.wait()
  506. except KeyboardInterrupt:
  507. p.wait()
  508. if p.returncode != 0:
  509. self.prompt_next_action("Build failed")
  510. else:
  511. self.output_enable(True)
  512. def lookup_pc_address(self, pc_addr):
  513. cmd = ["%saddr2line" % self.toolchain_prefix,
  514. "-pfiaC", "-e", self.elf_file, pc_addr]
  515. try:
  516. translation = subprocess.check_output(cmd, cwd=".")
  517. if b"?? ??:0" not in translation:
  518. yellow_print(translation.decode())
  519. except OSError as e:
  520. red_print("%s: %s" % (" ".join(cmd), e))
  521. def check_gdbstub_trigger(self, line):
  522. line = self._gdb_buffer + line
  523. self._gdb_buffer = b""
  524. m = re.search(b"\\$(T..)#(..)", line) # look for a gdb "reason" for a break
  525. if m is not None:
  526. try:
  527. chsum = sum(ord(bytes([p])) for p in m.group(1)) & 0xFF
  528. calc_chsum = int(m.group(2), 16)
  529. except ValueError:
  530. return # payload wasn't valid hex digits
  531. if chsum == calc_chsum:
  532. self.run_gdb()
  533. else:
  534. red_print("Malformed gdb message... calculated checksum %02x received %02x" % (chsum, calc_chsum))
  535. def run_gdb(self):
  536. with self: # disable console control
  537. sys.stderr.write(ANSI_NORMAL)
  538. try:
  539. cmd = ["%sgdb" % self.toolchain_prefix,
  540. "-ex", "set serial baud %d" % self.serial.baudrate,
  541. "-ex", "target remote %s" % self.serial.port,
  542. "-ex", "interrupt", # monitor has already parsed the first 'reason' command, need a second
  543. self.elf_file]
  544. process = subprocess.Popen(cmd, cwd=".")
  545. process.wait()
  546. except OSError as e:
  547. red_print("%s: %s" % (" ".join(cmd), e))
  548. except KeyboardInterrupt:
  549. pass # happens on Windows, maybe other OSes
  550. finally:
  551. try:
  552. # on Linux, maybe other OSes, gdb sometimes seems to be alive even after wait() returns...
  553. process.terminate()
  554. except Exception:
  555. pass
  556. try:
  557. # also on Linux, maybe other OSes, gdb sometimes exits uncleanly and breaks the tty mode
  558. subprocess.call(["stty", "sane"])
  559. except Exception:
  560. pass # don't care if there's no stty, we tried...
  561. self.prompt_next_action("gdb exited")
  562. def output_enable(self, enable):
  563. self._output_enabled = enable
  564. def output_toggle(self):
  565. self._output_enabled = not self._output_enabled
  566. yellow_print("\nToggle output display: {}, Type Ctrl-T Ctrl-Y to show/disable output again.".format(self._output_enabled))
  567. def main():
  568. parser = argparse.ArgumentParser("idf_monitor - a serial output monitor for esp-idf")
  569. parser.add_argument(
  570. '--port', '-p',
  571. help='Serial port device',
  572. default=os.environ.get('ESPTOOL_PORT', '/dev/ttyUSB0')
  573. )
  574. parser.add_argument(
  575. '--baud', '-b',
  576. help='Serial port baud rate',
  577. type=int,
  578. default=os.environ.get('MONITOR_BAUD', 115200))
  579. parser.add_argument(
  580. '--make', '-m',
  581. help='Command to run make',
  582. type=str, default='make')
  583. parser.add_argument(
  584. '--toolchain-prefix',
  585. help="Triplet prefix to add before cross-toolchain names",
  586. default=DEFAULT_TOOLCHAIN_PREFIX)
  587. parser.add_argument(
  588. "--eol",
  589. choices=['CR', 'LF', 'CRLF'],
  590. type=lambda c: c.upper(),
  591. help="End of line to use when sending to the serial port",
  592. default='CR')
  593. parser.add_argument(
  594. 'elf_file', help='ELF file of application',
  595. type=argparse.FileType('rb'))
  596. parser.add_argument(
  597. '--print_filter',
  598. help="Filtering string",
  599. default=DEFAULT_PRINT_FILTER)
  600. args = parser.parse_args()
  601. if args.port.startswith("/dev/tty."):
  602. args.port = args.port.replace("/dev/tty.", "/dev/cu.")
  603. yellow_print("--- WARNING: Serial ports accessed as /dev/tty.* will hang gdb if launched.")
  604. yellow_print("--- Using %s instead..." % args.port)
  605. serial_instance = serial.serial_for_url(args.port, args.baud,
  606. do_not_open=True)
  607. serial_instance.dtr = False
  608. serial_instance.rts = False
  609. args.elf_file.close() # don't need this as a file
  610. # remove the parallel jobserver arguments from MAKEFLAGS, as any
  611. # parent make is only running 1 job (monitor), so we can re-spawn
  612. # all of the child makes we need (the -j argument remains part of
  613. # MAKEFLAGS)
  614. try:
  615. makeflags = os.environ["MAKEFLAGS"]
  616. makeflags = re.sub(r"--jobserver[^ =]*=[0-9,]+ ?", "", makeflags)
  617. os.environ["MAKEFLAGS"] = makeflags
  618. except KeyError:
  619. pass # not running a make jobserver
  620. monitor = Monitor(serial_instance, args.elf_file.name, args.print_filter, args.make, args.toolchain_prefix, args.eol)
  621. yellow_print('--- idf_monitor on {p.name} {p.baudrate} ---'.format(
  622. p=serial_instance))
  623. yellow_print('--- Quit: {} | Menu: {} | Help: {} followed by {} ---'.format(
  624. key_description(monitor.exit_key),
  625. key_description(monitor.menu_key),
  626. key_description(monitor.menu_key),
  627. key_description(CTRL_H)))
  628. if args.print_filter != DEFAULT_PRINT_FILTER:
  629. yellow_print('--- Print filter: {} ---'.format(args.print_filter))
  630. monitor.main_loop()
  631. if os.name == 'nt':
  632. # Windows console stuff
  633. STD_OUTPUT_HANDLE = -11
  634. STD_ERROR_HANDLE = -12
  635. # wincon.h values
  636. FOREGROUND_INTENSITY = 8
  637. FOREGROUND_GREY = 7
  638. # matches the ANSI color change sequences that IDF sends
  639. RE_ANSI_COLOR = re.compile(b'\033\\[([01]);3([0-7])m')
  640. # list mapping the 8 ANSI colors (the indexes) to Windows Console colors
  641. ANSI_TO_WINDOWS_COLOR = [0, 4, 2, 6, 1, 5, 3, 7]
  642. GetStdHandle = ctypes.windll.kernel32.GetStdHandle
  643. SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
  644. class ANSIColorConverter(object):
  645. """Class to wrap a file-like output stream, intercept ANSI color codes,
  646. and convert them into calls to Windows SetConsoleTextAttribute.
  647. Doesn't support all ANSI terminal code escape sequences, only the sequences IDF uses.
  648. Ironically, in Windows this console output is normally wrapped by winpty which will then detect the console text
  649. color changes and convert these back to ANSI color codes for MSYS' terminal to display. However this is the
  650. least-bad working solution, as winpty doesn't support any "passthrough" mode for raw output.
  651. """
  652. def __init__(self, output=None, decode_output=False):
  653. self.output = output
  654. self.decode_output = decode_output
  655. self.handle = GetStdHandle(STD_ERROR_HANDLE if self.output == sys.stderr else STD_OUTPUT_HANDLE)
  656. self.matched = b''
  657. def _output_write(self, data):
  658. try:
  659. if self.decode_output:
  660. self.output.write(data.decode())
  661. else:
  662. self.output.write(data)
  663. except IOError:
  664. # Windows 10 bug since the Fall Creators Update, sometimes writing to console randomly throws
  665. # an exception (however, the character is still written to the screen)
  666. # Ref https://github.com/espressif/esp-idf/issues/1136
  667. pass
  668. def write(self, data):
  669. if isinstance(data, bytes):
  670. data = bytearray(data)
  671. else:
  672. data = bytearray(data, 'utf-8')
  673. for b in data:
  674. b = bytes([b])
  675. length = len(self.matched)
  676. if b == b'\033': # ESC
  677. self.matched = b
  678. elif (length == 1 and b == b'[') or (1 < length < 7):
  679. self.matched += b
  680. if self.matched == ANSI_NORMAL.encode('latin-1'): # reset console
  681. # Flush is required only with Python3 - switching color before it is printed would mess up the console
  682. self.flush()
  683. SetConsoleTextAttribute(self.handle, FOREGROUND_GREY)
  684. self.matched = b''
  685. elif len(self.matched) == 7: # could be an ANSI sequence
  686. m = re.match(RE_ANSI_COLOR, self.matched)
  687. if m is not None:
  688. color = ANSI_TO_WINDOWS_COLOR[int(m.group(2))]
  689. if m.group(1) == b'1':
  690. color |= FOREGROUND_INTENSITY
  691. # Flush is required only with Python3 - switching color before it is printed would mess up the console
  692. self.flush()
  693. SetConsoleTextAttribute(self.handle, color)
  694. else:
  695. self._output_write(self.matched) # not an ANSI color code, display verbatim
  696. self.matched = b''
  697. else:
  698. self._output_write(b)
  699. self.matched = b''
  700. def flush(self):
  701. self.output.flush()
  702. if __name__ == "__main__":
  703. main()