OpenOCD
command.c File Reference
Include dependency graph for command.c:

Go to the source code of this file.

Data Structures

struct  help_entry
 
struct  log_capture_state
 

Macros

#define __THIS__FILE__   "command.c"
 
#define DEFINE_PARSE_LONGLONG(name, type, min, max)    DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)
 
#define DEFINE_PARSE_NUM_TYPE(name, type, func, min, max)
 
#define DEFINE_PARSE_ULONGLONG(name, type, min, max)    DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)
 
#define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname)
 
#define HELP_LINE_WIDTH(_n)   (int)(76 - (2 * _n))
 

Functions

static __attribute__ ((format(PRINTF_ATTRIBUTE_FORMAT, 2, 3)))
 
int __register_commands (struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds, void *data, struct target *override_target)
 
static char * alloc_concatenate_strings (int argc, Jim_Obj *const *argv)
 
static bool command_can_run (struct command_context *cmd_ctx, struct command *c, const char *full_name)
 
int command_context_mode (struct command_context *cmd_ctx, enum command_mode mode)
 
void command_done (struct command_context *cmd_ctx)
 Frees the resources associated with a command context. More...
 
void command_exit (struct command_context *context)
 Shutdown a command context. More...
 
static struct commandcommand_find_from_name (Jim_Interp *interp, const char *name)
 Find a openocd command from fullname. More...
 
static void command_free (struct Jim_Interp *interp, void *priv)
 
 COMMAND_HANDLER (handle_echo)
 
 COMMAND_HANDLER (handle_find)
 
 COMMAND_HANDLER (handle_help_add_command)
 
 COMMAND_HANDLER (handle_help_command)
 
 COMMAND_HANDLER (handle_sleep_command)
 
static void command_help_show_indent (unsigned int n)
 
static void command_help_show_wrap (const char *str, unsigned int n, unsigned int n2)
 
static COMMAND_HELPER (command_help_show, struct help_entry *c, bool show_help, const char *cmd_match)
 
static COMMAND_HELPER (command_help_show_list, bool show_help, const char *cmd_match)
 
 COMMAND_HELPER (command_parse_str_to_buf, const char *str, void *buf, unsigned int buf_len)
 Parse a number (base 10, base 16 or base 8) and store the result into a bit buffer. More...
 
 COMMAND_HELPER (handle_command_parse_bool, bool *out, const char *label)
 
struct command_contextcommand_init (const char *startup_tcl, Jim_Interp *interp)
 Creates a new command context using the startup TCL provided and the existing Jim interpreter, if any. More...
 
static void command_log_capture_finish (struct log_capture_state *state)
 
static struct log_capture_statecommand_log_capture_start (Jim_Interp *interp)
 
static struct commandcommand_new (struct command_context *cmd_ctx, const char *full_name, const struct command_registration *cr)
 
void command_output_text (struct command_context *context, const char *data)
 
static int command_parse_bool (const char *in, bool *out, const char *on, const char *off)
 
int command_parse_bool_arg (const char *in, bool *out)
 
void command_print (struct command_invocation *cmd, const char *format,...)
 
void command_print_sameline (struct command_invocation *cmd, const char *format,...)
 
static int command_retval_set (Jim_Interp *interp, int retval)
 
int command_run_line (struct command_context *context, char *line)
 
int command_run_linef (struct command_context *context, const char *format,...)
 
void command_set_output_handler (struct command_context *context, command_output_handler_t output_handler, void *priv)
 
struct command_contextcopy_command_context (struct command_context *context)
 Creates a copy of an existing command context. More...
 
struct command_contextcurrent_command_context (Jim_Interp *interp)
 
static int exec_command (Jim_Interp *interp, struct command_context *context, struct command *c, int argc, Jim_Obj *const *argv)
 
static int help_add_command (struct command_context *cmd_ctx, const char *cmd_name, const char *help_text, const char *usage_text)
 
int help_del_all_commands (struct command_context *cmd_ctx)
 Unregisters the help for all commands. More...
 
static int help_del_command (struct command_context *cmd_ctx, const char *cmd_name)
 
