Menu

#473 No way to silence meta-data output in telnet interface

0.12.0
new
nobody
None
2026-08-07
2026-08-04
fsxx2
No

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:

  1. Print metadata to stderr.
  2. Suppress printing of metadata in general, via an openocd option. Least amount of overhead for communication over telnet interface.
  3. Control printing of metadata individually, via an openocd command.

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!

Discussion

  • Evgeniy Naydanov

    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:

    1. Use read_memory instead of mdh -- it's output is a Tcl list of read items with no additional formatting (in hex):
    $ echo 'read_memory 0x80000000 16 1;  exit' | netcat localhost 4444
    0xa001
    
    1. Write a custom Tcl function to output the memory in the format you like and load the script via -f when 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] vs read_memory
    • mw[bhwq] vs write_memory
    • reg vs get_reg

    I would still suggest you replace Telnet with Tcl RPC -- it provides better error handling and is better suited for scripting.

     
  • fsxx2

    fsxx2 - 2026-08-06

    Thanks for your very detailed and helpful reply!

    I was not aware of the alternative command forms, and tried them out:

    $ echo 'read_memory 0x40020810 16 1;  exit' | netcat localhost 4444
    ��������Open On-Chip Debugger
    > read_memory 0x40020810 16 1;  exit
    $
    

    Okay, it doesn't like the "; exit", but

    $ echo -e 'read_memory 0x40020810 16 1\nexit' | netcat localhost 4444
    ��������Open On-Chip Debugger
    > read_memory 0x40020410 16 1
    0xc230
    > exit
    $
    

    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:

    $ echo -e 'read_memory 0x40020810 16 1'$'\x1a' | netcat localhost 6666 | tr -d $'\x1a'
    0xc230
    $
    

    which is much better, yay! 😊 GPIO reading times for each port were cut roughly in half.

     
  • Jan Matyas

    Jan Matyas - 2026-08-07

    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:

    python3 -c '
    from py_openocd_client import PyOpenocdClient
    with PyOpenocdClient("localhost", 6666) as ocd:
        print(ocd.cmd("read_memory 0x40020810 16 1").out)
    '
    
     

Log in to post a comment.