1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008, Duane Ellis *
11 * openocd@duaneeellis.com *
13 * Copyright (C) 2008 by Spencer Oliver *
14 * spen@spen-soft.co.uk *
16 * Copyright (C) 2008 by Rick Altherr *
17 * kc8apf@kc8apf.net> *
19 * Copyright (C) 2011 by Broadcom Corporation *
20 * Evan Hunter - ehunter@broadcom.com *
22 * Copyright (C) ST-Ericsson SA 2011 *
23 * michel.jaouen@stericsson.com : smp minimum support *
25 * Copyright (C) 2011 Andreas Fritiofson *
26 * andreas.fritiofson@gmail.com *
27 ***************************************************************************/
33 #include <helper/align.h>
34 #include <helper/time_support.h>
35 #include <jtag/jtag.h>
36 #include <flash/nor/core.h>
39 #include "target_type.h"
40 #include "target_request.h"
41 #include "breakpoints.h"
45 #include "rtos/rtos.h"
46 #include "transport/transport.h"
49 #include "semihosting_common.h"
51 /* default halt wait timeout (ms) */
52 #define DEFAULT_HALT_TIMEOUT 5000
54 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
55 uint32_t count
, uint8_t *buffer
);
56 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
57 uint32_t count
, const uint8_t *buffer
);
58 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
59 int argc
, Jim_Obj
* const *argv
);
60 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
61 int argc
, Jim_Obj
* const *argv
);
62 static int target_register_user_commands(struct command_context
*cmd_ctx
);
63 static int target_get_gdb_fileio_info_default(struct target
*target
,
64 struct gdb_fileio_info
*fileio_info
);
65 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
66 int fileio_errno
, bool ctrl_c
);
69 extern struct target_type arm7tdmi_target
;
70 extern struct target_type arm720t_target
;
71 extern struct target_type arm9tdmi_target
;
72 extern struct target_type arm920t_target
;
73 extern struct target_type arm966e_target
;
74 extern struct target_type arm946e_target
;
75 extern struct target_type arm926ejs_target
;
76 extern struct target_type fa526_target
;
77 extern struct target_type feroceon_target
;
78 extern struct target_type dragonite_target
;
79 extern struct target_type xscale_target
;
80 extern struct target_type xtensa_chip_target
;
81 extern struct target_type cortexm_target
;
82 extern struct target_type cortexa_target
;
83 extern struct target_type aarch64_target
;
84 extern struct target_type cortexr4_target
;
85 extern struct target_type arm11_target
;
86 extern struct target_type ls1_sap_target
;
87 extern struct target_type mips_m4k_target
;
88 extern struct target_type mips_mips64_target
;
89 extern struct target_type avr_target
;
90 extern struct target_type dsp563xx_target
;
91 extern struct target_type dsp5680xx_target
;
92 extern struct target_type testee_target
;
93 extern struct target_type avr32_ap7k_target
;
94 extern struct target_type hla_target
;
95 extern struct target_type nds32_v2_target
;
96 extern struct target_type nds32_v3_target
;
97 extern struct target_type nds32_v3m_target
;
98 extern struct target_type esp32_target
;
99 extern struct target_type esp32s2_target
;
100 extern struct target_type esp32s3_target
;
101 extern struct target_type or1k_target
;
102 extern struct target_type quark_x10xx_target
;
103 extern struct target_type quark_d20xx_target
;
104 extern struct target_type stm8_target
;
105 extern struct target_type riscv_target
;
106 extern struct target_type mem_ap_target
;
107 extern struct target_type esirisc_target
;
108 extern struct target_type arcv2_target
;
110 static struct target_type
*target_types
[] = {
154 struct target
*all_targets
;
155 static struct target_event_callback
*target_event_callbacks
;
156 static struct target_timer_callback
*target_timer_callbacks
;
157 static int64_t target_timer_next_event_value
;
158 static LIST_HEAD(target_reset_callback_list
);
159 static LIST_HEAD(target_trace_callback_list
);
160 static const int polling_interval
= TARGET_DEFAULT_POLLING_INTERVAL
;
161 static LIST_HEAD(empty_smp_targets
);
163 static const struct jim_nvp nvp_assert
[] = {
164 { .name
= "assert", NVP_ASSERT
},
165 { .name
= "deassert", NVP_DEASSERT
},
166 { .name
= "T", NVP_ASSERT
},
167 { .name
= "F", NVP_DEASSERT
},
168 { .name
= "t", NVP_ASSERT
},
169 { .name
= "f", NVP_DEASSERT
},
170 { .name
= NULL
, .value
= -1 }
173 static const struct jim_nvp nvp_error_target
[] = {
174 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
175 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
176 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
177 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
178 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
179 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
180 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
181 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
182 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
183 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
184 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
185 { .value
= -1, .name
= NULL
}
188 static const char *target_strerror_safe(int err
)
190 const struct jim_nvp
*n
;
192 n
= jim_nvp_value2name_simple(nvp_error_target
, err
);
199 static const struct jim_nvp nvp_target_event
[] = {
201 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
202 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
203 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
204 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
205 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
206 { .value
= TARGET_EVENT_STEP_START
, .name
= "step-start" },
207 { .value
= TARGET_EVENT_STEP_END
, .name
= "step-end" },
209 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
210 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
212 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
213 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
214 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
215 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
216 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
217 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
218 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
219 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
221 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
222 { .value
= TARGET_EVENT_EXAMINE_FAIL
, .name
= "examine-fail" },
223 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
225 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
226 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
228 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
229 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
231 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
232 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
234 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
235 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
237 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
239 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100
, .name
= "semihosting-user-cmd-0x100" },
240 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101
, .name
= "semihosting-user-cmd-0x101" },
241 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102
, .name
= "semihosting-user-cmd-0x102" },
242 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103
, .name
= "semihosting-user-cmd-0x103" },
243 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104
, .name
= "semihosting-user-cmd-0x104" },
244 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105
, .name
= "semihosting-user-cmd-0x105" },
245 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106
, .name
= "semihosting-user-cmd-0x106" },
246 { .value
= TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107
, .name
= "semihosting-user-cmd-0x107" },
248 { .name
= NULL
, .value
= -1 }
251 static const struct jim_nvp nvp_target_state
[] = {
252 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
253 { .name
= "running", .value
= TARGET_RUNNING
},
254 { .name
= "halted", .value
= TARGET_HALTED
},
255 { .name
= "reset", .value
= TARGET_RESET
},
256 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
257 { .name
= NULL
, .value
= -1 },
260 static const struct jim_nvp nvp_target_debug_reason
[] = {
261 { .name
= "debug-request", .value
= DBG_REASON_DBGRQ
},
262 { .name
= "breakpoint", .value
= DBG_REASON_BREAKPOINT
},
263 { .name
= "watchpoint", .value
= DBG_REASON_WATCHPOINT
},
264 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
265 { .name
= "single-step", .value
= DBG_REASON_SINGLESTEP
},
266 { .name
= "target-not-halted", .value
= DBG_REASON_NOTHALTED
},
267 { .name
= "program-exit", .value
= DBG_REASON_EXIT
},
268 { .name
= "exception-catch", .value
= DBG_REASON_EXC_CATCH
},
269 { .name
= "undefined", .value
= DBG_REASON_UNDEFINED
},
270 { .name
= NULL
, .value
= -1 },
273 static const struct jim_nvp nvp_target_endian
[] = {
274 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
275 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
276 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
277 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
278 { .name
= NULL
, .value
= -1 },
281 static const struct jim_nvp nvp_reset_modes
[] = {
282 { .name
= "unknown", .value
= RESET_UNKNOWN
},
283 { .name
= "run", .value
= RESET_RUN
},
284 { .name
= "halt", .value
= RESET_HALT
},
285 { .name
= "init", .value
= RESET_INIT
},
286 { .name
= NULL
, .value
= -1 },
289 const char *debug_reason_name(struct target
*t
)
293 cp
= jim_nvp_value2name_simple(nvp_target_debug_reason
,
294 t
->debug_reason
)->name
;
296 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
297 cp
= "(*BUG*unknown*BUG*)";
302 const char *target_state_name(struct target
*t
)
305 cp
= jim_nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
307 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
308 cp
= "(*BUG*unknown*BUG*)";
311 if (!target_was_examined(t
) && t
->defer_examine
)
312 cp
= "examine deferred";
317 const char *target_event_name(enum target_event event
)
320 cp
= jim_nvp_value2name_simple(nvp_target_event
, event
)->name
;
322 LOG_ERROR("Invalid target event: %d", (int)(event
));
323 cp
= "(*BUG*unknown*BUG*)";
328 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
331 cp
= jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
333 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
334 cp
= "(*BUG*unknown*BUG*)";
339 /* determine the number of the new target */
340 static int new_target_number(void)
345 /* number is 0 based */
349 if (x
< t
->target_number
)
350 x
= t
->target_number
;
356 static void append_to_list_all_targets(struct target
*target
)
358 struct target
**t
= &all_targets
;
365 /* read a uint64_t from a buffer in target memory endianness */
366 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
368 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
369 return le_to_h_u64(buffer
);
371 return be_to_h_u64(buffer
);
374 /* read a uint32_t from a buffer in target memory endianness */
375 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
377 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
378 return le_to_h_u32(buffer
);
380 return be_to_h_u32(buffer
);
383 /* read a uint24_t from a buffer in target memory endianness */
384 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
386 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
387 return le_to_h_u24(buffer
);
389 return be_to_h_u24(buffer
);
392 /* read a uint16_t from a buffer in target memory endianness */
393 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
395 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
396 return le_to_h_u16(buffer
);
398 return be_to_h_u16(buffer
);
401 /* write a uint64_t to a buffer in target memory endianness */
402 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
404 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
405 h_u64_to_le(buffer
, value
);
407 h_u64_to_be(buffer
, value
);
410 /* write a uint32_t to a buffer in target memory endianness */
411 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
413 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
414 h_u32_to_le(buffer
, value
);
416 h_u32_to_be(buffer
, value
);
419 /* write a uint24_t to a buffer in target memory endianness */
420 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
422 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
423 h_u24_to_le(buffer
, value
);
425 h_u24_to_be(buffer
, value
);
428 /* write a uint16_t to a buffer in target memory endianness */
429 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
431 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
432 h_u16_to_le(buffer
, value
);
434 h_u16_to_be(buffer
, value
);
437 /* write a uint8_t to a buffer in target memory endianness */
438 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
443 /* write a uint64_t array to a buffer in target memory endianness */
444 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
447 for (i
= 0; i
< count
; i
++)
448 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
451 /* write a uint32_t array to a buffer in target memory endianness */
452 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
455 for (i
= 0; i
< count
; i
++)
456 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
459 /* write a uint16_t array to a buffer in target memory endianness */
460 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
463 for (i
= 0; i
< count
; i
++)
464 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
467 /* write a uint64_t array to a buffer in target memory endianness */
468 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
471 for (i
= 0; i
< count
; i
++)
472 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
475 /* write a uint32_t array to a buffer in target memory endianness */
476 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
479 for (i
= 0; i
< count
; i
++)
480 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
483 /* write a uint16_t array to a buffer in target memory endianness */
484 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
487 for (i
= 0; i
< count
; i
++)
488 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
491 /* return a pointer to a configured target; id is name or number */
492 struct target
*get_target(const char *id
)
494 struct target
*target
;
496 /* try as tcltarget name */
497 for (target
= all_targets
; target
; target
= target
->next
) {
498 if (!target_name(target
))
500 if (strcmp(id
, target_name(target
)) == 0)
504 /* It's OK to remove this fallback sometime after August 2010 or so */
506 /* no match, try as number */
508 if (parse_uint(id
, &num
) != ERROR_OK
)
511 for (target
= all_targets
; target
; target
= target
->next
) {
512 if (target
->target_number
== (int)num
) {
513 LOG_WARNING("use '%s' as target identifier, not '%u'",
514 target_name(target
), num
);
522 /* returns a pointer to the n-th configured target */
523 struct target
*get_target_by_num(int num
)
525 struct target
*target
= all_targets
;
528 if (target
->target_number
== num
)
530 target
= target
->next
;
536 struct target
*get_current_target(struct command_context
*cmd_ctx
)
538 struct target
*target
= get_current_target_or_null(cmd_ctx
);
541 LOG_ERROR("BUG: current_target out of bounds");
548 struct target
*get_current_target_or_null(struct command_context
*cmd_ctx
)
550 return cmd_ctx
->current_target_override
551 ? cmd_ctx
->current_target_override
552 : cmd_ctx
->current_target
;
555 int target_poll(struct target
*target
)
559 /* We can't poll until after examine */
560 if (!target_was_examined(target
)) {
561 /* Fail silently lest we pollute the log */
565 retval
= target
->type
->poll(target
);
566 if (retval
!= ERROR_OK
)
569 if (target
->halt_issued
) {
570 if (target
->state
== TARGET_HALTED
)
571 target
->halt_issued
= false;
573 int64_t t
= timeval_ms() - target
->halt_issued_time
;
574 if (t
> DEFAULT_HALT_TIMEOUT
) {
575 target
->halt_issued
= false;
576 LOG_INFO("Halt timed out, wake up GDB.");
577 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
585 int target_halt(struct target
*target
)
588 /* We can't poll until after examine */
589 if (!target_was_examined(target
)) {
590 LOG_ERROR("Target not examined yet");
594 retval
= target
->type
->halt(target
);
595 if (retval
!= ERROR_OK
)
598 target
->halt_issued
= true;
599 target
->halt_issued_time
= timeval_ms();
605 * Make the target (re)start executing using its saved execution
606 * context (possibly with some modifications).
608 * @param target Which target should start executing.
609 * @param current True to use the target's saved program counter instead
610 * of the address parameter
611 * @param address Optionally used as the program counter.
612 * @param handle_breakpoints True iff breakpoints at the resumption PC
613 * should be skipped. (For example, maybe execution was stopped by
614 * such a breakpoint, in which case it would be counterproductive to
616 * @param debug_execution False if all working areas allocated by OpenOCD
617 * should be released and/or restored to their original contents.
618 * (This would for example be true to run some downloaded "helper"
619 * algorithm code, which resides in one such working buffer and uses
620 * another for data storage.)
622 * @todo Resolve the ambiguity about what the "debug_execution" flag
623 * signifies. For example, Target implementations don't agree on how
624 * it relates to invalidation of the register cache, or to whether
625 * breakpoints and watchpoints should be enabled. (It would seem wrong
626 * to enable breakpoints when running downloaded "helper" algorithms
627 * (debug_execution true), since the breakpoints would be set to match
628 * target firmware being debugged, not the helper algorithm.... and
629 * enabling them could cause such helpers to malfunction (for example,
630 * by overwriting data with a breakpoint instruction. On the other
631 * hand the infrastructure for running such helpers might use this
632 * procedure but rely on hardware breakpoint to detect termination.)
634 int target_resume(struct target
*target
, int current
, target_addr_t address
,
635 int handle_breakpoints
, int debug_execution
)
639 /* We can't poll until after examine */
640 if (!target_was_examined(target
)) {
641 LOG_ERROR("Target not examined yet");
645 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
647 /* note that resume *must* be asynchronous. The CPU can halt before
648 * we poll. The CPU can even halt at the current PC as a result of
649 * a software breakpoint being inserted by (a bug?) the application.
652 * resume() triggers the event 'resumed'. The execution of TCL commands
653 * in the event handler causes the polling of targets. If the target has
654 * already halted for a breakpoint, polling will run the 'halted' event
655 * handler before the pending 'resumed' handler.
656 * Disable polling during resume() to guarantee the execution of handlers
657 * in the correct order.
659 bool save_poll_mask
= jtag_poll_mask();
660 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
661 jtag_poll_unmask(save_poll_mask
);
663 if (retval
!= ERROR_OK
)
666 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
671 static int target_process_reset(struct command_invocation
*cmd
, enum target_reset_mode reset_mode
)
676 n
= jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
678 LOG_ERROR("invalid reset mode");
682 struct target
*target
;
683 for (target
= all_targets
; target
; target
= target
->next
)
684 target_call_reset_callbacks(target
, reset_mode
);
686 /* disable polling during reset to make reset event scripts
687 * more predictable, i.e. dr/irscan & pathmove in events will
688 * not have JTAG operations injected into the middle of a sequence.
690 bool save_poll_mask
= jtag_poll_mask();
692 sprintf(buf
, "ocd_process_reset %s", n
->name
);
693 retval
= Jim_Eval(cmd
->ctx
->interp
, buf
);
695 jtag_poll_unmask(save_poll_mask
);
697 if (retval
!= JIM_OK
) {
698 Jim_MakeErrorMessage(cmd
->ctx
->interp
);
699 command_print(cmd
, "%s", Jim_GetString(Jim_GetResult(cmd
->ctx
->interp
), NULL
));
703 /* We want any events to be processed before the prompt */
704 retval
= target_call_timer_callbacks_now();
706 for (target
= all_targets
; target
; target
= target
->next
) {
707 target
->type
->check_reset(target
);
708 target
->running_alg
= false;
714 static int identity_virt2phys(struct target
*target
,
715 target_addr_t
virtual, target_addr_t
*physical
)
721 static int no_mmu(struct target
*target
, int *enabled
)
728 * Reset the @c examined flag for the given target.
729 * Pure paranoia -- targets are zeroed on allocation.
731 static inline void target_reset_examined(struct target
*target
)
733 target
->examined
= false;
736 static int default_examine(struct target
*target
)
738 target_set_examined(target
);
742 /* no check by default */
743 static int default_check_reset(struct target
*target
)
748 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
750 int target_examine_one(struct target
*target
)
752 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
754 int retval
= target
->type
->examine(target
);
755 if (retval
!= ERROR_OK
) {
756 target_reset_examined(target
);
757 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_FAIL
);
761 target_set_examined(target
);
762 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
767 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
769 struct target
*target
= priv
;
771 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
774 jtag_unregister_event_callback(jtag_enable_callback
, target
);
776 return target_examine_one(target
);
779 /* Targets that correctly implement init + examine, i.e.
780 * no communication with target during init:
784 int target_examine(void)
786 int retval
= ERROR_OK
;
787 struct target
*target
;
789 for (target
= all_targets
; target
; target
= target
->next
) {
790 /* defer examination, but don't skip it */
791 if (!target
->tap
->enabled
) {
792 jtag_register_event_callback(jtag_enable_callback
,
797 if (target
->defer_examine
)
800 int retval2
= target_examine_one(target
);
801 if (retval2
!= ERROR_OK
) {
802 LOG_WARNING("target %s examination failed", target_name(target
));
809 const char *target_type_name(struct target
*target
)
811 return target
->type
->name
;
814 static int target_soft_reset_halt(struct target
*target
)
816 if (!target_was_examined(target
)) {
817 LOG_ERROR("Target not examined yet");
820 if (!target
->type
->soft_reset_halt
) {
821 LOG_ERROR("Target %s does not support soft_reset_halt",
822 target_name(target
));
825 return target
->type
->soft_reset_halt(target
);
829 * Downloads a target-specific native code algorithm to the target,
830 * and executes it. * Note that some targets may need to set up, enable,
831 * and tear down a breakpoint (hard or * soft) to detect algorithm
832 * termination, while others may support lower overhead schemes where
833 * soft breakpoints embedded in the algorithm automatically terminate the
836 * @param target used to run the algorithm
837 * @param num_mem_params
839 * @param num_reg_params
844 * @param arch_info target-specific description of the algorithm.
846 int target_run_algorithm(struct target
*target
,
847 int num_mem_params
, struct mem_param
*mem_params
,
848 int num_reg_params
, struct reg_param
*reg_param
,
849 target_addr_t entry_point
, target_addr_t exit_point
,
850 int timeout_ms
, void *arch_info
)
852 int retval
= ERROR_FAIL
;
854 if (!target_was_examined(target
)) {
855 LOG_ERROR("Target not examined yet");
858 if (!target
->type
->run_algorithm
) {
859 LOG_ERROR("Target type '%s' does not support %s",
860 target_type_name(target
), __func__
);
864 target
->running_alg
= true;
865 retval
= target
->type
->run_algorithm(target
,
866 num_mem_params
, mem_params
,
867 num_reg_params
, reg_param
,
868 entry_point
, exit_point
, timeout_ms
, arch_info
);
869 target
->running_alg
= false;
876 * Executes a target-specific native code algorithm and leaves it running.
878 * @param target used to run the algorithm
879 * @param num_mem_params
881 * @param num_reg_params
885 * @param arch_info target-specific description of the algorithm.
887 int target_start_algorithm(struct target
*target
,
888 int num_mem_params
, struct mem_param
*mem_params
,
889 int num_reg_params
, struct reg_param
*reg_params
,
890 target_addr_t entry_point
, target_addr_t exit_point
,
893 int retval
= ERROR_FAIL
;
895 if (!target_was_examined(target
)) {
896 LOG_ERROR("Target not examined yet");
899 if (!target
->type
->start_algorithm
) {
900 LOG_ERROR("Target type '%s' does not support %s",
901 target_type_name(target
), __func__
);
904 if (target
->running_alg
) {
905 LOG_ERROR("Target is already running an algorithm");
909 target
->running_alg
= true;
910 retval
= target
->type
->start_algorithm(target
,
911 num_mem_params
, mem_params
,
912 num_reg_params
, reg_params
,
913 entry_point
, exit_point
, arch_info
);
920 * Waits for an algorithm started with target_start_algorithm() to complete.
922 * @param target used to run the algorithm
923 * @param num_mem_params
925 * @param num_reg_params
929 * @param arch_info target-specific description of the algorithm.
931 int target_wait_algorithm(struct target
*target
,
932 int num_mem_params
, struct mem_param
*mem_params
,
933 int num_reg_params
, struct reg_param
*reg_params
,
934 target_addr_t exit_point
, int timeout_ms
,
937 int retval
= ERROR_FAIL
;
939 if (!target
->type
->wait_algorithm
) {
940 LOG_ERROR("Target type '%s' does not support %s",
941 target_type_name(target
), __func__
);
944 if (!target
->running_alg
) {
945 LOG_ERROR("Target is not running an algorithm");
949 retval
= target
->type
->wait_algorithm(target
,
950 num_mem_params
, mem_params
,
951 num_reg_params
, reg_params
,
952 exit_point
, timeout_ms
, arch_info
);
953 if (retval
!= ERROR_TARGET_TIMEOUT
)
954 target
->running_alg
= false;
961 * Streams data to a circular buffer on target intended for consumption by code
962 * running asynchronously on target.
964 * This is intended for applications where target-specific native code runs
965 * on the target, receives data from the circular buffer, does something with
966 * it (most likely writing it to a flash memory), and advances the circular
969 * This assumes that the helper algorithm has already been loaded to the target,
970 * but has not been started yet. Given memory and register parameters are passed
973 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
976 * [buffer_start + 0, buffer_start + 4):
977 * Write Pointer address (aka head). Written and updated by this
978 * routine when new data is written to the circular buffer.
979 * [buffer_start + 4, buffer_start + 8):
980 * Read Pointer address (aka tail). Updated by code running on the
981 * target after it consumes data.
982 * [buffer_start + 8, buffer_start + buffer_size):
983 * Circular buffer contents.
985 * See contrib/loaders/flash/stm32f1x.S for an example.
987 * @param target used to run the algorithm
988 * @param buffer address on the host where data to be sent is located
989 * @param count number of blocks to send
990 * @param block_size size in bytes of each block
991 * @param num_mem_params count of memory-based params to pass to algorithm
992 * @param mem_params memory-based params to pass to algorithm
993 * @param num_reg_params count of register-based params to pass to algorithm
994 * @param reg_params memory-based params to pass to algorithm
995 * @param buffer_start address on the target of the circular buffer structure
996 * @param buffer_size size of the circular buffer structure
997 * @param entry_point address on the target to execute to start the algorithm
998 * @param exit_point address at which to set a breakpoint to catch the
999 * end of the algorithm; can be 0 if target triggers a breakpoint itself
1003 int target_run_flash_async_algorithm(struct target
*target
,
1004 const uint8_t *buffer
, uint32_t count
, int block_size
,
1005 int num_mem_params
, struct mem_param
*mem_params
,
1006 int num_reg_params
, struct reg_param
*reg_params
,
1007 uint32_t buffer_start
, uint32_t buffer_size
,
1008 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1013 const uint8_t *buffer_orig
= buffer
;
1015 /* Set up working area. First word is write pointer, second word is read pointer,
1016 * rest is fifo data area. */
1017 uint32_t wp_addr
= buffer_start
;
1018 uint32_t rp_addr
= buffer_start
+ 4;
1019 uint32_t fifo_start_addr
= buffer_start
+ 8;
1020 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1022 uint32_t wp
= fifo_start_addr
;
1023 uint32_t rp
= fifo_start_addr
;
1025 /* validate block_size is 2^n */
1026 assert(IS_PWR_OF_2(block_size
));
1028 retval
= target_write_u32(target
, wp_addr
, wp
);
1029 if (retval
!= ERROR_OK
)
1031 retval
= target_write_u32(target
, rp_addr
, rp
);
1032 if (retval
!= ERROR_OK
)
1035 /* Start up algorithm on target and let it idle while writing the first chunk */
1036 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1037 num_reg_params
, reg_params
,
1042 if (retval
!= ERROR_OK
) {
1043 LOG_ERROR("error starting target flash write algorithm");
1049 retval
= target_read_u32(target
, rp_addr
, &rp
);
1050 if (retval
!= ERROR_OK
) {
1051 LOG_ERROR("failed to get read pointer");
1055 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1056 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
1059 LOG_ERROR("flash write algorithm aborted by target");
1060 retval
= ERROR_FLASH_OPERATION_FAILED
;
1064 if (!IS_ALIGNED(rp
- fifo_start_addr
, block_size
) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
1065 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
1069 /* Count the number of bytes available in the fifo without
1070 * crossing the wrap around. Make sure to not fill it completely,
1071 * because that would make wp == rp and that's the empty condition. */
1072 uint32_t thisrun_bytes
;
1074 thisrun_bytes
= rp
- wp
- block_size
;
1075 else if (rp
> fifo_start_addr
)
1076 thisrun_bytes
= fifo_end_addr
- wp
;
1078 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1080 if (thisrun_bytes
== 0) {
1081 /* Throttle polling a bit if transfer is (much) faster than flash
1082 * programming. The exact delay shouldn't matter as long as it's
1083 * less than buffer size / flash speed. This is very unlikely to
1084 * run when using high latency connections such as USB. */
1087 /* to stop an infinite loop on some targets check and increment a timeout
1088 * this issue was observed on a stellaris using the new ICDI interface */
1089 if (timeout
++ >= 2500) {
1090 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1091 return ERROR_FLASH_OPERATION_FAILED
;
1096 /* reset our timeout */
1099 /* Limit to the amount of data we actually want to write */
1100 if (thisrun_bytes
> count
* block_size
)
1101 thisrun_bytes
= count
* block_size
;
1103 /* Force end of large blocks to be word aligned */
1104 if (thisrun_bytes
>= 16)
1105 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1107 /* Write data to fifo */
1108 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1109 if (retval
!= ERROR_OK
)
1112 /* Update counters and wrap write pointer */
1113 buffer
+= thisrun_bytes
;
1114 count
-= thisrun_bytes
/ block_size
;
1115 wp
+= thisrun_bytes
;
1116 if (wp
>= fifo_end_addr
)
1117 wp
= fifo_start_addr
;
1119 /* Store updated write pointer to target */
1120 retval
= target_write_u32(target
, wp_addr
, wp
);
1121 if (retval
!= ERROR_OK
)
1124 /* Avoid GDB timeouts */
1128 if (retval
!= ERROR_OK
) {
1129 /* abort flash write algorithm on target */
1130 target_write_u32(target
, wp_addr
, 0);
1133 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1134 num_reg_params
, reg_params
,
1139 if (retval2
!= ERROR_OK
) {
1140 LOG_ERROR("error waiting for target flash write algorithm");
1144 if (retval
== ERROR_OK
) {
1145 /* check if algorithm set rp = 0 after fifo writer loop finished */
1146 retval
= target_read_u32(target
, rp_addr
, &rp
);
1147 if (retval
== ERROR_OK
&& rp
== 0) {
1148 LOG_ERROR("flash write algorithm aborted by target");
1149 retval
= ERROR_FLASH_OPERATION_FAILED
;
1156 int target_run_read_async_algorithm(struct target
*target
,
1157 uint8_t *buffer
, uint32_t count
, int block_size
,
1158 int num_mem_params
, struct mem_param
*mem_params
,
1159 int num_reg_params
, struct reg_param
*reg_params
,
1160 uint32_t buffer_start
, uint32_t buffer_size
,
1161 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
1166 const uint8_t *buffer_orig
= buffer
;
1168 /* Set up working area. First word is write pointer, second word is read pointer,
1169 * rest is fifo data area. */
1170 uint32_t wp_addr
= buffer_start
;
1171 uint32_t rp_addr
= buffer_start
+ 4;
1172 uint32_t fifo_start_addr
= buffer_start
+ 8;
1173 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
1175 uint32_t wp
= fifo_start_addr
;
1176 uint32_t rp
= fifo_start_addr
;
1178 /* validate block_size is 2^n */
1179 assert(IS_PWR_OF_2(block_size
));
1181 retval
= target_write_u32(target
, wp_addr
, wp
);
1182 if (retval
!= ERROR_OK
)
1184 retval
= target_write_u32(target
, rp_addr
, rp
);
1185 if (retval
!= ERROR_OK
)
1188 /* Start up algorithm on target */
1189 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
1190 num_reg_params
, reg_params
,
1195 if (retval
!= ERROR_OK
) {
1196 LOG_ERROR("error starting target flash read algorithm");
1201 retval
= target_read_u32(target
, wp_addr
, &wp
);
1202 if (retval
!= ERROR_OK
) {
1203 LOG_ERROR("failed to get write pointer");
1207 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
1208 (size_t)(buffer
- buffer_orig
), count
, wp
, rp
);
1211 LOG_ERROR("flash read algorithm aborted by target");
1212 retval
= ERROR_FLASH_OPERATION_FAILED
;
1216 if (!IS_ALIGNED(wp
- fifo_start_addr
, block_size
) || wp
< fifo_start_addr
|| wp
>= fifo_end_addr
) {
1217 LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32
, wp
);
1221 /* Count the number of bytes available in the fifo without
1222 * crossing the wrap around. */
1223 uint32_t thisrun_bytes
;
1225 thisrun_bytes
= wp
- rp
;
1227 thisrun_bytes
= fifo_end_addr
- rp
;
1229 if (thisrun_bytes
== 0) {
1230 /* Throttle polling a bit if transfer is (much) faster than flash
1231 * reading. The exact delay shouldn't matter as long as it's
1232 * less than buffer size / flash speed. This is very unlikely to
1233 * run when using high latency connections such as USB. */
1236 /* to stop an infinite loop on some targets check and increment a timeout
1237 * this issue was observed on a stellaris using the new ICDI interface */
1238 if (timeout
++ >= 2500) {
1239 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1240 return ERROR_FLASH_OPERATION_FAILED
;
1245 /* Reset our timeout */
1248 /* Limit to the amount of data we actually want to read */
1249 if (thisrun_bytes
> count
* block_size
)
1250 thisrun_bytes
= count
* block_size
;
1252 /* Force end of large blocks to be word aligned */
1253 if (thisrun_bytes
>= 16)
1254 thisrun_bytes
-= (rp
+ thisrun_bytes
) & 0x03;
1256 /* Read data from fifo */
1257 retval
= target_read_buffer(target
, rp
, thisrun_bytes
, buffer
);
1258 if (retval
!= ERROR_OK
)
1261 /* Update counters and wrap write pointer */
1262 buffer
+= thisrun_bytes
;
1263 count
-= thisrun_bytes
/ block_size
;
1264 rp
+= thisrun_bytes
;
1265 if (rp
>= fifo_end_addr
)
1266 rp
= fifo_start_addr
;
1268 /* Store updated write pointer to target */
1269 retval
= target_write_u32(target
, rp_addr
, rp
);
1270 if (retval
!= ERROR_OK
)
1273 /* Avoid GDB timeouts */
1278 if (retval
!= ERROR_OK
) {
1279 /* abort flash write algorithm on target */
1280 target_write_u32(target
, rp_addr
, 0);
1283 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1284 num_reg_params
, reg_params
,
1289 if (retval2
!= ERROR_OK
) {
1290 LOG_ERROR("error waiting for target flash write algorithm");
1294 if (retval
== ERROR_OK
) {
1295 /* check if algorithm set wp = 0 after fifo writer loop finished */
1296 retval
= target_read_u32(target
, wp_addr
, &wp
);
1297 if (retval
== ERROR_OK
&& wp
== 0) {
1298 LOG_ERROR("flash read algorithm aborted by target");
1299 retval
= ERROR_FLASH_OPERATION_FAILED
;
1306 int target_read_memory(struct target
*target
,
1307 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1309 if (!target_was_examined(target
)) {
1310 LOG_ERROR("Target not examined yet");
1313 if (!target
->type
->read_memory
) {
1314 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1317 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1320 int target_read_phys_memory(struct target
*target
,
1321 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1323 if (!target_was_examined(target
)) {
1324 LOG_ERROR("Target not examined yet");
1327 if (!target
->type
->read_phys_memory
) {
1328 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1331 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1334 int target_write_memory(struct target
*target
,
1335 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1337 if (!target_was_examined(target
)) {
1338 LOG_ERROR("Target not examined yet");
1341 if (!target
->type
->write_memory
) {
1342 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1345 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1348 int target_write_phys_memory(struct target
*target
,
1349 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1351 if (!target_was_examined(target
)) {
1352 LOG_ERROR("Target not examined yet");
1355 if (!target
->type
->write_phys_memory
) {
1356 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1359 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1362 int target_add_breakpoint(struct target
*target
,
1363 struct breakpoint
*breakpoint
)
1365 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1366 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1367 return ERROR_TARGET_NOT_HALTED
;
1369 return target
->type
->add_breakpoint(target
, breakpoint
);
1372 int target_add_context_breakpoint(struct target
*target
,
1373 struct breakpoint
*breakpoint
)
1375 if (target
->state
!= TARGET_HALTED
) {
1376 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1377 return ERROR_TARGET_NOT_HALTED
;
1379 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1382 int target_add_hybrid_breakpoint(struct target
*target
,
1383 struct breakpoint
*breakpoint
)
1385 if (target
->state
!= TARGET_HALTED
) {
1386 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1387 return ERROR_TARGET_NOT_HALTED
;
1389 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1392 int target_remove_breakpoint(struct target
*target
,
1393 struct breakpoint
*breakpoint
)
1395 return target
->type
->remove_breakpoint(target
, breakpoint
);
1398 int target_add_watchpoint(struct target
*target
,
1399 struct watchpoint
*watchpoint
)
1401 if (target
->state
!= TARGET_HALTED
) {
1402 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1403 return ERROR_TARGET_NOT_HALTED
;
1405 return target
->type
->add_watchpoint(target
, watchpoint
);
1407 int target_remove_watchpoint(struct target
*target
,
1408 struct watchpoint
*watchpoint
)
1410 return target
->type
->remove_watchpoint(target
, watchpoint
);
1412 int target_hit_watchpoint(struct target
*target
,
1413 struct watchpoint
**hit_watchpoint
)
1415 if (target
->state
!= TARGET_HALTED
) {
1416 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1417 return ERROR_TARGET_NOT_HALTED
;
1420 if (!target
->type
->hit_watchpoint
) {
1421 /* For backward compatible, if hit_watchpoint is not implemented,
1422 * return ERROR_FAIL such that gdb_server will not take the nonsense
1427 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1430 const char *target_get_gdb_arch(struct target
*target
)
1432 if (!target
->type
->get_gdb_arch
)
1434 return target
->type
->get_gdb_arch(target
);
1437 int target_get_gdb_reg_list(struct target
*target
,
1438 struct reg
**reg_list
[], int *reg_list_size
,
1439 enum target_register_class reg_class
)
1441 int result
= ERROR_FAIL
;
1443 if (!target_was_examined(target
)) {
1444 LOG_ERROR("Target not examined yet");
1448 result
= target
->type
->get_gdb_reg_list(target
, reg_list
,
1449 reg_list_size
, reg_class
);
1452 if (result
!= ERROR_OK
) {
1459 int target_get_gdb_reg_list_noread(struct target
*target
,
1460 struct reg
**reg_list
[], int *reg_list_size
,
1461 enum target_register_class reg_class
)
1463 if (target
->type
->get_gdb_reg_list_noread
&&
1464 target
->type
->get_gdb_reg_list_noread(target
, reg_list
,
1465 reg_list_size
, reg_class
) == ERROR_OK
)
1467 return target_get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1470 bool target_supports_gdb_connection(struct target
*target
)
1473 * exclude all the targets that don't provide get_gdb_reg_list
1474 * or that have explicit gdb_max_connection == 0
1476 return !!target
->type
->get_gdb_reg_list
&& !!target
->gdb_max_connections
;
1479 int target_step(struct target
*target
,
1480 int current
, target_addr_t address
, int handle_breakpoints
)
1484 target_call_event_callbacks(target
, TARGET_EVENT_STEP_START
);
1486 retval
= target
->type
->step(target
, current
, address
, handle_breakpoints
);
1487 if (retval
!= ERROR_OK
)
1490 target_call_event_callbacks(target
, TARGET_EVENT_STEP_END
);
1495 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1497 if (target
->state
!= TARGET_HALTED
) {
1498 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1499 return ERROR_TARGET_NOT_HALTED
;
1501 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1504 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1506 if (target
->state
!= TARGET_HALTED
) {
1507 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1508 return ERROR_TARGET_NOT_HALTED
;
1510 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1513 target_addr_t
target_address_max(struct target
*target
)
1515 unsigned bits
= target_address_bits(target
);
1516 if (sizeof(target_addr_t
) * 8 == bits
)
1517 return (target_addr_t
) -1;
1519 return (((target_addr_t
) 1) << bits
) - 1;
1522 unsigned target_address_bits(struct target
*target
)
1524 if (target
->type
->address_bits
)
1525 return target
->type
->address_bits(target
);
1529 unsigned int target_data_bits(struct target
*target
)
1531 if (target
->type
->data_bits
)
1532 return target
->type
->data_bits(target
);
1536 static int target_profiling(struct target
*target
, uint32_t *samples
,
1537 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1539 return target
->type
->profiling(target
, samples
, max_num_samples
,
1540 num_samples
, seconds
);
1543 static int handle_target(void *priv
);
1545 static int target_init_one(struct command_context
*cmd_ctx
,
1546 struct target
*target
)
1548 target_reset_examined(target
);
1550 struct target_type
*type
= target
->type
;
1552 type
->examine
= default_examine
;
1554 if (!type
->check_reset
)
1555 type
->check_reset
= default_check_reset
;
1557 assert(type
->init_target
);
1559 int retval
= type
->init_target(cmd_ctx
, target
);
1560 if (retval
!= ERROR_OK
) {
1561 LOG_ERROR("target '%s' init failed", target_name(target
));
1565 /* Sanity-check MMU support ... stub in what we must, to help
1566 * implement it in stages, but warn if we need to do so.
1569 if (!type
->virt2phys
) {
1570 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1571 type
->virt2phys
= identity_virt2phys
;
1574 /* Make sure no-MMU targets all behave the same: make no
1575 * distinction between physical and virtual addresses, and
1576 * ensure that virt2phys() is always an identity mapping.
1578 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1579 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1582 type
->write_phys_memory
= type
->write_memory
;
1583 type
->read_phys_memory
= type
->read_memory
;
1584 type
->virt2phys
= identity_virt2phys
;
1587 if (!target
->type
->read_buffer
)
1588 target
->type
->read_buffer
= target_read_buffer_default
;
1590 if (!target
->type
->write_buffer
)
1591 target
->type
->write_buffer
= target_write_buffer_default
;
1593 if (!target
->type
->get_gdb_fileio_info
)
1594 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1596 if (!target
->type
->gdb_fileio_end
)
1597 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1599 if (!target
->type
->profiling
)
1600 target
->type
->profiling
= target_profiling_default
;
1605 static int target_init(struct command_context
*cmd_ctx
)
1607 struct target
*target
;
1610 for (target
= all_targets
; target
; target
= target
->next
) {
1611 retval
= target_init_one(cmd_ctx
, target
);
1612 if (retval
!= ERROR_OK
)
1619 retval
= target_register_user_commands(cmd_ctx
);
1620 if (retval
!= ERROR_OK
)
1623 retval
= target_register_timer_callback(&handle_target
,
1624 polling_interval
, TARGET_TIMER_TYPE_PERIODIC
, cmd_ctx
->interp
);
1625 if (retval
!= ERROR_OK
)
1631 COMMAND_HANDLER(handle_target_init_command
)
1636 return ERROR_COMMAND_SYNTAX_ERROR
;
1638 static bool target_initialized
;
1639 if (target_initialized
) {
1640 LOG_INFO("'target init' has already been called");
1643 target_initialized
= true;
1645 retval
= command_run_line(CMD_CTX
, "init_targets");
1646 if (retval
!= ERROR_OK
)
1649 retval
= command_run_line(CMD_CTX
, "init_target_events");
1650 if (retval
!= ERROR_OK
)
1653 retval
= command_run_line(CMD_CTX
, "init_board");
1654 if (retval
!= ERROR_OK
)
1657 LOG_DEBUG("Initializing targets...");
1658 return target_init(CMD_CTX
);
1661 int target_register_event_callback(int (*callback
)(struct target
*target
,
1662 enum target_event event
, void *priv
), void *priv
)
1664 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1667 return ERROR_COMMAND_SYNTAX_ERROR
;
1670 while ((*callbacks_p
)->next
)
1671 callbacks_p
= &((*callbacks_p
)->next
);
1672 callbacks_p
= &((*callbacks_p
)->next
);
1675 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1676 (*callbacks_p
)->callback
= callback
;
1677 (*callbacks_p
)->priv
= priv
;
1678 (*callbacks_p
)->next
= NULL
;
1683 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1684 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1686 struct target_reset_callback
*entry
;
1689 return ERROR_COMMAND_SYNTAX_ERROR
;
1691 entry
= malloc(sizeof(struct target_reset_callback
));
1693 LOG_ERROR("error allocating buffer for reset callback entry");
1694 return ERROR_COMMAND_SYNTAX_ERROR
;
1697 entry
->callback
= callback
;
1699 list_add(&entry
->list
, &target_reset_callback_list
);
1705 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1706 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1708 struct target_trace_callback
*entry
;
1711 return ERROR_COMMAND_SYNTAX_ERROR
;
1713 entry
= malloc(sizeof(struct target_trace_callback
));
1715 LOG_ERROR("error allocating buffer for trace callback entry");
1716 return ERROR_COMMAND_SYNTAX_ERROR
;
1719 entry
->callback
= callback
;
1721 list_add(&entry
->list
, &target_trace_callback_list
);
1727 int target_register_timer_callback(int (*callback
)(void *priv
),
1728 unsigned int time_ms
, enum target_timer_type type
, void *priv
)
1730 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1733 return ERROR_COMMAND_SYNTAX_ERROR
;
1736 while ((*callbacks_p
)->next
)
1737 callbacks_p
= &((*callbacks_p
)->next
);
1738 callbacks_p
= &((*callbacks_p
)->next
);
1741 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1742 (*callbacks_p
)->callback
= callback
;
1743 (*callbacks_p
)->type
= type
;
1744 (*callbacks_p
)->time_ms
= time_ms
;
1745 (*callbacks_p
)->removed
= false;
1747 (*callbacks_p
)->when
= timeval_ms() + time_ms
;
1748 target_timer_next_event_value
= MIN(target_timer_next_event_value
, (*callbacks_p
)->when
);
1750 (*callbacks_p
)->priv
= priv
;
1751 (*callbacks_p
)->next
= NULL
;
1756 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1757 enum target_event event
, void *priv
), void *priv
)
1759 struct target_event_callback
**p
= &target_event_callbacks
;
1760 struct target_event_callback
*c
= target_event_callbacks
;
1763 return ERROR_COMMAND_SYNTAX_ERROR
;
1766 struct target_event_callback
*next
= c
->next
;
1767 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1779 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1780 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1782 struct target_reset_callback
*entry
;
1785 return ERROR_COMMAND_SYNTAX_ERROR
;
1787 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1788 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1789 list_del(&entry
->list
);
1798 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1799 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1801 struct target_trace_callback
*entry
;
1804 return ERROR_COMMAND_SYNTAX_ERROR
;
1806 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1807 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1808 list_del(&entry
->list
);
1817 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1820 return ERROR_COMMAND_SYNTAX_ERROR
;
1822 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1824 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1833 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1835 struct target_event_callback
*callback
= target_event_callbacks
;
1836 struct target_event_callback
*next_callback
;
1838 if (event
== TARGET_EVENT_HALTED
) {
1839 /* execute early halted first */
1840 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1843 LOG_DEBUG("target event %i (%s) for core %s", event
,
1844 target_event_name(event
),
1845 target_name(target
));
1847 target_handle_event(target
, event
);
1850 next_callback
= callback
->next
;
1851 callback
->callback(target
, event
, callback
->priv
);
1852 callback
= next_callback
;
1858 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1860 struct target_reset_callback
*callback
;
1862 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1863 jim_nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1865 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1866 callback
->callback(target
, reset_mode
, callback
->priv
);
1871 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1873 struct target_trace_callback
*callback
;
1875 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1876 callback
->callback(target
, len
, data
, callback
->priv
);
1881 static int target_timer_callback_periodic_restart(
1882 struct target_timer_callback
*cb
, int64_t *now
)
1884 cb
->when
= *now
+ cb
->time_ms
;
1888 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1891 cb
->callback(cb
->priv
);
1893 if (cb
->type
== TARGET_TIMER_TYPE_PERIODIC
)
1894 return target_timer_callback_periodic_restart(cb
, now
);
1896 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1899 static int target_call_timer_callbacks_check_time(int checktime
)
1901 static bool callback_processing
;
1903 /* Do not allow nesting */
1904 if (callback_processing
)
1907 callback_processing
= true;
1911 int64_t now
= timeval_ms();
1913 /* Initialize to a default value that's a ways into the future.
1914 * The loop below will make it closer to now if there are
1915 * callbacks that want to be called sooner. */
1916 target_timer_next_event_value
= now
+ 1000;
1918 /* Store an address of the place containing a pointer to the
1919 * next item; initially, that's a standalone "root of the
1920 * list" variable. */
1921 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1922 while (callback
&& *callback
) {
1923 if ((*callback
)->removed
) {
1924 struct target_timer_callback
*p
= *callback
;
1925 *callback
= (*callback
)->next
;
1930 bool call_it
= (*callback
)->callback
&&
1931 ((!checktime
&& (*callback
)->type
== TARGET_TIMER_TYPE_PERIODIC
) ||
1932 now
>= (*callback
)->when
);
1935 target_call_timer_callback(*callback
, &now
);
1937 if (!(*callback
)->removed
&& (*callback
)->when
< target_timer_next_event_value
)
1938 target_timer_next_event_value
= (*callback
)->when
;
1940 callback
= &(*callback
)->next
;
1943 callback_processing
= false;
1947 int target_call_timer_callbacks()
1949 return target_call_timer_callbacks_check_time(1);
1952 /* invoke periodic callbacks immediately */
1953 int target_call_timer_callbacks_now()
1955 return target_call_timer_callbacks_check_time(0);
1958 int64_t target_timer_next_event(void)
1960 return target_timer_next_event_value
;
1963 /* Prints the working area layout for debug purposes */
1964 static void print_wa_layout(struct target
*target
)
1966 struct working_area
*c
= target
->working_areas
;
1969 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1970 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1971 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1976 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1977 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1979 assert(area
->free
); /* Shouldn't split an allocated area */
1980 assert(size
<= area
->size
); /* Caller should guarantee this */
1982 /* Split only if not already the right size */
1983 if (size
< area
->size
) {
1984 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1989 new_wa
->next
= area
->next
;
1990 new_wa
->size
= area
->size
- size
;
1991 new_wa
->address
= area
->address
+ size
;
1992 new_wa
->backup
= NULL
;
1993 new_wa
->user
= NULL
;
1994 new_wa
->free
= true;
1996 area
->next
= new_wa
;
1999 /* If backup memory was allocated to this area, it has the wrong size
2000 * now so free it and it will be reallocated if/when needed */
2002 area
->backup
= NULL
;
2006 /* Merge all adjacent free areas into one */
2007 static void target_merge_working_areas(struct target
*target
)
2009 struct working_area
*c
= target
->working_areas
;
2011 while (c
&& c
->next
) {
2012 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
2014 /* Find two adjacent free areas */
2015 if (c
->free
&& c
->next
->free
) {
2016 /* Merge the last into the first */
2017 c
->size
+= c
->next
->size
;
2019 /* Remove the last */
2020 struct working_area
*to_be_freed
= c
->next
;
2021 c
->next
= c
->next
->next
;
2022 free(to_be_freed
->backup
);
2025 /* If backup memory was allocated to the remaining area, it's has
2026 * the wrong size now */
2035 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
2037 /* Reevaluate working area address based on MMU state*/
2038 if (!target
->working_areas
) {
2042 retval
= target
->type
->mmu(target
, &enabled
);
2043 if (retval
!= ERROR_OK
)
2047 if (target
->working_area_phys_spec
) {
2048 LOG_DEBUG("MMU disabled, using physical "
2049 "address for working memory " TARGET_ADDR_FMT
,
2050 target
->working_area_phys
);
2051 target
->working_area
= target
->working_area_phys
;
2053 LOG_ERROR("No working memory available. "
2054 "Specify -work-area-phys to target.");
2055 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2058 if (target
->working_area_virt_spec
) {
2059 LOG_DEBUG("MMU enabled, using virtual "
2060 "address for working memory " TARGET_ADDR_FMT
,
2061 target
->working_area_virt
);
2062 target
->working_area
= target
->working_area_virt
;
2064 LOG_ERROR("No working memory available. "
2065 "Specify -work-area-virt to target.");
2066 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2070 /* Set up initial working area on first call */
2071 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
2073 new_wa
->next
= NULL
;
2074 new_wa
->size
= ALIGN_DOWN(target
->working_area_size
, 4); /* 4-byte align */
2075 new_wa
->address
= target
->working_area
;
2076 new_wa
->backup
= NULL
;
2077 new_wa
->user
= NULL
;
2078 new_wa
->free
= true;
2081 target
->working_areas
= new_wa
;
2084 /* only allocate multiples of 4 byte */
2085 size
= ALIGN_UP(size
, 4);
2087 struct working_area
*c
= target
->working_areas
;
2089 /* Find the first large enough working area */
2091 if (c
->free
&& c
->size
>= size
)
2097 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2099 /* Split the working area into the requested size */
2100 target_split_working_area(c
, size
);
2102 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
2105 if (target
->backup_working_area
) {
2107 c
->backup
= malloc(c
->size
);
2112 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
2113 if (retval
!= ERROR_OK
)
2117 /* mark as used, and return the new (reused) area */
2124 print_wa_layout(target
);
2129 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
2133 retval
= target_alloc_working_area_try(target
, size
, area
);
2134 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
2135 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
2140 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
2142 int retval
= ERROR_OK
;
2144 if (target
->backup_working_area
&& area
->backup
) {
2145 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
2146 if (retval
!= ERROR_OK
)
2147 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2148 area
->size
, area
->address
);
2154 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2155 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
2157 if (!area
|| area
->free
)
2160 int retval
= ERROR_OK
;
2162 retval
= target_restore_working_area(target
, area
);
2163 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
2164 if (retval
!= ERROR_OK
)
2170 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
2171 area
->size
, area
->address
);
2173 /* mark user pointer invalid */
2174 /* TODO: Is this really safe? It points to some previous caller's memory.
2175 * How could we know that the area pointer is still in that place and not
2176 * some other vital data? What's the purpose of this, anyway? */
2180 target_merge_working_areas(target
);
2182 print_wa_layout(target
);
2187 int target_free_working_area(struct target
*target
, struct working_area
*area
)
2189 return target_free_working_area_restore(target
, area
, 1);
2192 /* free resources and restore memory, if restoring memory fails,
2193 * free up resources anyway
2195 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
2197 struct working_area
*c
= target
->working_areas
;
2199 LOG_DEBUG("freeing all working areas");
2201 /* Loop through all areas, restoring the allocated ones and marking them as free */
2205 target_restore_working_area(target
, c
);
2207 *c
->user
= NULL
; /* Same as above */
2213 /* Run a merge pass to combine all areas into one */
2214 target_merge_working_areas(target
);
2216 print_wa_layout(target
);
2219 void target_free_all_working_areas(struct target
*target
)
2221 target_free_all_working_areas_restore(target
, 1);
2223 /* Now we have none or only one working area marked as free */
2224 if (target
->working_areas
) {
2225 /* Free the last one to allow on-the-fly moving and resizing */
2226 free(target
->working_areas
->backup
);
2227 free(target
->working_areas
);
2228 target
->working_areas
= NULL
;
2232 /* Find the largest number of bytes that can be allocated */
2233 uint32_t target_get_working_area_avail(struct target
*target
)
2235 struct working_area
*c
= target
->working_areas
;
2236 uint32_t max_size
= 0;
2239 return ALIGN_DOWN(target
->working_area_size
, 4);
2242 if (c
->free
&& max_size
< c
->size
)
2251 static void target_destroy(struct target
*target
)
2253 if (target
->type
->deinit_target
)
2254 target
->type
->deinit_target(target
);
2256 if (target
->semihosting
)
2257 free(target
->semihosting
->basedir
);
2258 free(target
->semihosting
);
2260 jtag_unregister_event_callback(jtag_enable_callback
, target
);
2262 struct target_event_action
*teap
= target
->event_action
;
2264 struct target_event_action
*next
= teap
->next
;
2265 Jim_DecrRefCount(teap
->interp
, teap
->body
);
2270 target_free_all_working_areas(target
);
2272 /* release the targets SMP list */
2274 struct target_list
*head
, *tmp
;
2276 list_for_each_entry_safe(head
, tmp
, target
->smp_targets
, lh
) {
2277 list_del(&head
->lh
);
2278 head
->target
->smp
= 0;
2281 if (target
->smp_targets
!= &empty_smp_targets
)
2282 free(target
->smp_targets
);
2286 rtos_destroy(target
);
2288 free(target
->gdb_port_override
);
2290 free(target
->trace_info
);
2291 free(target
->fileio_info
);
2292 free(target
->cmd_name
);
2296 void target_quit(void)
2298 struct target_event_callback
*pe
= target_event_callbacks
;
2300 struct target_event_callback
*t
= pe
->next
;
2304 target_event_callbacks
= NULL
;
2306 struct target_timer_callback
*pt
= target_timer_callbacks
;
2308 struct target_timer_callback
*t
= pt
->next
;
2312 target_timer_callbacks
= NULL
;
2314 for (struct target
*target
= all_targets
; target
;) {
2318 target_destroy(target
);
2325 int target_arch_state(struct target
*target
)
2329 LOG_WARNING("No target has been configured");
2333 if (target
->state
!= TARGET_HALTED
)
2336 retval
= target
->type
->arch_state(target
);
2340 static int target_get_gdb_fileio_info_default(struct target
*target
,
2341 struct gdb_fileio_info
*fileio_info
)
2343 /* If target does not support semi-hosting function, target
2344 has no need to provide .get_gdb_fileio_info callback.
2345 It just return ERROR_FAIL and gdb_server will return "Txx"
2346 as target halted every time. */
2350 static int target_gdb_fileio_end_default(struct target
*target
,
2351 int retcode
, int fileio_errno
, bool ctrl_c
)
2356 int target_profiling_default(struct target
*target
, uint32_t *samples
,
2357 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2359 struct timeval timeout
, now
;
2361 gettimeofday(&timeout
, NULL
);
2362 timeval_add_time(&timeout
, seconds
, 0);
2364 LOG_INFO("Starting profiling. Halting and resuming the"
2365 " target as often as we can...");
2367 uint32_t sample_count
= 0;
2368 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2369 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", true);
2371 int retval
= ERROR_OK
;
2373 target_poll(target
);
2374 if (target
->state
== TARGET_HALTED
) {
2375 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2376 samples
[sample_count
++] = t
;
2377 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2378 retval
= target_resume(target
, 1, 0, 0, 0);
2379 target_poll(target
);
2380 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2381 } else if (target
->state
== TARGET_RUNNING
) {
2382 /* We want to quickly sample the PC. */
2383 retval
= target_halt(target
);
2385 LOG_INFO("Target not halted or running");
2390 if (retval
!= ERROR_OK
)
2393 gettimeofday(&now
, NULL
);
2394 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2395 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2400 *num_samples
= sample_count
;
2404 /* Single aligned words are guaranteed to use 16 or 32 bit access
2405 * mode respectively, otherwise data is handled as quickly as
2408 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2410 LOG_DEBUG("writing buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2413 if (!target_was_examined(target
)) {
2414 LOG_ERROR("Target not examined yet");
2421 if ((address
+ size
- 1) < address
) {
2422 /* GDB can request this when e.g. PC is 0xfffffffc */
2423 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2429 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2432 static int target_write_buffer_default(struct target
*target
,
2433 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2436 unsigned int data_bytes
= target_data_bits(target
) / 8;
2438 /* Align up to maximum bytes. The loop condition makes sure the next pass
2439 * will have something to do with the size we leave to it. */
2441 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2443 if (address
& size
) {
2444 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2445 if (retval
!= ERROR_OK
)
2453 /* Write the data with as large access size as possible. */
2454 for (; size
> 0; size
/= 2) {
2455 uint32_t aligned
= count
- count
% size
;
2457 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2458 if (retval
!= ERROR_OK
)
2469 /* Single aligned words are guaranteed to use 16 or 32 bit access
2470 * mode respectively, otherwise data is handled as quickly as
2473 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2475 LOG_DEBUG("reading buffer of %" PRIu32
" byte at " TARGET_ADDR_FMT
,
2478 if (!target_was_examined(target
)) {
2479 LOG_ERROR("Target not examined yet");
2486 if ((address
+ size
- 1) < address
) {
2487 /* GDB can request this when e.g. PC is 0xfffffffc */
2488 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2494 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2497 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2500 unsigned int data_bytes
= target_data_bits(target
) / 8;
2502 /* Align up to maximum bytes. The loop condition makes sure the next pass
2503 * will have something to do with the size we leave to it. */
2505 size
< data_bytes
&& count
>= size
* 2 + (address
& size
);
2507 if (address
& size
) {
2508 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2509 if (retval
!= ERROR_OK
)
2517 /* Read the data with as large access size as possible. */
2518 for (; size
> 0; size
/= 2) {
2519 uint32_t aligned
= count
- count
% size
;
2521 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2522 if (retval
!= ERROR_OK
)
2533 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t *crc
)
2538 uint32_t checksum
= 0;
2539 if (!target_was_examined(target
)) {
2540 LOG_ERROR("Target not examined yet");
2543 if (!target
->type
->checksum_memory
) {
2544 LOG_ERROR("Target %s doesn't support checksum_memory", target_name(target
));
2548 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2549 if (retval
!= ERROR_OK
) {
2550 buffer
= malloc(size
);
2552 LOG_ERROR("error allocating buffer for section (%" PRIu32
" bytes)", size
);
2553 return ERROR_COMMAND_SYNTAX_ERROR
;
2555 retval
= target_read_buffer(target
, address
, size
, buffer
);
2556 if (retval
!= ERROR_OK
) {
2561 /* convert to target endianness */
2562 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2563 uint32_t target_data
;
2564 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2565 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2568 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2577 int target_blank_check_memory(struct target
*target
,
2578 struct target_memory_check_block
*blocks
, int num_blocks
,
2579 uint8_t erased_value
)
2581 if (!target_was_examined(target
)) {
2582 LOG_ERROR("Target not examined yet");
2586 if (!target
->type
->blank_check_memory
)
2587 return ERROR_NOT_IMPLEMENTED
;
2589 return target
->type
->blank_check_memory(target
, blocks
, num_blocks
, erased_value
);
2592 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2594 uint8_t value_buf
[8];
2595 if (!target_was_examined(target
)) {
2596 LOG_ERROR("Target not examined yet");
2600 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2602 if (retval
== ERROR_OK
) {
2603 *value
= target_buffer_get_u64(target
, value_buf
);
2604 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2609 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2616 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2618 uint8_t value_buf
[4];
2619 if (!target_was_examined(target
)) {
2620 LOG_ERROR("Target not examined yet");
2624 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2626 if (retval
== ERROR_OK
) {
2627 *value
= target_buffer_get_u32(target
, value_buf
);
2628 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2633 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2640 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2642 uint8_t value_buf
[2];
2643 if (!target_was_examined(target
)) {
2644 LOG_ERROR("Target not examined yet");
2648 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2650 if (retval
== ERROR_OK
) {
2651 *value
= target_buffer_get_u16(target
, value_buf
);
2652 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2657 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2664 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2666 if (!target_was_examined(target
)) {
2667 LOG_ERROR("Target not examined yet");
2671 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2673 if (retval
== ERROR_OK
) {
2674 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2679 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2686 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2689 uint8_t value_buf
[8];
2690 if (!target_was_examined(target
)) {
2691 LOG_ERROR("Target not examined yet");
2695 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2699 target_buffer_set_u64(target
, value_buf
, value
);
2700 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2701 if (retval
!= ERROR_OK
)
2702 LOG_DEBUG("failed: %i", retval
);
2707 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2710 uint8_t value_buf
[4];
2711 if (!target_was_examined(target
)) {
2712 LOG_ERROR("Target not examined yet");
2716 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2720 target_buffer_set_u32(target
, value_buf
, value
);
2721 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2722 if (retval
!= ERROR_OK
)
2723 LOG_DEBUG("failed: %i", retval
);
2728 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2731 uint8_t value_buf
[2];
2732 if (!target_was_examined(target
)) {
2733 LOG_ERROR("Target not examined yet");
2737 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2741 target_buffer_set_u16(target
, value_buf
, value
);
2742 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2743 if (retval
!= ERROR_OK
)
2744 LOG_DEBUG("failed: %i", retval
);
2749 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2752 if (!target_was_examined(target
)) {
2753 LOG_ERROR("Target not examined yet");
2757 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2760 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2761 if (retval
!= ERROR_OK
)
2762 LOG_DEBUG("failed: %i", retval
);
2767 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2770 uint8_t value_buf
[8];
2771 if (!target_was_examined(target
)) {
2772 LOG_ERROR("Target not examined yet");
2776 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2780 target_buffer_set_u64(target
, value_buf
, value
);
2781 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2782 if (retval
!= ERROR_OK
)
2783 LOG_DEBUG("failed: %i", retval
);
2788 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2791 uint8_t value_buf
[4];
2792 if (!target_was_examined(target
)) {
2793 LOG_ERROR("Target not examined yet");
2797 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2801 target_buffer_set_u32(target
, value_buf
, value
);
2802 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2803 if (retval
!= ERROR_OK
)
2804 LOG_DEBUG("failed: %i", retval
);
2809 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2812 uint8_t value_buf
[2];
2813 if (!target_was_examined(target
)) {
2814 LOG_ERROR("Target not examined yet");
2818 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2822 target_buffer_set_u16(target
, value_buf
, value
);
2823 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2824 if (retval
!= ERROR_OK
)
2825 LOG_DEBUG("failed: %i", retval
);
2830 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2833 if (!target_was_examined(target
)) {
2834 LOG_ERROR("Target not examined yet");
2838 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2841 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2842 if (retval
!= ERROR_OK
)
2843 LOG_DEBUG("failed: %i", retval
);
2848 static int find_target(struct command_invocation
*cmd
, const char *name
)
2850 struct target
*target
= get_target(name
);
2852 command_print(cmd
, "Target: %s is unknown, try one of:\n", name
);
2855 if (!target
->tap
->enabled
) {
2856 command_print(cmd
, "Target: TAP %s is disabled, "
2857 "can't be the current target\n",
2858 target
->tap
->dotted_name
);
2862 cmd
->ctx
->current_target
= target
;
2863 if (cmd
->ctx
->current_target_override
)
2864 cmd
->ctx
->current_target_override
= target
;
2870 COMMAND_HANDLER(handle_targets_command
)
2872 int retval
= ERROR_OK
;
2873 if (CMD_ARGC
== 1) {
2874 retval
= find_target(CMD
, CMD_ARGV
[0]);
2875 if (retval
== ERROR_OK
) {
2881 struct target
*target
= all_targets
;
2882 command_print(CMD
, " TargetName Type Endian TapName State ");
2883 command_print(CMD
, "-- ------------------ ---------- ------ ------------------ ------------");
2888 if (target
->tap
->enabled
)
2889 state
= target_state_name(target
);
2891 state
= "tap-disabled";
2893 if (CMD_CTX
->current_target
== target
)
2896 /* keep columns lined up to match the headers above */
2898 "%2d%c %-18s %-10s %-6s %-18s %s",
2899 target
->target_number
,
2901 target_name(target
),
2902 target_type_name(target
),
2903 jim_nvp_value2name_simple(nvp_target_endian
,
2904 target
->endianness
)->name
,
2905 target
->tap
->dotted_name
,
2907 target
= target
->next
;
2913 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2915 static int power_dropout
;
2916 static int srst_asserted
;
2918 static int run_power_restore
;
2919 static int run_power_dropout
;
2920 static int run_srst_asserted
;
2921 static int run_srst_deasserted
;
2923 static int sense_handler(void)
2925 static int prev_srst_asserted
;
2926 static int prev_power_dropout
;
2928 int retval
= jtag_power_dropout(&power_dropout
);
2929 if (retval
!= ERROR_OK
)
2933 power_restored
= prev_power_dropout
&& !power_dropout
;
2935 run_power_restore
= 1;
2937 int64_t current
= timeval_ms();
2938 static int64_t last_power
;
2939 bool wait_more
= last_power
+ 2000 > current
;
2940 if (power_dropout
&& !wait_more
) {
2941 run_power_dropout
= 1;
2942 last_power
= current
;
2945 retval
= jtag_srst_asserted(&srst_asserted
);
2946 if (retval
!= ERROR_OK
)
2949 int srst_deasserted
;
2950 srst_deasserted
= prev_srst_asserted
&& !srst_asserted
;
2952 static int64_t last_srst
;
2953 wait_more
= last_srst
+ 2000 > current
;
2954 if (srst_deasserted
&& !wait_more
) {
2955 run_srst_deasserted
= 1;
2956 last_srst
= current
;
2959 if (!prev_srst_asserted
&& srst_asserted
)
2960 run_srst_asserted
= 1;
2962 prev_srst_asserted
= srst_asserted
;
2963 prev_power_dropout
= power_dropout
;
2965 if (srst_deasserted
|| power_restored
) {
2966 /* Other than logging the event we can't do anything here.
2967 * Issuing a reset is a particularly bad idea as we might
2968 * be inside a reset already.
2975 /* process target state changes */
2976 static int handle_target(void *priv
)
2978 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2979 int retval
= ERROR_OK
;
2981 if (!is_jtag_poll_safe()) {
2982 /* polling is disabled currently */
2986 /* we do not want to recurse here... */
2987 static int recursive
;
2991 /* danger! running these procedures can trigger srst assertions and power dropouts.
2992 * We need to avoid an infinite loop/recursion here and we do that by
2993 * clearing the flags after running these events.
2995 int did_something
= 0;
2996 if (run_srst_asserted
) {
2997 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2998 Jim_Eval(interp
, "srst_asserted");
3001 if (run_srst_deasserted
) {
3002 Jim_Eval(interp
, "srst_deasserted");
3005 if (run_power_dropout
) {
3006 LOG_INFO("Power dropout detected, running power_dropout proc.");
3007 Jim_Eval(interp
, "power_dropout");
3010 if (run_power_restore
) {
3011 Jim_Eval(interp
, "power_restore");
3015 if (did_something
) {
3016 /* clear detect flags */
3020 /* clear action flags */
3022 run_srst_asserted
= 0;
3023 run_srst_deasserted
= 0;
3024 run_power_restore
= 0;
3025 run_power_dropout
= 0;
3030 /* Poll targets for state changes unless that's globally disabled.
3031 * Skip targets that are currently disabled.
3033 for (struct target
*target
= all_targets
;
3034 is_jtag_poll_safe() && target
;
3035 target
= target
->next
) {
3037 if (!target_was_examined(target
))
3040 if (!target
->tap
->enabled
)
3043 if (target
->backoff
.times
> target
->backoff
.count
) {
3044 /* do not poll this time as we failed previously */
3045 target
->backoff
.count
++;
3048 target
->backoff
.count
= 0;
3050 /* only poll target if we've got power and srst isn't asserted */
3051 if (!power_dropout
&& !srst_asserted
) {
3052 /* polling may fail silently until the target has been examined */
3053 retval
= target_poll(target
);
3054 if (retval
!= ERROR_OK
) {
3055 /* 100ms polling interval. Increase interval between polling up to 5000ms */
3056 if (target
->backoff
.times
* polling_interval
< 5000) {
3057 target
->backoff
.times
*= 2;
3058 target
->backoff
.times
++;
3061 /* Tell GDB to halt the debugger. This allows the user to
3062 * run monitor commands to handle the situation.
3064 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3066 if (target
->backoff
.times
> 0) {
3067 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
3068 target_reset_examined(target
);
3069 retval
= target_examine_one(target
);
3070 /* Target examination could have failed due to unstable connection,
3071 * but we set the examined flag anyway to repoll it later */
3072 if (retval
!= ERROR_OK
) {
3073 target_set_examined(target
);
3074 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
3075 target
->backoff
.times
* polling_interval
);
3080 /* Since we succeeded, we reset backoff count */
3081 target
->backoff
.times
= 0;
3088 COMMAND_HANDLER(handle_reg_command
)
3092 struct target
*target
= get_current_target(CMD_CTX
);
3093 struct reg
*reg
= NULL
;
3095 /* list all available registers for the current target */
3096 if (CMD_ARGC
== 0) {
3097 struct reg_cache
*cache
= target
->reg_cache
;
3099 unsigned int count
= 0;
3103 command_print(CMD
, "===== %s", cache
->name
);
3105 for (i
= 0, reg
= cache
->reg_list
;
3106 i
< cache
->num_regs
;
3107 i
++, reg
++, count
++) {
3108 if (reg
->exist
== false || reg
->hidden
)
3110 /* only print cached values if they are valid */
3112 char *value
= buf_to_hex_str(reg
->value
,
3115 "(%i) %s (/%" PRIu32
"): 0x%s%s",
3123 command_print(CMD
, "(%i) %s (/%" PRIu32
")",
3128 cache
= cache
->next
;
3134 /* access a single register by its ordinal number */
3135 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
3137 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
3139 struct reg_cache
*cache
= target
->reg_cache
;
3140 unsigned int count
= 0;
3143 for (i
= 0; i
< cache
->num_regs
; i
++) {
3144 if (count
++ == num
) {
3145 reg
= &cache
->reg_list
[i
];
3151 cache
= cache
->next
;
3155 command_print(CMD
, "%i is out of bounds, the current target "
3156 "has only %i registers (0 - %i)", num
, count
, count
- 1);
3160 /* access a single register by its name */
3161 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], true);
3167 assert(reg
); /* give clang a hint that we *know* reg is != NULL here */
3172 /* display a register */
3173 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
3174 && (CMD_ARGV
[1][0] <= '9')))) {
3175 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
3178 if (reg
->valid
== 0) {
3179 int retval
= reg
->type
->get(reg
);
3180 if (retval
!= ERROR_OK
) {
3181 LOG_ERROR("Could not read register '%s'", reg
->name
);
3185 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3186 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3191 /* set register value */
3192 if (CMD_ARGC
== 2) {
3193 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
3196 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
3198 int retval
= reg
->type
->set(reg
, buf
);
3199 if (retval
!= ERROR_OK
) {
3200 LOG_ERROR("Could not write to register '%s'", reg
->name
);
3202 char *value
= buf_to_hex_str(reg
->value
, reg
->size
);
3203 command_print(CMD
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
3212 return ERROR_COMMAND_SYNTAX_ERROR
;
3215 command_print(CMD
, "register %s not found in current target", CMD_ARGV
[0]);
3219 COMMAND_HANDLER(handle_poll_command
)
3221 int retval
= ERROR_OK
;
3222 struct target
*target
= get_current_target(CMD_CTX
);
3224 if (CMD_ARGC
== 0) {
3225 command_print(CMD
, "background polling: %s",
3226 jtag_poll_get_enabled() ? "on" : "off");
3227 command_print(CMD
, "TAP: %s (%s)",
3228 target
->tap
->dotted_name
,
3229 target
->tap
->enabled
? "enabled" : "disabled");
3230 if (!target
->tap
->enabled
)
3232 retval
= target_poll(target
);
3233 if (retval
!= ERROR_OK
)
3235 retval
= target_arch_state(target
);
3236 if (retval
!= ERROR_OK
)
3238 } else if (CMD_ARGC
== 1) {
3240 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
3241 jtag_poll_set_enabled(enable
);
3243 return ERROR_COMMAND_SYNTAX_ERROR
;
3248 COMMAND_HANDLER(handle_wait_halt_command
)
3251 return ERROR_COMMAND_SYNTAX_ERROR
;
3253 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
3254 if (1 == CMD_ARGC
) {
3255 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
3256 if (retval
!= ERROR_OK
)
3257 return ERROR_COMMAND_SYNTAX_ERROR
;
3260 struct target
*target
= get_current_target(CMD_CTX
);
3261 return target_wait_state(target
, TARGET_HALTED
, ms
);
3264 /* wait for target state to change. The trick here is to have a low
3265 * latency for short waits and not to suck up all the CPU time
3268 * After 500ms, keep_alive() is invoked
3270 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
3273 int64_t then
= 0, cur
;
3277 retval
= target_poll(target
);
3278 if (retval
!= ERROR_OK
)
3280 if (target
->state
== state
)
3285 then
= timeval_ms();
3286 LOG_DEBUG("waiting for target %s...",
3287 jim_nvp_value2name_simple(nvp_target_state
, state
)->name
);
3293 if ((cur
-then
) > ms
) {
3294 LOG_ERROR("timed out while waiting for target %s",
3295 jim_nvp_value2name_simple(nvp_target_state
, state
)->name
);
3303 COMMAND_HANDLER(handle_halt_command
)
3307 struct target
*target
= get_current_target(CMD_CTX
);
3309 target
->verbose_halt_msg
= true;
3311 int retval
= target_halt(target
);
3312 if (retval
!= ERROR_OK
)
3315 if (CMD_ARGC
== 1) {
3316 unsigned wait_local
;
3317 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
3318 if (retval
!= ERROR_OK
)
3319 return ERROR_COMMAND_SYNTAX_ERROR
;
3324 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
3327 COMMAND_HANDLER(handle_soft_reset_halt_command
)
3329 struct target
*target
= get_current_target(CMD_CTX
);
3331 LOG_TARGET_INFO(target
, "requesting target halt and executing a soft reset");
3333 target_soft_reset_halt(target
);
3338 COMMAND_HANDLER(handle_reset_command
)
3341 return ERROR_COMMAND_SYNTAX_ERROR
;
3343 enum target_reset_mode reset_mode
= RESET_RUN
;
3344 if (CMD_ARGC
== 1) {
3345 const struct jim_nvp
*n
;
3346 n
= jim_nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
3347 if ((!n
->name
) || (n
->value
== RESET_UNKNOWN
))
3348 return ERROR_COMMAND_SYNTAX_ERROR
;
3349 reset_mode
= n
->value
;
3352 /* reset *all* targets */
3353 return target_process_reset(CMD
, reset_mode
);
3357 COMMAND_HANDLER(handle_resume_command
)
3361 return ERROR_COMMAND_SYNTAX_ERROR
;
3363 struct target
*target
= get_current_target(CMD_CTX
);
3365 /* with no CMD_ARGV, resume from current pc, addr = 0,
3366 * with one arguments, addr = CMD_ARGV[0],
3367 * handle breakpoints, not debugging */
3368 target_addr_t addr
= 0;
3369 if (CMD_ARGC
== 1) {
3370 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3374 return target_resume(target
, current
, addr
, 1, 0);
3377 COMMAND_HANDLER(handle_step_command
)
3380 return ERROR_COMMAND_SYNTAX_ERROR
;
3384 /* with no CMD_ARGV, step from current pc, addr = 0,
3385 * with one argument addr = CMD_ARGV[0],
3386 * handle breakpoints, debugging */
3387 target_addr_t addr
= 0;
3389 if (CMD_ARGC
== 1) {
3390 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3394 struct target
*target
= get_current_target(CMD_CTX
);
3396 return target_step(target
, current_pc
, addr
, 1);
3399 void target_handle_md_output(struct command_invocation
*cmd
,
3400 struct target
*target
, target_addr_t address
, unsigned size
,
3401 unsigned count
, const uint8_t *buffer
)
3403 const unsigned line_bytecnt
= 32;
3404 unsigned line_modulo
= line_bytecnt
/ size
;
3406 char output
[line_bytecnt
* 4 + 1];
3407 unsigned output_len
= 0;
3409 const char *value_fmt
;
3412 value_fmt
= "%16.16"PRIx64
" ";
3415 value_fmt
= "%8.8"PRIx64
" ";
3418 value_fmt
= "%4.4"PRIx64
" ";
3421 value_fmt
= "%2.2"PRIx64
" ";
3424 /* "can't happen", caller checked */
3425 LOG_ERROR("invalid memory read size: %u", size
);
3429 for (unsigned i
= 0; i
< count
; i
++) {
3430 if (i
% line_modulo
== 0) {
3431 output_len
+= snprintf(output
+ output_len
,
3432 sizeof(output
) - output_len
,
3433 TARGET_ADDR_FMT
": ",
3434 (address
+ (i
* size
)));
3438 const uint8_t *value_ptr
= buffer
+ i
* size
;
3441 value
= target_buffer_get_u64(target
, value_ptr
);
3444 value
= target_buffer_get_u32(target
, value_ptr
);
3447 value
= target_buffer_get_u16(target
, value_ptr
);
3452 output_len
+= snprintf(output
+ output_len
,
3453 sizeof(output
) - output_len
,
3456 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3457 command_print(cmd
, "%s", output
);
3463 COMMAND_HANDLER(handle_md_command
)
3466 return ERROR_COMMAND_SYNTAX_ERROR
;
3469 switch (CMD_NAME
[2]) {
3483 return ERROR_COMMAND_SYNTAX_ERROR
;
3486 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3487 int (*fn
)(struct target
*target
,
3488 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3492 fn
= target_read_phys_memory
;
3494 fn
= target_read_memory
;
3495 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3496 return ERROR_COMMAND_SYNTAX_ERROR
;
3498 target_addr_t address
;
3499 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3503 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3505 uint8_t *buffer
= calloc(count
, size
);
3507 LOG_ERROR("Failed to allocate md read buffer");
3511 struct target
*target
= get_current_target(CMD_CTX
);
3512 int retval
= fn(target
, address
, size
, count
, buffer
);
3513 if (retval
== ERROR_OK
)
3514 target_handle_md_output(CMD
, target
, address
, size
, count
, buffer
);
3521 typedef int (*target_write_fn
)(struct target
*target
,
3522 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3524 static int target_fill_mem(struct target
*target
,
3525 target_addr_t address
,
3533 /* We have to write in reasonably large chunks to be able
3534 * to fill large memory areas with any sane speed */
3535 const unsigned chunk_size
= 16384;
3536 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3538 LOG_ERROR("Out of memory");
3542 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3543 switch (data_size
) {
3545 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3548 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3551 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3554 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3561 int retval
= ERROR_OK
;
3563 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3566 if (current
> chunk_size
)
3567 current
= chunk_size
;
3568 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3569 if (retval
!= ERROR_OK
)
3571 /* avoid GDB timeouts */
3580 COMMAND_HANDLER(handle_mw_command
)
3583 return ERROR_COMMAND_SYNTAX_ERROR
;
3584 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3589 fn
= target_write_phys_memory
;
3591 fn
= target_write_memory
;
3592 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3593 return ERROR_COMMAND_SYNTAX_ERROR
;
3595 target_addr_t address
;
3596 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3599 COMMAND_PARSE_NUMBER(u64
, CMD_ARGV
[1], value
);
3603 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3605 struct target
*target
= get_current_target(CMD_CTX
);
3607 switch (CMD_NAME
[2]) {
3621 return ERROR_COMMAND_SYNTAX_ERROR
;
3624 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3627 static COMMAND_HELPER(parse_load_image_command
, struct image
*image
,
3628 target_addr_t
*min_address
, target_addr_t
*max_address
)
3630 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3631 return ERROR_COMMAND_SYNTAX_ERROR
;
3633 /* a base address isn't always necessary,
3634 * default to 0x0 (i.e. don't relocate) */
3635 if (CMD_ARGC
>= 2) {
3637 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3638 image
->base_address
= addr
;
3639 image
->base_address_set
= true;
3641 image
->base_address_set
= false;
3643 image
->start_address_set
= false;
3646 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3647 if (CMD_ARGC
== 5) {
3648 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3649 /* use size (given) to find max (required) */
3650 *max_address
+= *min_address
;
3653 if (*min_address
> *max_address
)
3654 return ERROR_COMMAND_SYNTAX_ERROR
;
3659 COMMAND_HANDLER(handle_load_image_command
)
3663 uint32_t image_size
;
3664 target_addr_t min_address
= 0;
3665 target_addr_t max_address
= -1;
3668 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
3669 &image
, &min_address
, &max_address
);
3670 if (retval
!= ERROR_OK
)
3673 struct target
*target
= get_current_target(CMD_CTX
);
3675 struct duration bench
;
3676 duration_start(&bench
);
3678 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3683 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3684 buffer
= malloc(image
.sections
[i
].size
);
3687 "error allocating buffer for section (%d bytes)",
3688 (int)(image
.sections
[i
].size
));
3689 retval
= ERROR_FAIL
;
3693 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3694 if (retval
!= ERROR_OK
) {
3699 uint32_t offset
= 0;
3700 uint32_t length
= buf_cnt
;
3702 /* DANGER!!! beware of unsigned comparison here!!! */
3704 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3705 (image
.sections
[i
].base_address
< max_address
)) {
3707 if (image
.sections
[i
].base_address
< min_address
) {
3708 /* clip addresses below */
3709 offset
+= min_address
-image
.sections
[i
].base_address
;
3713 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3714 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3716 retval
= target_write_buffer(target
,
3717 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3718 if (retval
!= ERROR_OK
) {
3722 image_size
+= length
;
3723 command_print(CMD
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3724 (unsigned int)length
,
3725 image
.sections
[i
].base_address
+ offset
);
3731 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3732 command_print(CMD
, "downloaded %" PRIu32
" bytes "
3733 "in %fs (%0.3f KiB/s)", image_size
,
3734 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3737 image_close(&image
);
3743 COMMAND_HANDLER(handle_dump_image_command
)
3745 struct fileio
*fileio
;
3747 int retval
, retvaltemp
;
3748 target_addr_t address
, size
;
3749 struct duration bench
;
3750 struct target
*target
= get_current_target(CMD_CTX
);
3753 return ERROR_COMMAND_SYNTAX_ERROR
;
3755 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3756 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3758 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3759 buffer
= malloc(buf_size
);
3763 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3764 if (retval
!= ERROR_OK
) {
3769 duration_start(&bench
);
3772 size_t size_written
;
3773 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3774 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3775 if (retval
!= ERROR_OK
)
3778 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3779 if (retval
!= ERROR_OK
)
3782 size
-= this_run_size
;
3783 address
+= this_run_size
;
3788 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3790 retval
= fileio_size(fileio
, &filesize
);
3791 if (retval
!= ERROR_OK
)
3794 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3795 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3798 retvaltemp
= fileio_close(fileio
);
3799 if (retvaltemp
!= ERROR_OK
)
3808 IMAGE_CHECKSUM_ONLY
= 2
3811 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3815 uint32_t image_size
;
3817 uint32_t checksum
= 0;
3818 uint32_t mem_checksum
= 0;
3822 struct target
*target
= get_current_target(CMD_CTX
);
3825 return ERROR_COMMAND_SYNTAX_ERROR
;
3828 LOG_ERROR("no target selected");
3832 struct duration bench
;
3833 duration_start(&bench
);
3835 if (CMD_ARGC
>= 2) {
3837 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3838 image
.base_address
= addr
;
3839 image
.base_address_set
= true;
3841 image
.base_address_set
= false;
3842 image
.base_address
= 0x0;
3845 image
.start_address_set
= false;
3847 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3848 if (retval
!= ERROR_OK
)
3854 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
3855 buffer
= malloc(image
.sections
[i
].size
);
3858 "error allocating buffer for section (%" PRIu32
" bytes)",
3859 image
.sections
[i
].size
);
3862 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3863 if (retval
!= ERROR_OK
) {
3868 if (verify
>= IMAGE_VERIFY
) {
3869 /* calculate checksum of image */
3870 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3871 if (retval
!= ERROR_OK
) {
3876 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3877 if (retval
!= ERROR_OK
) {
3881 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3882 LOG_ERROR("checksum mismatch");
3884 retval
= ERROR_FAIL
;
3887 if (checksum
!= mem_checksum
) {
3888 /* failed crc checksum, fall back to a binary compare */
3892 LOG_ERROR("checksum mismatch - attempting binary compare");
3894 data
= malloc(buf_cnt
);
3896 retval
= target_read_buffer(target
, image
.sections
[i
].base_address
, buf_cnt
, data
);
3897 if (retval
== ERROR_OK
) {
3899 for (t
= 0; t
< buf_cnt
; t
++) {
3900 if (data
[t
] != buffer
[t
]) {
3902 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3904 (unsigned)(t
+ image
.sections
[i
].base_address
),
3907 if (diffs
++ >= 127) {
3908 command_print(CMD
, "More than 128 errors, the rest are not printed.");
3920 command_print(CMD
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3921 image
.sections
[i
].base_address
,
3926 image_size
+= buf_cnt
;
3929 command_print(CMD
, "No more differences found.");
3932 retval
= ERROR_FAIL
;
3933 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
3934 command_print(CMD
, "verified %" PRIu32
" bytes "
3935 "in %fs (%0.3f KiB/s)", image_size
,
3936 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3939 image_close(&image
);
3944 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3946 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3949 COMMAND_HANDLER(handle_verify_image_command
)
3951 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3954 COMMAND_HANDLER(handle_test_image_command
)
3956 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3959 static int handle_bp_command_list(struct command_invocation
*cmd
)
3961 struct target
*target
= get_current_target(cmd
->ctx
);
3962 struct breakpoint
*breakpoint
= target
->breakpoints
;
3963 while (breakpoint
) {
3964 if (breakpoint
->type
== BKPT_SOFT
) {
3965 char *buf
= buf_to_hex_str(breakpoint
->orig_instr
,
3966 breakpoint
->length
);
3967 command_print(cmd
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, 0x%s",
3968 breakpoint
->address
,
3973 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3974 command_print(cmd
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %u",
3976 breakpoint
->length
, breakpoint
->number
);
3977 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3978 command_print(cmd
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %u",
3979 breakpoint
->address
,
3980 breakpoint
->length
, breakpoint
->number
);
3981 command_print(cmd
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3984 command_print(cmd
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %u",
3985 breakpoint
->address
,
3986 breakpoint
->length
, breakpoint
->number
);
3989 breakpoint
= breakpoint
->next
;
3994 static int handle_bp_command_set(struct command_invocation
*cmd
,
3995 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3997 struct target
*target
= get_current_target(cmd
->ctx
);
4001 retval
= breakpoint_add(target
, addr
, length
, hw
);
4002 /* error is always logged in breakpoint_add(), do not print it again */
4003 if (retval
== ERROR_OK
)
4004 command_print(cmd
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
4006 } else if (addr
== 0) {
4007 if (!target
->type
->add_context_breakpoint
) {
4008 LOG_ERROR("Context breakpoint not available");
4009 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
4011 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
4012 /* error is always logged in context_breakpoint_add(), do not print it again */
4013 if (retval
== ERROR_OK
)
4014 command_print(cmd
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
4017 if (!target
->type
->add_hybrid_breakpoint
) {
4018 LOG_ERROR("Hybrid breakpoint not available");
4019 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
4021 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
4022 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
4023 if (retval
== ERROR_OK
)
4024 command_print(cmd
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
4029 COMMAND_HANDLER(handle_bp_command
)
4038 return handle_bp_command_list(CMD
);
4042 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4043 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4044 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4047 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
4049 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4050 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4052 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4053 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
4055 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
4056 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4058 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4063 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4064 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
4065 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
4066 return handle_bp_command_set(CMD
, addr
, asid
, length
, hw
);
4069 return ERROR_COMMAND_SYNTAX_ERROR
;
4073 COMMAND_HANDLER(handle_rbp_command
)
4076 return ERROR_COMMAND_SYNTAX_ERROR
;
4078 struct target
*target
= get_current_target(CMD_CTX
);
4080 if (!strcmp(CMD_ARGV
[0], "all")) {
4081 breakpoint_remove_all(target
);
4084 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4086 breakpoint_remove(target
, addr
);
4092 COMMAND_HANDLER(handle_wp_command
)
4094 struct target
*target
= get_current_target(CMD_CTX
);
4096 if (CMD_ARGC
== 0) {
4097 struct watchpoint
*watchpoint
= target
->watchpoints
;
4099 while (watchpoint
) {
4100 command_print(CMD
, "address: " TARGET_ADDR_FMT
4101 ", len: 0x%8.8" PRIx32
4102 ", r/w/a: %i, value: 0x%8.8" PRIx32
4103 ", mask: 0x%8.8" PRIx32
,
4104 watchpoint
->address
,
4106 (int)watchpoint
->rw
,
4109 watchpoint
= watchpoint
->next
;
4114 enum watchpoint_rw type
= WPT_ACCESS
;
4115 target_addr_t addr
= 0;
4116 uint32_t length
= 0;
4117 uint32_t data_value
= 0x0;
4118 uint32_t data_mask
= 0xffffffff;
4122 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
4125 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
4128 switch (CMD_ARGV
[2][0]) {
4139 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
4140 return ERROR_COMMAND_SYNTAX_ERROR
;
4144 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
4145 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4149 return ERROR_COMMAND_SYNTAX_ERROR
;
4152 int retval
= watchpoint_add(target
, addr
, length
, type
,
4153 data_value
, data_mask
);
4154 if (retval
!= ERROR_OK
)
4155 LOG_ERROR("Failure setting watchpoints");
4160 COMMAND_HANDLER(handle_rwp_command
)
4163 return ERROR_COMMAND_SYNTAX_ERROR
;
4166 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
4168 struct target
*target
= get_current_target(CMD_CTX
);
4169 watchpoint_remove(target
, addr
);
4175 * Translate a virtual address to a physical address.
4177 * The low-level target implementation must have logged a detailed error
4178 * which is forwarded to telnet/GDB session.
4180 COMMAND_HANDLER(handle_virt2phys_command
)
4183 return ERROR_COMMAND_SYNTAX_ERROR
;
4186 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
4189 struct target
*target
= get_current_target(CMD_CTX
);
4190 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
4191 if (retval
== ERROR_OK
)
4192 command_print(CMD
, "Physical address " TARGET_ADDR_FMT
"", pa
);
4197 static void write_data(FILE *f
, const void *data
, size_t len
)
4199 size_t written
= fwrite(data
, 1, len
, f
);
4201 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
4204 static void write_long(FILE *f
, int l
, struct target
*target
)
4208 target_buffer_set_u32(target
, val
, l
);
4209 write_data(f
, val
, 4);
4212 static void write_string(FILE *f
, char *s
)
4214 write_data(f
, s
, strlen(s
));
4217 typedef unsigned char UNIT
[2]; /* unit of profiling */
4219 /* Dump a gmon.out histogram file. */
4220 static void write_gmon(uint32_t *samples
, uint32_t sample_num
, const char *filename
, bool with_range
,
4221 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
4224 FILE *f
= fopen(filename
, "w");
4227 write_string(f
, "gmon");
4228 write_long(f
, 0x00000001, target
); /* Version */
4229 write_long(f
, 0, target
); /* padding */
4230 write_long(f
, 0, target
); /* padding */
4231 write_long(f
, 0, target
); /* padding */
4233 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
4234 write_data(f
, &zero
, 1);
4236 /* figure out bucket size */
4240 min
= start_address
;
4245 for (i
= 0; i
< sample_num
; i
++) {
4246 if (min
> samples
[i
])
4248 if (max
< samples
[i
])
4252 /* max should be (largest sample + 1)
4253 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4257 int address_space
= max
- min
;
4258 assert(address_space
>= 2);
4260 /* FIXME: What is the reasonable number of buckets?
4261 * The profiling result will be more accurate if there are enough buckets. */
4262 static const uint32_t max_buckets
= 128 * 1024; /* maximum buckets. */
4263 uint32_t num_buckets
= address_space
/ sizeof(UNIT
);
4264 if (num_buckets
> max_buckets
)
4265 num_buckets
= max_buckets
;
4266 int *buckets
= malloc(sizeof(int) * num_buckets
);
4271 memset(buckets
, 0, sizeof(int) * num_buckets
);
4272 for (i
= 0; i
< sample_num
; i
++) {
4273 uint32_t address
= samples
[i
];
4275 if ((address
< min
) || (max
<= address
))
4278 long long a
= address
- min
;
4279 long long b
= num_buckets
;
4280 long long c
= address_space
;
4281 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
4285 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4286 write_long(f
, min
, target
); /* low_pc */
4287 write_long(f
, max
, target
); /* high_pc */
4288 write_long(f
, num_buckets
, target
); /* # of buckets */
4289 float sample_rate
= sample_num
/ (duration_ms
/ 1000.0);
4290 write_long(f
, sample_rate
, target
);
4291 write_string(f
, "seconds");
4292 for (i
= 0; i
< (15-strlen("seconds")); i
++)
4293 write_data(f
, &zero
, 1);
4294 write_string(f
, "s");
4296 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4298 char *data
= malloc(2 * num_buckets
);
4300 for (i
= 0; i
< num_buckets
; i
++) {
4305 data
[i
* 2] = val
&0xff;
4306 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
4309 write_data(f
, data
, num_buckets
* 2);
4317 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4318 * which will be used as a random sampling of PC */
4319 COMMAND_HANDLER(handle_profile_command
)
4321 struct target
*target
= get_current_target(CMD_CTX
);
4323 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
4324 return ERROR_COMMAND_SYNTAX_ERROR
;
4326 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
4328 uint32_t num_of_samples
;
4329 int retval
= ERROR_OK
;
4330 bool halted_before_profiling
= target
->state
== TARGET_HALTED
;
4332 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
4334 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
4336 LOG_ERROR("No memory to store samples.");
4340 uint64_t timestart_ms
= timeval_ms();
4342 * Some cores let us sample the PC without the
4343 * annoying halt/resume step; for example, ARMv7 PCSR.
4344 * Provide a way to use that more efficient mechanism.
4346 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4347 &num_of_samples
, offset
);
4348 if (retval
!= ERROR_OK
) {
4352 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4354 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4356 retval
= target_poll(target
);
4357 if (retval
!= ERROR_OK
) {
4362 if (target
->state
== TARGET_RUNNING
&& halted_before_profiling
) {
4363 /* The target was halted before we started and is running now. Halt it,
4364 * for consistency. */
4365 retval
= target_halt(target
);
4366 if (retval
!= ERROR_OK
) {
4370 } else if (target
->state
== TARGET_HALTED
&& !halted_before_profiling
) {
4371 /* The target was running before we started and is halted now. Resume
4372 * it, for consistency. */
4373 retval
= target_resume(target
, 1, 0, 0, 0);
4374 if (retval
!= ERROR_OK
) {
4380 retval
= target_poll(target
);
4381 if (retval
!= ERROR_OK
) {
4386 uint32_t start_address
= 0;
4387 uint32_t end_address
= 0;
4388 bool with_range
= false;
4389 if (CMD_ARGC
== 4) {
4391 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4392 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4395 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4396 with_range
, start_address
, end_address
, target
, duration_ms
);
4397 command_print(CMD
, "Wrote %s", CMD_ARGV
[1]);
4403 static int new_u64_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint64_t val
)
4406 Jim_Obj
*obj_name
, *obj_val
;
4409 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4413 obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4414 jim_wide wide_val
= val
;
4415 obj_val
= Jim_NewWideObj(interp
, wide_val
);
4416 if (!obj_name
|| !obj_val
) {
4421 Jim_IncrRefCount(obj_name
);
4422 Jim_IncrRefCount(obj_val
);
4423 result
= Jim_SetVariable(interp
, obj_name
, obj_val
);
4424 Jim_DecrRefCount(interp
, obj_name
);
4425 Jim_DecrRefCount(interp
, obj_val
);
4427 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4431 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4435 LOG_WARNING("DEPRECATED! use 'read_memory' not 'mem2array'");
4437 /* argv[0] = name of array to receive the data
4438 * argv[1] = desired element width in bits
4439 * argv[2] = memory address
4440 * argv[3] = count of times to read
4441 * argv[4] = optional "phys"
4443 if (argc
< 4 || argc
> 5) {
4444 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4448 /* Arg 0: Name of the array variable */
4449 const char *varname
= Jim_GetString(argv
[0], NULL
);
4451 /* Arg 1: Bit width of one element */
4453 e
= Jim_GetLong(interp
, argv
[1], &l
);
4456 const unsigned int width_bits
= l
;
4458 if (width_bits
!= 8 &&
4462 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4463 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4464 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4467 const unsigned int width
= width_bits
/ 8;
4469 /* Arg 2: Memory address */
4471 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4474 target_addr_t addr
= (target_addr_t
)wide_addr
;
4476 /* Arg 3: Number of elements to read */
4477 e
= Jim_GetLong(interp
, argv
[3], &l
);
4483 bool is_phys
= false;
4486 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4487 if (!strncmp(phys
, "phys", str_len
))
4493 /* Argument checks */
4495 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4496 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4499 if ((addr
+ (len
* width
)) < addr
) {
4500 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4501 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4505 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4506 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4507 "mem2array: too large read request, exceeds 64K items", NULL
);
4512 ((width
== 2) && ((addr
& 1) == 0)) ||
4513 ((width
== 4) && ((addr
& 3) == 0)) ||
4514 ((width
== 8) && ((addr
& 7) == 0))) {
4515 /* alignment correct */
4518 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4519 sprintf(buf
, "mem2array address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4522 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4531 const size_t buffersize
= 4096;
4532 uint8_t *buffer
= malloc(buffersize
);
4539 /* Slurp... in buffer size chunks */
4540 const unsigned int max_chunk_len
= buffersize
/ width
;
4541 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4545 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4547 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4548 if (retval
!= ERROR_OK
) {
4550 LOG_ERROR("mem2array: Read @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4554 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4555 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4559 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4563 v
= target_buffer_get_u64(target
, &buffer
[i
*width
]);
4566 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4569 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4572 v
= buffer
[i
] & 0x0ff;
4575 new_u64_array_element(interp
, varname
, idx
, v
);
4578 addr
+= chunk_len
* width
;
4584 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4589 static int target_jim_read_memory(Jim_Interp
*interp
, int argc
,
4590 Jim_Obj
* const *argv
)
4593 * argv[1] = memory address
4594 * argv[2] = desired element width in bits
4595 * argv[3] = number of elements to read
4596 * argv[4] = optional "phys"
4599 if (argc
< 4 || argc
> 5) {
4600 Jim_WrongNumArgs(interp
, 1, argv
, "address width count ['phys']");
4604 /* Arg 1: Memory address. */
4607 e
= Jim_GetWide(interp
, argv
[1], &wide_addr
);
4612 target_addr_t addr
= (target_addr_t
)wide_addr
;
4614 /* Arg 2: Bit width of one element. */
4616 e
= Jim_GetLong(interp
, argv
[2], &l
);
4621 const unsigned int width_bits
= l
;
4623 /* Arg 3: Number of elements to read. */
4624 e
= Jim_GetLong(interp
, argv
[3], &l
);
4631 /* Arg 4: Optional 'phys'. */
4632 bool is_phys
= false;
4635 const char *phys
= Jim_GetString(argv
[4], NULL
);
4637 if (strcmp(phys
, "phys")) {
4638 Jim_SetResultFormatted(interp
, "invalid argument '%s', must be 'phys'", phys
);
4645 switch (width_bits
) {
4652 Jim_SetResultString(interp
, "invalid width, must be 8, 16, 32 or 64", -1);
4656 const unsigned int width
= width_bits
/ 8;
4658 if ((addr
+ (count
* width
)) < addr
) {
4659 Jim_SetResultString(interp
, "read_memory: addr + count wraps to zero", -1);
4663 if (count
> 65536) {
4664 Jim_SetResultString(interp
, "read_memory: too large read request, exeeds 64K elements", -1);
4668 struct command_context
*cmd_ctx
= current_command_context(interp
);
4669 assert(cmd_ctx
!= NULL
);
4670 struct target
*target
= get_current_target(cmd_ctx
);
4672 const size_t buffersize
= 4096;
4673 uint8_t *buffer
= malloc(buffersize
);
4676 LOG_ERROR("Failed to allocate memory");
4680 Jim_Obj
*result_list
= Jim_NewListObj(interp
, NULL
, 0);
4681 Jim_IncrRefCount(result_list
);
4684 const unsigned int max_chunk_len
= buffersize
/ width
;
4685 const size_t chunk_len
= MIN(count
, max_chunk_len
);
4690 retval
= target_read_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4692 retval
= target_read_memory(target
, addr
, width
, chunk_len
, buffer
);
4694 if (retval
!= ERROR_OK
) {
4695 LOG_ERROR("read_memory: read at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
4696 addr
, width_bits
, chunk_len
);
4697 Jim_SetResultString(interp
, "read_memory: failed to read memory", -1);
4702 for (size_t i
= 0; i
< chunk_len
; i
++) {
4707 v
= target_buffer_get_u64(target
, &buffer
[i
* width
]);
4710 v
= target_buffer_get_u32(target
, &buffer
[i
* width
]);
4713 v
= target_buffer_get_u16(target
, &buffer
[i
* width
]);
4721 snprintf(value_buf
, sizeof(value_buf
), "0x%" PRIx64
, v
);
4723 Jim_ListAppendElement(interp
, result_list
,
4724 Jim_NewStringObj(interp
, value_buf
, -1));
4728 addr
+= chunk_len
* width
;
4734 Jim_DecrRefCount(interp
, result_list
);
4738 Jim_SetResult(interp
, result_list
);
4739 Jim_DecrRefCount(interp
, result_list
);
4744 static int get_u64_array_element(Jim_Interp
*interp
, const char *varname
, size_t idx
, uint64_t *val
)
4746 char *namebuf
= alloc_printf("%s(%zu)", varname
, idx
);
4750 Jim_Obj
*obj_name
= Jim_NewStringObj(interp
, namebuf
, -1);
4756 Jim_IncrRefCount(obj_name
);
4757 Jim_Obj
*obj_val
= Jim_GetVariable(interp
, obj_name
, JIM_ERRMSG
);
4758 Jim_DecrRefCount(interp
, obj_name
);
4764 int result
= Jim_GetWide(interp
, obj_val
, &wide_val
);
4769 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4770 int argc
, Jim_Obj
*const *argv
)
4774 LOG_WARNING("DEPRECATED! use 'write_memory' not 'array2mem'");
4776 /* argv[0] = name of array from which to read the data
4777 * argv[1] = desired element width in bits
4778 * argv[2] = memory address
4779 * argv[3] = number of elements to write
4780 * argv[4] = optional "phys"
4782 if (argc
< 4 || argc
> 5) {
4783 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4787 /* Arg 0: Name of the array variable */
4788 const char *varname
= Jim_GetString(argv
[0], NULL
);
4790 /* Arg 1: Bit width of one element */
4792 e
= Jim_GetLong(interp
, argv
[1], &l
);
4795 const unsigned int width_bits
= l
;
4797 if (width_bits
!= 8 &&
4801 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4802 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4803 "Invalid width param. Must be one of: 8, 16, 32 or 64.", NULL
);
4806 const unsigned int width
= width_bits
/ 8;
4808 /* Arg 2: Memory address */
4810 e
= Jim_GetWide(interp
, argv
[2], &wide_addr
);
4813 target_addr_t addr
= (target_addr_t
)wide_addr
;
4815 /* Arg 3: Number of elements to write */
4816 e
= Jim_GetLong(interp
, argv
[3], &l
);
4822 bool is_phys
= false;
4825 const char *phys
= Jim_GetString(argv
[4], &str_len
);
4826 if (!strncmp(phys
, "phys", str_len
))
4832 /* Argument checks */
4834 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4835 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4836 "array2mem: zero width read?", NULL
);
4840 if ((addr
+ (len
* width
)) < addr
) {
4841 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4842 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4843 "array2mem: addr + len - wraps to zero?", NULL
);
4848 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4849 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4850 "array2mem: too large memory write request, exceeds 64K items", NULL
);
4855 ((width
== 2) && ((addr
& 1) == 0)) ||
4856 ((width
== 4) && ((addr
& 3) == 0)) ||
4857 ((width
== 8) && ((addr
& 7) == 0))) {
4858 /* alignment correct */
4861 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4862 sprintf(buf
, "array2mem address: " TARGET_ADDR_FMT
" is not aligned for %" PRIu32
" byte reads",
4865 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4874 const size_t buffersize
= 4096;
4875 uint8_t *buffer
= malloc(buffersize
);
4883 /* Slurp... in buffer size chunks */
4884 const unsigned int max_chunk_len
= buffersize
/ width
;
4886 const size_t chunk_len
= MIN(len
, max_chunk_len
); /* in elements.. */
4888 /* Fill the buffer */
4889 for (size_t i
= 0; i
< chunk_len
; i
++, idx
++) {
4891 if (get_u64_array_element(interp
, varname
, idx
, &v
) != JIM_OK
) {
4897 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
4900 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4903 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4906 buffer
[i
] = v
& 0x0ff;
4912 /* Write the buffer to memory */
4915 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
4917 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
4918 if (retval
!= ERROR_OK
) {
4920 LOG_ERROR("array2mem: Write @ " TARGET_ADDR_FMT
", w=%u, cnt=%zu, failed",
4924 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4925 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4929 addr
+= chunk_len
* width
;
4934 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4939 static int target_jim_write_memory(Jim_Interp
*interp
, int argc
,
4940 Jim_Obj
* const *argv
)
4943 * argv[1] = memory address
4944 * argv[2] = desired element width in bits
4945 * argv[3] = list of data to write
4946 * argv[4] = optional "phys"
4949 if (argc
< 4 || argc
> 5) {
4950 Jim_WrongNumArgs(interp
, 1, argv
, "address width data ['phys']");
4954 /* Arg 1: Memory address. */
4957 e
= Jim_GetWide(interp
, argv
[1], &wide_addr
);
4962 target_addr_t addr
= (target_addr_t
)wide_addr
;
4964 /* Arg 2: Bit width of one element. */
4966 e
= Jim_GetLong(interp
, argv
[2], &l
);
4971 const unsigned int width_bits
= l
;
4972 size_t count
= Jim_ListLength(interp
, argv
[3]);
4974 /* Arg 4: Optional 'phys'. */
4975 bool is_phys
= false;
4978 const char *phys
= Jim_GetString(argv
[4], NULL
);
4980 if (strcmp(phys
, "phys")) {
4981 Jim_SetResultFormatted(interp
, "invalid argument '%s', must be 'phys'", phys
);
4988 switch (width_bits
) {
4995 Jim_SetResultString(interp
, "invalid width, must be 8, 16, 32 or 64", -1);
4999 const unsigned int width
= width_bits
/ 8;
5001 if ((addr
+ (count
* width
)) < addr
) {
5002 Jim_SetResultString(interp
, "write_memory: addr + len wraps to zero", -1);
5006 if (count
> 65536) {
5007 Jim_SetResultString(interp
, "write_memory: too large memory write request, exceeds 64K elements", -1);
5011 struct command_context
*cmd_ctx
= current_command_context(interp
);
5012 assert(cmd_ctx
!= NULL
);
5013 struct target
*target
= get_current_target(cmd_ctx
);
5015 const size_t buffersize
= 4096;
5016 uint8_t *buffer
= malloc(buffersize
);
5019 LOG_ERROR("Failed to allocate memory");
5026 const unsigned int max_chunk_len
= buffersize
/ width
;
5027 const size_t chunk_len
= MIN(count
, max_chunk_len
);
5029 for (size_t i
= 0; i
< chunk_len
; i
++, j
++) {
5030 Jim_Obj
*tmp
= Jim_ListGetIndex(interp
, argv
[3], j
);
5031 jim_wide element_wide
;
5032 Jim_GetWide(interp
, tmp
, &element_wide
);
5034 const uint64_t v
= element_wide
;
5038 target_buffer_set_u64(target
, &buffer
[i
* width
], v
);
5041 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
5044 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
5047 buffer
[i
] = v
& 0x0ff;
5057 retval
= target_write_phys_memory(target
, addr
, width
, chunk_len
, buffer
);
5059 retval
= target_write_memory(target
, addr
, width
, chunk_len
, buffer
);
5061 if (retval
!= ERROR_OK
) {
5062 LOG_ERROR("write_memory: write at " TARGET_ADDR_FMT
" with width=%u and count=%zu failed",
5063 addr
, width_bits
, chunk_len
);
5064 Jim_SetResultString(interp
, "write_memory: failed to write memory", -1);
5069 addr
+= chunk_len
* width
;
5077 /* FIX? should we propagate errors here rather than printing them
5080 void target_handle_event(struct target
*target
, enum target_event e
)
5082 struct target_event_action
*teap
;
5085 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5086 if (teap
->event
== e
) {
5087 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
5088 target
->target_number
,
5089 target_name(target
),
5090 target_type_name(target
),
5092 target_event_name(e
),
5093 Jim_GetString(teap
->body
, NULL
));
5095 /* Override current target by the target an event
5096 * is issued from (lot of scripts need it).
5097 * Return back to previous override as soon
5098 * as the handler processing is done */
5099 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
5100 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
5101 cmd_ctx
->current_target_override
= target
;
5103 retval
= Jim_EvalObj(teap
->interp
, teap
->body
);
5105 cmd_ctx
->current_target_override
= saved_target_override
;
5107 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
5110 if (retval
== JIM_RETURN
)
5111 retval
= teap
->interp
->returnCode
;
5113 if (retval
!= JIM_OK
) {
5114 Jim_MakeErrorMessage(teap
->interp
);
5115 LOG_USER("Error executing event %s on target %s:\n%s",
5116 target_event_name(e
),
5117 target_name(target
),
5118 Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
5119 /* clean both error code and stacktrace before return */
5120 Jim_Eval(teap
->interp
, "error \"\" \"\"");
5126 static int target_jim_get_reg(Jim_Interp
*interp
, int argc
,
5127 Jim_Obj
* const *argv
)
5132 const char *option
= Jim_GetString(argv
[1], NULL
);
5134 if (!strcmp(option
, "-force")) {
5139 Jim_SetResultFormatted(interp
, "invalid option '%s'", option
);
5145 Jim_WrongNumArgs(interp
, 1, argv
, "[-force] list");
5149 const int length
= Jim_ListLength(interp
, argv
[1]);
5151 Jim_Obj
*result_dict
= Jim_NewDictObj(interp
, NULL
, 0);
5156 struct command_context
*cmd_ctx
= current_command_context(interp
);
5157 assert(cmd_ctx
!= NULL
);
5158 const struct target
*target
= get_current_target(cmd_ctx
);
5160 for (int i
= 0; i
< length
; i
++) {
5161 Jim_Obj
*elem
= Jim_ListGetIndex(interp
, argv
[1], i
);
5166 const char *reg_name
= Jim_String(elem
);
5168 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5171 if (!reg
|| !reg
->exist
) {
5172 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5177 int retval
= reg
->type
->get(reg
);
5179 if (retval
!= ERROR_OK
) {
5180 Jim_SetResultFormatted(interp
, "failed to read register '%s'",
5186 char *reg_value
= buf_to_hex_str(reg
->value
, reg
->size
);
5189 LOG_ERROR("Failed to allocate memory");
5193 char *tmp
= alloc_printf("0x%s", reg_value
);
5198 LOG_ERROR("Failed to allocate memory");
5202 Jim_DictAddElement(interp
, result_dict
, elem
,
5203 Jim_NewStringObj(interp
, tmp
, -1));
5208 Jim_SetResult(interp
, result_dict
);
5213 static int target_jim_set_reg(Jim_Interp
*interp
, int argc
,
5214 Jim_Obj
* const *argv
)
5217 Jim_WrongNumArgs(interp
, 1, argv
, "dict");
5222 #if JIM_VERSION >= 80
5223 Jim_Obj
**dict
= Jim_DictPairs(interp
, argv
[1], &tmp
);
5229 int ret
= Jim_DictPairs(interp
, argv
[1], &dict
, &tmp
);
5235 const unsigned int length
= tmp
;
5236 struct command_context
*cmd_ctx
= current_command_context(interp
);
5238 const struct target
*target
= get_current_target(cmd_ctx
);
5240 for (unsigned int i
= 0; i
< length
; i
+= 2) {
5241 const char *reg_name
= Jim_String(dict
[i
]);
5242 const char *reg_value
= Jim_String(dict
[i
+ 1]);
5243 struct reg
*reg
= register_get_by_name(target
->reg_cache
, reg_name
,
5246 if (!reg
|| !reg
->exist
) {
5247 Jim_SetResultFormatted(interp
, "unknown register '%s'", reg_name
);
5251 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
5254 LOG_ERROR("Failed to allocate memory");
5258 str_to_buf(reg_value
, strlen(reg_value
), buf
, reg
->size
, 0);
5259 int retval
= reg
->type
->set(reg
, buf
);
5262 if (retval
!= ERROR_OK
) {
5263 Jim_SetResultFormatted(interp
, "failed to set '%s' to register '%s'",
5264 reg_value
, reg_name
);
5273 * Returns true only if the target has a handler for the specified event.
5275 bool target_has_event_action(struct target
*target
, enum target_event event
)
5277 struct target_event_action
*teap
;
5279 for (teap
= target
->event_action
; teap
; teap
= teap
->next
) {
5280 if (teap
->event
== event
)
5286 enum target_cfg_param
{
5289 TCFG_WORK_AREA_VIRT
,
5290 TCFG_WORK_AREA_PHYS
,
5291 TCFG_WORK_AREA_SIZE
,
5292 TCFG_WORK_AREA_BACKUP
,
5295 TCFG_CHAIN_POSITION
,
5300 TCFG_GDB_MAX_CONNECTIONS
,
5303 static struct jim_nvp nvp_config_opts
[] = {
5304 { .name
= "-type", .value
= TCFG_TYPE
},
5305 { .name
= "-event", .value
= TCFG_EVENT
},
5306 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
5307 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
5308 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
5309 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
5310 { .name
= "-endian", .value
= TCFG_ENDIAN
},
5311 { .name
= "-coreid", .value
= TCFG_COREID
},
5312 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
5313 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
5314 { .name
= "-rtos", .value
= TCFG_RTOS
},
5315 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
5316 { .name
= "-gdb-port", .value
= TCFG_GDB_PORT
},
5317 { .name
= "-gdb-max-connections", .value
= TCFG_GDB_MAX_CONNECTIONS
},
5318 { .name
= NULL
, .value
= -1 }
5321 static int target_configure(struct jim_getopt_info
*goi
, struct target
*target
)
5328 /* parse config or cget options ... */
5329 while (goi
->argc
> 0) {
5330 Jim_SetEmptyResult(goi
->interp
);
5331 /* jim_getopt_debug(goi); */
5333 if (target
->type
->target_jim_configure
) {
5334 /* target defines a configure function */
5335 /* target gets first dibs on parameters */
5336 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
5345 /* otherwise we 'continue' below */
5347 e
= jim_getopt_nvp(goi
, nvp_config_opts
, &n
);
5349 jim_getopt_nvp_unknown(goi
, nvp_config_opts
, 0);
5355 if (goi
->isconfigure
) {
5356 Jim_SetResultFormatted(goi
->interp
,
5357 "not settable: %s", n
->name
);
5361 if (goi
->argc
!= 0) {
5362 Jim_WrongNumArgs(goi
->interp
,
5363 goi
->argc
, goi
->argv
,
5368 Jim_SetResultString(goi
->interp
,
5369 target_type_name(target
), -1);
5373 if (goi
->argc
== 0) {
5374 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
5378 e
= jim_getopt_nvp(goi
, nvp_target_event
, &n
);
5380 jim_getopt_nvp_unknown(goi
, nvp_target_event
, 1);
5384 if (goi
->isconfigure
) {
5385 if (goi
->argc
!= 1) {
5386 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
5390 if (goi
->argc
!= 0) {
5391 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
5397 struct target_event_action
*teap
;
5399 teap
= target
->event_action
;
5400 /* replace existing? */
5402 if (teap
->event
== (enum target_event
)n
->value
)
5407 if (goi
->isconfigure
) {
5408 /* START_DEPRECATED_TPIU */
5409 if (n
->value
== TARGET_EVENT_TRACE_CONFIG
)
5410 LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n
->name
);
5411 /* END_DEPRECATED_TPIU */
5413 bool replace
= true;
5416 teap
= calloc(1, sizeof(*teap
));
5419 teap
->event
= n
->value
;
5420 teap
->interp
= goi
->interp
;
5421 jim_getopt_obj(goi
, &o
);
5423 Jim_DecrRefCount(teap
->interp
, teap
->body
);
5424 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
5427 * Tcl/TK - "tk events" have a nice feature.
5428 * See the "BIND" command.
5429 * We should support that here.
5430 * You can specify %X and %Y in the event code.
5431 * The idea is: %T - target name.
5432 * The idea is: %N - target number
5433 * The idea is: %E - event name.
5435 Jim_IncrRefCount(teap
->body
);
5438 /* add to head of event list */
5439 teap
->next
= target
->event_action
;
5440 target
->event_action
= teap
;
5442 Jim_SetEmptyResult(goi
->interp
);
5446 Jim_SetEmptyResult(goi
->interp
);
5448 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
5454 case TCFG_WORK_AREA_VIRT
:
5455 if (goi
->isconfigure
) {
5456 target_free_all_working_areas(target
);
5457 e
= jim_getopt_wide(goi
, &w
);
5460 target
->working_area_virt
= w
;
5461 target
->working_area_virt_spec
= true;
5466 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
5470 case TCFG_WORK_AREA_PHYS
:
5471 if (goi
->isconfigure
) {
5472 target_free_all_working_areas(target
);
5473 e
= jim_getopt_wide(goi
, &w
);
5476 target
->working_area_phys
= w
;
5477 target
->working_area_phys_spec
= true;
5482 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
5486 case TCFG_WORK_AREA_SIZE
:
5487 if (goi
->isconfigure
) {
5488 target_free_all_working_areas(target
);
5489 e
= jim_getopt_wide(goi
, &w
);
5492 target
->working_area_size
= w
;
5497 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
5501 case TCFG_WORK_AREA_BACKUP
:
5502 if (goi
->isconfigure
) {
5503 target_free_all_working_areas(target
);
5504 e
= jim_getopt_wide(goi
, &w
);
5507 /* make this exactly 1 or 0 */
5508 target
->backup_working_area
= (!!w
);
5513 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
5514 /* loop for more e*/
5519 if (goi
->isconfigure
) {
5520 e
= jim_getopt_nvp(goi
, nvp_target_endian
, &n
);
5522 jim_getopt_nvp_unknown(goi
, nvp_target_endian
, 1);
5525 target
->endianness
= n
->value
;
5530 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5532 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5533 n
= jim_nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
5535 Jim_SetResultString(goi
->interp
, n
->name
, -1);
5540 if (goi
->isconfigure
) {
5541 e
= jim_getopt_wide(goi
, &w
);
5544 target
->coreid
= (int32_t)w
;
5549 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->coreid
));
5553 case TCFG_CHAIN_POSITION
:
5554 if (goi
->isconfigure
) {
5556 struct jtag_tap
*tap
;
5558 if (target
->has_dap
) {
5559 Jim_SetResultString(goi
->interp
,
5560 "target requires -dap parameter instead of -chain-position!", -1);
5564 target_free_all_working_areas(target
);
5565 e
= jim_getopt_obj(goi
, &o_t
);
5568 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
5572 target
->tap_configured
= true;
5577 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
5578 /* loop for more e*/
5581 if (goi
->isconfigure
) {
5582 e
= jim_getopt_wide(goi
, &w
);
5585 target
->dbgbase
= (uint32_t)w
;
5586 target
->dbgbase_set
= true;
5591 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
5597 int result
= rtos_create(goi
, target
);
5598 if (result
!= JIM_OK
)
5604 case TCFG_DEFER_EXAMINE
:
5606 target
->defer_examine
= true;
5611 if (goi
->isconfigure
) {
5612 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5613 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5614 Jim_SetResultString(goi
->interp
, "-gdb-port must be configured before 'init'", -1);
5619 e
= jim_getopt_string(goi
, &s
, NULL
);
5622 free(target
->gdb_port_override
);
5623 target
->gdb_port_override
= strdup(s
);
5628 Jim_SetResultString(goi
->interp
, target
->gdb_port_override
? target
->gdb_port_override
: "undefined", -1);
5632 case TCFG_GDB_MAX_CONNECTIONS
:
5633 if (goi
->isconfigure
) {
5634 struct command_context
*cmd_ctx
= current_command_context(goi
->interp
);
5635 if (cmd_ctx
->mode
!= COMMAND_CONFIG
) {
5636 Jim_SetResultString(goi
->interp
, "-gdb-max-connections must be configured before 'init'", -1);
5640 e
= jim_getopt_wide(goi
, &w
);
5643 target
->gdb_max_connections
= (w
< 0) ? CONNECTION_LIMIT_UNLIMITED
: (int)w
;
5648 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->gdb_max_connections
));
5651 } /* while (goi->argc) */
5654 /* done - we return */
5658 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5660 struct command
*c
= jim_to_command(interp
);
5661 struct jim_getopt_info goi
;
5663 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5664 goi
.isconfigure
= !strcmp(c
->name
, "configure");
5666 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5667 "missing: -option ...");
5670 struct command_context
*cmd_ctx
= current_command_context(interp
);
5672 struct target
*target
= get_current_target(cmd_ctx
);
5673 return target_configure(&goi
, target
);
5676 static int jim_target_mem2array(Jim_Interp
*interp
,
5677 int argc
, Jim_Obj
*const *argv
)
5679 struct command_context
*cmd_ctx
= current_command_context(interp
);
5681 struct target
*target
= get_current_target(cmd_ctx
);
5682 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5685 static int jim_target_array2mem(Jim_Interp
*interp
,
5686 int argc
, Jim_Obj
*const *argv
)
5688 struct command_context
*cmd_ctx
= current_command_context(interp
);
5690 struct target
*target
= get_current_target(cmd_ctx
);
5691 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5694 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5696 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5700 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5702 bool allow_defer
= false;
5704 struct jim_getopt_info goi
;
5705 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5707 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5708 Jim_SetResultFormatted(goi
.interp
,
5709 "usage: %s ['allow-defer']", cmd_name
);
5713 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5716 int e
= jim_getopt_obj(&goi
, &obj
);
5722 struct command_context
*cmd_ctx
= current_command_context(interp
);
5724 struct target
*target
= get_current_target(cmd_ctx
);
5725 if (!target
->tap
->enabled
)
5726 return jim_target_tap_disabled(interp
);
5728 if (allow_defer
&& target
->defer_examine
) {
5729 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5730 LOG_INFO("Use arp_examine command to examine it manually!");
5734 int e
= target
->type
->examine(target
);
5735 if (e
!= ERROR_OK
) {
5736 target_reset_examined(target
);
5740 target_set_examined(target
);
5745 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5747 struct command_context
*cmd_ctx
= current_command_context(interp
);
5749 struct target
*target
= get_current_target(cmd_ctx
);
5751 Jim_SetResultBool(interp
, target_was_examined(target
));
5755 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5757 struct command_context
*cmd_ctx
= current_command_context(interp
);
5759 struct target
*target
= get_current_target(cmd_ctx
);
5761 Jim_SetResultBool(interp
, target
->defer_examine
);
5765 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5768 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5771 struct command_context
*cmd_ctx
= current_command_context(interp
);
5773 struct target
*target
= get_current_target(cmd_ctx
);
5775 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5781 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5784 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5787 struct command_context
*cmd_ctx
= current_command_context(interp
);
5789 struct target
*target
= get_current_target(cmd_ctx
);
5790 if (!target
->tap
->enabled
)
5791 return jim_target_tap_disabled(interp
);
5794 if (!(target_was_examined(target
)))
5795 e
= ERROR_TARGET_NOT_EXAMINED
;
5797 e
= target
->type
->poll(target
);
5803 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5805 struct jim_getopt_info goi
;
5806 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5808 if (goi
.argc
!= 2) {
5809 Jim_WrongNumArgs(interp
, 0, argv
,
5810 "([tT]|[fF]|assert|deassert) BOOL");
5815 int e
= jim_getopt_nvp(&goi
, nvp_assert
, &n
);
5817 jim_getopt_nvp_unknown(&goi
, nvp_assert
, 1);
5820 /* the halt or not param */
5822 e
= jim_getopt_wide(&goi
, &a
);
5826 struct command_context
*cmd_ctx
= current_command_context(interp
);
5828 struct target
*target
= get_current_target(cmd_ctx
);
5829 if (!target
->tap
->enabled
)
5830 return jim_target_tap_disabled(interp
);
5832 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5833 Jim_SetResultFormatted(interp
,
5834 "No target-specific reset for %s",
5835 target_name(target
));
5839 if (target
->defer_examine
)
5840 target_reset_examined(target
);
5842 /* determine if we should halt or not. */
5843 target
->reset_halt
= (a
!= 0);
5844 /* When this happens - all workareas are invalid. */
5845 target_free_all_working_areas_restore(target
, 0);
5848 if (n
->value
== NVP_ASSERT
)
5849 e
= target
->type
->assert_reset(target
);
5851 e
= target
->type
->deassert_reset(target
);
5852 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5855 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5858 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5861 struct command_context
*cmd_ctx
= current_command_context(interp
);
5863 struct target
*target
= get_current_target(cmd_ctx
);
5864 if (!target
->tap
->enabled
)
5865 return jim_target_tap_disabled(interp
);
5866 int e
= target
->type
->halt(target
);
5867 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5870 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5872 struct jim_getopt_info goi
;
5873 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5875 /* params: <name> statename timeoutmsecs */
5876 if (goi
.argc
!= 2) {
5877 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5878 Jim_SetResultFormatted(goi
.interp
,
5879 "%s <state_name> <timeout_in_msec>", cmd_name
);
5884 int e
= jim_getopt_nvp(&goi
, nvp_target_state
, &n
);
5886 jim_getopt_nvp_unknown(&goi
, nvp_target_state
, 1);
5890 e
= jim_getopt_wide(&goi
, &a
);
5893 struct command_context
*cmd_ctx
= current_command_context(interp
);
5895 struct target
*target
= get_current_target(cmd_ctx
);
5896 if (!target
->tap
->enabled
)
5897 return jim_target_tap_disabled(interp
);
5899 e
= target_wait_state(target
, n
->value
, a
);
5900 if (e
!= ERROR_OK
) {
5901 Jim_Obj
*obj
= Jim_NewIntObj(interp
, e
);
5902 Jim_SetResultFormatted(goi
.interp
,
5903 "target: %s wait %s fails (%#s) %s",
5904 target_name(target
), n
->name
,
5905 obj
, target_strerror_safe(e
));
5910 /* List for human, Events defined for this target.
5911 * scripts/programs should use 'name cget -event NAME'
5913 COMMAND_HANDLER(handle_target_event_list
)
5915 struct target
*target
= get_current_target(CMD_CTX
);
5916 struct target_event_action
*teap
= target
->event_action
;
5918 command_print(CMD
, "Event actions for target (%d) %s\n",
5919 target
->target_number
,
5920 target_name(target
));
5921 command_print(CMD
, "%-25s | Body", "Event");
5922 command_print(CMD
, "------------------------- | "
5923 "----------------------------------------");
5925 command_print(CMD
, "%-25s | %s",
5926 target_event_name(teap
->event
),
5927 Jim_GetString(teap
->body
, NULL
));
5930 command_print(CMD
, "***END***");
5933 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5936 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5939 struct command_context
*cmd_ctx
= current_command_context(interp
);
5941 struct target
*target
= get_current_target(cmd_ctx
);
5942 Jim_SetResultString(interp
, target_state_name(target
), -1);
5945 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5947 struct jim_getopt_info goi
;
5948 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
5949 if (goi
.argc
!= 1) {
5950 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5951 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5955 int e
= jim_getopt_nvp(&goi
, nvp_target_event
, &n
);
5957 jim_getopt_nvp_unknown(&goi
, nvp_target_event
, 1);
5960 struct command_context
*cmd_ctx
= current_command_context(interp
);
5962 struct target
*target
= get_current_target(cmd_ctx
);
5963 target_handle_event(target
, n
->value
);
5967 static const struct command_registration target_instance_command_handlers
[] = {
5969 .name
= "configure",
5970 .mode
= COMMAND_ANY
,
5971 .jim_handler
= jim_target_configure
,
5972 .help
= "configure a new target for use",
5973 .usage
= "[target_attribute ...]",
5977 .mode
= COMMAND_ANY
,
5978 .jim_handler
= jim_target_configure
,
5979 .help
= "returns the specified target attribute",
5980 .usage
= "target_attribute",
5984 .handler
= handle_mw_command
,
5985 .mode
= COMMAND_EXEC
,
5986 .help
= "Write 64-bit word(s) to target memory",
5987 .usage
= "address data [count]",
5991 .handler
= handle_mw_command
,
5992 .mode
= COMMAND_EXEC
,
5993 .help
= "Write 32-bit word(s) to target memory",
5994 .usage
= "address data [count]",
5998 .handler
= handle_mw_command
,
5999 .mode
= COMMAND_EXEC
,
6000 .help
= "Write 16-bit half-word(s) to target memory",
6001 .usage
= "address data [count]",
6005 .handler
= handle_mw_command
,
6006 .mode
= COMMAND_EXEC
,
6007 .help
= "Write byte(s) to target memory",
6008 .usage
= "address data [count]",
6012 .handler
= handle_md_command
,
6013 .mode
= COMMAND_EXEC
,
6014 .help
= "Display target memory as 64-bit words",
6015 .usage
= "address [count]",
6019 .handler
= handle_md_command
,
6020 .mode
= COMMAND_EXEC
,
6021 .help
= "Display target memory as 32-bit words",
6022 .usage
= "address [count]",
6026 .handler
= handle_md_command
,
6027 .mode
= COMMAND_EXEC
,
6028 .help
= "Display target memory as 16-bit half-words",
6029 .usage
= "address [count]",
6033 .handler
= handle_md_command
,
6034 .mode
= COMMAND_EXEC
,
6035 .help
= "Display target memory as 8-bit bytes",
6036 .usage
= "address [count]",
6039 .name
= "array2mem",
6040 .mode
= COMMAND_EXEC
,
6041 .jim_handler
= jim_target_array2mem
,
6042 .help
= "Writes Tcl array of 8/16/32 bit numbers "
6044 .usage
= "arrayname bitwidth address count",
6047 .name
= "mem2array",
6048 .mode
= COMMAND_EXEC
,
6049 .jim_handler
= jim_target_mem2array
,
6050 .help
= "Loads Tcl array of 8/16/32 bit numbers "
6051 "from target memory",
6052 .usage
= "arrayname bitwidth address count",
6056 .mode
= COMMAND_EXEC
,
6057 .jim_handler
= target_jim_get_reg
,
6058 .help
= "Get register values from the target",
6063 .mode
= COMMAND_EXEC
,
6064 .jim_handler
= target_jim_set_reg
,
6065 .help
= "Set target register values",
6069 .name
= "read_memory",
6070 .mode
= COMMAND_EXEC
,
6071 .jim_handler
= target_jim_read_memory
,
6072 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6073 .usage
= "address width count ['phys']",
6076 .name
= "write_memory",
6077 .mode
= COMMAND_EXEC
,
6078 .jim_handler
= target_jim_write_memory
,
6079 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6080 .usage
= "address width data ['phys']",
6083 .name
= "eventlist",
6084 .handler
= handle_target_event_list
,
6085 .mode
= COMMAND_EXEC
,
6086 .help
= "displays a table of events defined for this target",
6091 .mode
= COMMAND_EXEC
,
6092 .jim_handler
= jim_target_current_state
,
6093 .help
= "displays the current state of this target",
6096 .name
= "arp_examine",
6097 .mode
= COMMAND_EXEC
,
6098 .jim_handler
= jim_target_examine
,
6099 .help
= "used internally for reset processing",
6100 .usage
= "['allow-defer']",
6103 .name
= "was_examined",
6104 .mode
= COMMAND_EXEC
,
6105 .jim_handler
= jim_target_was_examined
,
6106 .help
= "used internally for reset processing",
6109 .name
= "examine_deferred",
6110 .mode
= COMMAND_EXEC
,
6111 .jim_handler
= jim_target_examine_deferred
,
6112 .help
= "used internally for reset processing",
6115 .name
= "arp_halt_gdb",
6116 .mode
= COMMAND_EXEC
,
6117 .jim_handler
= jim_target_halt_gdb
,
6118 .help
= "used internally for reset processing to halt GDB",
6122 .mode
= COMMAND_EXEC
,
6123 .jim_handler
= jim_target_poll
,
6124 .help
= "used internally for reset processing",
6127 .name
= "arp_reset",
6128 .mode
= COMMAND_EXEC
,
6129 .jim_handler
= jim_target_reset
,
6130 .help
= "used internally for reset processing",
6134 .mode
= COMMAND_EXEC
,
6135 .jim_handler
= jim_target_halt
,
6136 .help
= "used internally for reset processing",
6139 .name
= "arp_waitstate",
6140 .mode
= COMMAND_EXEC
,
6141 .jim_handler
= jim_target_wait_state
,
6142 .help
= "used internally for reset processing",
6145 .name
= "invoke-event",
6146 .mode
= COMMAND_EXEC
,
6147 .jim_handler
= jim_target_invoke_event
,
6148 .help
= "invoke handler for specified event",
6149 .usage
= "event_name",
6151 COMMAND_REGISTRATION_DONE
6154 static int target_create(struct jim_getopt_info
*goi
)
6161 struct target
*target
;
6162 struct command_context
*cmd_ctx
;
6164 cmd_ctx
= current_command_context(goi
->interp
);
6167 if (goi
->argc
< 3) {
6168 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
6173 jim_getopt_obj(goi
, &new_cmd
);
6174 /* does this command exist? */
6175 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_NONE
);
6177 cp
= Jim_GetString(new_cmd
, NULL
);
6178 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
6183 e
= jim_getopt_string(goi
, &cp
, NULL
);
6186 struct transport
*tr
= get_current_transport();
6187 if (tr
->override_target
) {
6188 e
= tr
->override_target(&cp
);
6189 if (e
!= ERROR_OK
) {
6190 LOG_ERROR("The selected transport doesn't support this target");
6193 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
6195 /* now does target type exist */
6196 for (x
= 0 ; target_types
[x
] ; x
++) {
6197 if (strcmp(cp
, target_types
[x
]->name
) == 0) {
6202 if (!target_types
[x
]) {
6203 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
6204 for (x
= 0 ; target_types
[x
] ; x
++) {
6205 if (target_types
[x
+ 1]) {
6206 Jim_AppendStrings(goi
->interp
,
6207 Jim_GetResult(goi
->interp
),
6208 target_types
[x
]->name
,
6211 Jim_AppendStrings(goi
->interp
,
6212 Jim_GetResult(goi
->interp
),
6214 target_types
[x
]->name
, NULL
);
6221 target
= calloc(1, sizeof(struct target
));
6223 LOG_ERROR("Out of memory");
6227 /* set empty smp cluster */
6228 target
->smp_targets
= &empty_smp_targets
;
6230 /* set target number */
6231 target
->target_number
= new_target_number();
6233 /* allocate memory for each unique target type */
6234 target
->type
= malloc(sizeof(struct target_type
));
6235 if (!target
->type
) {
6236 LOG_ERROR("Out of memory");
6241 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
6243 /* default to first core, override with -coreid */
6246 target
->working_area
= 0x0;
6247 target
->working_area_size
= 0x0;
6248 target
->working_areas
= NULL
;
6249 target
->backup_working_area
= 0;
6251 target
->state
= TARGET_UNKNOWN
;
6252 target
->debug_reason
= DBG_REASON_UNDEFINED
;
6253 target
->reg_cache
= NULL
;
6254 target
->breakpoints
= NULL
;
6255 target
->watchpoints
= NULL
;
6256 target
->next
= NULL
;
6257 target
->arch_info
= NULL
;
6259 target
->verbose_halt_msg
= true;
6261 target
->halt_issued
= false;
6263 /* initialize trace information */
6264 target
->trace_info
= calloc(1, sizeof(struct trace
));
6265 if (!target
->trace_info
) {
6266 LOG_ERROR("Out of memory");
6272 target
->dbgmsg
= NULL
;
6273 target
->dbg_msg_enabled
= 0;
6275 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
6277 target
->rtos
= NULL
;
6278 target
->rtos_auto_detect
= false;
6280 target
->gdb_port_override
= NULL
;
6281 target
->gdb_max_connections
= 1;
6283 /* Do the rest as "configure" options */
6284 goi
->isconfigure
= 1;
6285 e
= target_configure(goi
, target
);
6288 if (target
->has_dap
) {
6289 if (!target
->dap_configured
) {
6290 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
6294 if (!target
->tap_configured
) {
6295 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
6299 /* tap must be set after target was configured */
6305 rtos_destroy(target
);
6306 free(target
->gdb_port_override
);
6307 free(target
->trace_info
);
6313 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
6314 /* default endian to little if not specified */
6315 target
->endianness
= TARGET_LITTLE_ENDIAN
;
6318 cp
= Jim_GetString(new_cmd
, NULL
);
6319 target
->cmd_name
= strdup(cp
);
6320 if (!target
->cmd_name
) {
6321 LOG_ERROR("Out of memory");
6322 rtos_destroy(target
);
6323 free(target
->gdb_port_override
);
6324 free(target
->trace_info
);
6330 if (target
->type
->target_create
) {
6331 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
6332 if (e
!= ERROR_OK
) {
6333 LOG_DEBUG("target_create failed");
6334 free(target
->cmd_name
);
6335 rtos_destroy(target
);
6336 free(target
->gdb_port_override
);
6337 free(target
->trace_info
);
6344 /* create the target specific commands */
6345 if (target
->type
->commands
) {
6346 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
6348 LOG_ERROR("unable to register '%s' commands", cp
);
6351 /* now - create the new target name command */
6352 const struct command_registration target_subcommands
[] = {
6354 .chain
= target_instance_command_handlers
,
6357 .chain
= target
->type
->commands
,
6359 COMMAND_REGISTRATION_DONE
6361 const struct command_registration target_commands
[] = {
6364 .mode
= COMMAND_ANY
,
6365 .help
= "target command group",
6367 .chain
= target_subcommands
,
6369 COMMAND_REGISTRATION_DONE
6371 e
= register_commands_override_target(cmd_ctx
, NULL
, target_commands
, target
);
6372 if (e
!= ERROR_OK
) {
6373 if (target
->type
->deinit_target
)
6374 target
->type
->deinit_target(target
);
6375 free(target
->cmd_name
);
6376 rtos_destroy(target
);
6377 free(target
->gdb_port_override
);
6378 free(target
->trace_info
);
6384 /* append to end of list */
6385 append_to_list_all_targets(target
);
6387 cmd_ctx
->current_target
= target
;
6391 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6394 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6397 struct command_context
*cmd_ctx
= current_command_context(interp
);
6400 struct target
*target
= get_current_target_or_null(cmd_ctx
);
6402 Jim_SetResultString(interp
, target_name(target
), -1);
6406 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6409 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6412 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
6413 for (unsigned x
= 0; target_types
[x
]; x
++) {
6414 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
6415 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
6420 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6423 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
6426 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
6427 struct target
*target
= all_targets
;
6429 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
6430 Jim_NewStringObj(interp
, target_name(target
), -1));
6431 target
= target
->next
;
6436 static struct target_list
*
6437 __attribute__((warn_unused_result
))
6438 create_target_list_node(Jim_Obj
*const name
) {
6440 const char *targetname
= Jim_GetString(name
, &len
);
6441 struct target
*target
= get_target(targetname
);
6442 LOG_DEBUG("%s ", targetname
);
6446 struct target_list
*new = malloc(sizeof(struct target_list
));
6448 LOG_ERROR("Out of memory");
6452 new->target
= target
;
6456 static int get_target_with_common_rtos_type(struct list_head
*lh
, struct target
**result
)
6458 struct target
*target
= NULL
;
6459 struct target_list
*curr
;
6460 foreach_smp_target(curr
, lh
) {
6461 struct rtos
*curr_rtos
= curr
->target
->rtos
;
6463 if (target
&& target
->rtos
&& target
->rtos
->type
!= curr_rtos
->type
) {
6464 LOG_ERROR("Different rtos types in members of one smp target!");
6467 target
= curr
->target
;
6474 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6476 static int smp_group
= 1;
6479 LOG_DEBUG("Empty SMP target");
6482 LOG_DEBUG("%d", argc
);
6483 /* argv[1] = target to associate in smp
6484 * argv[2] = target to associate in smp
6488 struct list_head
*lh
= malloc(sizeof(*lh
));
6490 LOG_ERROR("Out of memory");
6495 for (int i
= 1; i
< argc
; i
++) {
6496 struct target_list
*new = create_target_list_node(argv
[i
]);
6498 list_add_tail(&new->lh
, lh
);
6500 /* now parse the list of cpu and put the target in smp mode*/
6501 struct target_list
*curr
;
6502 foreach_smp_target(curr
, lh
) {
6503 struct target
*target
= curr
->target
;
6504 target
->smp
= smp_group
;
6505 target
->smp_targets
= lh
;
6509 struct target
*rtos_target
;
6510 int retval
= get_target_with_common_rtos_type(lh
, &rtos_target
);
6511 if (retval
== JIM_OK
&& rtos_target
)
6512 retval
= rtos_smp_init(rtos_target
);
6518 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
6520 struct jim_getopt_info goi
;
6521 jim_getopt_setup(&goi
, interp
, argc
- 1, argv
+ 1);
6523 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
6524 "<name> <target_type> [<target_options> ...]");
6527 return target_create(&goi
);
6530 static const struct command_registration target_subcommand_handlers
[] = {
6533 .mode
= COMMAND_CONFIG
,
6534 .handler
= handle_target_init_command
,
6535 .help
= "initialize targets",
6540 .mode
= COMMAND_CONFIG
,
6541 .jim_handler
= jim_target_create
,
6542 .usage
= "name type '-chain-position' name [options ...]",
6543 .help
= "Creates and selects a new target",
6547 .mode
= COMMAND_ANY
,
6548 .jim_handler
= jim_target_current
,
6549 .help
= "Returns the currently selected target",
6553 .mode
= COMMAND_ANY
,
6554 .jim_handler
= jim_target_types
,
6555 .help
= "Returns the available target types as "
6556 "a list of strings",
6560 .mode
= COMMAND_ANY
,
6561 .jim_handler
= jim_target_names
,
6562 .help
= "Returns the names of all targets as a list of strings",
6566 .mode
= COMMAND_ANY
,
6567 .jim_handler
= jim_target_smp
,
6568 .usage
= "targetname1 targetname2 ...",
6569 .help
= "gather several target in a smp list"
6572 COMMAND_REGISTRATION_DONE
6576 target_addr_t address
;
6582 static int fastload_num
;
6583 static struct fast_load
*fastload
;
6585 static void free_fastload(void)
6588 for (int i
= 0; i
< fastload_num
; i
++)
6589 free(fastload
[i
].data
);
6595 COMMAND_HANDLER(handle_fast_load_image_command
)
6599 uint32_t image_size
;
6600 target_addr_t min_address
= 0;
6601 target_addr_t max_address
= -1;
6605 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command
,
6606 &image
, &min_address
, &max_address
);
6607 if (retval
!= ERROR_OK
)
6610 struct duration bench
;
6611 duration_start(&bench
);
6613 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
6614 if (retval
!= ERROR_OK
)
6619 fastload_num
= image
.num_sections
;
6620 fastload
= malloc(sizeof(struct fast_load
)*image
.num_sections
);
6622 command_print(CMD
, "out of memory");
6623 image_close(&image
);
6626 memset(fastload
, 0, sizeof(struct fast_load
)*image
.num_sections
);
6627 for (unsigned int i
= 0; i
< image
.num_sections
; i
++) {
6628 buffer
= malloc(image
.sections
[i
].size
);
6630 command_print(CMD
, "error allocating buffer for section (%d bytes)",
6631 (int)(image
.sections
[i
].size
));
6632 retval
= ERROR_FAIL
;
6636 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
6637 if (retval
!= ERROR_OK
) {
6642 uint32_t offset
= 0;
6643 uint32_t length
= buf_cnt
;
6645 /* DANGER!!! beware of unsigned comparison here!!! */
6647 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
6648 (image
.sections
[i
].base_address
< max_address
)) {
6649 if (image
.sections
[i
].base_address
< min_address
) {
6650 /* clip addresses below */
6651 offset
+= min_address
-image
.sections
[i
].base_address
;
6655 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
6656 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
6658 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
6659 fastload
[i
].data
= malloc(length
);
6660 if (!fastload
[i
].data
) {
6662 command_print(CMD
, "error allocating buffer for section (%" PRIu32
" bytes)",
6664 retval
= ERROR_FAIL
;
6667 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
6668 fastload
[i
].length
= length
;
6670 image_size
+= length
;
6671 command_print(CMD
, "%u bytes written at address 0x%8.8x",
6672 (unsigned int)length
,
6673 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
6679 if ((retval
== ERROR_OK
) && (duration_measure(&bench
) == ERROR_OK
)) {
6680 command_print(CMD
, "Loaded %" PRIu32
" bytes "
6681 "in %fs (%0.3f KiB/s)", image_size
,
6682 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
6685 "WARNING: image has not been loaded to target!"
6686 "You can issue a 'fast_load' to finish loading.");
6689 image_close(&image
);
6691 if (retval
!= ERROR_OK
)
6697 COMMAND_HANDLER(handle_fast_load_command
)
6700 return ERROR_COMMAND_SYNTAX_ERROR
;
6702 LOG_ERROR("No image in memory");
6706 int64_t ms
= timeval_ms();
6708 int retval
= ERROR_OK
;
6709 for (i
= 0; i
< fastload_num
; i
++) {
6710 struct target
*target
= get_current_target(CMD_CTX
);
6711 command_print(CMD
, "Write to 0x%08x, length 0x%08x",
6712 (unsigned int)(fastload
[i
].address
),
6713 (unsigned int)(fastload
[i
].length
));
6714 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
6715 if (retval
!= ERROR_OK
)
6717 size
+= fastload
[i
].length
;
6719 if (retval
== ERROR_OK
) {
6720 int64_t after
= timeval_ms();
6721 command_print(CMD
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
6726 static const struct command_registration target_command_handlers
[] = {
6729 .handler
= handle_targets_command
,
6730 .mode
= COMMAND_ANY
,
6731 .help
= "change current default target (one parameter) "
6732 "or prints table of all targets (no parameters)",
6733 .usage
= "[target]",
6737 .mode
= COMMAND_CONFIG
,
6738 .help
= "configure target",
6739 .chain
= target_subcommand_handlers
,
6742 COMMAND_REGISTRATION_DONE
6745 int target_register_commands(struct command_context
*cmd_ctx
)
6747 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6750 static bool target_reset_nag
= true;
6752 bool get_target_reset_nag(void)
6754 return target_reset_nag
;
6757 COMMAND_HANDLER(handle_target_reset_nag
)
6759 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6760 &target_reset_nag
, "Nag after each reset about options to improve "
6764 COMMAND_HANDLER(handle_ps_command
)
6766 struct target
*target
= get_current_target(CMD_CTX
);
6768 if (target
->state
!= TARGET_HALTED
) {
6769 LOG_INFO("target not halted !!");
6773 if ((target
->rtos
) && (target
->rtos
->type
)
6774 && (target
->rtos
->type
->ps_command
)) {
6775 display
= target
->rtos
->type
->ps_command(target
);
6776 command_print(CMD
, "%s", display
);
6781 return ERROR_TARGET_FAILURE
;
6785 static void binprint(struct command_invocation
*cmd
, const char *text
, const uint8_t *buf
, int size
)
6788 command_print_sameline(cmd
, "%s", text
);
6789 for (int i
= 0; i
< size
; i
++)
6790 command_print_sameline(cmd
, " %02x", buf
[i
]);
6791 command_print(cmd
, " ");
6794 COMMAND_HANDLER(handle_test_mem_access_command
)
6796 struct target
*target
= get_current_target(CMD_CTX
);
6798 int retval
= ERROR_OK
;
6800 if (target
->state
!= TARGET_HALTED
) {
6801 LOG_INFO("target not halted !!");
6806 return ERROR_COMMAND_SYNTAX_ERROR
;
6808 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6811 size_t num_bytes
= test_size
+ 4;
6813 struct working_area
*wa
= NULL
;
6814 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6815 if (retval
!= ERROR_OK
) {
6816 LOG_ERROR("Not enough working area");
6820 uint8_t *test_pattern
= malloc(num_bytes
);
6822 for (size_t i
= 0; i
< num_bytes
; i
++)
6823 test_pattern
[i
] = rand();
6825 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6826 if (retval
!= ERROR_OK
) {
6827 LOG_ERROR("Test pattern write failed");
6831 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6832 for (int size
= 1; size
<= 4; size
*= 2) {
6833 for (int offset
= 0; offset
< 4; offset
++) {
6834 uint32_t count
= test_size
/ size
;
6835 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6836 uint8_t *read_ref
= malloc(host_bufsiz
);
6837 uint8_t *read_buf
= malloc(host_bufsiz
);
6839 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6840 read_ref
[i
] = rand();
6841 read_buf
[i
] = read_ref
[i
];
6843 command_print_sameline(CMD
,
6844 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6845 size
, offset
, host_offset
? "un" : "");
6847 struct duration bench
;
6848 duration_start(&bench
);
6850 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6851 read_buf
+ size
+ host_offset
);
6853 duration_measure(&bench
);
6855 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6856 command_print(CMD
, "Unsupported alignment");
6858 } else if (retval
!= ERROR_OK
) {
6859 command_print(CMD
, "Memory read failed");
6863 /* replay on host */
6864 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6867 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6869 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6870 duration_elapsed(&bench
),
6871 duration_kbps(&bench
, count
* size
));
6873 command_print(CMD
, "Compare failed");
6874 binprint(CMD
, "ref:", read_ref
, host_bufsiz
);
6875 binprint(CMD
, "buf:", read_buf
, host_bufsiz
);
6887 target_free_working_area(target
, wa
);
6890 num_bytes
= test_size
+ 4 + 4 + 4;
6892 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6893 if (retval
!= ERROR_OK
) {
6894 LOG_ERROR("Not enough working area");
6898 test_pattern
= malloc(num_bytes
);
6900 for (size_t i
= 0; i
< num_bytes
; i
++)
6901 test_pattern
[i
] = rand();
6903 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6904 for (int size
= 1; size
<= 4; size
*= 2) {
6905 for (int offset
= 0; offset
< 4; offset
++) {
6906 uint32_t count
= test_size
/ size
;
6907 size_t host_bufsiz
= count
* size
+ host_offset
;
6908 uint8_t *read_ref
= malloc(num_bytes
);
6909 uint8_t *read_buf
= malloc(num_bytes
);
6910 uint8_t *write_buf
= malloc(host_bufsiz
);
6912 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6913 write_buf
[i
] = rand();
6914 command_print_sameline(CMD
,
6915 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6916 size
, offset
, host_offset
? "un" : "");
6918 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6919 if (retval
!= ERROR_OK
) {
6920 command_print(CMD
, "Test pattern write failed");
6924 /* replay on host */
6925 memcpy(read_ref
, test_pattern
, num_bytes
);
6926 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6928 struct duration bench
;
6929 duration_start(&bench
);
6931 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6932 write_buf
+ host_offset
);
6934 duration_measure(&bench
);
6936 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6937 command_print(CMD
, "Unsupported alignment");
6939 } else if (retval
!= ERROR_OK
) {
6940 command_print(CMD
, "Memory write failed");
6945 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6946 if (retval
!= ERROR_OK
) {
6947 command_print(CMD
, "Test pattern write failed");
6952 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6954 command_print(CMD
, "Pass in %fs (%0.3f KiB/s)",
6955 duration_elapsed(&bench
),
6956 duration_kbps(&bench
, count
* size
));
6958 command_print(CMD
, "Compare failed");
6959 binprint(CMD
, "ref:", read_ref
, num_bytes
);
6960 binprint(CMD
, "buf:", read_buf
, num_bytes
);
6971 target_free_working_area(target
, wa
);
6975 static const struct command_registration target_exec_command_handlers
[] = {
6977 .name
= "fast_load_image",
6978 .handler
= handle_fast_load_image_command
,
6979 .mode
= COMMAND_ANY
,
6980 .help
= "Load image into server memory for later use by "
6981 "fast_load; primarily for profiling",
6982 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6983 "[min_address [max_length]]",
6986 .name
= "fast_load",
6987 .handler
= handle_fast_load_command
,
6988 .mode
= COMMAND_EXEC
,
6989 .help
= "loads active fast load image to current target "
6990 "- mainly for profiling purposes",
6995 .handler
= handle_profile_command
,
6996 .mode
= COMMAND_EXEC
,
6997 .usage
= "seconds filename [start end]",
6998 .help
= "profiling samples the CPU PC",
7000 /** @todo don't register virt2phys() unless target supports it */
7002 .name
= "virt2phys",
7003 .handler
= handle_virt2phys_command
,
7004 .mode
= COMMAND_ANY
,
7005 .help
= "translate a virtual address into a physical address",
7006 .usage
= "virtual_address",
7010 .handler
= handle_reg_command
,
7011 .mode
= COMMAND_EXEC
,
7012 .help
= "display (reread from target with \"force\") or set a register; "
7013 "with no arguments, displays all registers and their values",
7014 .usage
= "[(register_number|register_name) [(value|'force')]]",
7018 .handler
= handle_poll_command
,
7019 .mode
= COMMAND_EXEC
,
7020 .help
= "poll target state; or reconfigure background polling",
7021 .usage
= "['on'|'off']",
7024 .name
= "wait_halt",
7025 .handler
= handle_wait_halt_command
,
7026 .mode
= COMMAND_EXEC
,
7027 .help
= "wait up to the specified number of milliseconds "
7028 "(default 5000) for a previously requested halt",
7029 .usage
= "[milliseconds]",
7033 .handler
= handle_halt_command
,
7034 .mode
= COMMAND_EXEC
,
7035 .help
= "request target to halt, then wait up to the specified "
7036 "number of milliseconds (default 5000) for it to complete",
7037 .usage
= "[milliseconds]",
7041 .handler
= handle_resume_command
,
7042 .mode
= COMMAND_EXEC
,
7043 .help
= "resume target execution from current PC or address",
7044 .usage
= "[address]",
7048 .handler
= handle_reset_command
,
7049 .mode
= COMMAND_EXEC
,
7050 .usage
= "[run|halt|init]",
7051 .help
= "Reset all targets into the specified mode. "
7052 "Default reset mode is run, if not given.",
7055 .name
= "soft_reset_halt",
7056 .handler
= handle_soft_reset_halt_command
,
7057 .mode
= COMMAND_EXEC
,
7059 .help
= "halt the target and do a soft reset",
7063 .handler
= handle_step_command
,
7064 .mode
= COMMAND_EXEC
,
7065 .help
= "step one instruction from current PC or address",
7066 .usage
= "[address]",
7070 .handler
= handle_md_command
,
7071 .mode
= COMMAND_EXEC
,
7072 .help
= "display memory double-words",
7073 .usage
= "['phys'] address [count]",
7077 .handler
= handle_md_command
,
7078 .mode
= COMMAND_EXEC
,
7079 .help
= "display memory words",
7080 .usage
= "['phys'] address [count]",
7084 .handler
= handle_md_command
,
7085 .mode
= COMMAND_EXEC
,
7086 .help
= "display memory half-words",
7087 .usage
= "['phys'] address [count]",
7091 .handler
= handle_md_command
,
7092 .mode
= COMMAND_EXEC
,
7093 .help
= "display memory bytes",
7094 .usage
= "['phys'] address [count]",
7098 .handler
= handle_mw_command
,
7099 .mode
= COMMAND_EXEC
,
7100 .help
= "write memory double-word",
7101 .usage
= "['phys'] address value [count]",
7105 .handler
= handle_mw_command
,
7106 .mode
= COMMAND_EXEC
,
7107 .help
= "write memory word",
7108 .usage
= "['phys'] address value [count]",
7112 .handler
= handle_mw_command
,
7113 .mode
= COMMAND_EXEC
,
7114 .help
= "write memory half-word",
7115 .usage
= "['phys'] address value [count]",
7119 .handler
= handle_mw_command
,
7120 .mode
= COMMAND_EXEC
,
7121 .help
= "write memory byte",
7122 .usage
= "['phys'] address value [count]",
7126 .handler
= handle_bp_command
,
7127 .mode
= COMMAND_EXEC
,
7128 .help
= "list or set hardware or software breakpoint",
7129 .usage
= "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
7133 .handler
= handle_rbp_command
,
7134 .mode
= COMMAND_EXEC
,
7135 .help
= "remove breakpoint",
7136 .usage
= "'all' | address",
7140 .handler
= handle_wp_command
,
7141 .mode
= COMMAND_EXEC
,
7142 .help
= "list (no params) or create watchpoints",
7143 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
7147 .handler
= handle_rwp_command
,
7148 .mode
= COMMAND_EXEC
,
7149 .help
= "remove watchpoint",
7153 .name
= "load_image",
7154 .handler
= handle_load_image_command
,
7155 .mode
= COMMAND_EXEC
,
7156 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
7157 "[min_address] [max_length]",
7160 .name
= "dump_image",
7161 .handler
= handle_dump_image_command
,
7162 .mode
= COMMAND_EXEC
,
7163 .usage
= "filename address size",
7166 .name
= "verify_image_checksum",
7167 .handler
= handle_verify_image_checksum_command
,
7168 .mode
= COMMAND_EXEC
,
7169 .usage
= "filename [offset [type]]",
7172 .name
= "verify_image",
7173 .handler
= handle_verify_image_command
,
7174 .mode
= COMMAND_EXEC
,
7175 .usage
= "filename [offset [type]]",
7178 .name
= "test_image",
7179 .handler
= handle_test_image_command
,
7180 .mode
= COMMAND_EXEC
,
7181 .usage
= "filename [offset [type]]",
7185 .mode
= COMMAND_EXEC
,
7186 .jim_handler
= target_jim_get_reg
,
7187 .help
= "Get register values from the target",
7192 .mode
= COMMAND_EXEC
,
7193 .jim_handler
= target_jim_set_reg
,
7194 .help
= "Set target register values",
7198 .name
= "read_memory",
7199 .mode
= COMMAND_EXEC
,
7200 .jim_handler
= target_jim_read_memory
,
7201 .help
= "Read Tcl list of 8/16/32/64 bit numbers from target memory",
7202 .usage
= "address width count ['phys']",
7205 .name
= "write_memory",
7206 .mode
= COMMAND_EXEC
,
7207 .jim_handler
= target_jim_write_memory
,
7208 .help
= "Write Tcl list of 8/16/32/64 bit numbers to target memory",
7209 .usage
= "address width data ['phys']",
7212 .name
= "reset_nag",
7213 .handler
= handle_target_reset_nag
,
7214 .mode
= COMMAND_ANY
,
7215 .help
= "Nag after each reset about options that could have been "
7216 "enabled to improve performance.",
7217 .usage
= "['enable'|'disable']",
7221 .handler
= handle_ps_command
,
7222 .mode
= COMMAND_EXEC
,
7223 .help
= "list all tasks",
7227 .name
= "test_mem_access",
7228 .handler
= handle_test_mem_access_command
,
7229 .mode
= COMMAND_EXEC
,
7230 .help
= "Test the target's memory access functions",
7234 COMMAND_REGISTRATION_DONE
7236 static int target_register_user_commands(struct command_context
*cmd_ctx
)
7238 int retval
= ERROR_OK
;
7239 retval
= target_request_register_commands(cmd_ctx
);
7240 if (retval
!= ERROR_OK
)
7243 retval
= trace_register_commands(cmd_ctx
);
7244 if (retval
!= ERROR_OK
)
7248 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);