OpenOCD
openocd.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007-2010 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 Richard Missenden *
11  * richard.missenden@googlemail.com *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include "openocd.h"
19 #include <jtag/adapter.h>
20 #include <jtag/jtag.h>
21 #include <transport/transport.h>
22 #include <helper/util.h>
23 #include <helper/configuration.h>
24 #include <flash/nor/core.h>
25 #include <flash/nand/core.h>
26 #include <pld/pld.h>
27 #include <target/arm_cti.h>
28 #include <target/arm_adi_v5.h>
29 #include <target/arm_tpiu_swo.h>
30 #include <rtt/rtt.h>
31 
32 #include <server/server.h>
33 #include <server/gdb_server.h>
34 #include <server/rtt_server.h>
35 
36 #ifdef HAVE_STRINGS_H
37 #include <strings.h>
38 #endif
39 
40 #ifdef PKGBLDDATE
41 #define OPENOCD_VERSION \
42  "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
43 #else
44 #define OPENOCD_VERSION \
45  "Open On-Chip Debugger " VERSION RELSTR
46 #endif
47 
48 static const char openocd_startup_tcl[] = {
49 #include "startup_tcl.inc"
50 0 /* Terminate with zero */
51 };
52 
53 /* Give scripts and TELNET a way to find out what version this is */
54 COMMAND_HANDLER(handler_version_command)
55 {
56  char *version_str = OPENOCD_VERSION;
57 
58  if (CMD_ARGC > 1)
60 
61  if (CMD_ARGC == 1) {
62  if (strcmp("git", CMD_ARGV[0]))
64 
65  version_str = GITVERSION;
66  }
67 
68  command_print(CMD, "%s", version_str);
69 
70  return ERROR_OK;
71 }
72 
74  enum target_event event,
75  void *priv)
76 {
77  switch (event) {
79  target->verbose_halt_msg = false;
80  break;
82  target->verbose_halt_msg = true;
83  break;
85  if (target->verbose_halt_msg) {
86  /* do not display information when debugger caused the halt */
88  }
89  break;
90  default:
91  break;
92  }
93 
94  return ERROR_OK;
95 }
96 
97 static bool init_at_startup = true;
98 
99 COMMAND_HANDLER(handle_noinit_command)
100 {
101  if (CMD_ARGC != 0)
103  init_at_startup = false;
104  return ERROR_OK;
105 }
106 
107 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
108 COMMAND_HANDLER(handle_init_command)
109 {
110 
111  if (CMD_ARGC != 0)
113 
114  int retval;
115  static int initialized;
116  if (initialized)
117  return ERROR_OK;
118 
119  initialized = 1;
120 
121  bool save_poll_mask = jtag_poll_mask();
122 
123  retval = command_run_line(CMD_CTX, "target init");
124  if (retval != ERROR_OK)
125  return ERROR_FAIL;
126 
127  retval = adapter_init(CMD_CTX);
128  if (retval != ERROR_OK) {
129  /* we must be able to set up the debug adapter */
130  return retval;
131  }
132 
133  LOG_DEBUG("Debug Adapter init complete");
134 
135  /* "transport init" verifies the expected devices are present;
136  * for JTAG, it checks the list of configured TAPs against
137  * what's discoverable, possibly with help from the platform's
138  * JTAG event handlers. (which require COMMAND_EXEC)
139  */
141 
142  retval = command_run_line(CMD_CTX, "transport init");
143  if (retval != ERROR_OK)
144  return ERROR_FAIL;
145 
146  retval = command_run_line(CMD_CTX, "dap init");
147  if (retval != ERROR_OK)
148  return ERROR_FAIL;
149 
150  LOG_DEBUG("Examining targets...");
151  if (target_examine() != ERROR_OK)
152  LOG_DEBUG("target examination failed");
153 
155 
156  if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
157  return ERROR_FAIL;
158 
159  if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
160  return ERROR_FAIL;
161 
162  if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
163  return ERROR_FAIL;
165 
166  /* in COMMAND_EXEC, after target_examine(), only tpiu or only swo */
167  if (command_run_line(CMD_CTX, "tpiu init") != ERROR_OK)
168  return ERROR_FAIL;
169 
170  jtag_poll_unmask(save_poll_mask);
171 
172  /* initialize telnet subsystem */
174  return ERROR_FAIL;
175 
177 
178  if (command_run_line(CMD_CTX, "_run_post_init_commands") != ERROR_OK)
179  return ERROR_FAIL;
180 
181  return ERROR_OK;
182 }
183 
184 COMMAND_HANDLER(handle_add_script_search_dir_command)
185 {
186  if (CMD_ARGC != 1)
188 
190 
191  return ERROR_OK;
192 }
193 
194 static const struct command_registration openocd_command_handlers[] = {
195  {
196  .name = "version",
197  .handler = handler_version_command,
198  .mode = COMMAND_ANY,
199  .help = "show program version",
200  .usage = "[git]",
201  },
202  {
203  .name = "noinit",
204  .handler = &handle_noinit_command,
205  .mode = COMMAND_CONFIG,
206  .help = "Prevent 'init' from being called at startup.",
207  .usage = ""
208  },
209  {
210  .name = "init",
211  .handler = &handle_init_command,
212  .mode = COMMAND_ANY,
213  .help = "Initializes configured targets and servers. "
214  "Changes command mode from CONFIG to EXEC. "
215  "Unless 'noinit' is called, this command is "
216  "called automatically at the end of startup.",
217  .usage = ""
218  },
219  {
220  .name = "add_script_search_dir",
221  .handler = &handle_add_script_search_dir_command,
222  .mode = COMMAND_ANY,
223  .help = "dir to search for config files and scripts",
224  .usage = "<directory>"
225  },
227 };
228 
229 static int openocd_register_commands(struct command_context *cmd_ctx)
230 {
232 }
233 
235 
236 static int (* const command_registrants[])(struct command_context *cmd_ctx_value) = {
251 };
252 
253 static struct command_context *setup_command_handler(Jim_Interp *interp)
254 {
255  log_init();
256  LOG_DEBUG("log_init: complete");
257 
259 
260  /* register subsystem commands */
261  for (unsigned int i = 0; i < ARRAY_SIZE(command_registrants); i++) {
262  int retval = (*command_registrants[i])(cmd_ctx);
263  if (retval != ERROR_OK) {
264  command_done(cmd_ctx);
265  return NULL;
266  }
267  }
268  LOG_DEBUG("command registration: complete");
269 
271  "Licensed under GNU GPL v2\n");
272 
273  global_cmd_ctx = cmd_ctx;
274 
275  return cmd_ctx;
276 }
277 
283 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
284 {
285  int ret;
286 
287  if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
288  return ERROR_FAIL;
289 
290  if (server_preinit() != ERROR_OK)
291  return ERROR_FAIL;
292 
293  ret = parse_config_file(cmd_ctx);
294  if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
295  /* Shutdown command encountered while processing the initial
296  * commands/scripts. */
297  server_quit(); /* gdb server may be initialized by -c init */
298  return ERROR_OK;
299  } else if (ret != ERROR_OK) {
300  server_quit(); /* gdb server may be initialized by -c init */
301  return ERROR_FAIL;
302  }
303 
304  ret = server_init(cmd_ctx);
305  if (ret != ERROR_OK)
306  return ERROR_FAIL;
307 
308  if (init_at_startup) {
309  ret = command_run_line(cmd_ctx, "init");
310  if (ret != ERROR_OK) {
311  server_quit();
312  return ERROR_FAIL;
313  }
314  }
315 
316  server_loop(cmd_ctx);
317 
318  server_quit();
319 
320  return ERROR_OK;
321 }
322 
323 /* normally this is the main() function entry, but if OpenOCD is linked
324  * into application, then this fn will not be invoked, but rather that
325  * application will have it's own implementation of main(). */
326 int openocd_main(int argc, char *argv[])
327 {
328  /* initialize commandline interface */
329  struct command_context *cmd_ctx;
330 
331  cmd_ctx = setup_command_handler(NULL);
332 
333  if (util_init(cmd_ctx) != ERROR_OK)
334  return EXIT_FAILURE;
335 
336  if (rtt_init() != ERROR_OK)
337  return EXIT_FAILURE;
338 
339  LOG_OUTPUT("For bug reports, read\n\t"
340  "http://openocd.org/doc/doxygen/bugs.html"
341  "\n");
342 
345 
347 
348  /* Start the executable meat that can evolve into thread in future. */
349  int ret = openocd_thread(argc, argv, cmd_ctx);
350 
354  server_free();
355 
356  unregister_all_commands(cmd_ctx, NULL);
357  help_del_all_commands(cmd_ctx);
358 
359  /* free all DAP and CTI objects */
361  dap_cleanup_all();
362 
363  adapter_quit();
364 
366 
367  /* Shutdown commandline interface */
368  command_exit(cmd_ctx);
369 
370  rtt_exit();
371  free_config();
372 
373  log_exit();
374 
375 #if USE_GCOV
376  /* Always explicitly dump coverage data before terminating.
377  * Otherwise coverage would not be dumped when exit_on_signal occurs. */
378  void __gcov_dump(void);
379  __gcov_dump();
380 #endif
381 
382  if (ret != ERROR_OK) {
383  /* An error occurred before the server could be fully started.
384  * For example during the processing of initial commands/scripts. */
385  return EXIT_FAILURE;
386  }
387 
388  // Otherwise check the shutdown reason of the server
390  // Server terminated by a signal.
392 
393  // Server terminated by shutdown command.
395 }
int adapter_quit(void)
Shutdown the debug adapter upon program exit.
Definition: adapter.c:195
int adapter_register_commands(struct command_context *ctx)
Register the commands which deal with arbitrary debug adapter drivers.
Definition: adapter.c:1328
int adapter_init(struct command_context *cmd_ctx)
Do low-level setup like initializing registers, output signals, and clocking.
Definition: adapter.c:130
This defines formats and data structures used to talk to ADIv5 entities.
int dap_cleanup_all(void)
Definition: arm_dap.c:159
int dap_register_commands(struct command_context *cmd_ctx)
Definition: arm_dap.c:531
int cti_register_commands(struct command_context *cmd_ctx)
Definition: arm_cti.c:580
int arm_cti_cleanup_all(void)
Definition: arm_cti.c:228
int arm_tpiu_swo_register_commands(struct command_context *cmd_ctx)
int arm_tpiu_swo_cleanup_all(void)
Definition: arm_tpiu_swo.c:209
int help_del_all_commands(struct command_context *cmd_ctx)
Unregisters the help for all commands.
Definition: command.c:946
void command_done(struct command_context *cmd_ctx)
Frees the resources associated with a command context.
Definition: command.c:584
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
void command_exit(struct command_context *context)
Shutdown a command context.
Definition: command.c:1202
struct command_context * command_init(const char *startup_tcl, Jim_Interp *interp)
Creates a new command context using the startup TCL provided and the existing Jim interpreter,...
Definition: command.c:1167
int command_context_mode(struct command_context *cmd_ctx, enum command_mode mode)
Definition: command.c:1212
void command_set_output_handler(struct command_context *context, command_output_handler_t output_handler, void *priv)
Definition: command.c:568
int unregister_all_commands(struct command_context *context, const char *cmd_prefix)
Unregisters all commands from the specified context.
Definition: command.c:314
int command_run_line(struct command_context *context, char *line)
Definition: command.c:497
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define ERROR_COMMAND_CLOSE_CONNECTION
Definition: command.h:404
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
int parse_config_file(struct command_context *cmd_ctx)
void add_script_search_dir(const char *dir)
Definition: configuration.c:25
void free_config(void)
Definition: configuration.c:45
int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
Definition: options.c:265
int configuration_output_handler(struct command_context *cmd_ctx, const char *line)
Definition: options.c:50
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
void flash_free_all_banks(void)
Deallocates all flash banks.
int gdb_register_commands(struct command_context *cmd_ctx)
Definition: gdb_server.c:4274
int gdb_target_add_all(struct target *target)
Definition: gdb_server.c:4031
void gdb_service_free(void)
Definition: gdb_server.c:4281
void jtag_poll_unmask(bool saved)
Restore saved mask for polling.
Definition: jtag/core.c:178
bool jtag_poll_mask(void)
Mask (disable) polling and return the current mask status that should be feed to jtag_poll_unmask() t...
Definition: jtag/core.c:171
The JTAG interface can be implemented with a software or hardware fifo.
int log_register_commands(struct command_context *cmd_ctx)
Definition: log.c:281
void log_init(void)
Initialize logging module.
Definition: log.c:286
void log_exit(void)
Definition: log.c:306
#define LOG_OUTPUT(expr ...)
Definition: log.h:156
#define ERROR_FAIL
Definition: log.h:188
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
int nand_register_commands(struct command_context *cmd_ctx)
Upper level NOR flash interfaces.
int flash_register_commands(struct command_context *cmd_ctx)
Registers the 'flash' subsystem commands.
static const struct command_registration openocd_command_handlers[]
Definition: openocd.c:194
COMMAND_HANDLER(handler_version_command)
Definition: openocd.c:54
struct command_context * global_cmd_ctx
Definition: openocd.c:234
#define OPENOCD_VERSION
Definition: openocd.c:44
static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
OpenOCD runtime meat that can become single-thread in future.
Definition: openocd.c:283
static int log_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
Definition: openocd.c:73
static bool init_at_startup
Definition: openocd.c:97
static struct command_context * setup_command_handler(Jim_Interp *interp)
Definition: openocd.c:253
static int(*const command_registrants[])(struct command_context *cmd_ctx_value)
Definition: openocd.c:236
static int openocd_register_commands(struct command_context *cmd_ctx)
Definition: openocd.c:229
static const char openocd_startup_tcl[]
Definition: openocd.c:48
int openocd_main(int argc, char *argv[])
Different applications can define this entry point to override the default openocd main function.
Definition: openocd.c:326
int pld_register_commands(struct command_context *cmd_ctx)
Definition: pld.c:366
int rtt_init(void)
Initialize Real-Time Transfer (RTT).
Definition: rtt/rtt.c:48
int rtt_exit(void)
Shutdown Real-Time Transfer (RTT).
Definition: rtt/rtt.c:65
int rtt_server_register_commands(struct command_context *ctx)
Definition: rtt_server.c:247
int server_get_last_signal_number(void)
Definition: server.c:628
int server_preinit(void)
Definition: server.c:702
uint8_t server_get_exit_status_code(void)
Definition: server.c:637
int server_host_os_close(void)
Definition: server.c:694
void server_free(void)
Definition: server.c:749
bool server_terminated_by_signal(void)
Definition: server.c:623
int server_host_os_entry(void)
Definition: server.c:674
int exit_on_signal(int sig)
Definition: server.c:759
void server_loop(struct command_context *command_context)
Definition: server.c:437
int server_register_commands(struct command_context *cmd_ctx)
Definition: server.c:963
int server_init(struct command_context *cmd_ctx)
Definition: server.c:722
void server_quit(void)
Definition: server.c:739
Jim_Interp * interp
Definition: command.h:53
const char * name
Definition: command.h:239
Definition: target.h:119
bool verbose_halt_msg
Definition: target.h:178
struct target * all_targets
Definition: target.c:117
int target_register_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1623
int target_arch_state(struct target *target)
Definition: target.c:2314
int target_register_commands(struct command_context *cmd_ctx)
Definition: target.c:6360
int target_examine(void)
Definition: target.c:725
target_event
Definition: target.h:253
@ TARGET_EVENT_HALTED
Definition: target.h:265
@ TARGET_EVENT_GDB_START
Definition: target.h:272
@ TARGET_EVENT_GDB_END
Definition: target.h:273
int transport_register_commands(struct command_context *ctx)
Definition: transport.c:432
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define NULL
Definition: usb.h:16
int util_init(struct command_context *cmd_ctx)
Definition: util.c:39