static int jim_capture (Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
static int jim_command_dispatch (Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
static int jim_command_mode (Jim_Interp *interp, int argc, Jim_Obj *const *argv)
 
bool jimcmd_is_oocd_command (Jim_Cmd *cmd)
 Return true if the command cmd is registered by OpenOCD. More...
 
static bool jimcmd_is_proc (Jim_Cmd *cmd)
 
void * jimcmd_privdata (Jim_Cmd *cmd)
 Return the pointer to the command's private data specified during the registration of command cmd . More...
 
void process_jim_events (struct command_context *cmd_ctx)
 
static struct commandregister_command (struct command_context *context, const char *cmd_prefix, const struct command_registration *cr)
 
static void script_debug (Jim_Interp *interp, unsigned int argc, Jim_Obj *const *argv)
 
static void tcl_output (void *privData, const char *file, unsigned int line, const char *function, const char *string)
 
int unregister_all_commands (struct command_context *context, const char *cmd_prefix)
 Unregisters all commands from the specified context. More...
 
static int unregister_command (struct command_context *context, const char *cmd_prefix, const char *name)
 

Variables

static const struct command_registration command_builtin_handlers []
 
static const struct command_registration command_subcommand_handlers []
 
struct command_contextglobal_cmd_ctx
 

Macro Definition Documentation

◆ __THIS__FILE__

#define __THIS__FILE__   "command.c"

Definition at line 31 of file command.c.

◆ DEFINE_PARSE_LONGLONG

#define DEFINE_PARSE_LONGLONG (   name,
  type,
  min,
  max 
)     DEFINE_PARSE_WRAPPER(name, type, min, max, long long, _llong)

Definition at line 1328 of file command.c.

◆ DEFINE_PARSE_NUM_TYPE

#define DEFINE_PARSE_NUM_TYPE (   name,
  type,
  func,
  min,
  max 
)
Value:
int parse ## name(const char *str, type * ul) \
{ \
if (!*str) { \
LOG_ERROR("Invalid command argument"); \
} \
char *end; \
errno = 0; \
*ul = func(str, &end, 0); \
if (*end) { \
LOG_ERROR("Invalid command argument"); \
} \
if ((max == *ul) && (errno == ERANGE)) { \
LOG_ERROR("Argument overflow"); \
} \
if (min && (min == *ul) && (errno == ERANGE)) { \
LOG_ERROR("Argument underflow"); \
} \
return ERROR_OK; \
}
const char * name
Definition: armv4_5.c:76
#define ERROR_COMMAND_ARGUMENT_UNDERFLOW
Definition: command.h:406
#define ERROR_COMMAND_ARGUMENT_OVERFLOW
Definition: command.h:405
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
uint8_t type
Definition: esp_usb_jtag.c:0
#define ERROR_OK
Definition: log.h:164

Definition at line 1274 of file command.c.

◆ DEFINE_PARSE_ULONGLONG

#define DEFINE_PARSE_ULONGLONG (   name,
  type,
  min,
  max 
)     DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long long, _ullong)

Definition at line 1318 of file command.c.

◆ DEFINE_PARSE_WRAPPER

#define DEFINE_PARSE_WRAPPER (   name,
  type,
  min,
  max,
  functype,
  funcname 
)
Value:
int parse ## name(const char *str, type * ul) \
{ \
functype n; \
int retval = parse ## funcname(str, &n); \
if (retval != ERROR_OK) \
return retval; \
if (n > max) \
if (min) \
*ul = n; \
return ERROR_OK; \
}

Definition at line 1303 of file command.c.

◆ HELP_LINE_WIDTH

#define HELP_LINE_WIDTH (   _n)    (int)(76 - (2 * _n))

Definition at line 729 of file command.c.

Function Documentation

◆ __attribute__()

static __attribute__ ( (format(PRINTF_ATTRIBUTE_FORMAT, 2, 3))  )
static

◆ __register_commands()

int __register_commands ( struct command_context cmd_ctx,
const char *  cmd_prefix,
const struct command_registration cmds,
void *  data,
struct target override_target 
)

◆ alloc_concatenate_strings()

static char* alloc_concatenate_strings ( int  argc,
Jim_Obj *const *  argv 
)
static

Definition at line 859 of file command.c.

References alloc_printf(), LOG_ERROR, and NULL.

Referenced by jim_command_mode().

◆ command_can_run()

static bool command_can_run ( struct command_context cmd_ctx,
struct command c,
const char *  full_name 
)
static

◆ command_context_mode()

int command_context_mode ( struct command_context cmd_ctx,
enum command_mode  mode 
)

Definition at line 1254 of file command.c.

References ERROR_COMMAND_SYNTAX_ERROR, ERROR_OK, command_context::mode, and mode.

Referenced by COMMAND_HANDLER(), and openocd_main().

◆ command_done()

void command_done ( struct command_context context)

Frees the resources associated with a command context.

The commands are not removed, so unregister_all_commands() must be called first.

Parameters
contextThe command_context that will be destroyed.

Definition at line 644 of file command.c.

Referenced by add_connection(), command_exit(), remove_connection(), and setup_command_handler().

◆ command_exit()

void command_exit ( struct command_context context)

Shutdown a command context.

Free the command context and the associated Jim interpreter.

Parameters
contextThe command_context that will be destroyed.

Definition at line 1244 of file command.c.

References command_done(), command_context::help_list, and command_context::interp.

Referenced by openocd_main().

◆ command_find_from_name()

static struct command* command_find_from_name ( Jim_Interp *  interp,
const char *  name 
)
static

Find a openocd command from fullname.

Returns
Returns the named command if it is registred in interp. Returns NULL otherwise.

Definition at line 179 of file command.c.

References cmd, jimcmd_is_oocd_command(), jimcmd_is_proc(), jimcmd_privdata(), name, and NULL.

Referenced by __attribute__(), and register_command().

◆ command_free()

static void command_free ( struct Jim_Interp *  interp,
void *  priv 
)
static

Definition at line 231 of file command.c.

References command::name, and priv.

Referenced by register_command().

◆ COMMAND_HANDLER() [1/5]

COMMAND_HANDLER ( handle_echo  )

Definition at line 668 of file command.c.

References CMD_ARGC, CMD_ARGV, ERROR_FAIL, ERROR_OK, LOG_USER, and LOG_USER_N.

◆ COMMAND_HANDLER() [2/5]

COMMAND_HANDLER ( handle_find  )

◆ COMMAND_HANDLER() [3/5]

COMMAND_HANDLER ( handle_help_add_command  )

◆ COMMAND_HANDLER() [4/5]

COMMAND_HANDLER ( handle_help_command  )

Definition at line 830 of file command.c.

References alloc_printf(), CALL_COMMAND_HANDLER, CMD_ARGC, CMD_ARGV, CMD_NAME, and LOG_ERROR.

◆ COMMAND_HANDLER() [5/5]

COMMAND_HANDLER ( handle_sleep_command  )

Definition at line 1091 of file command.c.

◆ command_help_show_indent()

static void command_help_show_indent ( unsigned int  n)
static

Definition at line 731 of file command.c.

References LOG_USER_N.

Referenced by command_help_show_wrap().

◆ command_help_show_wrap()

static void command_help_show_wrap ( const char *  str,
unsigned int  n,
unsigned int  n2 
)
static

Definition at line 736 of file command.c.

References command_help_show_indent(), HELP_LINE_WIDTH, and LOG_USER.

Referenced by COMMAND_HELPER().

◆ COMMAND_HELPER() [1/4]

static COMMAND_HELPER ( command_help_show  ,
struct help_entry c,
bool  show_help,
const char *  cmd_match 
)
static

◆ COMMAND_HELPER() [2/4]

static COMMAND_HELPER ( command_help_show_list  ,
bool  show_help,
const char *  cmd_match 
)
static

Definition at line 720 of file command.c.

References CALL_COMMAND_HANDLER, CMD_CTX, ERROR_OK, lh(), and list_for_each_entry.

◆ COMMAND_HELPER() [3/4]

COMMAND_HELPER ( command_parse_str_to_buf  ,
const char *  str,
void *  buf,
unsigned int  buf_len 
)

Parse a number (base 10, base 16 or base 8) and store the result into a bit buffer.

Use the prefixes '0' and '0x' for base 8 and 16, otherwise defaults to base 10.

In case of parsing error, a user-readable error message is produced.

Definition at line 1363 of file command.c.

References CMD, command_print(), ERROR_COMMAND_ARGUMENT_INVALID, ERROR_INVALID_NUMBER, ERROR_NUMBER_EXCEEDS_BUFFER, ERROR_OK, and str_to_buf().

◆ COMMAND_HELPER() [4/4]

COMMAND_HELPER ( handle_command_parse_bool  ,
bool *  out,
const char *  label 
)

◆ command_init()

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, if any.

If interp == NULL, then command_init creates a command interpreter.

Definition at line 1209 of file command.c.

References command_builtin_handlers, COMMAND_EXEC, command_context::help_list, INIT_LIST_HEAD(), command_context::interp, LOG_ERROR, LOG_USER_N, command_context::mode, NULL, and register_commands().

Referenced by setup_command_handler().

◆ command_log_capture_finish()

static void command_log_capture_finish ( struct log_capture_state state)
static

Definition at line 104 of file command.c.

References log_remove_callback(), state, and tcl_output().

Referenced by jim_capture().

◆ command_log_capture_start()

static struct log_capture_state* command_log_capture_start ( Jim_Interp *  interp)
static

Definition at line 68 of file command.c.

References log_capture_state::interp, log_add_callback(), LOG_ERROR, NULL, state, and tcl_output().

Referenced by jim_capture().

◆ command_new()

◆ command_output_text()

void command_output_text ( struct command_context context,
const char *  data 
)

Definition at line 414 of file command.c.

References command_context::output_handler.

Referenced by command_run_line(), target_asciimsg(), and target_hexmsg().

◆ command_parse_bool()

static int command_parse_bool ( const char *  in,
bool *  out,
const char *  on,
const char *  off 
)
static

Definition at line 1336 of file command.c.

References ERROR_COMMAND_SYNTAX_ERROR, and ERROR_OK.

Referenced by command_parse_bool_arg().

◆ command_parse_bool_arg()

int command_parse_bool_arg ( const char *  in,
bool *  out 
)

Definition at line 1348 of file command.c.

References command_parse_bool(), ERROR_COMMAND_SYNTAX_ERROR, and ERROR_OK.

Referenced by COMMAND_HELPER().

◆ command_print()

void command_print ( struct command_invocation cmd,
const char *  format,
  ... 
)

Definition at line 443 of file command.c.

References alloc_vprintf(), and cmd.

Referenced by __COMMAND_HANDLER(), a64_disassemble(), adiv6_dap_read_baseptr(), arm7a_handle_l2x_cache_info_command(), arm920t_verify_pointer(), arm926ejs_verify_pointer(), arm946e_verify_pointer(), arm966e_verify_pointer(), armv4_5_handle_cache_info_command(), armv7a_handle_cache_info_command(), armv8_handle_cache_info_command(), armv8_handle_inner_cache_info_command(), binprint(), COMMAND_HANDLER(), COMMAND_HELPER(), cortex_m_verify_pointer(), dap_devtype_display(), dap_info_ap_header(), dap_info_cs_component(), dap_info_mem_ap_header(), dap_info_rom_table_entry(), esirisc_trace_analyze(), esirisc_trace_analyze_buffer(), esirisc_trace_analyze_full(), esirisc_trace_analyze_memory(), esirisc_trace_analyze_simple(), esirisc_trace_dump(), esirisc_trace_dump_buffer(), esirisc_trace_dump_memory(), esp32_apptrace_cmd_ctx_init(), esp32_apptrace_cmd_init(), esp32_apptrace_connect_targets(), esp32_cmd_apptrace_generic(), esp32_sysview_cmd_init(), esp_semihosting_basedir_command(), etmv1_analyze_trace(), find_target(), get_target_with_common_rtos_type(), handle_bp_command_list(), handle_bp_command_set(), handle_iod_output(), mips32_cp0_get_all_regs(), mips32_cp0_get_reg_by_name(), mips32_cp0_get_reg_by_number(), mips32_cp0_set_reg_by_name(), mips32_cp0_set_reg_by_number(), mips32_dsp_get_all_regs(), mips32_dsp_get_register(), mips32_dsp_set_register(), mips32_verify_pointer(), mips_m4k_verify_pointer(), nand_fileio_start(), nand_list_walker(), nvp_unknown_command_print(), PLD_CREATE_COMMAND_HANDLER(), show_config(), show_config_ip_address(), show_config_mac_address(), show_config_target_power(), show_config_usb_address(), target_handle_md_output(), target_process_reset(), xscale_analyze_trace(), xscale_display_instruction(), and xscale_verify_pointer().

◆ command_print_sameline()

◆ command_retval_set()

static int command_retval_set ( Jim_Interp *  interp,
int  retval 
)
static

Definition at line 128 of file command.c.

References ERROR_OK, and log_capture_state::interp.

Referenced by exec_command().

◆ command_run_line()

◆ command_run_linef()

int command_run_linef ( struct command_context context,
const char *  format,
  ... 
)

◆ command_set_output_handler()

void command_set_output_handler ( struct command_context context,
command_output_handler_t  output_handler,
void *  priv 
)

◆ copy_command_context()

struct command_context* copy_command_context ( struct command_context cmd_ctx)

Creates a copy of an existing command context.

This does not create a deep copy of the command list, so modifications in one context will affect all shared contexts. The caller must track reference counting and ensure the commands are freed before destroying the last instance.

Parameters
cmd_ctxThe command_context that will be copied.
Returns
A new command_context with the same state as the original.

Definition at line 635 of file command.c.

Referenced by add_connection().

◆ current_command_context()

◆ exec_command()

static int exec_command ( Jim_Interp *  interp,
struct command_context context,
struct command c,
int  argc,
Jim_Obj *const *  argv 
)
static

◆ help_add_command()

static int help_add_command ( struct command_context cmd_ctx,
const char *  cmd_name,
const char *  help_text,
const char *  usage_text 
)
static

◆ help_del_all_commands()

int help_del_all_commands ( struct command_context cmd_ctx)

Unregisters the help for all commands.

Used at exit to remove the help added through the commands 'add_help_text' and 'add_usage_text'.

Parameters
cmd_ctxThe context that will be cleared of registered helps.
Returns
ERROR_OK on success, or an error code.

Definition at line 988 of file command.c.

References help_entry::cmd_name, ERROR_OK, help_entry::help, command_context::help_list, help_entry::lh, lh(), list_del(), list_for_each_entry_safe, and help_entry::usage.

Referenced by openocd_main().

◆ help_del_command()

static int help_del_command ( struct command_context cmd_ctx,
const char *  cmd_name 
)
static

◆ jim_capture()

static int jim_capture ( Jim_Interp *  interp,
int  argc,
Jim_Obj *const *  argv 
)
static

◆ jim_command_dispatch()

◆ jim_command_mode()

static int jim_command_mode ( Jim_Interp *  interp,
int  argc,
Jim_Obj *const *  argv 
)
static

◆ jimcmd_is_oocd_command()

bool jimcmd_is_oocd_command ( Jim_Cmd *  cmd)

Return true if the command cmd is registered by OpenOCD.

Definition at line 51 of file command.c.

References cmd, and jim_command_dispatch().

Referenced by command_find_from_name(), jim_command_mode(), and telnet_auto_complete().

◆ jimcmd_is_proc()

static bool jimcmd_is_proc ( Jim_Cmd *  cmd)
inlinestatic

Definition at line 46 of file command.c.

References cmd.

Referenced by command_find_from_name(), and jim_command_mode().

◆ jimcmd_privdata()

void* jimcmd_privdata ( Jim_Cmd *  cmd)

Return the pointer to the command's private data specified during the registration of command cmd .

Definition at line 56 of file command.c.

References cmd, and NULL.

Referenced by command_find_from_name(), jim_command_mode(), and telnet_auto_complete().

◆ process_jim_events()

void process_jim_events ( struct command_context cmd_ctx)

Definition at line 1263 of file command.c.

References command_context::interp.

Referenced by server_loop().

◆ register_command()

static struct command* register_command ( struct command_context context,
const char *  cmd_prefix,
const struct command_registration cr 
)
static

◆ script_debug()

static void script_debug ( Jim_Interp *  interp,
unsigned int  argc,
Jim_Obj *const *  argv 
)
static

Definition at line 141 of file command.c.

References alloc_printf(), debug_level, LOG_DEBUG, LOG_LVL_DEBUG, and NULL.

Referenced by jim_command_dispatch().

◆ tcl_output()

static void tcl_output ( void *  privData,
const char *  file,
unsigned int  line,
const char *  function,
const char *  string 
)
static

Definition at line 61 of file command.c.

References state.

Referenced by command_log_capture_finish(), and command_log_capture_start().

◆ unregister_all_commands()

int unregister_all_commands ( struct command_context cmd_ctx,
const char *  cmd_prefix 
)

Unregisters all commands from the specified context.

Parameters
cmd_ctxThe context that will be cleared of registered commands.
cmd_prefixIf given, only clear commands from under this one command.
Returns
ERROR_OK on success, or an error code.

Definition at line 386 of file command.c.

References ERROR_OK.

Referenced by openocd_main().

◆ unregister_command()

static int unregister_command ( struct command_context context,
const char *  cmd_prefix,
const char *  name 
)
static

Definition at line 402 of file command.c.

References ERROR_COMMAND_SYNTAX_ERROR, and name.

Referenced by __register_commands().

Variable Documentation

◆ command_builtin_handlers

const struct command_registration command_builtin_handlers[]
static

Definition at line 1091 of file command.c.

Referenced by command_init().

◆ command_subcommand_handlers

const struct command_registration command_subcommand_handlers[]
static
Initial value:
= {
{
.name = "mode",
.mode = COMMAND_ANY,
.jim_handler = jim_command_mode,
.usage = "[command_name ...]",
.help = "Returns the command modes allowed by a command: "
"'any', 'config', or 'exec'. If no command is "
"specified, returns the current command mode. "
"Returns 'unknown' if an unknown command is given. "
"Command can be multiple tokens.",
},
}
static int jim_command_mode(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
Definition: command.c:939
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_ANY
Definition: command.h:42

Definition at line 1091 of file command.c.

◆ global_cmd_ctx

struct command_context* global_cmd_ctx
extern

Definition at line 233 of file openocd.c.

Referenced by current_command_context().