1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "configuration.h"
25 #include "binarybuffer.h"
31 #include <sys/types.h>
39 #include <time_support.h>
41 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
43 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
58 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 int handle_load_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 int handle_dump_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 extern target_type_t arm7tdmi_target;
71 extern target_type_t arm720t_target;
72 extern target_type_t arm9tdmi_target;
73 extern target_type_t arm920t_target;
75 target_type_t *target_types[] =
84 target_t *targets = NULL;
85 target_event_callback_t *target_event_callbacks = NULL;
86 target_timer_callback_t *target_timer_callbacks = NULL;
88 char *target_state_strings[] =
97 char *target_debug_reason_strings[] =
99 "debug request", "breakpoint", "watchpoint",
100 "watchpoint and breakpoint", "single step",
104 char *target_endianess_strings[] =
110 enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
112 static int target_continous_poll = 1;
114 /* returns a pointer to the n-th configured target */
115 target_t* get_target_by_num(int num)
117 target_t *target = targets;
124 target = target->next;
131 int get_num_by_target(target_t *query_target)
133 target_t *target = targets;
138 if (target == query_target)
140 target = target->next;
147 target_t* get_current_target(command_context_t *cmd_ctx)
149 target_t *target = get_target_by_num(cmd_ctx->current_target);
153 ERROR("BUG: current_target out of bounds");
160 /* Process target initialization, when target entered debug out of reset
161 * the handler is unregistered at the end of this function, so it's only called once
163 int target_init_handler(struct target_s *target, enum target_event event, void *priv)
166 struct command_context_s *cmd_ctx = priv;
168 if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
170 script = fopen(target->reset_script, "r");
173 ERROR("couldn't open script file %s", target->reset_script);
177 INFO("executing reset script '%s'", target->reset_script);
178 command_run_file(cmd_ctx, script, COMMAND_EXEC);
181 jtag_execute_queue();
183 target_unregister_event_callback(target_init_handler, priv);
189 int target_run_and_halt_handler(void *priv)
191 target_t *target = priv;
193 target->type->halt(target);
198 int target_process_reset(struct command_context_s *cmd_ctx)
200 int retval = ERROR_OK;
206 target->type->assert_reset(target);
207 target = target->next;
209 jtag_execute_queue();
211 /* request target halt if necessary, and schedule further action */
215 switch (target->reset_mode)
218 /* nothing to do if target just wants to be run */
220 case RESET_RUN_AND_HALT:
222 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
224 case RESET_RUN_AND_INIT:
226 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
227 target_register_event_callback(target_init_handler, cmd_ctx);
230 target->type->halt(target);
233 target->type->halt(target);
234 target_register_event_callback(target_init_handler, cmd_ctx);
237 ERROR("BUG: unknown target->reset_mode");
239 target = target->next;
245 target->type->deassert_reset(target);
246 target = target->next;
248 jtag_execute_queue();
253 int target_init(struct command_context_s *cmd_ctx)
255 target_t *target = targets;
259 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
261 ERROR("target '%s' init failed", target->type->name);
264 target = target->next;
269 target_register_user_commands(cmd_ctx);
270 target_register_timer_callback(handle_target, 100, 1, NULL);
273 if (startup_mode == DAEMON_RESET)
274 target_process_reset(cmd_ctx);
279 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
281 target_event_callback_t **callbacks_p = &target_event_callbacks;
283 if (callback == NULL)
285 return ERROR_INVALID_ARGUMENTS;
290 while ((*callbacks_p)->next)
291 callbacks_p = &((*callbacks_p)->next);
292 callbacks_p = &((*callbacks_p)->next);
295 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
296 (*callbacks_p)->callback = callback;
297 (*callbacks_p)->priv = priv;
298 (*callbacks_p)->next = NULL;
303 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
305 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
308 if (callback == NULL)
310 return ERROR_INVALID_ARGUMENTS;
315 while ((*callbacks_p)->next)
316 callbacks_p = &((*callbacks_p)->next);
317 callbacks_p = &((*callbacks_p)->next);
320 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
321 (*callbacks_p)->callback = callback;
322 (*callbacks_p)->periodic = periodic;
323 (*callbacks_p)->time_ms = time_ms;
325 gettimeofday(&now, NULL);
326 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
327 time_ms -= (time_ms % 1000);
328 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
329 if ((*callbacks_p)->when.tv_usec > 1000000)
331 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
332 (*callbacks_p)->when.tv_sec += 1;
335 (*callbacks_p)->priv = priv;
336 (*callbacks_p)->next = NULL;
341 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
343 target_event_callback_t **p = &target_event_callbacks;
344 target_event_callback_t *c = target_event_callbacks;
346 if (callback == NULL)
348 return ERROR_INVALID_ARGUMENTS;
353 target_event_callback_t *next = c->next;
354 if ((c->callback == callback) && (c->priv == priv))
368 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
370 target_timer_callback_t **p = &target_timer_callbacks;
371 target_timer_callback_t *c = target_timer_callbacks;
373 if (callback == NULL)
375 return ERROR_INVALID_ARGUMENTS;
380 target_timer_callback_t *next = c->next;
381 if ((c->callback == callback) && (c->priv == priv))
395 int target_call_event_callbacks(target_t *target, enum target_event event)
397 target_event_callback_t *callback = target_event_callbacks;
398 target_event_callback_t *next_callback;
400 DEBUG("target event %i", event);
404 next_callback = callback->next;
405 callback->callback(target, event, callback->priv);
406 callback = next_callback;
412 int target_call_timer_callbacks()
414 target_timer_callback_t *callback = target_timer_callbacks;
415 target_timer_callback_t *next_callback;
418 gettimeofday(&now, NULL);
422 next_callback = callback->next;
424 if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
425 || (now.tv_sec > callback->when.tv_sec))
427 callback->callback(callback->priv);
428 if (callback->periodic)
430 int time_ms = callback->time_ms;
431 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
432 time_ms -= (time_ms % 1000);
433 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
434 if (callback->when.tv_usec > 1000000)
436 callback->when.tv_usec = callback->when.tv_usec - 1000000;
437 callback->when.tv_sec += 1;
441 target_unregister_timer_callback(callback->callback, callback->priv);
444 callback = next_callback;
450 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
452 working_area_t *c = target->working_areas;
453 working_area_t *new_wa = NULL;
455 /* only allocate multiples of 4 byte */
458 ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
459 size = CEIL(size, 4);
462 /* see if there's already a matching working area */
465 if ((c->free) && (c->size == size))
473 /* if not, allocate a new one */
476 working_area_t **p = &target->working_areas;
477 u32 first_free = target->working_area;
478 u32 free_size = target->working_area_size;
480 DEBUG("allocating new working area");
482 c = target->working_areas;
485 first_free += c->size;
486 free_size -= c->size;
491 if (free_size < size)
493 WARNING("not enough working area available");
494 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
497 new_wa = malloc(sizeof(working_area_t));
500 new_wa->address = first_free;
502 if (target->backup_working_area)
504 new_wa->backup = malloc(new_wa->size);
505 target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
509 new_wa->backup = NULL;
512 /* put new entry in list */
516 /* mark as used, and return the new (reused) area */
526 int target_free_working_area(struct target_s *target, working_area_t *area)
531 if (target->backup_working_area)
532 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
536 /* mark user pointer invalid */
543 int target_free_all_working_areas(struct target_s *target)
545 working_area_t *c = target->working_areas;
549 working_area_t *next = c->next;
550 target_free_working_area(target, c);
560 target->working_areas = NULL;
565 int target_register_commands(struct command_context_s *cmd_ctx)
567 register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, NULL);
568 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
569 register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
570 register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
571 register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, NULL);
572 register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_CONFIG, NULL);
577 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
581 DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
583 /* handle writes of less than 4 byte */
586 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
590 /* handle unaligned head bytes */
593 int unaligned = 4 - (address % 4);
595 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
599 address += unaligned;
603 /* handle aligned words */
606 int aligned = size - (size % 4);
608 /* use bulk writes above a certain limit. This may have to be changed */
611 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
616 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
625 /* handle tail writes of less than 4 bytes */
628 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
635 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
639 DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
641 /* handle reads of less than 4 byte */
644 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
648 /* handle unaligned head bytes */
651 int unaligned = 4 - (address % 4);
653 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
656 address += unaligned;
660 /* handle aligned words */
663 int aligned = size - (size % 4);
665 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
672 /* handle tail writes of less than 4 bytes */
675 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
682 int target_register_user_commands(struct command_context_s *cmd_ctx)
684 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
685 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
686 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt");
687 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
688 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
689 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction");
690 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init|run_and_halt|run_and_init]");
691 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
693 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
694 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
695 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
697 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value>");
698 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value>");
699 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value>");
701 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
702 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
703 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
704 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
706 register_command(cmd_ctx, NULL, "load_binary", handle_load_binary_command, COMMAND_EXEC, "load binary <file> <address>");
707 register_command(cmd_ctx, NULL, "dump_binary", handle_dump_binary_command, COMMAND_EXEC, "dump binary <file> <address> <size>");
712 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
714 target_t *target = targets;
719 int num = strtoul(args[0], NULL, 0);
724 target = target->next;
728 cmd_ctx->current_target = num;
730 command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
737 command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
738 target = target->next;
744 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
751 ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
755 /* search for the specified target */
756 if (args[0] && (args[0][0] != 0))
758 for (i = 0; target_types[i]; i++)
760 if (strcmp(args[0], target_types[i]->name) == 0)
762 target_t **last_target_p = &targets;
764 /* register target specific commands */
765 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
767 ERROR("couldn't register '%s' commands", args[0]);
773 while ((*last_target_p)->next)
774 last_target_p = &((*last_target_p)->next);
775 last_target_p = &((*last_target_p)->next);
778 *last_target_p = malloc(sizeof(target_t));
780 (*last_target_p)->type = target_types[i];
782 if (strcmp(args[1], "big") == 0)
783 (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
784 else if (strcmp(args[1], "little") == 0)
785 (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
788 ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
792 /* what to do on a target reset */
793 if (strcmp(args[2], "reset_halt") == 0)
794 (*last_target_p)->reset_mode = RESET_HALT;
795 else if (strcmp(args[2], "reset_run") == 0)
796 (*last_target_p)->reset_mode = RESET_RUN;
797 else if (strcmp(args[2], "reset_init") == 0)
798 (*last_target_p)->reset_mode = RESET_INIT;
799 else if (strcmp(args[2], "run_and_halt") == 0)
800 (*last_target_p)->reset_mode = RESET_RUN_AND_HALT;
801 else if (strcmp(args[2], "run_and_init") == 0)
802 (*last_target_p)->reset_mode = RESET_RUN_AND_INIT;
805 ERROR("unknown target startup mode %s", args[2]);
808 (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
810 (*last_target_p)->reset_script = NULL;
811 (*last_target_p)->post_halt_script = NULL;
812 (*last_target_p)->pre_resume_script = NULL;
814 (*last_target_p)->working_area = 0x0;
815 (*last_target_p)->working_area_size = 0x0;
816 (*last_target_p)->working_areas = NULL;
817 (*last_target_p)->backup_working_area = 0;
819 (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
820 (*last_target_p)->state = TARGET_UNKNOWN;
821 (*last_target_p)->reg_cache = NULL;
822 (*last_target_p)->breakpoints = NULL;
823 (*last_target_p)->watchpoints = NULL;
824 (*last_target_p)->next = NULL;
825 (*last_target_p)->arch_info = NULL;
827 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
835 /* no matching target found */
838 ERROR("target '%s' not found", args[0]);
845 /* usage: target_script <target#> <event> <script_file> */
846 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
848 target_t *target = NULL;
852 ERROR("incomplete target_script command");
856 target = get_target_by_num(strtoul(args[0], NULL, 0));
860 ERROR("target number '%s' not defined", args[0]);
864 if (strcmp(args[1], "reset") == 0)
866 if (target->reset_script)
867 free(target->reset_script);
868 target->reset_script = strdup(args[2]);
870 else if (strcmp(args[1], "post_halt") == 0)
872 if (target->post_halt_script)
873 free(target->post_halt_script);
874 target->post_halt_script = strdup(args[2]);
876 else if (strcmp(args[1], "pre_resume") == 0)
878 if (target->pre_resume_script)
879 free(target->pre_resume_script);
880 target->pre_resume_script = strdup(args[2]);
884 ERROR("unknown event type: '%s", args[1]);
891 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
893 target_t *target = NULL;
897 ERROR("incomplete run_and_halt_time command");
901 target = get_target_by_num(strtoul(args[0], NULL, 0));
905 ERROR("target number '%s' not defined", args[0]);
909 target->run_and_halt_time = strtoul(args[1], NULL, 0);
914 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
916 target_t *target = NULL;
920 ERROR("incomplete working_area command. usage: working_area <target#> <address> <size> <'backup'|'nobackup'>");
924 target = get_target_by_num(strtoul(args[0], NULL, 0));
928 ERROR("target number '%s' not defined", args[0]);
932 target->working_area = strtoul(args[1], NULL, 0);
933 target->working_area_size = strtoul(args[2], NULL, 0);
935 if (strcmp(args[3], "backup") == 0)
937 target->backup_working_area = 1;
939 else if (strcmp(args[3], "nobackup") == 0)
941 target->backup_working_area = 0;
945 ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
953 /* process target state changes */
954 int handle_target(void *priv)
957 target_t *target = targets;
961 /* only poll if target isn't already halted */
962 if (target->state != TARGET_HALTED)
964 if (target_continous_poll)
965 if ((retval = target->type->poll(target)) < 0)
967 ERROR("couldn't poll target, exiting");
972 target = target->next;
978 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
987 target = get_current_target(cmd_ctx);
989 /* list all available registers for the current target */
992 reg_cache_t *cache = target->reg_cache;
998 for (i = 0; i < cache->num_regs; i++)
1000 value = buf_to_char(cache->reg_list[i].value, cache->reg_list[i].size);
1001 command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", count++, cache->reg_list[i].name, cache->reg_list[i].size, value, cache->reg_list[i].dirty, cache->reg_list[i].valid);
1004 cache = cache->next;
1010 /* access a single register by its ordinal number */
1011 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1013 int num = strtoul(args[0], NULL, 0);
1014 reg_cache_t *cache = target->reg_cache;
1020 for (i = 0; i < cache->num_regs; i++)
1024 reg = &cache->reg_list[i];
1030 cache = cache->next;
1035 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1038 } else /* access a single register by its name */
1040 reg = register_get_by_name(target->reg_cache, args[0], 1);
1044 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1049 /* display a register */
1050 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1052 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1055 if (reg->valid == 0)
1057 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1058 if (arch_type == NULL)
1060 ERROR("BUG: encountered unregistered arch type");
1063 arch_type->get(reg);
1065 value = buf_to_char(reg->value, reg->size);
1066 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1071 /* set register value */
1074 u32 new_value = strtoul(args[1], NULL, 0);
1075 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1076 if (arch_type == NULL)
1078 ERROR("BUG: encountered unregistered arch type");
1082 arch_type->set(reg, new_value);
1083 value = buf_to_char(reg->value, reg->size);
1084 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1090 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1095 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1097 target_t *target = get_current_target(cmd_ctx);
1102 command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
1103 if (target->state == TARGET_HALTED)
1105 target->type->arch_state(target, buffer, 512);
1107 command_print(cmd_ctx, "%s", buffer);
1112 if (strcmp(args[0], "on") == 0)
1114 target_continous_poll = 1;
1116 else if (strcmp(args[0], "off") == 0)
1118 target_continous_poll = 0;
1126 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1128 target_t *target = get_current_target(cmd_ctx);
1129 struct timeval timeout, now;
1131 gettimeofday(&timeout, NULL);
1132 timeval_add_time(&timeout, 5, 0);
1134 command_print(cmd_ctx, "waiting for target halted...");
1136 while(target->type->poll(target))
1138 if (target->state == TARGET_HALTED)
1140 command_print(cmd_ctx, "target halted");
1143 target_call_timer_callbacks();
1145 gettimeofday(&now, NULL);
1146 if ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))
1148 command_print(cmd_ctx, "timed out while waiting for target halt");
1149 ERROR("timed out while waiting for target halt");
1157 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1160 target_t *target = get_current_target(cmd_ctx);
1164 command_print(cmd_ctx, "requesting target halt...");
1166 if ((retval = target->type->halt(target)) != ERROR_OK)
1170 case ERROR_TARGET_ALREADY_HALTED:
1171 command_print(cmd_ctx, "target already halted");
1173 case ERROR_TARGET_TIMEOUT:
1174 command_print(cmd_ctx, "target timed out... shutting down");
1177 command_print(cmd_ctx, "unknown error... shutting down");
1186 /* what to do on daemon startup */
1187 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1191 if (strcmp(args[0], "attach") == 0)
1193 startup_mode = DAEMON_ATTACH;
1196 else if (strcmp(args[0], "reset") == 0)
1198 startup_mode = DAEMON_RESET;
1203 WARNING("invalid daemon_startup configuration directive: %s", args[0]);
1208 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1210 target_t *target = get_current_target(cmd_ctx);
1213 command_print(cmd_ctx, "requesting target halt and executing a soft reset");
1215 if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
1219 case ERROR_TARGET_TIMEOUT:
1220 command_print(cmd_ctx, "target timed out... shutting down");
1223 command_print(cmd_ctx, "unknown error... shutting down");
1231 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1233 target_t *target = get_current_target(cmd_ctx);
1234 enum target_reset_mode reset_mode = RESET_RUN;
1240 if (strcmp("run", args[0]) == 0)
1241 reset_mode = RESET_RUN;
1242 else if (strcmp("halt", args[0]) == 0)
1243 reset_mode = RESET_HALT;
1244 else if (strcmp("init", args[0]) == 0)
1245 reset_mode = RESET_INIT;
1246 else if (strcmp("run_and_halt", args[0]) == 0)
1248 reset_mode = RESET_RUN_AND_HALT;
1251 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1254 else if (strcmp("run_and_init", args[0]) == 0)
1256 reset_mode = RESET_RUN_AND_INIT;
1259 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1264 command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
1267 target->reset_mode = reset_mode;
1270 target_process_reset(cmd_ctx);
1275 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1278 target_t *target = get_current_target(cmd_ctx);
1283 retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1285 retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1288 command_print(cmd_ctx, "usage: resume [address]");
1292 if (retval != ERROR_OK)
1296 case ERROR_TARGET_NOT_HALTED:
1297 command_print(cmd_ctx, "target not halted");
1300 command_print(cmd_ctx, "unknown error... shutting down");
1308 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1310 target_t *target = get_current_target(cmd_ctx);
1315 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1318 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1323 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1336 target_t *target = get_current_target(cmd_ctx);
1342 count = strtoul(args[1], NULL, 0);
1344 address = strtoul(args[0], NULL, 0);
1362 buffer = calloc(count, size);
1363 if ((retval = target->type->read_memory(target, address, size, count, buffer)) != ERROR_OK)
1367 case ERROR_TARGET_UNALIGNED_ACCESS:
1368 command_print(cmd_ctx, "error: address not aligned");
1370 case ERROR_TARGET_NOT_HALTED:
1371 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1373 case ERROR_TARGET_DATA_ABORT:
1374 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1377 command_print(cmd_ctx, "error: unknown error");
1384 for (i = 0; i < count; i++)
1387 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1392 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", ((u32*)buffer)[i]);
1395 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", ((u16*)buffer)[i]);
1398 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", ((u8*)buffer)[i]);
1402 if ((i%8 == 7) || (i == count - 1))
1404 command_print(cmd_ctx, output);
1414 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1419 target_t *target = get_current_target(cmd_ctx);
1424 address = strtoul(args[0], NULL, 0);
1425 value = strtoul(args[1], NULL, 0);
1430 retval = target->type->write_memory(target, address, 4, 1, (u8*)&value);
1433 retval = target->type->write_memory(target, address, 2, 1, (u8*)&value);
1436 retval = target->type->write_memory(target, address, 1, 1, (u8*)&value);
1444 case ERROR_TARGET_UNALIGNED_ACCESS:
1445 command_print(cmd_ctx, "error: address not aligned");
1447 case ERROR_TARGET_DATA_ABORT:
1448 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1450 case ERROR_TARGET_NOT_HALTED:
1451 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1456 command_print(cmd_ctx, "error: unknown error");
1464 int handle_load_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1468 struct stat binary_stat;
1474 struct timeval start, end;
1476 target_t *target = get_current_target(cmd_ctx);
1480 command_print(cmd_ctx, "usage: load_binary <filename> <address>");
1484 address = strtoul(args[1], NULL, 0);
1486 if (stat(args[0], &binary_stat) == -1)
1488 ERROR("couldn't stat() %s: %s", args[0], strerror(errno));
1489 command_print(cmd_ctx, "error accessing file %s", args[0]);
1493 if (!(binary = fopen(args[0], "r")))
1495 ERROR("couldn't open %s: %s", args[0], strerror(errno));
1496 command_print(cmd_ctx, "error accessing file %s", args[0]);
1500 buffer = malloc(128 * 1024);
1502 gettimeofday(&start, NULL);
1504 binary_size = binary_stat.st_size;
1505 while (binary_size > 0)
1507 buf_cnt = fread(buffer, 1, 128*1024, binary);
1508 target_write_buffer(target, address, buf_cnt, buffer);
1510 binary_size -= buf_cnt;
1513 gettimeofday(&end, NULL);
1517 command_print(cmd_ctx, "downloaded %lli byte in %is %ius", (long long) binary_stat.st_size, end.tv_sec - start.tv_sec, end.tv_usec - start.tv_usec);
1525 int handle_dump_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1532 target_t *target = get_current_target(cmd_ctx);
1536 command_print(cmd_ctx, "usage: dump_binary <filename> <address> <size>");
1540 address = strtoul(args[1], NULL, 0);
1541 size = strtoul(args[2], NULL, 0);
1543 if (!(binary = fopen(args[0], "w")))
1545 ERROR("couldn't open %s for writing: %s", args[0], strerror(errno));
1546 command_print(cmd_ctx, "error accessing file %s", args[0]);
1550 if ((address & 3) || (size & 3))
1552 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
1558 u32 this_run_size = (size > 560) ? 560 : size;
1559 target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
1560 fwrite(buffer, 1, this_run_size, binary);
1561 size -= this_run_size;
1562 address += this_run_size;
1571 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1574 target_t *target = get_current_target(cmd_ctx);
1578 breakpoint_t *breakpoint = target->breakpoints;
1582 if (breakpoint->type == BKPT_SOFT)
1584 char* buf = buf_to_char(breakpoint->orig_instr, breakpoint->length);
1585 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
1590 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
1592 breakpoint = breakpoint->next;
1600 length = strtoul(args[1], NULL, 0);
1603 if (strcmp(args[2], "hw") == 0)
1606 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
1610 case ERROR_TARGET_NOT_HALTED:
1611 command_print(cmd_ctx, "target must be halted to set breakpoints");
1613 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1614 command_print(cmd_ctx, "no more breakpoints available");
1617 command_print(cmd_ctx, "unknown error, breakpoint not set");
1626 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1628 target_t *target = get_current_target(cmd_ctx);
1631 breakpoint_remove(target, strtoul(args[0], NULL, 0));
1636 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1638 target_t *target = get_current_target(cmd_ctx);
1642 watchpoint_t *watchpoint = target->watchpoints;
1646 command_print(cmd_ctx, "address: 0x%8.8x, mask: 0x%8.8x, r/w/a: %i, value: 0x%8.8x, mask: 0x%8.8x", watchpoint->address, watchpoint->length, watchpoint->rw, watchpoint->value, watchpoint->mask);
1647 watchpoint = watchpoint->next;
1652 enum watchpoint_rw type = WPT_ACCESS;
1653 u32 data_value = 0x0;
1654 u32 data_mask = 0xffffffff;
1670 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1676 data_value = strtoul(args[3], NULL, 0);
1680 data_mask = strtoul(args[4], NULL, 0);
1682 watchpoint_add(target, strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0), type, data_value, data_mask);
1686 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1692 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1694 target_t *target = get_current_target(cmd_ctx);
1697 watchpoint_remove(target, strtoul(args[0], NULL, 0));