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 /* read a u32 from a buffer in target memory endianness */
115 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
117 if (target->endianness == TARGET_LITTLE_ENDIAN)
118 return le_to_h_u32(buffer);
120 return be_to_h_u32(buffer);
123 /* read a u16 from a buffer in target memory endianness */
124 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
126 if (target->endianness == TARGET_LITTLE_ENDIAN)
127 return le_to_h_u16(buffer);
129 return be_to_h_u16(buffer);
132 /* write a u32 to a buffer in target memory endianness */
133 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
135 if (target->endianness == TARGET_LITTLE_ENDIAN)
136 h_u32_to_le(buffer, value);
138 h_u32_to_be(buffer, value);
141 /* write a u16 to a buffer in target memory endianness */
142 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
144 if (target->endianness == TARGET_LITTLE_ENDIAN)
145 h_u16_to_le(buffer, value);
147 h_u16_to_be(buffer, value);
150 /* returns a pointer to the n-th configured target */
151 target_t* get_target_by_num(int num)
153 target_t *target = targets;
160 target = target->next;
167 int get_num_by_target(target_t *query_target)
169 target_t *target = targets;
174 if (target == query_target)
176 target = target->next;
183 target_t* get_current_target(command_context_t *cmd_ctx)
185 target_t *target = get_target_by_num(cmd_ctx->current_target);
189 ERROR("BUG: current_target out of bounds");
196 /* Process target initialization, when target entered debug out of reset
197 * the handler is unregistered at the end of this function, so it's only called once
199 int target_init_handler(struct target_s *target, enum target_event event, void *priv)
202 struct command_context_s *cmd_ctx = priv;
204 if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
206 script = fopen(target->reset_script, "r");
209 ERROR("couldn't open script file %s", target->reset_script);
213 INFO("executing reset script '%s'", target->reset_script);
214 command_run_file(cmd_ctx, script, COMMAND_EXEC);
217 jtag_execute_queue();
219 target_unregister_event_callback(target_init_handler, priv);
225 int target_run_and_halt_handler(void *priv)
227 target_t *target = priv;
229 target->type->halt(target);
234 int target_process_reset(struct command_context_s *cmd_ctx)
236 int retval = ERROR_OK;
242 target->type->assert_reset(target);
243 target = target->next;
245 jtag_execute_queue();
247 /* request target halt if necessary, and schedule further action */
251 switch (target->reset_mode)
254 /* nothing to do if target just wants to be run */
256 case RESET_RUN_AND_HALT:
258 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
260 case RESET_RUN_AND_INIT:
262 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
263 target_register_event_callback(target_init_handler, cmd_ctx);
266 target->type->halt(target);
269 target->type->halt(target);
270 target_register_event_callback(target_init_handler, cmd_ctx);
273 ERROR("BUG: unknown target->reset_mode");
275 target = target->next;
281 target->type->deassert_reset(target);
282 target = target->next;
284 jtag_execute_queue();
289 int target_init(struct command_context_s *cmd_ctx)
291 target_t *target = targets;
295 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
297 ERROR("target '%s' init failed", target->type->name);
300 target = target->next;
305 target_register_user_commands(cmd_ctx);
306 target_register_timer_callback(handle_target, 100, 1, NULL);
309 if (startup_mode == DAEMON_RESET)
310 target_process_reset(cmd_ctx);
315 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
317 target_event_callback_t **callbacks_p = &target_event_callbacks;
319 if (callback == NULL)
321 return ERROR_INVALID_ARGUMENTS;
326 while ((*callbacks_p)->next)
327 callbacks_p = &((*callbacks_p)->next);
328 callbacks_p = &((*callbacks_p)->next);
331 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
332 (*callbacks_p)->callback = callback;
333 (*callbacks_p)->priv = priv;
334 (*callbacks_p)->next = NULL;
339 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
341 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
344 if (callback == NULL)
346 return ERROR_INVALID_ARGUMENTS;
351 while ((*callbacks_p)->next)
352 callbacks_p = &((*callbacks_p)->next);
353 callbacks_p = &((*callbacks_p)->next);
356 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
357 (*callbacks_p)->callback = callback;
358 (*callbacks_p)->periodic = periodic;
359 (*callbacks_p)->time_ms = time_ms;
361 gettimeofday(&now, NULL);
362 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
363 time_ms -= (time_ms % 1000);
364 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
365 if ((*callbacks_p)->when.tv_usec > 1000000)
367 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
368 (*callbacks_p)->when.tv_sec += 1;
371 (*callbacks_p)->priv = priv;
372 (*callbacks_p)->next = NULL;
377 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
379 target_event_callback_t **p = &target_event_callbacks;
380 target_event_callback_t *c = target_event_callbacks;
382 if (callback == NULL)
384 return ERROR_INVALID_ARGUMENTS;
389 target_event_callback_t *next = c->next;
390 if ((c->callback == callback) && (c->priv == priv))
404 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
406 target_timer_callback_t **p = &target_timer_callbacks;
407 target_timer_callback_t *c = target_timer_callbacks;
409 if (callback == NULL)
411 return ERROR_INVALID_ARGUMENTS;
416 target_timer_callback_t *next = c->next;
417 if ((c->callback == callback) && (c->priv == priv))
431 int target_call_event_callbacks(target_t *target, enum target_event event)
433 target_event_callback_t *callback = target_event_callbacks;
434 target_event_callback_t *next_callback;
436 DEBUG("target event %i", event);
440 next_callback = callback->next;
441 callback->callback(target, event, callback->priv);
442 callback = next_callback;
448 int target_call_timer_callbacks()
450 target_timer_callback_t *callback = target_timer_callbacks;
451 target_timer_callback_t *next_callback;
454 gettimeofday(&now, NULL);
458 next_callback = callback->next;
460 if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
461 || (now.tv_sec > callback->when.tv_sec))
463 callback->callback(callback->priv);
464 if (callback->periodic)
466 int time_ms = callback->time_ms;
467 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
468 time_ms -= (time_ms % 1000);
469 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
470 if (callback->when.tv_usec > 1000000)
472 callback->when.tv_usec = callback->when.tv_usec - 1000000;
473 callback->when.tv_sec += 1;
477 target_unregister_timer_callback(callback->callback, callback->priv);
480 callback = next_callback;
486 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
488 working_area_t *c = target->working_areas;
489 working_area_t *new_wa = NULL;
491 /* only allocate multiples of 4 byte */
494 ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
495 size = CEIL(size, 4);
498 /* see if there's already a matching working area */
501 if ((c->free) && (c->size == size))
509 /* if not, allocate a new one */
512 working_area_t **p = &target->working_areas;
513 u32 first_free = target->working_area;
514 u32 free_size = target->working_area_size;
516 DEBUG("allocating new working area");
518 c = target->working_areas;
521 first_free += c->size;
522 free_size -= c->size;
527 if (free_size < size)
529 WARNING("not enough working area available");
530 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
533 new_wa = malloc(sizeof(working_area_t));
536 new_wa->address = first_free;
538 if (target->backup_working_area)
540 new_wa->backup = malloc(new_wa->size);
541 target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
545 new_wa->backup = NULL;
548 /* put new entry in list */
552 /* mark as used, and return the new (reused) area */
562 int target_free_working_area(struct target_s *target, working_area_t *area)
567 if (target->backup_working_area)
568 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
572 /* mark user pointer invalid */
579 int target_free_all_working_areas(struct target_s *target)
581 working_area_t *c = target->working_areas;
585 working_area_t *next = c->next;
586 target_free_working_area(target, c);
596 target->working_areas = NULL;
601 int target_register_commands(struct command_context_s *cmd_ctx)
603 register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, NULL);
604 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
605 register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
606 register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
607 register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, NULL);
608 register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_CONFIG, NULL);
613 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
617 DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
619 /* handle writes of less than 4 byte */
622 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
626 /* handle unaligned head bytes */
629 int unaligned = 4 - (address % 4);
631 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
635 address += unaligned;
639 /* handle aligned words */
642 int aligned = size - (size % 4);
644 /* use bulk writes above a certain limit. This may have to be changed */
647 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
652 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
661 /* handle tail writes of less than 4 bytes */
664 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
671 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
675 DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
677 /* handle reads of less than 4 byte */
680 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
684 /* handle unaligned head bytes */
687 int unaligned = 4 - (address % 4);
689 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
692 address += unaligned;
696 /* handle aligned words */
699 int aligned = size - (size % 4);
701 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
708 /* handle tail writes of less than 4 bytes */
711 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
718 int target_register_user_commands(struct command_context_s *cmd_ctx)
720 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
721 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
722 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt");
723 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
724 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
725 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction");
726 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init|run_and_halt|run_and_init]");
727 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
729 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
730 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
731 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
733 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value>");
734 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value>");
735 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value>");
737 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
738 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
739 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
740 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
742 register_command(cmd_ctx, NULL, "load_binary", handle_load_binary_command, COMMAND_EXEC, "load binary <file> <address>");
743 register_command(cmd_ctx, NULL, "dump_binary", handle_dump_binary_command, COMMAND_EXEC, "dump binary <file> <address> <size>");
748 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
750 target_t *target = targets;
755 int num = strtoul(args[0], NULL, 0);
760 target = target->next;
764 cmd_ctx->current_target = num;
766 command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
773 command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
774 target = target->next;
780 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
787 ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
791 /* search for the specified target */
792 if (args[0] && (args[0][0] != 0))
794 for (i = 0; target_types[i]; i++)
796 if (strcmp(args[0], target_types[i]->name) == 0)
798 target_t **last_target_p = &targets;
800 /* register target specific commands */
801 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
803 ERROR("couldn't register '%s' commands", args[0]);
809 while ((*last_target_p)->next)
810 last_target_p = &((*last_target_p)->next);
811 last_target_p = &((*last_target_p)->next);
814 *last_target_p = malloc(sizeof(target_t));
816 (*last_target_p)->type = target_types[i];
818 if (strcmp(args[1], "big") == 0)
819 (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
820 else if (strcmp(args[1], "little") == 0)
821 (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
824 ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
828 /* what to do on a target reset */
829 if (strcmp(args[2], "reset_halt") == 0)
830 (*last_target_p)->reset_mode = RESET_HALT;
831 else if (strcmp(args[2], "reset_run") == 0)
832 (*last_target_p)->reset_mode = RESET_RUN;
833 else if (strcmp(args[2], "reset_init") == 0)
834 (*last_target_p)->reset_mode = RESET_INIT;
835 else if (strcmp(args[2], "run_and_halt") == 0)
836 (*last_target_p)->reset_mode = RESET_RUN_AND_HALT;
837 else if (strcmp(args[2], "run_and_init") == 0)
838 (*last_target_p)->reset_mode = RESET_RUN_AND_INIT;
841 ERROR("unknown target startup mode %s", args[2]);
844 (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
846 (*last_target_p)->reset_script = NULL;
847 (*last_target_p)->post_halt_script = NULL;
848 (*last_target_p)->pre_resume_script = NULL;
850 (*last_target_p)->working_area = 0x0;
851 (*last_target_p)->working_area_size = 0x0;
852 (*last_target_p)->working_areas = NULL;
853 (*last_target_p)->backup_working_area = 0;
855 (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
856 (*last_target_p)->state = TARGET_UNKNOWN;
857 (*last_target_p)->reg_cache = NULL;
858 (*last_target_p)->breakpoints = NULL;
859 (*last_target_p)->watchpoints = NULL;
860 (*last_target_p)->next = NULL;
861 (*last_target_p)->arch_info = NULL;
863 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
871 /* no matching target found */
874 ERROR("target '%s' not found", args[0]);
881 /* usage: target_script <target#> <event> <script_file> */
882 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
884 target_t *target = NULL;
888 ERROR("incomplete target_script command");
892 target = get_target_by_num(strtoul(args[0], NULL, 0));
896 ERROR("target number '%s' not defined", args[0]);
900 if (strcmp(args[1], "reset") == 0)
902 if (target->reset_script)
903 free(target->reset_script);
904 target->reset_script = strdup(args[2]);
906 else if (strcmp(args[1], "post_halt") == 0)
908 if (target->post_halt_script)
909 free(target->post_halt_script);
910 target->post_halt_script = strdup(args[2]);
912 else if (strcmp(args[1], "pre_resume") == 0)
914 if (target->pre_resume_script)
915 free(target->pre_resume_script);
916 target->pre_resume_script = strdup(args[2]);
920 ERROR("unknown event type: '%s", args[1]);
927 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
929 target_t *target = NULL;
933 ERROR("incomplete run_and_halt_time command");
937 target = get_target_by_num(strtoul(args[0], NULL, 0));
941 ERROR("target number '%s' not defined", args[0]);
945 target->run_and_halt_time = strtoul(args[1], NULL, 0);
950 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
952 target_t *target = NULL;
956 ERROR("incomplete working_area command. usage: working_area <target#> <address> <size> <'backup'|'nobackup'>");
960 target = get_target_by_num(strtoul(args[0], NULL, 0));
964 ERROR("target number '%s' not defined", args[0]);
968 target->working_area = strtoul(args[1], NULL, 0);
969 target->working_area_size = strtoul(args[2], NULL, 0);
971 if (strcmp(args[3], "backup") == 0)
973 target->backup_working_area = 1;
975 else if (strcmp(args[3], "nobackup") == 0)
977 target->backup_working_area = 0;
981 ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
989 /* process target state changes */
990 int handle_target(void *priv)
993 target_t *target = targets;
997 /* only poll if target isn't already halted */
998 if (target->state != TARGET_HALTED)
1000 if (target_continous_poll)
1001 if ((retval = target->type->poll(target)) < 0)
1003 ERROR("couldn't poll target, exiting");
1008 target = target->next;
1014 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1023 target = get_current_target(cmd_ctx);
1025 /* list all available registers for the current target */
1028 reg_cache_t *cache = target->reg_cache;
1034 for (i = 0; i < cache->num_regs; i++)
1036 value = buf_to_char(cache->reg_list[i].value, cache->reg_list[i].size);
1037 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);
1040 cache = cache->next;
1046 /* access a single register by its ordinal number */
1047 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1049 int num = strtoul(args[0], NULL, 0);
1050 reg_cache_t *cache = target->reg_cache;
1056 for (i = 0; i < cache->num_regs; i++)
1060 reg = &cache->reg_list[i];
1066 cache = cache->next;
1071 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1074 } else /* access a single register by its name */
1076 reg = register_get_by_name(target->reg_cache, args[0], 1);
1080 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1085 /* display a register */
1086 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1088 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1091 if (reg->valid == 0)
1093 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1094 if (arch_type == NULL)
1096 ERROR("BUG: encountered unregistered arch type");
1099 arch_type->get(reg);
1101 value = buf_to_char(reg->value, reg->size);
1102 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1107 /* set register value */
1110 u32 new_value = strtoul(args[1], NULL, 0);
1111 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1112 if (arch_type == NULL)
1114 ERROR("BUG: encountered unregistered arch type");
1118 arch_type->set(reg, new_value);
1119 value = buf_to_char(reg->value, reg->size);
1120 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1126 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1131 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1133 target_t *target = get_current_target(cmd_ctx);
1138 command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
1139 if (target->state == TARGET_HALTED)
1141 target->type->arch_state(target, buffer, 512);
1143 command_print(cmd_ctx, "%s", buffer);
1148 if (strcmp(args[0], "on") == 0)
1150 target_continous_poll = 1;
1152 else if (strcmp(args[0], "off") == 0)
1154 target_continous_poll = 0;
1162 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1164 target_t *target = get_current_target(cmd_ctx);
1165 struct timeval timeout, now;
1167 gettimeofday(&timeout, NULL);
1168 timeval_add_time(&timeout, 5, 0);
1170 command_print(cmd_ctx, "waiting for target halted...");
1172 while(target->type->poll(target))
1174 if (target->state == TARGET_HALTED)
1176 command_print(cmd_ctx, "target halted");
1179 target_call_timer_callbacks();
1181 gettimeofday(&now, NULL);
1182 if ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))
1184 command_print(cmd_ctx, "timed out while waiting for target halt");
1185 ERROR("timed out while waiting for target halt");
1193 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1196 target_t *target = get_current_target(cmd_ctx);
1200 command_print(cmd_ctx, "requesting target halt...");
1202 if ((retval = target->type->halt(target)) != ERROR_OK)
1206 case ERROR_TARGET_ALREADY_HALTED:
1207 command_print(cmd_ctx, "target already halted");
1209 case ERROR_TARGET_TIMEOUT:
1210 command_print(cmd_ctx, "target timed out... shutting down");
1213 command_print(cmd_ctx, "unknown error... shutting down");
1222 /* what to do on daemon startup */
1223 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1227 if (strcmp(args[0], "attach") == 0)
1229 startup_mode = DAEMON_ATTACH;
1232 else if (strcmp(args[0], "reset") == 0)
1234 startup_mode = DAEMON_RESET;
1239 WARNING("invalid daemon_startup configuration directive: %s", args[0]);
1244 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1246 target_t *target = get_current_target(cmd_ctx);
1249 command_print(cmd_ctx, "requesting target halt and executing a soft reset");
1251 if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
1255 case ERROR_TARGET_TIMEOUT:
1256 command_print(cmd_ctx, "target timed out... shutting down");
1259 command_print(cmd_ctx, "unknown error... shutting down");
1267 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1269 target_t *target = get_current_target(cmd_ctx);
1270 enum target_reset_mode reset_mode = RESET_RUN;
1276 if (strcmp("run", args[0]) == 0)
1277 reset_mode = RESET_RUN;
1278 else if (strcmp("halt", args[0]) == 0)
1279 reset_mode = RESET_HALT;
1280 else if (strcmp("init", args[0]) == 0)
1281 reset_mode = RESET_INIT;
1282 else if (strcmp("run_and_halt", args[0]) == 0)
1284 reset_mode = RESET_RUN_AND_HALT;
1287 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1290 else if (strcmp("run_and_init", args[0]) == 0)
1292 reset_mode = RESET_RUN_AND_INIT;
1295 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1300 command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
1303 target->reset_mode = reset_mode;
1306 target_process_reset(cmd_ctx);
1311 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1314 target_t *target = get_current_target(cmd_ctx);
1319 retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1321 retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1324 command_print(cmd_ctx, "usage: resume [address]");
1328 if (retval != ERROR_OK)
1332 case ERROR_TARGET_NOT_HALTED:
1333 command_print(cmd_ctx, "target not halted");
1336 command_print(cmd_ctx, "unknown error... shutting down");
1344 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1346 target_t *target = get_current_target(cmd_ctx);
1351 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1354 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1359 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1372 target_t *target = get_current_target(cmd_ctx);
1378 count = strtoul(args[1], NULL, 0);
1380 address = strtoul(args[0], NULL, 0);
1398 buffer = calloc(count, size);
1399 if ((retval = target->type->read_memory(target, address, size, count, buffer)) != ERROR_OK)
1403 case ERROR_TARGET_UNALIGNED_ACCESS:
1404 command_print(cmd_ctx, "error: address not aligned");
1406 case ERROR_TARGET_NOT_HALTED:
1407 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1409 case ERROR_TARGET_DATA_ABORT:
1410 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1413 command_print(cmd_ctx, "error: unknown error");
1420 for (i = 0; i < count; i++)
1423 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1428 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", ((u32*)buffer)[i]);
1431 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", ((u16*)buffer)[i]);
1434 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", ((u8*)buffer)[i]);
1438 if ((i%8 == 7) || (i == count - 1))
1440 command_print(cmd_ctx, output);
1450 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1455 target_t *target = get_current_target(cmd_ctx);
1460 address = strtoul(args[0], NULL, 0);
1461 value = strtoul(args[1], NULL, 0);
1466 retval = target->type->write_memory(target, address, 4, 1, (u8*)&value);
1469 retval = target->type->write_memory(target, address, 2, 1, (u8*)&value);
1472 retval = target->type->write_memory(target, address, 1, 1, (u8*)&value);
1480 case ERROR_TARGET_UNALIGNED_ACCESS:
1481 command_print(cmd_ctx, "error: address not aligned");
1483 case ERROR_TARGET_DATA_ABORT:
1484 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1486 case ERROR_TARGET_NOT_HALTED:
1487 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1492 command_print(cmd_ctx, "error: unknown error");
1500 int handle_load_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1504 struct stat binary_stat;
1510 struct timeval start, end;
1512 target_t *target = get_current_target(cmd_ctx);
1516 command_print(cmd_ctx, "usage: load_binary <filename> <address>");
1520 address = strtoul(args[1], NULL, 0);
1522 if (stat(args[0], &binary_stat) == -1)
1524 ERROR("couldn't stat() %s: %s", args[0], strerror(errno));
1525 command_print(cmd_ctx, "error accessing file %s", args[0]);
1529 if (!(binary = fopen(args[0], "r")))
1531 ERROR("couldn't open %s: %s", args[0], strerror(errno));
1532 command_print(cmd_ctx, "error accessing file %s", args[0]);
1536 buffer = malloc(128 * 1024);
1538 gettimeofday(&start, NULL);
1540 binary_size = binary_stat.st_size;
1541 while (binary_size > 0)
1543 buf_cnt = fread(buffer, 1, 128*1024, binary);
1544 target_write_buffer(target, address, buf_cnt, buffer);
1546 binary_size -= buf_cnt;
1549 gettimeofday(&end, NULL);
1553 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);
1561 int handle_dump_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1568 target_t *target = get_current_target(cmd_ctx);
1572 command_print(cmd_ctx, "usage: dump_binary <filename> <address> <size>");
1576 address = strtoul(args[1], NULL, 0);
1577 size = strtoul(args[2], NULL, 0);
1579 if (!(binary = fopen(args[0], "w")))
1581 ERROR("couldn't open %s for writing: %s", args[0], strerror(errno));
1582 command_print(cmd_ctx, "error accessing file %s", args[0]);
1586 if ((address & 3) || (size & 3))
1588 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
1594 u32 this_run_size = (size > 560) ? 560 : size;
1595 target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
1596 fwrite(buffer, 1, this_run_size, binary);
1597 size -= this_run_size;
1598 address += this_run_size;
1607 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1610 target_t *target = get_current_target(cmd_ctx);
1614 breakpoint_t *breakpoint = target->breakpoints;
1618 if (breakpoint->type == BKPT_SOFT)
1620 char* buf = buf_to_char(breakpoint->orig_instr, breakpoint->length);
1621 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
1626 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
1628 breakpoint = breakpoint->next;
1636 length = strtoul(args[1], NULL, 0);
1639 if (strcmp(args[2], "hw") == 0)
1642 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
1646 case ERROR_TARGET_NOT_HALTED:
1647 command_print(cmd_ctx, "target must be halted to set breakpoints");
1649 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1650 command_print(cmd_ctx, "no more breakpoints available");
1653 command_print(cmd_ctx, "unknown error, breakpoint not set");
1662 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1664 target_t *target = get_current_target(cmd_ctx);
1667 breakpoint_remove(target, strtoul(args[0], NULL, 0));
1672 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1674 target_t *target = get_current_target(cmd_ctx);
1678 watchpoint_t *watchpoint = target->watchpoints;
1682 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);
1683 watchpoint = watchpoint->next;
1688 enum watchpoint_rw type = WPT_ACCESS;
1689 u32 data_value = 0x0;
1690 u32 data_mask = 0xffffffff;
1706 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1712 data_value = strtoul(args[3], NULL, 0);
1716 data_mask = strtoul(args[4], NULL, 0);
1718 watchpoint_add(target, strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0), type, data_value, data_mask);
1722 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1728 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1730 target_t *target = get_current_target(cmd_ctx);
1733 watchpoint_remove(target, strtoul(args[0], NULL, 0));