Skip to content

Question: gdb/openocd synchronization/possible race condition #186

Closed

Description

@TommyMurphyTM1234

I hope it's OK to post this here in case anybody has some insight even though admittedly it's almost certainly not a GNU ARM Eclipse plugin issue per se (or at all) and I realise that the FAQ says to only post GNU ARM Eclipse issues.

I see here (http://gnuarmeclipse.github.io/debug/openocd/) under "Quote the entire echo command" a snippet about gdb synchronizing with openocd using the -c 'echo "Started by GNU ARM Eclipse"' command.

Is that the sum total of synchronization between gdb and openocd?
At what point does it happen?
In particular does that synchronization ensure that openocd has initialized fully (e.g. gdb/mi port 3333 and telnet port 4444 servers if applicable etc.) or just that it has started up but may still have initialization to do?

My problem is that in some cases it looks like gdb tries to attach to openocd's gdb/mi port 3333 before openocd has successfully initialized that "server" and I get this:

Error in final launch sequence
Failed to execute MI command:
-target-select remote localhost:3333
Error message from debugger back end:
localhost:3333: No connection could be made because the target machine actively refused it.
Failed to execute MI command:
-target-select remote localhost:3333
Error message from debugger back end:
localhost:3333: No connection could be made because the target machine actively refused it.
localhost:3333: No connection could be made because the target machine actively refused it.

Specifically I don't get this if I have one of our FlashPro programmers attached (driven by a custom JTAG driver that I have added to openocd). But if I have more than one then the added time taken to enumerate/init etc. these seems to delay openocd sufficiently that it has not yet initialized the gdb/mi port 3333 server by the time that gdb attempts to connect.

However even though it has not happened to date with a single programmer attached it seems to me that there is some latent race condition between gdb and openocd and it was simply luck that avoided problems previously?

Any tips on how to synchronize gdb and openocd in order to avoid this problem?

Thanks and apologies again if this is somewhat off topic from GNU ARM Eclipse proper.

Regards
Tommy`

Activity

  1. ilg-ul commented on Mar 7, 2017

    @ilg-ul
    Contributor

    I hope it's OK to post this here

    let's say it is somehow related, but generally questions should be addressed via the project forum.

    -c 'echo "Started by GNU ARM Eclipse"' ... the sum total of synchronization

    unfortunately it is the only portable synchronisation method I could find.

    At what point does it happen?

    it happens when OpenOCD decides to print this message. in most cases it is when the board is ready.

    does that synchronization ensure that openocd has initialized fully

    I have no control over this.

    some latent race condition between gdb and openocd

    for you to have a reference, the SEGGER J-Link GDB Server, by design, prints a message to confirm it is waiting for connections. this method is a good synchronisation method, and I don't recall such problems with the J-Link plug-in.

    OpenOCD does not print such messages, so I had to find a workaround. I personally do not use OpenOCD, and do not recommend it to users.

    if you decide to improve OpenOCD, I suggest you contribute a patch to OpenOCD, to print the waiting connection message, and later I can update the plug-ins to use it.

  2. self-assigned this
    on Mar 7, 2017
  3. TommyMurphyTM1234 commented on Mar 7, 2017

    @TommyMurphyTM1234
    Author

    Hi Liviu - thanks for the feedback.
    That's very useful.
    I will investigate further on the openocd side of things.
    Just to clarify one thing - I guess that the plugin waits for the "Started by GNU ARM Eclipse" string to appear from openocd?
    What does it do once it sees it?
    Regards
    Tommy

  4. ilg-ul commented on Mar 7, 2017

    @ilg-ul
    Contributor

    What does it do once it sees it?

    starts the GDB client.

  5. TommyMurphyTM1234 commented on Mar 7, 2017

    @TommyMurphyTM1234
    Author

    Thanks Liviu - I was confusing gdb starting openocd (piped mode) with socket mode used here where openocd and gdb are started separately (in this case by the plugin). I'll continue looking into this to see if I can get a better solution. At the moment the only one I have is to put a counter delay at the top of gdbinit which is a real hack! :-)

  6. ilg-ul commented on Mar 7, 2017

    @ilg-ul
    Contributor

    looking into this to see if I can get a better solution

    yeah, you can switch to J-Link.

  7. TommyMurphyTM1234 commented on Mar 7, 2017

    @TommyMurphyTM1234
    Author

    I hear ya! :-) But unfortunately I have to support OpenOCD in this case. :-(
    Thanks a lot for your feedback on this issue.

  8. TommyMurphyTM1234 commented on Mar 7, 2017

    @TommyMurphyTM1234
    Author

    In case it helps anybody else ... one hack (obviously not ideal) which helps is to delay for a period before allowing gdb to proceed. The period to delay will vary and must be chosen by trial and error.
    One way to do this in a portable way (since Windows doesn't seem to have a workable equivalent of shell sleep x) is to use ping - e.g. change Debugger > GDB Client Setup > Commands to

    shell ping 127.0.0.1 -n 3
    set mem inaccessible-by-default off
    

    Where -n specifies the number of seconds (- 1?) to delay and the value needs to be large enough to give OpenOCD time to fully init.
    But as I said it's just a hack and the value may differ from one machine/environment/load to another.

  9. TommyMurphyTM1234 commented on Apr 5, 2017

    @TommyMurphyTM1234
    Author

    FWIW I have worked around this for now using my own hack to OpenOCD but this is not an ideal/general fix for everybody. When I get the time I will follow up on this with the OpenOCD project - e.g. start a discussion about the issue and maybe request an enhancement to address it.
    For now here is my code change to src/openocd.c (0.10.0 release version) in case it helps anybody else:

    /** OpenOCD runtime meat that can become single-thread in future. It parse
     * commandline, reads configuration, sets up the target and starts server loop.
     * Commandline arguments are passed into this function from openocd_main().
     */
    static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
    {
    	int ret;
    	
    #ifdef MICROSEMI
    	/*
    	 * If launched by GNU ARM Eclipse (GAE) then strip the trailing -c and 
    	 * 'echo "Started by GNU ARM Eclipse"' command line args. GAE uses this 
    	 * string as a simple synchronization mechanism to tell it when it can 
    	 * launch GDB which will then connect to OpenOCD's GDB/MI socket interface.
    	 * However this echo command is processed by parse_config_file() but
    	 * the GDB/MI socket interface is only initialized/configure later by
    	 * command_run_line() so there's a race condition whereby GDB may try to 
    	 * connect before this interface is ready. So instead the string is echoed
    	 * later after we know that the GDB/MI interface is ready and waiting for 
    	 * connections.
    	 * 
    	 * See also: https://sourceforge.net/p/openocd/tickets/147/
    	 */
    	int f_GAE = 0;
    	if (strstr(argv[argc-1], "Started by GNU ARM Eclipse"))
    	{
    		argc -= 2;
    		f_GAE = 1;
    	}
    #endif /* MICROSEMI */
    	
    	if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
    		return ERROR_FAIL;
    
    	if (server_preinit() != ERROR_OK)
    		return ERROR_FAIL;
    
    	ret = parse_config_file(cmd_ctx);
    	if (ret == ERROR_COMMAND_CLOSE_CONNECTION)
    		return ERROR_OK;
    	else if (ret != ERROR_OK)
    		return ERROR_FAIL;
    
    	ret = server_init(cmd_ctx);
    	if (ERROR_OK != ret)
    		return ERROR_FAIL;
    
    	if (init_at_startup) {
    		ret = command_run_line(cmd_ctx, "init");
    		if (ERROR_OK != ret)
    			return ERROR_FAIL;
    	}
    
    #ifdef MICROSEMI
    	/* See comments above */
    	if (f_GAE)
    	{
    		LOG_USER("Started by GNU ARM Eclipse");
    	}
    #endif /* MICROSEMI */
    
    	ret = server_loop(cmd_ctx);
    
    	int last_signal = server_quit();
    	if (last_signal != ERROR_OK)
    		return last_signal;
    
    	if (ret != ERROR_OK)
    		return ERROR_FAIL;
    	return ERROR_OK;
    }
    

    Liviu - I presume that you build OpenOCD as-is and don't apply any of your own patches/mods to it?

  10. ilg-ul commented on Apr 5, 2017

    @ilg-ul
    Contributor

    I presume that you build OpenOCD as-is

    that's correct. unless absolutely necessary, I avoid adding patches, since they increase maintenance cost.

    unfortunately your patch is not very fortunate, I would search for a more elaborate solution.

  11. TommyMurphyTM1234 commented on Apr 5, 2017

    @TommyMurphyTM1234
    Author

    unfortunately your patch is not very fortunate, I would search for a more elaborate solution.

    I agree but I am using that for now as I build OpenOCD with some other mods right now.
    As I said I will chase it up with the OpenOCD project when I get a chance - hopefully soon.
    Thanks.

  12. ilg-ul commented on Apr 5, 2017

    @ilg-ul
    Contributor

    a more portable trick would be a small delay in ms, default 500, user configurable in the plug-in.

  13. TommyMurphyTM1234 commented on Apr 5, 2017

    @TommyMurphyTM1234
    Author

    I don't think that depending on a timed delay is a good idea.
    I think your original idea of having openocd (optionally?) emit some message/banner to announce when it is truly ready for connections is a better idea.
    Or having it delay processing command line specified "echo" commands until it is ready for connections so that the existing synchronization with the "Started by GNU ARM Eclipse" output works as is.
    As I said I'll flag the issue with the OpenOCD project ASAP to see what they think about this and to see if it has been reported/mentioned before now.

  14. ilg-ul commented on Apr 5, 2017

    @ilg-ul
    Contributor

    having openocd (optionally?) emit some message/banner to announce when it is truly ready for connections is a better idea

    if you manage to push some changes to the mainstream openocd to have a stable, 100% reliable way of getting this message, even if when enabled by a switch or command, then I'll consider updating the plug-in, although we need to maintain compatibility with older versions too.

  15. 18 remaining items

  16. ilg-ul commented on Sep 4, 2017

    @ilg-ul
    Contributor

    if you confirm that hunting for this string solves your problem, I'll update the plug-ins.

  17. TommyMurphyTM1234 commented on Sep 4, 2017

    @TommyMurphyTM1234
    Author

    as long as future versions will not change it, yes... :-(

    Hmmm - yes - forward compatibility is always a challenge... :-|

    if you confirm that hunting for this string solves your problem, I'll update the plug-ins.

    Thanks - I'm looking into it now with my modified GME openocd plugin and will do some experimentation and report back.

    BTW - in relation to earlier questions about the relative timing of messages from OpenOCD this might clarify:

    Open On-Chip Debugger 0.10.0+dev-00142-g645b474b-dirty (2017-09-03-13:53)
    Licensed under GNU GPL v2
    For bug reports, read
    	http://openocd.org/doc/doxygen/bugs.html
    M2S090
    Info : only one transport option; autoselect 'jtag'
    adapter speed: 6000 kHz
    cortex_m reset_config sysresetreq
    trst_only separate trst_push_pull
    do_board_reset_init
    2017-09-03 14:31:45.629 : Started by GNU ARM Eclipse
    Info : 2017-09-03 14:31:45.631 : Listening on port 6666 for tcl connections
    Info : 2017-09-03 14:31:45.631 : Listening on port 4444 for telnet connections
    Info : 2017-09-03 14:31:45.632 : microsemi_flashpro_initialize start
    Info : FlashPro ports available: E200OP5M0
    Info : FlashPro port used: E200OP5M0
    Info : 2017-09-03 14:31:46.105 : microsemi_flashpro_initialize end
    Info : clock speed 6000 kHz
    Info : JTAG tap: M2S090.tap tap/device found: 0x2f8071cf (mfg: 0x0e7 (GateField), part: 0xf807, ver: 0x2)
    Info : JTAG tap: M2S090.tap disabled
    Info : JTAG tap: M2S090.dap enabled
    2017-09-03 14:31:46.107 : Info : Cortex-M3 IDCODE = 0x4ba00477
    Info : M2S090.cpu: hardware has 6 breakpoints, 4 watchpoints
    Info : 2017-09-03 14:31:46.138 : Listening on port 3333 for gdb connections
    Info : 2017-09-03 14:31:46.478 : accepting 'gdb' connection on tcp/3333
    undefined debug reason 7 - target needs reset
    Warn : target not halted
    
    Info : dropped 'gdb' connection
    
    

    The "Listening on port 3333 for gdb connections" message seems to be a SiFive addition - not sure why.
    Note that the "Started by GNU MCU Eclipse" message appears very early on and (in relative terms) well before the "accepting 'gdb' connections on tcp/3333" message.
    In some (many?) cases the intervening initialization will happen "quick enough" that by the time the plugin starts gdb and it tries to make a GDB RSP connection to port 3333 everything is ready and it works.
    But in some cases (e.g. a JTAG interface driver that takes "longer" to init - such as ours) then this will not be the case and GDB will be launched and will attempt a GDB RSP connection to 3333 before the socket it completely ready.

  18. AntonKrug commented on Sep 4, 2017

    @AntonKrug
    Contributor

    I apologise if this is a stupid question, but would it be viable to probe the port with netstat or lsof to see if the advertised port is really LISTENING?

    Not even sure if there is Windows equivalent for it.

  19. ilg-ul commented on Sep 4, 2017

    @ilg-ul
    Contributor

    as long as it is intended to help, there is no such thing as a stupid question.

    it might work, but:

    • it is not very portable across linux/macOS/windows
    • it would require to parse the ouput
    • if it is not listening, what to do? loop forever and invoke the program until it is?

    it seems complicated and heavy.

  20. truhlikfredy commented on Sep 4, 2017

    @truhlikfredy

    Probing it on Java level gets rid of the parsing:

    http://www.geekality.net/2013/04/30/java-simple-check-to-see-if-a-server-is-listening-on-a-port/

    But I'm not sure that a empty connection and disconnecting will trigger something undesired on the openOCD side.

    I was trying to change the wait till the "accepting 'gdb' connection" and I got timeouts, so I wondered if I never received that text.

    So I put lot of System.out.println and run eclipse with -consoleLog and for some reason last text I received was the "Started by GNU ARM Eclipse". I'm wondering would it be possible that something interrupts/breaks the error pipe and I'm not receiving any more information from the current pipe?

  21. ilg-ul commented on Sep 4, 2017

    @ilg-ul
    Contributor

    where exactly is written the openocd log, to stdout or stderr? is it the same stream as the one used for -c echo?

  22. TommyMurphyTM1234 commented on Sep 4, 2017

    @TommyMurphyTM1234
    Author

    This is what we were getting in the log (e.g. when Eclipse is launched with -consoleLog):

    !ENTRY ilg.gnumcueclipse.debug.gdbjtag.openocd 4 1 2017-09-04 13:45:45.477
    !MESSAGE D:\sandbox\eclipse\oxygen\eclipse\/../openocd/bin/openocd.exe -c gdb_port 3333 -c telnet_port 4444 --command set DEVICE M2S090 --file board/microsemi-cortex-m3.cfg -c echo "Started by GNU MCU Eclipse"
    Open On-Chip Debugger 0.10.0+dev-00142-g645b474b-dirty (2017-09-03-13:53)
    Licensed under GNU GPL v2
    For bug reports, read
            http://openocd.org/doc/doxygen/bugs.html
    M2S090
    Info : only one transport option; autoselect 'jtag'
    adapter speed: 6000 kHz
    cortex_m reset_config sysresetreq
    trst_only separate trst_push_pull
    do_board_reset_init
    2017-09-04 13:45:45.564 : Started by GNU MCU Eclipse
    Info : 2017-09-04 13:45:45.565 : Listening on port 6666 for tcl connections
    Info : 2017-09-04 13:45:45.566 : Listening on port 4444 for telnet connections
    Info : 2017-09-04 13:45:45.566 : microsemi_flashpro_initialize start
    Info : FlashPro ports available: E200OP5M0
    Info : FlashPro port used: E200OP5M0
    Info : 2017-09-04 13:45:46.038 : microsemi_flashpro_initialize end
    Info : clock speed 6000 kHz
    Info : JTAG tap: M2S090.tap tap/device found: 0x2f8071cf (mfg: 0x0e7 (GateField), part: 0xf807, ver: 0x2)
    Info : JTAG tap: M2S090.tap disabled
    Info : JTAG tap: M2S090.dap enabled
    2017-09-04 13:45:46.040 : Info : Cortex-M3 IDCODE = 0x4ba00477
    Info : M2S090.cpu: hardware has 6 breakpoints, 4 watchpoints
    Info : 2017-09-04 13:45:46.069 : Listening on port 3333 for gdb connections
    

    The "accepting 'gdb' connections on tcp/3333" message does't appear there even though it is appearing in the OpenOCD log in Eclipse and is going to the stderr as it's coloured red in the console and other LOG_INFO messages are obviously visible to the plugin Java code.

    It's very very odd....

  23. TommyMurphyTM1234 commented on Sep 4, 2017

    @TommyMurphyTM1234
    Author

    So I put lot of System.out.println and run eclipse with -consoleLog and for some reason last text I received was the "Started by GNU ARM Eclipse". I'm wondering would it be possible that something interrupts/breaks the error pipe and I'm not receiving any more information from the current pipe

    Just to clarify - Anton sees "Started by GNU ARM Eclipse" last because he is using a version of OpenOCD with my ugly hack that moves this log message to the end of openocd.c:openocd_thread() instead of it appearing at the start as it normally does in the unhacked openocd.

  24. ilg-ul commented on Sep 4, 2017

    @ilg-ul
    Contributor

    it looks like there was a good reason why I used the -c echo hack...

  25. truhlikfredy commented on Sep 4, 2017

    @truhlikfredy

    In our case it should be the stderr

    I should probably try the same with the vanilla OpenOCD. I tried to debug the plugin, so I got the Eclipse SDK and I got so far that the SDK Eclipse was able to compile the plugin and then run Eclipse Oxygen with it, but I didn't far enough to be able to debug it.

    Something odd/funny is on the OpenOCD which doesn't not work well with the pipes the way they are handled.

  26. h4tr3d commented on Dec 7, 2017

    @h4tr3d

    Hi guys!

    By the way, I found that GDB 8.0 + OpenOCD 0.10.0 in most case unusable combination in case, when some RTOS (ThreadX in my case) is used and RTOS support module used by OpenOCD. Often I observe such messages when try to stop process:

    Cannot execute this command without a live selected thread.
    

    All work good with GDB 7.7.1 (original) and 7.8 (Linaro branch) and OpenOCD 0.10.0. Currently I have no ideas how it report and just freeze used GDB version.

    Issue found on Manjaro Linux using GDB located in repos and Qt Creator, but I reproduce issues using only GDB and OpenOCD from command line. I deal with Cypress FX3 chip in our production board.

  27. ilg-ul commented on Dec 7, 2017

    @ilg-ul
    Contributor

    I reproduce issues using only GDB and OpenOCD from command line

    I cannot confirm it, for my RISC-V tests I use the latest GDB and the latest OpenOCD and did not encounter this problem.

  28. h4tr3d commented on Dec 7, 2017

    @h4tr3d

    RISC-V

    Cypress FX3 uses ARM926E-JS core and RTOS ThreadX. OpenOCD ability to handle RTOS threads is used too. Seems, that RTOS and it handling on the OpenOCD/GDB side is a major thing.

  29. TommyMurphyTM1234 commented on Dec 7, 2017

    @TommyMurphyTM1234
    Author

    I don't see what these posts have to do with the original topic of this thread?!

  30. ilg-ul commented on Dec 7, 2017

    @ilg-ul
    Contributor

    they probably don't.

    @h4tr3d, for support and general questions, please use the project forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions