The telnet interface could be a nice an easily usable interface for scripting, but unfortunately, there seems to be no way to let it output just the raw data one requested, but instead there is always some metadata printed:
$ echo -e 'mdh 0x40020810\nexit' | netcat localhost 4444
��������Open On-Chip Debugger
> mdh 0x40020410
0x40020410: 0280
> exit
$
Which leads to all sorts of contortions you have to go through just to extract the actual data, e.g.:
$ echo -e 'mdh 0x40020410\nexit' | netcat localhost 4444 | sed -n '3s/.*: //p' | tr -d ' \r'
0280
$
What I would instead expect is metadata printed to stderr, real data printed to stdout, so one can use something like this:
$ echo -e 'mdh 0x40020810\nexit' | netcat localhost 4444 2>/dev/null
0280
$
So, my suggested solutions would be, in descending order of personal preference:
Depending on your resources, you could of course implement 1+3 or 2+3, so users have both an easy control for general metadata output supression, and a means to finely control metadata output. Implementing 1+2+3 is the luxury option. :-)
Admittedly, my current use case of quickly polling GPIOs on a connected target to track it LEDs in realtime on the PC screen might be an edge case, but even in general, having to parse the openocd output IMHO unnecessarily binds time and memory space resources.
Thanks for your consideration!
AFAIU, Telnet is not designed for RPC, but rather for an interactive session. Please consider using Tcl RPC server instead.
https://openocd.org/doc-release/html/Tcl-Scripting-API.html#Tcl-RPC-server
I'm sorry, I've misunderstood your issue initially.
IIUC, you would like to change the format of the
mdh's output.I would like to suggest the following alternatives:
read_memoryinstead ofmdh-- it's output is a Tcl list of read items with no additional formatting (in hex):-fwhen running OpenOCD.I'd be cautious about changing the output format of an existing command. This will break user scripts.
Most OpenOCD commands don't print to stdout or stderr, they are Tcl procedures and they return a string. This string is then reported to the connection.
A global option that would control the output format (human-friendly vs machine-friendly) can be a solution, but currently the issue is solved by an alternative -- different commands for humans vs scripts:
md[bhwq]vsread_memorymw[bhwq]vswrite_memoryregvsget_regI would still suggest you replace Telnet with Tcl RPC -- it provides better error handling and is better suited for scripting.
Thanks for your very detailed and helpful reply!
I was not aware of the alternative command forms, and tried them out:
Okay, it doesn't like the "; exit", but
works, and at least it forgoes with the memory address prefix in the output -- but the all the other metadata remains.
I then tried the Tcl API available at port 6666, as you suggested:
which is much better, yay! 😊 GPIO reading times for each port were cut roughly in half.
Hello fsxx2,
I agree with Evgeniy that the Tcl API is better suited for scripting and connecting external programs to OpenOCD.
If you'd want a more robust tool than the simple netcat, you can talk to OpenOCD via the PyOpenocdClient library. (For transparency, I'm the author.) This library will handle the meta-characters used in the Tcl RPC channel, and will also properly report errors that might occur, like connection errors or failed Tcl commands.
Here is a python command that will read the value from memory and print it directly to the standard output: