OpenOCD
target.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007-2010 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008, Duane Ellis *
11  * openocd@duaneeellis.com *
12  * *
13  * Copyright (C) 2008 by Spencer Oliver *
14  * spen@spen-soft.co.uk *
15  * *
16  * Copyright (C) 2008 by Rick Altherr *
17  * kc8apf@kc8apf.net> *
18  * *
19  * Copyright (C) 2011 by Broadcom Corporation *
20  * Evan Hunter - ehunter@broadcom.com *
21  * *
22  * Copyright (C) ST-Ericsson SA 2011 *
23  * michel.jaouen@stericsson.com : smp minimum support *
24  * *
25  * Copyright (C) 2011 Andreas Fritiofson *
26  * andreas.fritiofson@gmail.com *
27  ***************************************************************************/
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <helper/align.h>
34 #include <helper/list.h>
35 #include <helper/nvp.h>
36 #include <helper/time_support.h>
37 #include <jtag/jtag.h>
38 #include <flash/nor/core.h>
39 
40 #include "target.h"
41 #include "target_type.h"
42 #include "target_request.h"
43 #include "breakpoints.h"
44 #include "register.h"
45 #include "trace.h"
46 #include "image.h"
47 #include "rtos/rtos.h"
48 #include "transport/transport.h"
49 #include "arm_cti.h"
50 #include "smp.h"
51 #include "semihosting_common.h"
52 
53 /* default halt wait timeout (ms) */
54 #define DEFAULT_HALT_TIMEOUT 5000
55 
57  enum target_event event;
58  Jim_Interp *interp;
59  Jim_Obj *body;
60  struct list_head list;
61 };
62 
64  uint32_t count, uint8_t *buffer);
66  uint32_t count, const uint8_t *buffer);
67 static int target_register_user_commands(struct command_context *cmd_ctx);
69  struct gdb_fileio_info *fileio_info);
70 static int target_gdb_fileio_end_default(struct target *target, int retcode,
71  int fileio_errno, bool ctrl_c);
72 
73 static struct target_type *target_types[] = {
74  // Keep in alphabetic order this list of targets
76  &arcv2_target,
77  &arm11_target,
87  &avr_target,
97  &esp32_target,
98  &fa526_target,
100  &hla_target,
102  &mem_ap_target,
105  &or1k_target,
108  &riscv_target,
109  &stm8_target,
110  &testee_target,
111  &xscale_target,
113 };
114 
119 static OOCD_LIST_HEAD(target_reset_callback_list);
120 static OOCD_LIST_HEAD(target_trace_callback_list);
122 static OOCD_LIST_HEAD(empty_smp_targets);
123 
127 };
128 
129 static const struct nvp nvp_assert[] = {
130  { .name = "assert", NVP_ASSERT },
131  { .name = "deassert", NVP_DEASSERT },
132  { .name = "T", NVP_ASSERT },
133  { .name = "F", NVP_DEASSERT },
134  { .name = "t", NVP_ASSERT },
135  { .name = "f", NVP_DEASSERT },
136  { .name = NULL, .value = -1 }
137 };
138 
139 static const struct nvp nvp_error_target[] = {
140  { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
141  { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
142  { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
143  { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
144  { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
145  { .value = ERROR_TARGET_UNALIGNED_ACCESS, .name = "err-unaligned-access" },
146  { .value = ERROR_TARGET_DATA_ABORT, .name = "err-data-abort" },
147  { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE, .name = "err-resource-not-available" },
148  { .value = ERROR_TARGET_TRANSLATION_FAULT, .name = "err-translation-fault" },
149  { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
150  { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
151  { .value = -1, .name = NULL }
152 };
153 
154 static const char *target_strerror_safe(int err)
155 {
156  const struct nvp *n;
157 
159  if (!n->name)
160  return "unknown";
161  else
162  return n->name;
163 }
164 
165 static const struct nvp nvp_target_event[] = {
166 
167  { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
168  { .value = TARGET_EVENT_HALTED, .name = "halted" },
169  { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
170  { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
171  { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
172  { .value = TARGET_EVENT_STEP_START, .name = "step-start" },
173  { .value = TARGET_EVENT_STEP_END, .name = "step-end" },
174 
175  { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
176  { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
177 
178  { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
179  { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
180  { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
181  { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
182  { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
183  { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
184  { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
185  { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
186 
187  { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
188  { .value = TARGET_EVENT_EXAMINE_FAIL, .name = "examine-fail" },
189  { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
190 
191  { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
192  { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
193 
194  { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
195  { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
196 
197  { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
198  { .value = TARGET_EVENT_GDB_FLASH_WRITE_END, .name = "gdb-flash-write-end" },
199 
200  { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
201  { .value = TARGET_EVENT_GDB_FLASH_ERASE_END, .name = "gdb-flash-erase-end" },
202 
203  { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
204 
205  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100, .name = "semihosting-user-cmd-0x100" },
206  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101, .name = "semihosting-user-cmd-0x101" },
207  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102, .name = "semihosting-user-cmd-0x102" },
208  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103, .name = "semihosting-user-cmd-0x103" },
209  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104, .name = "semihosting-user-cmd-0x104" },
210  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105, .name = "semihosting-user-cmd-0x105" },
211  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106, .name = "semihosting-user-cmd-0x106" },
212  { .value = TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107, .name = "semihosting-user-cmd-0x107" },
213 
214  { .name = NULL, .value = -1 }
215 };
216 
217 static const struct nvp nvp_target_state[] = {
218  { .name = "unknown", .value = TARGET_UNKNOWN },
219  { .name = "running", .value = TARGET_RUNNING },
220  { .name = "halted", .value = TARGET_HALTED },
221  { .name = "reset", .value = TARGET_RESET },
222  { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
223  { .name = "unavailable", .value = TARGET_UNAVAILABLE },
224  { .name = NULL, .value = -1 },
225 };
226 
227 static const struct nvp nvp_target_debug_reason[] = {
228  { .name = "debug-request", .value = DBG_REASON_DBGRQ },
229  { .name = "breakpoint", .value = DBG_REASON_BREAKPOINT },
230  { .name = "watchpoint", .value = DBG_REASON_WATCHPOINT },
231  { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
232  { .name = "single-step", .value = DBG_REASON_SINGLESTEP },
233  { .name = "target-not-halted", .value = DBG_REASON_NOTHALTED },
234  { .name = "program-exit", .value = DBG_REASON_EXIT },
235  { .name = "exception-catch", .value = DBG_REASON_EXC_CATCH },
236  { .name = "undefined", .value = DBG_REASON_UNDEFINED },
237  { .name = NULL, .value = -1 },
238 };
239 
240 static const struct nvp nvp_target_endian[] = {
241  { .name = "big", .value = TARGET_BIG_ENDIAN },
242  { .name = "little", .value = TARGET_LITTLE_ENDIAN },
243  { .name = "be", .value = TARGET_BIG_ENDIAN },
244  { .name = "le", .value = TARGET_LITTLE_ENDIAN },
245  { .name = NULL, .value = -1 },
246 };
247 
248 static const struct nvp nvp_reset_modes[] = {
249  { .name = "unknown", .value = RESET_UNKNOWN },
250  { .name = "run", .value = RESET_RUN },
251  { .name = "halt", .value = RESET_HALT },
252  { .name = "init", .value = RESET_INIT },
253  { .name = NULL, .value = -1 },
254 };
255 
256 const char *debug_reason_name(const struct target *t)
257 {
258  const char *cp;
259 
261  t->debug_reason)->name;
262  if (!cp) {
263  LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
264  cp = "(*BUG*unknown*BUG*)";
265  }
266  return cp;
267 }
268 
269 const char *target_state_name(const struct target *t)
270 {
271  const char *cp;
273  if (!cp) {
274  LOG_ERROR("Invalid target state: %d", (int)(t->state));
275  cp = "(*BUG*unknown*BUG*)";
276  }
277 
278  if (!target_was_examined(t) && t->defer_examine)
279  cp = "examine deferred";
280 
281  return cp;
282 }
283 
284 const char *target_event_name(enum target_event event)
285 {
286  const char *cp;
287  cp = nvp_value2name(nvp_target_event, event)->name;
288  if (!cp) {
289  LOG_ERROR("Invalid target event: %d", (int)(event));
290  cp = "(*BUG*unknown*BUG*)";
291  }
292  return cp;
293 }
294 
295 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
296 {
297  const char *cp;
298  cp = nvp_value2name(nvp_reset_modes, reset_mode)->name;
299  if (!cp) {
300  LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
301  cp = "(*BUG*unknown*BUG*)";
302  }
303  return cp;
304 }
305 
307 {
308  struct target **t = &all_targets;
309 
310  while (*t)
311  t = &((*t)->next);
312  *t = target;
313 }
314 
315 /* read a uint64_t from a buffer in target memory endianness */
316 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
317 {
319  return le_to_h_u64(buffer);
320  else
321  return be_to_h_u64(buffer);
322 }
323 
324 /* read a uint32_t from a buffer in target memory endianness */
325 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
326 {
328  return le_to_h_u32(buffer);
329  else
330  return be_to_h_u32(buffer);
331 }
332 
333 /* read a uint24_t from a buffer in target memory endianness */
334 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
335 {
337  return le_to_h_u24(buffer);
338  else
339  return be_to_h_u24(buffer);
340 }
341 
342 /* read a uint16_t from a buffer in target memory endianness */
343 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
344 {
346  return le_to_h_u16(buffer);
347  else
348  return be_to_h_u16(buffer);
349 }
350 
351 /* write a uint64_t to a buffer in target memory endianness */
352 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
353 {
355  h_u64_to_le(buffer, value);
356  else
357  h_u64_to_be(buffer, value);
358 }
359 
360 /* write a uint32_t to a buffer in target memory endianness */
361 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
362 {
364  h_u32_to_le(buffer, value);
365  else
366  h_u32_to_be(buffer, value);
367 }
368 
369 /* write a uint24_t to a buffer in target memory endianness */
370 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
371 {
373  h_u24_to_le(buffer, value);
374  else
375  h_u24_to_be(buffer, value);
376 }
377 
378 /* write a uint16_t to a buffer in target memory endianness */
379 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
380 {
382  h_u16_to_le(buffer, value);
383  else
384  h_u16_to_be(buffer, value);
385 }
386 
387 /* write a uint8_t to a buffer in target memory endianness */
388 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
389 {
390  *buffer = value;
391 }
392 
393 /* write a uint64_t array to a buffer in target memory endianness */
394 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
395 {
396  uint32_t i;
397  for (i = 0; i < count; i++)
398  dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
399 }
400 
401 /* write a uint32_t array to a buffer in target memory endianness */
402 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
403 {
404  uint32_t i;
405  for (i = 0; i < count; i++)
406  dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
407 }
408 
409 /* write a uint16_t array to a buffer in target memory endianness */
410 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
411 {
412  uint32_t i;
413  for (i = 0; i < count; i++)
414  dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
415 }
416 
417 /* write a uint64_t array to a buffer in target memory endianness */
418 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
419 {
420  uint32_t i;
421  for (i = 0; i < count; i++)
422  target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
423 }
424 
425 /* write a uint32_t array to a buffer in target memory endianness */
426 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
427 {
428  uint32_t i;
429  for (i = 0; i < count; i++)
430  target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
431 }
432 
433 /* write a uint16_t array to a buffer in target memory endianness */
434 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
435 {
436  uint32_t i;
437  for (i = 0; i < count; i++)
438  target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
439 }
440 
441 /* return a pointer to a configured target; id is name or index in all_targets */
442 struct target *get_target(const char *id)
443 {
444  struct target *target;
445 
446  /* try as tcltarget name */
447  for (target = all_targets; target; target = target->next) {
448  if (!target_name(target))
449  continue;
450  if (strcmp(id, target_name(target)) == 0)
451  return target;
452  }
453 
454  /* try as index */
455  unsigned int index, counter;
456  if (parse_uint(id, &index) != ERROR_OK)
457  return NULL;
458 
459  for (target = all_targets, counter = index;
460  target && counter;
461  target = target->next, --counter)
462  ;
463 
464  return target;
465 }
466 
467 struct target *get_current_target(struct command_context *cmd_ctx)
468 {
469  struct target *target = get_current_target_or_null(cmd_ctx);
470 
471  if (!target) {
472  LOG_ERROR("BUG: current_target out of bounds");
473  exit(-1);
474  }
475 
476  return target;
477 }
478 
480 {
481  return cmd_ctx->current_target_override
482  ? cmd_ctx->current_target_override
483  : cmd_ctx->current_target;
484 }
485 
487 {
488  int retval;
489 
490  /* We can't poll until after examine */
491  if (!target_was_examined(target)) {
492  /* Fail silently lest we pollute the log */
494  }
495 
496  retval = target->type->poll(target);
497  if (retval != ERROR_OK)
498  return retval;
499 
500  if (target->halt_issued) {
501  if (target->state == TARGET_HALTED)
502  target->halt_issued = false;
503  else {
504  int64_t t = timeval_ms() - target->halt_issued_time;
505  if (t > DEFAULT_HALT_TIMEOUT) {
506  target->halt_issued = false;
507  LOG_INFO("Halt timed out, wake up GDB.");
509  }
510  }
511  }
512 
513  return ERROR_OK;
514 }
515 
517 {
518  int retval;
519 
520  if (!target_was_examined(target)) {
521  LOG_TARGET_ERROR(target, "not examined");
523  }
524 
525  retval = target->type->halt(target);
526  if (retval != ERROR_OK)
527  return retval;
528 
529  target->halt_issued = true;
531 
532  return ERROR_OK;
533 }
534 
565 int target_resume(struct target *target, bool current, target_addr_t address,
566  bool handle_breakpoints, bool debug_execution)
567 {
568  int retval;
569 
570  if (!target_was_examined(target)) {
571  LOG_TARGET_ERROR(target, "not examined");
573  }
574 
576 
577  /* note that resume *must* be asynchronous. The CPU can halt before
578  * we poll. The CPU can even halt at the current PC as a result of
579  * a software breakpoint being inserted by (a bug?) the application.
580  */
581  /*
582  * resume() triggers the event 'resumed'. The execution of TCL commands
583  * in the event handler causes the polling of targets. If the target has
584  * already halted for a breakpoint, polling will run the 'halted' event
585  * handler before the pending 'resumed' handler.
586  * Disable polling during resume() to guarantee the execution of handlers
587  * in the correct order.
588  */
589  bool save_poll_mask = jtag_poll_mask();
590  retval = target->type->resume(target, current, address, handle_breakpoints,
591  debug_execution);
592  jtag_poll_unmask(save_poll_mask);
593 
594  if (retval != ERROR_OK)
595  return retval;
596 
598 
599  return retval;
600 }
601 
602 static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
603 {
604  char buf[100];
605  int retval;
606  const struct nvp *n;
607  n = nvp_value2name(nvp_reset_modes, reset_mode);
608  if (!n->name) {
609  LOG_ERROR("invalid reset mode");
610  return ERROR_FAIL;
611  }
612 
613  struct target *target;
615  target_call_reset_callbacks(target, reset_mode);
616 
617  /* disable polling during reset to make reset event scripts
618  * more predictable, i.e. dr/irscan & pathmove in events will
619  * not have JTAG operations injected into the middle of a sequence.
620  */
621  bool save_poll_mask = jtag_poll_mask();
622 
623  sprintf(buf, "ocd_process_reset %s", n->name);
624  retval = Jim_Eval(cmd->ctx->interp, buf);
625 
626  jtag_poll_unmask(save_poll_mask);
627 
628  if (retval != JIM_OK) {
629  Jim_MakeErrorMessage(cmd->ctx->interp);
630  command_print(cmd, "%s", Jim_GetString(Jim_GetResult(cmd->ctx->interp), NULL));
631  return ERROR_FAIL;
632  }
633 
634  /* We want any events to be processed before the prompt */
636 
637  for (target = all_targets; target; target = target->next) {
639  target->running_alg = false;
640  }
641 
642  return retval;
643 }
644 
645 static int identity_virt2phys(struct target *target,
646  target_addr_t virtual, target_addr_t *physical)
647 {
648  *physical = virtual;
649  return ERROR_OK;
650 }
651 
652 static int no_mmu(struct target *target, bool *enabled)
653 {
654  *enabled = false;
655  return ERROR_OK;
656 }
657 
661 static inline void target_reset_examined(struct target *target)
662 {
663  target->examined = false;
664 }
665 
666 static inline void target_reset_active_polled(struct target *target)
667 {
668  target->active_polled = false;
669 }
670 
671 static int default_examine(struct target *target)
672 {
673  return ERROR_OK;
674 }
675 
676 /* no check by default */
677 static int default_check_reset(struct target *target)
678 {
679  return ERROR_OK;
680 }
681 
682 /* Equivalent Tcl code arp_examine_one is in src/target/startup.tcl
683  * Keep in sync */
685 {
686  LOG_TARGET_DEBUG(target, "Examination started");
687 
689 
690  int retval = target->type->examine(target);
691  if (retval != ERROR_OK) {
692  LOG_TARGET_ERROR(target, "Examination failed");
693  LOG_TARGET_DEBUG(target, "examine() returned error code %d", retval);
696  return retval;
697  }
698 
701 
702  LOG_TARGET_INFO(target, "Examination succeed");
703  return ERROR_OK;
704 }
705 
706 static int jtag_enable_callback(enum jtag_event event, void *priv)
707 {
708  struct target *target = priv;
709 
710  if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
711  return ERROR_OK;
712 
714 
715  return target_examine_one(target);
716 }
717 
718 /* Targets that correctly implement init + examine, i.e.
719  * no communication with target during init:
720  *
721  * XScale
722  */
723 int target_examine(void)
724 {
725  int retval = ERROR_OK;
726  struct target *target;
727 
728  for (target = all_targets; target; target = target->next) {
729  /* defer examination, but don't skip it */
730  if (!target->tap->enabled) {
732  target);
733  continue;
734  }
735 
736  if (target->defer_examine)
737  continue;
738 
739  int retval2 = target_examine_one(target);
740  if (retval2 != ERROR_OK) {
741  LOG_WARNING("target %s examination failed", target_name(target));
742  retval = retval2;
743  }
744  }
745  return retval;
746 }
747 
748 const char *target_type_name(const struct target *target)
749 {
750  return target->type->name;
751 }
752 
754 {
755  if (!target_was_examined(target)) {
756  LOG_TARGET_ERROR(target, "not examined");
758  }
759  if (!target->type->soft_reset_halt) {
760  LOG_ERROR("Target %s does not support soft_reset_halt",
762  return ERROR_FAIL;
763  }
764  return target->type->soft_reset_halt(target);
765 }
766 
786  int num_mem_params, struct mem_param *mem_params,
787  int num_reg_params, struct reg_param *reg_param,
788  target_addr_t entry_point, target_addr_t exit_point,
789  unsigned int timeout_ms, void *arch_info)
790 {
791  int retval = ERROR_FAIL;
792 
793  if (!target_was_examined(target)) {
794  LOG_TARGET_ERROR(target, "not examined");
795  retval = ERROR_TARGET_NOT_EXAMINED;
796  goto done;
797  }
798  if (!target->type->run_algorithm) {
799  LOG_ERROR("Target type '%s' does not support %s",
800  target_type_name(target), __func__);
801  goto done;
802  }
803 
804  target->running_alg = true;
805  retval = target->type->run_algorithm(target,
806  num_mem_params, mem_params,
807  num_reg_params, reg_param,
808  entry_point, exit_point, timeout_ms, arch_info);
809  target->running_alg = false;
810 
811 done:
812  return retval;
813 }
814 
828  int num_mem_params, struct mem_param *mem_params,
829  int num_reg_params, struct reg_param *reg_params,
830  target_addr_t entry_point, target_addr_t exit_point,
831  void *arch_info)
832 {
833  int retval = ERROR_FAIL;
834 
835  if (!target_was_examined(target)) {
836  LOG_TARGET_ERROR(target, "not examined");
837  retval = ERROR_TARGET_NOT_EXAMINED;
838  goto done;
839  }
840  if (!target->type->start_algorithm) {
841  LOG_ERROR("Target type '%s' does not support %s",
842  target_type_name(target), __func__);
843  goto done;
844  }
845  if (target->running_alg) {
846  LOG_ERROR("Target is already running an algorithm");
847  goto done;
848  }
849 
850  target->running_alg = true;
851  retval = target->type->start_algorithm(target,
852  num_mem_params, mem_params,
853  num_reg_params, reg_params,
854  entry_point, exit_point, arch_info);
855 
856 done:
857  return retval;
858 }
859 
873  int num_mem_params, struct mem_param *mem_params,
874  int num_reg_params, struct reg_param *reg_params,
875  target_addr_t exit_point, unsigned int timeout_ms,
876  void *arch_info)
877 {
878  int retval = ERROR_FAIL;
879 
880  if (!target->type->wait_algorithm) {
881  LOG_ERROR("Target type '%s' does not support %s",
882  target_type_name(target), __func__);
883  goto done;
884  }
885  if (!target->running_alg) {
886  LOG_ERROR("Target is not running an algorithm");
887  goto done;
888  }
889 
890  retval = target->type->wait_algorithm(target,
891  num_mem_params, mem_params,
892  num_reg_params, reg_params,
893  exit_point, timeout_ms, arch_info);
894  if (retval != ERROR_TARGET_TIMEOUT)
895  target->running_alg = false;
896 
897 done:
898  return retval;
899 }
900 
945  const uint8_t *buffer, uint32_t count, int block_size,
946  int num_mem_params, struct mem_param *mem_params,
947  int num_reg_params, struct reg_param *reg_params,
948  uint32_t buffer_start, uint32_t buffer_size,
949  uint32_t entry_point, uint32_t exit_point, void *arch_info)
950 {
951  int retval;
952  int timeout = 0;
953 
954  const uint8_t *buffer_orig = buffer;
955 
956  /* Set up working area. First word is write pointer, second word is read pointer,
957  * rest is fifo data area. */
958  uint32_t wp_addr = buffer_start;
959  uint32_t rp_addr = buffer_start + 4;
960  uint32_t fifo_start_addr = buffer_start + 8;
961  uint32_t fifo_end_addr = buffer_start + buffer_size;
962 
963  uint32_t wp = fifo_start_addr;
964  uint32_t rp = fifo_start_addr;
965 
966  /* validate block_size is 2^n */
967  assert(IS_PWR_OF_2(block_size));
968 
969  retval = target_write_u32(target, wp_addr, wp);
970  if (retval != ERROR_OK)
971  return retval;
972  retval = target_write_u32(target, rp_addr, rp);
973  if (retval != ERROR_OK)
974  return retval;
975 
976  /* Start up algorithm on target and let it idle while writing the first chunk */
977  retval = target_start_algorithm(target, num_mem_params, mem_params,
978  num_reg_params, reg_params,
979  entry_point,
980  exit_point,
981  arch_info);
982 
983  if (retval != ERROR_OK) {
984  LOG_ERROR("error starting target flash write algorithm");
985  return retval;
986  }
987 
988  while (count > 0) {
989 
990  retval = target_read_u32(target, rp_addr, &rp);
991  if (retval != ERROR_OK) {
992  LOG_ERROR("failed to get read pointer");
993  break;
994  }
995 
996  LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
997  (size_t) (buffer - buffer_orig), count, wp, rp);
998 
999  if (rp == 0) {
1000  LOG_ERROR("flash write algorithm aborted by target");
1002  break;
1003  }
1004 
1005  if (!IS_ALIGNED(rp - fifo_start_addr, block_size) || rp < fifo_start_addr || rp >= fifo_end_addr) {
1006  LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
1007  break;
1008  }
1009 
1010  /* Count the number of bytes available in the fifo without
1011  * crossing the wrap around. Make sure to not fill it completely,
1012  * because that would make wp == rp and that's the empty condition. */
1013  uint32_t thisrun_bytes;
1014  if (rp > wp)
1015  thisrun_bytes = rp - wp - block_size;
1016  else if (rp > fifo_start_addr)
1017  thisrun_bytes = fifo_end_addr - wp;
1018  else
1019  thisrun_bytes = fifo_end_addr - wp - block_size;
1020 
1021  if (thisrun_bytes == 0) {
1022  /* Throttle polling a bit if transfer is (much) faster than flash
1023  * programming. The exact delay shouldn't matter as long as it's
1024  * less than buffer size / flash speed. This is very unlikely to
1025  * run when using high latency connections such as USB. */
1026  alive_sleep(2);
1027 
1028  /* to stop an infinite loop on some targets check and increment a timeout
1029  * this issue was observed on a stellaris using the new ICDI interface */
1030  if (timeout++ >= 2500) {
1031  LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1033  }
1034  continue;
1035  }
1036 
1037  /* reset our timeout */
1038  timeout = 0;
1039 
1040  /* Limit to the amount of data we actually want to write */
1041  if (thisrun_bytes > count * block_size)
1042  thisrun_bytes = count * block_size;
1043 
1044  /* Force end of large blocks to be word aligned */
1045  if (thisrun_bytes >= 16)
1046  thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
1047 
1048  /* Write data to fifo */
1049  retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
1050  if (retval != ERROR_OK)
1051  break;
1052 
1053  /* Update counters and wrap write pointer */
1054  buffer += thisrun_bytes;
1055  count -= thisrun_bytes / block_size;
1056  wp += thisrun_bytes;
1057  if (wp >= fifo_end_addr)
1058  wp = fifo_start_addr;
1059 
1060  /* Store updated write pointer to target */
1061  retval = target_write_u32(target, wp_addr, wp);
1062  if (retval != ERROR_OK)
1063  break;
1064 
1065  /* Avoid GDB timeouts */
1066  keep_alive();
1067  }
1068 
1069  if (retval != ERROR_OK) {
1070  /* abort flash write algorithm on target */
1071  target_write_u32(target, wp_addr, 0);
1072  }
1073 
1074  int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1075  num_reg_params, reg_params,
1076  exit_point,
1077  10000,
1078  arch_info);
1079 
1080  if (retval2 != ERROR_OK) {
1081  LOG_ERROR("error waiting for target flash write algorithm");
1082  retval = retval2;
1083  }
1084 
1085  if (retval == ERROR_OK) {
1086  /* check if algorithm set rp = 0 after fifo writer loop finished */
1087  retval = target_read_u32(target, rp_addr, &rp);
1088  if (retval == ERROR_OK && rp == 0) {
1089  LOG_ERROR("flash write algorithm aborted by target");
1091  }
1092  }
1093 
1094  return retval;
1095 }
1096 
1098  uint8_t *buffer, uint32_t count, int block_size,
1099  int num_mem_params, struct mem_param *mem_params,
1100  int num_reg_params, struct reg_param *reg_params,
1101  uint32_t buffer_start, uint32_t buffer_size,
1102  uint32_t entry_point, uint32_t exit_point, void *arch_info)
1103 {
1104  int retval;
1105  int timeout = 0;
1106 
1107  const uint8_t *buffer_orig = buffer;
1108 
1109  /* Set up working area. First word is write pointer, second word is read pointer,
1110  * rest is fifo data area. */
1111  uint32_t wp_addr = buffer_start;
1112  uint32_t rp_addr = buffer_start + 4;
1113  uint32_t fifo_start_addr = buffer_start + 8;
1114  uint32_t fifo_end_addr = buffer_start + buffer_size;
1115 
1116  uint32_t wp = fifo_start_addr;
1117  uint32_t rp = fifo_start_addr;
1118 
1119  /* validate block_size is 2^n */
1120  assert(IS_PWR_OF_2(block_size));
1121 
1122  retval = target_write_u32(target, wp_addr, wp);
1123  if (retval != ERROR_OK)
1124  return retval;
1125  retval = target_write_u32(target, rp_addr, rp);
1126  if (retval != ERROR_OK)
1127  return retval;
1128 
1129  /* Start up algorithm on target */
1130  retval = target_start_algorithm(target, num_mem_params, mem_params,
1131  num_reg_params, reg_params,
1132  entry_point,
1133  exit_point,
1134  arch_info);
1135 
1136  if (retval != ERROR_OK) {
1137  LOG_ERROR("error starting target flash read algorithm");
1138  return retval;
1139  }
1140 
1141  while (count > 0) {
1142  retval = target_read_u32(target, wp_addr, &wp);
1143  if (retval != ERROR_OK) {
1144  LOG_ERROR("failed to get write pointer");
1145  break;
1146  }
1147 
1148  LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
1149  (size_t)(buffer - buffer_orig), count, wp, rp);
1150 
1151  if (wp == 0) {
1152  LOG_ERROR("flash read algorithm aborted by target");
1154  break;
1155  }
1156 
1157  if (!IS_ALIGNED(wp - fifo_start_addr, block_size) || wp < fifo_start_addr || wp >= fifo_end_addr) {
1158  LOG_ERROR("corrupted fifo write pointer 0x%" PRIx32, wp);
1159  break;
1160  }
1161 
1162  /* Count the number of bytes available in the fifo without
1163  * crossing the wrap around. */
1164  uint32_t thisrun_bytes;
1165  if (wp >= rp)
1166  thisrun_bytes = wp - rp;
1167  else
1168  thisrun_bytes = fifo_end_addr - rp;
1169 
1170  if (thisrun_bytes == 0) {
1171  /* Throttle polling a bit if transfer is (much) faster than flash
1172  * reading. The exact delay shouldn't matter as long as it's
1173  * less than buffer size / flash speed. This is very unlikely to
1174  * run when using high latency connections such as USB. */
1175  alive_sleep(2);
1176 
1177  /* to stop an infinite loop on some targets check and increment a timeout
1178  * this issue was observed on a stellaris using the new ICDI interface */
1179  if (timeout++ >= 2500) {
1180  LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1182  }
1183  continue;
1184  }
1185 
1186  /* Reset our timeout */
1187  timeout = 0;
1188 
1189  /* Limit to the amount of data we actually want to read */
1190  if (thisrun_bytes > count * block_size)
1191  thisrun_bytes = count * block_size;
1192 
1193  /* Force end of large blocks to be word aligned */
1194  if (thisrun_bytes >= 16)
1195  thisrun_bytes -= (rp + thisrun_bytes) & 0x03;
1196 
1197  /* Read data from fifo */
1198  retval = target_read_buffer(target, rp, thisrun_bytes, buffer);
1199  if (retval != ERROR_OK)
1200  break;
1201 
1202  /* Update counters and wrap write pointer */
1203  buffer += thisrun_bytes;
1204  count -= thisrun_bytes / block_size;
1205  rp += thisrun_bytes;
1206  if (rp >= fifo_end_addr)
1207  rp = fifo_start_addr;
1208 
1209  /* Store updated write pointer to target */
1210  retval = target_write_u32(target, rp_addr, rp);
1211  if (retval != ERROR_OK)
1212  break;
1213 
1214  /* Avoid GDB timeouts */
1215  keep_alive();
1216 
1218  retval = ERROR_SERVER_INTERRUPTED;
1219  break;
1220  }
1221  }
1222 
1223  if (retval != ERROR_OK) {
1224  /* abort flash write algorithm on target */
1225  target_write_u32(target, rp_addr, 0);
1226  }
1227 
1228  int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1229  num_reg_params, reg_params,
1230  exit_point,
1231  10000,
1232  arch_info);
1233 
1234  if (retval2 != ERROR_OK) {
1235  LOG_ERROR("error waiting for target flash write algorithm");
1236  retval = retval2;
1237  }
1238 
1239  if (retval == ERROR_OK) {
1240  /* check if algorithm set wp = 0 after fifo writer loop finished */
1241  retval = target_read_u32(target, wp_addr, &wp);
1242  if (retval == ERROR_OK && wp == 0) {
1243  LOG_ERROR("flash read algorithm aborted by target");
1245  }
1246  }
1247 
1248  return retval;
1249 }
1250 
1252 {
1253  if (target->type->memory_ready)
1254  return target->type->memory_ready(target);
1255 
1256  return target_was_examined(target);
1257 }
1258 
1260  target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1261 {
1262  if (!target_memory_ready(target)) {
1263  LOG_TARGET_ERROR(target, "Memory not ready");
1264  return ERROR_FAIL;
1265  }
1266  if (!target->type->read_memory) {
1267  LOG_TARGET_ERROR(target, "doesn't support read_memory");
1268  return ERROR_FAIL;
1269  }
1271 }
1272 
1274  target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1275 {
1276  if (!target_memory_ready(target)) {
1277  LOG_TARGET_ERROR(target, "Memory not ready");
1278  return ERROR_FAIL;
1279  }
1280  if (!target->type->read_phys_memory) {
1281  LOG_TARGET_ERROR(target, "doesn't support read_phys_memory");
1282  return ERROR_FAIL;
1283  }
1285 }
1286 
1288  target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1289 {
1290  if (!target_memory_ready(target)) {
1291  LOG_TARGET_ERROR(target, "Memory not ready");
1292  return ERROR_FAIL;
1293  }
1294  if (!target->type->write_memory) {
1295  LOG_TARGET_ERROR(target, "doesn't support write_memory");
1296  return ERROR_FAIL;
1297  }
1299 }
1300 
1302  target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1303 {
1304  if (!target_memory_ready(target)) {
1305  LOG_TARGET_ERROR(target, "Memory not ready");
1306  return ERROR_FAIL;
1307  }
1308  if (!target->type->write_phys_memory) {
1309  LOG_TARGET_ERROR(target, "doesn't support write_phys_memory");
1310  return ERROR_FAIL;
1311  }
1313 }
1314 
1316  struct breakpoint *breakpoint)
1317 {
1318  if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1319  LOG_TARGET_ERROR(target, "not halted (add breakpoint)");
1320  return ERROR_TARGET_NOT_HALTED;
1321  }
1323 }
1324 
1326  struct breakpoint *breakpoint)
1327 {
1328  if (target->state != TARGET_HALTED) {
1329  LOG_TARGET_ERROR(target, "not halted (add context breakpoint)");
1330  return ERROR_TARGET_NOT_HALTED;
1331  }
1333 }
1334 
1336  struct breakpoint *breakpoint)
1337 {
1338  if (target->state != TARGET_HALTED) {
1339  LOG_TARGET_ERROR(target, "not halted (add hybrid breakpoint)");
1340  return ERROR_TARGET_NOT_HALTED;
1341  }
1343 }
1344 
1346  struct breakpoint *breakpoint)
1347 {
1349 }
1350 
1352  struct watchpoint *watchpoint)
1353 {
1354  if (target->state != TARGET_HALTED) {
1355  LOG_TARGET_ERROR(target, "not halted (add watchpoint)");
1356  return ERROR_TARGET_NOT_HALTED;
1357  }
1359 }
1361  struct watchpoint *watchpoint)
1362 {
1364 }
1366  struct watchpoint **hit_watchpoint)
1367 {
1368  if (target->state != TARGET_HALTED) {
1369  LOG_TARGET_ERROR(target, "not halted (hit watchpoint)");
1370  return ERROR_TARGET_NOT_HALTED;
1371  }
1372 
1373  if (!target->type->hit_watchpoint) {
1374  /* For backward compatible, if hit_watchpoint is not implemented,
1375  * return error such that gdb_server will not take the nonsense
1376  * information. */
1377  return ERROR_NOT_IMPLEMENTED;
1378  }
1379 
1380  return target->type->hit_watchpoint(target, hit_watchpoint);
1381 }
1382 
1383 const char *target_get_gdb_arch(const struct target *target)
1384 {
1385  if (!target->type->get_gdb_arch)
1386  return NULL;
1387  return target->type->get_gdb_arch(target);
1388 }
1389 
1391  struct reg **reg_list[], int *reg_list_size,
1392  enum target_register_class reg_class)
1393 {
1394  int result = ERROR_FAIL;
1395 
1396  if (!target_was_examined(target)) {
1397  LOG_TARGET_ERROR(target, "not examined");
1398  result = ERROR_TARGET_NOT_EXAMINED;
1399  goto done;
1400  }
1401 
1402  result = target->type->get_gdb_reg_list(target, reg_list,
1403  reg_list_size, reg_class);
1404 
1405 done:
1406  if (result != ERROR_OK) {
1407  *reg_list = NULL;
1408  *reg_list_size = 0;
1409  }
1410  return result;
1411 }
1412 
1414  struct reg **reg_list[], int *reg_list_size,
1415  enum target_register_class reg_class)
1416 {
1419  reg_list_size, reg_class) == ERROR_OK)
1420  return ERROR_OK;
1421  return target_get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1422 }
1423 
1425 {
1426  /*
1427  * exclude all the targets that don't provide get_gdb_reg_list
1428  * or that have explicit gdb_max_connection == 0
1429  */
1431 }
1432 
1434  bool current, target_addr_t address, bool handle_breakpoints)
1435 {
1436  int retval;
1437 
1438  if (!target_was_examined(target)) {
1439  LOG_TARGET_ERROR(target, "not examined");
1441  }
1442 
1444 
1445  retval = target->type->step(target, current, address, handle_breakpoints);
1446  if (retval != ERROR_OK)
1447  return retval;
1448 
1450 
1451  return retval;
1452 }
1453 
1455 {
1456  if (target->state != TARGET_HALTED) {
1457  LOG_TARGET_ERROR(target, "not halted (gdb fileio)");
1458  return ERROR_TARGET_NOT_HALTED;
1459  }
1461 }
1462 
1463 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1464 {
1465  if (target->state != TARGET_HALTED) {
1466  LOG_TARGET_ERROR(target, "not halted (gdb fileio end)");
1467  return ERROR_TARGET_NOT_HALTED;
1468  }
1469  return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1470 }
1471 
1473 {
1474  unsigned int bits = target_address_bits(target);
1475  if (sizeof(target_addr_t) * 8 == bits)
1476  return (target_addr_t) -1;
1477  else
1478  return (((target_addr_t) 1) << bits) - 1;
1479 }
1480 
1481 unsigned int target_address_bits(struct target *target)
1482 {
1483  if (target->type->address_bits)
1484  return target->type->address_bits(target);
1485  return 32;
1486 }
1487 
1488 unsigned int target_data_bits(struct target *target)
1489 {
1490  if (target->type->data_bits)
1491  return target->type->data_bits(target);
1492  return 32;
1493 }
1494 
1495 static int target_profiling(struct target *target, uint32_t *samples,
1496  uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1497 {
1498  return target->type->profiling(target, samples, max_num_samples,
1499  num_samples, seconds);
1500 }
1501 
1502 static int handle_target(void *priv);
1503 
1504 static int target_init_one(struct command_context *cmd_ctx,
1505  struct target *target)
1506 {
1509 
1510  struct target_type *type = target->type;
1511  if (!type->examine)
1512  type->examine = default_examine;
1513 
1514  if (!type->check_reset)
1515  type->check_reset = default_check_reset;
1516 
1517  assert(type->init_target);
1518 
1519  int retval = type->init_target(cmd_ctx, target);
1520  if (retval != ERROR_OK) {
1521  LOG_ERROR("target '%s' init failed", target_name(target));
1522  return retval;
1523  }
1524 
1525  /* Sanity-check MMU support ... stub in what we must, to help
1526  * implement it in stages, but warn if we need to do so.
1527  */
1528  if (type->mmu) {
1529  if (!type->virt2phys) {
1530  LOG_ERROR("type '%s' is missing virt2phys", target_name(target));
1531  type->virt2phys = identity_virt2phys;
1532  }
1533  } else {
1534  /* Make sure no-MMU targets all behave the same: make no
1535  * distinction between physical and virtual addresses, and
1536  * ensure that virt2phys() is always an identity mapping.
1537  */
1538  if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1539  LOG_WARNING("type '%s' has bad MMU hooks", target_name(target));
1540 
1541  type->mmu = no_mmu;
1542  type->write_phys_memory = type->write_memory;
1543  type->read_phys_memory = type->read_memory;
1544  type->virt2phys = identity_virt2phys;
1545  }
1546 
1547  if (!target->type->read_buffer)
1549 
1550  if (!target->type->write_buffer)
1552 
1555 
1556  if (!target->type->gdb_fileio_end)
1558 
1559  if (!target->type->profiling)
1561 
1562  return ERROR_OK;
1563 }
1564 
1565 static int target_init(struct command_context *cmd_ctx)
1566 {
1567  struct target *target;
1568  int retval;
1569 
1570  for (target = all_targets; target; target = target->next) {
1571  retval = target_init_one(cmd_ctx, target);
1572  if (retval != ERROR_OK)
1573  return retval;
1574  }
1575 
1576  if (!all_targets)
1577  return ERROR_OK;
1578 
1579  retval = target_register_user_commands(cmd_ctx);
1580  if (retval != ERROR_OK)
1581  return retval;
1582 
1585  if (retval != ERROR_OK)
1586  return retval;
1587 
1588  return ERROR_OK;
1589 }
1590 
1591 COMMAND_HANDLER(handle_target_init_command)
1592 {
1593  int retval;
1594 
1595  if (CMD_ARGC != 0)
1597 
1598  static bool target_initialized;
1599  if (target_initialized) {
1600  LOG_INFO("'target init' has already been called");
1601  return ERROR_OK;
1602  }
1603  target_initialized = true;
1604 
1605  retval = command_run_line(CMD_CTX, "init_targets");
1606  if (retval != ERROR_OK)
1607  return retval;
1608 
1609  retval = command_run_line(CMD_CTX, "init_target_events");
1610  if (retval != ERROR_OK)
1611  return retval;
1612 
1613  retval = command_run_line(CMD_CTX, "init_board");
1614  if (retval != ERROR_OK)
1615  return retval;
1616 
1617  LOG_DEBUG("Initializing targets...");
1618  return target_init(CMD_CTX);
1619 }
1620 
1621 int target_register_event_callback(int (*callback)(struct target *target,
1622  enum target_event event, void *priv), void *priv)
1623 {
1624  struct target_event_callback **callbacks_p = &target_event_callbacks;
1625 
1626  if (!callback)
1628 
1629  if (*callbacks_p) {
1630  while ((*callbacks_p)->next)
1631  callbacks_p = &((*callbacks_p)->next);
1632  callbacks_p = &((*callbacks_p)->next);
1633  }
1634 
1635  (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1636  (*callbacks_p)->callback = callback;
1637  (*callbacks_p)->priv = priv;
1638  (*callbacks_p)->next = NULL;
1639 
1640  return ERROR_OK;
1641 }
1642 
1644  enum target_reset_mode reset_mode, void *priv), void *priv)
1645 {
1646  struct target_reset_callback *entry;
1647 
1648  if (!callback)
1650 
1651  entry = malloc(sizeof(struct target_reset_callback));
1652  if (!entry) {
1653  LOG_ERROR("error allocating buffer for reset callback entry");
1655  }
1656 
1657  entry->callback = callback;
1658  entry->priv = priv;
1659  list_add(&entry->list, &target_reset_callback_list);
1660 
1661 
1662  return ERROR_OK;
1663 }
1664 
1666  size_t len, uint8_t *data, void *priv), void *priv)
1667 {
1668  struct target_trace_callback *entry;
1669 
1670  if (!callback)
1672 
1673  entry = malloc(sizeof(struct target_trace_callback));
1674  if (!entry) {
1675  LOG_ERROR("error allocating buffer for trace callback entry");
1677  }
1678 
1679  entry->callback = callback;
1680  entry->priv = priv;
1681  list_add(&entry->list, &target_trace_callback_list);
1682 
1683 
1684  return ERROR_OK;
1685 }
1686 
1688  unsigned int time_ms, enum target_timer_type type, void *priv)
1689 {
1690  struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1691 
1692  if (!callback)
1694 
1695  if (*callbacks_p) {
1696  while ((*callbacks_p)->next)
1697  callbacks_p = &((*callbacks_p)->next);
1698  callbacks_p = &((*callbacks_p)->next);
1699  }
1700 
1701  (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1702  (*callbacks_p)->callback = callback;
1703  (*callbacks_p)->type = type;
1704  (*callbacks_p)->time_ms = time_ms;
1705  (*callbacks_p)->removed = false;
1706 
1707  (*callbacks_p)->when = timeval_ms() + time_ms;
1709 
1710  (*callbacks_p)->priv = priv;
1711  (*callbacks_p)->next = NULL;
1712 
1713  return ERROR_OK;
1714 }
1715 
1717  enum target_event event, void *priv), void *priv)
1718 {
1721 
1722  if (!callback)
1724 
1725  while (c) {
1726  struct target_event_callback *next = c->next;
1727  if ((c->callback == callback) && (c->priv == priv)) {
1728  *p = next;
1729  free(c);
1730  return ERROR_OK;
1731  } else
1732  p = &(c->next);
1733  c = next;
1734  }
1735 
1736  return ERROR_OK;
1737 }
1738 
1740  enum target_reset_mode reset_mode, void *priv), void *priv)
1741 {
1742  struct target_reset_callback *entry;
1743 
1744  if (!callback)
1746 
1747  list_for_each_entry(entry, &target_reset_callback_list, list) {
1748  if (entry->callback == callback && entry->priv == priv) {
1749  list_del(&entry->list);
1750  free(entry);
1751  break;
1752  }
1753  }
1754 
1755  return ERROR_OK;
1756 }
1757 
1759  size_t len, uint8_t *data, void *priv), void *priv)
1760 {
1761  struct target_trace_callback *entry;
1762 
1763  if (!callback)
1765 
1766  list_for_each_entry(entry, &target_trace_callback_list, list) {
1767  if (entry->callback == callback && entry->priv == priv) {
1768  list_del(&entry->list);
1769  free(entry);
1770  break;
1771  }
1772  }
1773 
1774  return ERROR_OK;
1775 }
1776 
1778 {
1779  if (!callback)
1781 
1783  c; c = c->next) {
1784  if ((c->callback == callback) && (c->priv == priv)) {
1785  c->removed = true;
1786  return ERROR_OK;
1787  }
1788  }
1789 
1790  return ERROR_FAIL;
1791 }
1792 
1794 {
1796  struct target_event_callback *next_callback;
1797 
1798  if (event == TARGET_EVENT_HALTED) {
1799  /* execute early halted first */
1801  }
1802 
1803  LOG_DEBUG("target event %i (%s) for core %s", event,
1804  target_event_name(event),
1805  target_name(target));
1806 
1807  target_handle_event(target, event);
1808 
1809  while (callback) {
1810  next_callback = callback->next;
1811  callback->callback(target, event, callback->priv);
1812  callback = next_callback;
1813  }
1814 
1815  return ERROR_OK;
1816 }
1817 
1819 {
1821 
1822  LOG_DEBUG("target reset %i (%s)", reset_mode,
1823  nvp_value2name(nvp_reset_modes, reset_mode)->name);
1824 
1825  list_for_each_entry(callback, &target_reset_callback_list, list)
1826  callback->callback(target, reset_mode, callback->priv);
1827 
1828  return ERROR_OK;
1829 }
1830 
1831 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1832 {
1834 
1835  list_for_each_entry(callback, &target_trace_callback_list, list)
1836  callback->callback(target, len, data, callback->priv);
1837 
1838  return ERROR_OK;
1839 }
1840 
1842  struct target_timer_callback *cb, int64_t *now)
1843 {
1844  cb->when = *now + cb->time_ms;
1845  return ERROR_OK;
1846 }
1847 
1849  int64_t *now)
1850 {
1851  cb->callback(cb->priv);
1852 
1853  if (cb->type == TARGET_TIMER_TYPE_PERIODIC)
1855 
1857 }
1858 
1860 {
1861  static bool callback_processing;
1862 
1863  /* Do not allow nesting */
1864  if (callback_processing)
1865  return ERROR_OK;
1866 
1867  callback_processing = true;
1868 
1869  keep_alive();
1870 
1871  int64_t now = timeval_ms();
1872 
1873  /* Initialize to a default value that's a ways into the future.
1874  * The loop below will make it closer to now if there are
1875  * callbacks that want to be called sooner. */
1876  target_timer_next_event_value = now + 1000;
1877 
1878  /* Store an address of the place containing a pointer to the
1879  * next item; initially, that's a standalone "root of the
1880  * list" variable. */
1882  while (callback && *callback) {
1883  if ((*callback)->removed) {
1884  struct target_timer_callback *p = *callback;
1885  *callback = (*callback)->next;
1886  free(p);
1887  continue;
1888  }
1889 
1890  bool call_it = (*callback)->callback &&
1891  ((!checktime && (*callback)->type == TARGET_TIMER_TYPE_PERIODIC) ||
1892  now >= (*callback)->when);
1893 
1894  if (call_it)
1896 
1897  if (!(*callback)->removed && (*callback)->when < target_timer_next_event_value)
1898  target_timer_next_event_value = (*callback)->when;
1899 
1900  callback = &(*callback)->next;
1901  }
1902 
1903  callback_processing = false;
1904  return ERROR_OK;
1905 }
1906 
1908 {
1910 }
1911 
1912 /* invoke periodic callbacks immediately */
1914 {
1916 }
1917 
1919 {
1921 }
1922 
1923 /* Prints the working area layout for debug purposes */
1924 static void print_wa_layout(struct target *target)
1925 {
1926  struct working_area *c = target->working_areas;
1927 
1928  while (c) {
1929  LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1930  c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1931  c->address, c->address + c->size - 1, c->size);
1932  c = c->next;
1933  }
1934 }
1935 
1936 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1937 static void target_split_working_area(struct working_area *area, uint32_t size)
1938 {
1939  assert(area->free); /* Shouldn't split an allocated area */
1940  assert(size <= area->size); /* Caller should guarantee this */
1941 
1942  /* Split only if not already the right size */
1943  if (size < area->size) {
1944  struct working_area *new_wa = malloc(sizeof(*new_wa));
1945 
1946  if (!new_wa)
1947  return;
1948 
1949  new_wa->next = area->next;
1950  new_wa->size = area->size - size;
1951  new_wa->address = area->address + size;
1952  new_wa->backup = NULL;
1953  new_wa->user = NULL;
1954  new_wa->free = true;
1955 
1956  area->next = new_wa;
1957  area->size = size;
1958 
1959  /* If backup memory was allocated to this area, it has the wrong size
1960  * now so free it and it will be reallocated if/when needed */
1961  free(area->backup);
1962  area->backup = NULL;
1963  }
1964 }
1965 
1966 /* Merge all adjacent free areas into one */
1968 {
1969  struct working_area *c = target->working_areas;
1970 
1971  while (c && c->next) {
1972  assert(c->next->address == c->address + c->size); /* This is an invariant */
1973 
1974  /* Find two adjacent free areas */
1975  if (c->free && c->next->free) {
1976  /* Merge the last into the first */
1977  c->size += c->next->size;
1978 
1979  /* Remove the last */
1980  struct working_area *to_be_freed = c->next;
1981  c->next = c->next->next;
1982  free(to_be_freed->backup);
1983  free(to_be_freed);
1984 
1985  /* If backup memory was allocated to the remaining area, it's has
1986  * the wrong size now */
1987  free(c->backup);
1988  c->backup = NULL;
1989  } else {
1990  c = c->next;
1991  }
1992  }
1993 }
1994 
1995 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1996 {
1997  /* Reevaluate working area address based on MMU state*/
1998  if (!target->working_areas) {
1999  int retval;
2000  bool enabled;
2001 
2002  retval = target->type->mmu(target, &enabled);
2003  if (retval != ERROR_OK)
2004  return retval;
2005 
2006  if (!enabled) {
2008  LOG_DEBUG("MMU disabled, using physical "
2009  "address for working memory " TARGET_ADDR_FMT,
2012  } else {
2013  LOG_ERROR("No working memory available. "
2014  "Specify -work-area-phys to target.");
2016  }
2017  } else {
2019  LOG_DEBUG("MMU enabled, using virtual "
2020  "address for working memory " TARGET_ADDR_FMT,
2023  } else {
2024  LOG_ERROR("No working memory available. "
2025  "Specify -work-area-virt to target.");
2027  }
2028  }
2029 
2030  /* Set up initial working area on first call */
2031  struct working_area *new_wa = malloc(sizeof(*new_wa));
2032  if (new_wa) {
2033  new_wa->next = NULL;
2034  new_wa->size = ALIGN_DOWN(target->working_area_size, 4); /* 4-byte align */
2035  new_wa->address = target->working_area;
2036  new_wa->backup = NULL;
2037  new_wa->user = NULL;
2038  new_wa->free = true;
2039  }
2040 
2041  target->working_areas = new_wa;
2042  }
2043 
2044  /* only allocate multiples of 4 byte */
2045  size = ALIGN_UP(size, 4);
2046 
2047  struct working_area *c = target->working_areas;
2048 
2049  /* Find the first large enough working area */
2050  while (c) {
2051  if (c->free && c->size >= size)
2052  break;
2053  c = c->next;
2054  }
2055 
2056  if (!c)
2058 
2059  /* Split the working area into the requested size */
2061 
2062  LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
2063  size, c->address);
2064 
2065  if (target->backup_working_area) {
2066  if (!c->backup) {
2067  c->backup = malloc(c->size);
2068  if (!c->backup)
2069  return ERROR_FAIL;
2070  }
2071 
2072  int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
2073  if (retval != ERROR_OK)
2074  return retval;
2075  }
2076 
2077  /* mark as used, and return the new (reused) area */
2078  c->free = false;
2079  *area = c;
2080 
2081  /* user pointer */
2082  c->user = area;
2083 
2085 
2086  return ERROR_OK;
2087 }
2088 
2089 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
2090 {
2091  int retval;
2092 
2093  retval = target_alloc_working_area_try(target, size, area);
2095  LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
2096  return retval;
2097 
2098 }
2099 
2100 static int target_restore_working_area(struct target *target, struct working_area *area)
2101 {
2102  int retval = ERROR_OK;
2103 
2104  if (target->backup_working_area && area->backup) {
2105  retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
2106  if (retval != ERROR_OK)
2107  LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
2108  area->size, area->address);
2109  }
2110 
2111  return retval;
2112 }
2113 
2114 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
2115 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
2116 {
2117  if (!area || area->free)
2118  return ERROR_OK;
2119 
2120  int retval = ERROR_OK;
2121  if (restore) {
2122  retval = target_restore_working_area(target, area);
2123  /* REVISIT: Perhaps the area should be freed even if restoring fails. */
2124  if (retval != ERROR_OK)
2125  return retval;
2126  }
2127 
2128  area->free = true;
2129 
2130  LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
2131  area->size, area->address);
2132 
2133  /* mark user pointer invalid */
2134  /* TODO: Is this really safe? It points to some previous caller's memory.
2135  * How could we know that the area pointer is still in that place and not
2136  * some other vital data? What's the purpose of this, anyway? */
2137  *area->user = NULL;
2138  area->user = NULL;
2139 
2141 
2143 
2144  return retval;
2145 }
2146 
2148 {
2149  return target_free_working_area_restore(target, area, 1);
2150 }
2151 
2152 /* free resources and restore memory, if restoring memory fails,
2153  * free up resources anyway
2154  */
2155 static void target_free_all_working_areas_restore(struct target *target, int restore)
2156 {
2157  struct working_area *c = target->working_areas;
2158 
2159  LOG_DEBUG("freeing all working areas");
2160 
2161  /* Loop through all areas, restoring the allocated ones and marking them as free */
2162  while (c) {
2163  if (!c->free) {
2164  if (restore)
2166  c->free = true;
2167  *c->user = NULL; /* Same as above */
2168  c->user = NULL;
2169  }
2170  c = c->next;
2171  }
2172 
2173  /* Run a merge pass to combine all areas into one */
2175 
2177 }
2178 
2180 {
2182 
2183  /* Now we have none or only one working area marked as free */
2184  if (target->working_areas) {
2185  /* Free the last one to allow on-the-fly moving and resizing */
2189  }
2190 }
2191 
2192 /* Find the largest number of bytes that can be allocated */
2194 {
2195  struct working_area *c = target->working_areas;
2196  uint32_t max_size = 0;
2197 
2198  if (!c)
2199  return ALIGN_DOWN(target->working_area_size, 4);
2200 
2201  while (c) {
2202  if (c->free && max_size < c->size)
2203  max_size = c->size;
2204 
2205  c = c->next;
2206  }
2207 
2208  return max_size;
2209 }
2210 
2211 static void target_destroy(struct target *target)
2212 {
2215 
2216  if (target->type->deinit_target)
2218 
2219  if (target->semihosting)
2222 
2224 
2225  struct target_event_action *teap, *temp;
2227  list_del(&teap->list);
2228  Jim_DecrRefCount(teap->interp, teap->body);
2229  free(teap);
2230  }
2231 
2233 
2234  /* release the targets SMP list */
2235  if (target->smp) {
2236  struct target_list *head, *tmp;
2237 
2239  list_del(&head->lh);
2240  head->target->smp = 0;
2241  free(head);
2242  }
2243  if (target->smp_targets != &empty_smp_targets)
2244  free(target->smp_targets);
2245  target->smp = 0;
2246  }
2247 
2249 
2250  free(target->gdb_port_override);
2251  free(target->type);
2252  free(target->trace_info);
2253  free(target->fileio_info);
2254  free(target->cmd_name);
2255  free(target);
2256 }
2257 
2258 void target_quit(void)
2259 {
2261  while (pe) {
2262  struct target_event_callback *t = pe->next;
2263  free(pe);
2264  pe = t;
2265  }
2267 
2269  while (pt) {
2270  struct target_timer_callback *t = pt->next;
2271  free(pt);
2272  pt = t;
2273  }
2275 
2276  for (struct target *target = all_targets; target;) {
2277  struct target *tmp;
2278 
2279  tmp = target->next;
2281  target = tmp;
2282  }
2283 
2284  all_targets = NULL;
2285 }
2286 
2288 {
2289  int retval;
2290  if (!target) {
2291  LOG_WARNING("No target has been configured");
2292  return ERROR_OK;
2293  }
2294 
2295  if (target->state != TARGET_HALTED)
2296  return ERROR_OK;
2297 
2298  retval = target->type->arch_state(target);
2299  return retval;
2300 }
2301 
2303  struct gdb_fileio_info *fileio_info)
2304 {
2305  /* If target does not support semi-hosting function, target
2306  has no need to provide .get_gdb_fileio_info callback.
2307  It just return ERROR_FAIL and gdb_server will return "Txx"
2308  as target halted every time. */
2309  return ERROR_FAIL;
2310 }
2311 
2313  int retcode, int fileio_errno, bool ctrl_c)
2314 {
2315  return ERROR_OK;
2316 }
2317 
2318 int target_profiling_default(struct target *target, uint32_t *samples,
2319  uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2320 {
2321  struct timeval timeout, now;
2322 
2324  timeval_add_time(&timeout, seconds, 0);
2325 
2326  LOG_INFO("Starting profiling. Halting and resuming the"
2327  " target as often as we can...");
2328 
2329  uint32_t sample_count = 0;
2330  /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2331  struct reg *reg = register_get_by_name(target->reg_cache, "pc", true);
2332 
2333  int retval = ERROR_OK;
2334  for (;;) {
2336  if (target->state == TARGET_HALTED) {
2337  uint32_t t = buf_get_u32(reg->value, 0, 32);
2338  samples[sample_count++] = t;
2339  /* current pc, addr = 0, do not handle breakpoints, not debugging */
2340  retval = target_resume(target, true, 0, false, false);
2342  alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2343  } else if (target->state == TARGET_RUNNING) {
2344  /* We want to quickly sample the PC. */
2345  retval = target_halt(target);
2346  } else {
2347  LOG_INFO("Target not halted or running");
2348  retval = ERROR_OK;
2349  break;
2350  }
2351 
2352  if (retval != ERROR_OK)
2353  break;
2354 
2355  gettimeofday(&now, NULL);
2356  if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
2357  LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2358  break;
2359  }
2360  }
2361 
2362  *num_samples = sample_count;
2363  return retval;
2364 }
2365 
2366 /* Single aligned words are guaranteed to use 16 or 32 bit access
2367  * mode respectively, otherwise data is handled as quickly as
2368  * possible
2369  */
2370 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2371 {
2372  LOG_DEBUG("writing buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2373  size, address);
2374 
2375  if (!target_memory_ready(target)) {
2376  LOG_TARGET_ERROR(target, "Memory not ready");
2377  return ERROR_FAIL;
2378  }
2379 
2380  if (size == 0)
2381  return ERROR_OK;
2382 
2383  if ((address + size - 1) < address) {
2384  /* GDB can request this when e.g. PC is 0xfffffffc */
2385  LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2386  address,
2387  size);
2388  return ERROR_FAIL;
2389  }
2390 
2392 }
2393 
2395  target_addr_t address, uint32_t count, const uint8_t *buffer)
2396 {
2397  uint32_t size;
2398  unsigned int data_bytes = target_data_bits(target) / 8;
2399 
2400  /* Align up to maximum bytes. The loop condition makes sure the next pass
2401  * will have something to do with the size we leave to it. */
2402  for (size = 1;
2403  size < data_bytes && count >= size * 2 + (address & size);
2404  size *= 2) {
2405  if (address & size) {
2406  int retval = target_write_memory(target, address, size, 1, buffer);
2407  if (retval != ERROR_OK)
2408  return retval;
2409  address += size;
2410  count -= size;
2411  buffer += size;
2412  }
2413  }
2414 
2415  /* Write the data with as large access size as possible. */
2416  for (; size > 0; size /= 2) {
2417  uint32_t aligned = count - count % size;
2418  if (aligned > 0) {
2419  int retval = target_write_memory(target, address, size, aligned / size, buffer);
2420  if (retval != ERROR_OK)
2421  return retval;
2422  address += aligned;
2423  count -= aligned;
2424  buffer += aligned;
2425  }
2426  }
2427 
2428  return ERROR_OK;
2429 }
2430 
2431 /* Single aligned words are guaranteed to use 16 or 32 bit access
2432  * mode respectively, otherwise data is handled as quickly as
2433  * possible
2434  */
2436 {
2437  LOG_DEBUG("reading buffer of %" PRIu32 " byte at " TARGET_ADDR_FMT,
2438  size, address);
2439 
2440  if (!target_memory_ready(target)) {
2441  LOG_TARGET_ERROR(target, "Memory not ready");
2442  return ERROR_FAIL;
2443  }
2444 
2445  if (size == 0)
2446  return ERROR_OK;
2447 
2448  if ((address + size - 1) < address) {
2449  /* GDB can request this when e.g. PC is 0xfffffffc */
2450  LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2451  address,
2452  size);
2453  return ERROR_FAIL;
2454  }
2455 
2457 }
2458 
2460 {
2461  uint32_t size;
2462  unsigned int data_bytes = target_data_bits(target) / 8;
2463 
2464  /* Align up to maximum bytes. The loop condition makes sure the next pass
2465  * will have something to do with the size we leave to it. */
2466  for (size = 1;
2467  size < data_bytes && count >= size * 2 + (address & size);
2468  size *= 2) {
2469  if (address & size) {
2470  int retval = target_read_memory(target, address, size, 1, buffer);
2471  if (retval != ERROR_OK)
2472  return retval;
2473  address += size;
2474  count -= size;
2475  buffer += size;
2476  }
2477  }
2478 
2479  /* Read the data with as large access size as possible. */
2480  for (; size > 0; size /= 2) {
2481  uint32_t aligned = count - count % size;
2482  if (aligned > 0) {
2483  int retval = target_read_memory(target, address, size, aligned / size, buffer);
2484  if (retval != ERROR_OK)
2485  return retval;
2486  address += aligned;
2487  count -= aligned;
2488  buffer += aligned;
2489  }
2490  }
2491 
2492  return ERROR_OK;
2493 }
2494 
2495 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
2496 {
2497  int retval;
2498  if (!target_was_examined(target)) {
2499  LOG_TARGET_ERROR(target, "not examined");
2501  }
2502 
2503  if (target->type->checksum_memory) {
2504  retval = target->type->checksum_memory(target, address, size, crc);
2505  if (retval == ERROR_OK)
2506  return ERROR_OK;
2507  } else {
2508  LOG_TARGET_INFO(target, "doesn't support fast checksum_memory, using slow read memory");
2509  }
2510 
2511  uint8_t *buffer = malloc(size);
2512  if (!buffer) {
2513  LOG_ERROR("error allocating buffer for section (%" PRIu32 " bytes)", size);
2514  return ERROR_FAIL;
2515  }
2516 
2518 
2519  if (retval == ERROR_OK)
2520  retval = image_calculate_checksum(buffer, size, crc);
2521 
2522  free(buffer);
2523  return retval;
2524 }
2525 
2527  struct target_memory_check_block *blocks, unsigned int num_blocks,
2528  uint8_t erased_value, unsigned int *checked)
2529 {
2530  if (!target_was_examined(target)) {
2531  LOG_TARGET_ERROR(target, "not examined");
2533  }
2534 
2536  return ERROR_NOT_IMPLEMENTED;
2537 
2538  return target->type->blank_check_memory(target, blocks, num_blocks,
2539  erased_value, checked);
2540 }
2541 
2543 {
2544  uint8_t value_buf[8];
2545 
2546  int retval = target_read_memory(target, address, 8, 1, value_buf);
2547 
2548  if (retval == ERROR_OK) {
2549  *value = target_buffer_get_u64(target, value_buf);
2550  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2551  address,
2552  *value);
2553  } else {
2554  *value = 0x0;
2555  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2556  address);
2557  }
2558 
2559  return retval;
2560 }
2561 
2563 {
2564  uint8_t value_buf[4];
2565 
2566  int retval = target_read_memory(target, address, 4, 1, value_buf);
2567 
2568  if (retval == ERROR_OK) {
2569  *value = target_buffer_get_u32(target, value_buf);
2570  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2571  address,
2572  *value);
2573  } else {
2574  *value = 0x0;
2575  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2576  address);
2577  }
2578 
2579  return retval;
2580 }
2581 
2583 {
2584  uint8_t value_buf[2];
2585 
2586  int retval = target_read_memory(target, address, 2, 1, value_buf);
2587 
2588  if (retval == ERROR_OK) {
2589  *value = target_buffer_get_u16(target, value_buf);
2590  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2591  address,
2592  *value);
2593  } else {
2594  *value = 0x0;
2595  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2596  address);
2597  }
2598 
2599  return retval;
2600 }
2601 
2603 {
2604  int retval = target_read_memory(target, address, 1, 1, value);
2605 
2606  if (retval == ERROR_OK) {
2607  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2608  address,
2609  *value);
2610  } else {
2611  *value = 0x0;
2612  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2613  address);
2614  }
2615 
2616  return retval;
2617 }
2618 
2620 {
2621  int retval;
2622  uint8_t value_buf[8];
2623 
2624  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2625  address,
2626  value);
2627 
2628  target_buffer_set_u64(target, value_buf, value);
2629  retval = target_write_memory(target, address, 8, 1, value_buf);
2630  if (retval != ERROR_OK)
2631  LOG_DEBUG("failed: %i", retval);
2632 
2633  return retval;
2634 }
2635 
2637 {
2638  int retval;
2639  uint8_t value_buf[4];
2640 
2641  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2642  address,
2643  value);
2644 
2645  target_buffer_set_u32(target, value_buf, value);
2646  retval = target_write_memory(target, address, 4, 1, value_buf);
2647  if (retval != ERROR_OK)
2648  LOG_DEBUG("failed: %i", retval);
2649 
2650  return retval;
2651 }
2652 
2654 {
2655  int retval;
2656  uint8_t value_buf[2];
2657 
2658  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2659  address,
2660  value);
2661 
2662  target_buffer_set_u16(target, value_buf, value);
2663  retval = target_write_memory(target, address, 2, 1, value_buf);
2664  if (retval != ERROR_OK)
2665  LOG_DEBUG("failed: %i", retval);
2666 
2667  return retval;
2668 }
2669 
2671 {
2672  int retval;
2673 
2674  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2675  address, value);
2676 
2677  retval = target_write_memory(target, address, 1, 1, &value);
2678  if (retval != ERROR_OK)
2679  LOG_DEBUG("failed: %i", retval);
2680 
2681  return retval;
2682 }
2683 
2685 {
2686  int retval;
2687  uint8_t value_buf[8];
2688 
2689  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2690  address,
2691  value);
2692 
2693  target_buffer_set_u64(target, value_buf, value);
2694  retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2695  if (retval != ERROR_OK)
2696  LOG_DEBUG("failed: %i", retval);
2697 
2698  return retval;
2699 }
2700 
2702 {
2703  int retval;
2704  uint8_t value_buf[4];
2705 
2706  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2707  address,
2708  value);
2709 
2710  target_buffer_set_u32(target, value_buf, value);
2711  retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2712  if (retval != ERROR_OK)
2713  LOG_DEBUG("failed: %i", retval);
2714 
2715  return retval;
2716 }
2717 
2719 {
2720  int retval;
2721  uint8_t value_buf[2];
2722 
2723  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2724  address,
2725  value);
2726 
2727  target_buffer_set_u16(target, value_buf, value);
2728  retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2729  if (retval != ERROR_OK)
2730  LOG_DEBUG("failed: %i", retval);
2731 
2732  return retval;
2733 }
2734 
2736 {
2737  int retval;
2738 
2739  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2740  address, value);
2741 
2742  retval = target_write_phys_memory(target, address, 1, 1, &value);
2743  if (retval != ERROR_OK)
2744  LOG_DEBUG("failed: %i", retval);
2745 
2746  return retval;
2747 }
2748 
2749 static int find_target(struct command_invocation *cmd, const char *name)
2750 {
2751  struct target *target = get_target(name);
2752  if (!target) {
2753  command_print(cmd, "Target: %s is unknown, try one of:\n", name);
2754  return ERROR_FAIL;
2755  }
2756  if (!target->tap->enabled) {
2757  command_print(cmd, "Target: TAP %s is disabled, "
2758  "can't be the current target\n",
2759  target->tap->dotted_name);
2760  return ERROR_FAIL;
2761  }
2762 
2763  cmd->ctx->current_target = target;
2764  if (cmd->ctx->current_target_override)
2765  cmd->ctx->current_target_override = target;
2766 
2767  return ERROR_OK;
2768 }
2769 
2770 
2771 COMMAND_HANDLER(handle_targets_command)
2772 {
2773  int retval = ERROR_OK;
2774  if (CMD_ARGC == 1) {
2775  retval = find_target(CMD, CMD_ARGV[0]);
2776  if (retval == ERROR_OK) {
2777  /* we're done! */
2778  return retval;
2779  }
2780  }
2781 
2782  unsigned int index = 0;
2783  command_print(CMD, " TargetName Type Endian TapName State ");
2784  command_print(CMD, "-- ------------------ ---------- ------ ------------------ ------------");
2785  for (struct target *target = all_targets; target; target = target->next, ++index) {
2786  const char *state;
2787  char marker = ' ';
2788 
2789  if (target->tap->enabled)
2791  else
2792  state = "tap-disabled";
2793 
2794  if (CMD_CTX->current_target == target)
2795  marker = '*';
2796 
2797  /* keep columns lined up to match the headers above */
2799  "%2d%c %-18s %-10s %-6s %-18s %s",
2800  index,
2801  marker,
2806  state);
2807  }
2808 
2809  return retval;
2810 }
2811 
2812 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2813 
2814 static int power_dropout;
2815 static int srst_asserted;
2816 
2821 
2822 static int sense_handler(void)
2823 {
2824  static int prev_srst_asserted;
2825  static int prev_power_dropout;
2826 
2827  int retval = jtag_power_dropout(&power_dropout);
2828  if (retval != ERROR_OK)
2829  return retval;
2830 
2831  int power_restored;
2832  power_restored = prev_power_dropout && !power_dropout;
2833  if (power_restored)
2834  run_power_restore = 1;
2835 
2836  int64_t current = timeval_ms();
2837  static int64_t last_power;
2838  bool wait_more = last_power + 2000 > current;
2839  if (power_dropout && !wait_more) {
2840  run_power_dropout = 1;
2841  last_power = current;
2842  }
2843 
2844  retval = jtag_srst_asserted(&srst_asserted);
2845  if (retval != ERROR_OK)
2846  return retval;
2847 
2848  int srst_deasserted;
2849  srst_deasserted = prev_srst_asserted && !srst_asserted;
2850 
2851  static int64_t last_srst;
2852  wait_more = last_srst + 2000 > current;
2853  if (srst_deasserted && !wait_more) {
2854  run_srst_deasserted = 1;
2855  last_srst = current;
2856  }
2857 
2858  if (!prev_srst_asserted && srst_asserted)
2859  run_srst_asserted = 1;
2860 
2861  prev_srst_asserted = srst_asserted;
2862  prev_power_dropout = power_dropout;
2863 
2864  if (srst_deasserted || power_restored) {
2865  /* Other than logging the event we can't do anything here.
2866  * Issuing a reset is a particularly bad idea as we might
2867  * be inside a reset already.
2868  */
2869  }
2870 
2871  return ERROR_OK;
2872 }
2873 
2874 /* process target state changes */
2875 static int handle_target(void *priv)
2876 {
2877  Jim_Interp *interp = (Jim_Interp *)priv;
2878  int retval = ERROR_OK;
2879 
2880  if (!is_jtag_poll_safe()) {
2881  /* polling is disabled currently */
2882  return ERROR_OK;
2883  }
2884 
2885  /* we do not want to recurse here... */
2886  static int recursive;
2887  if (!recursive) {
2888  recursive = 1;
2889  sense_handler();
2890  /* danger! running these procedures can trigger srst assertions and power dropouts.
2891  * We need to avoid an infinite loop/recursion here and we do that by
2892  * clearing the flags after running these events.
2893  */
2894  int did_something = 0;
2895  if (run_srst_asserted) {
2896  LOG_INFO("srst asserted detected, running srst_asserted proc.");
2897  Jim_Eval(interp, "srst_asserted");
2898  did_something = 1;
2899  }
2900  if (run_srst_deasserted) {
2901  Jim_Eval(interp, "srst_deasserted");
2902  did_something = 1;
2903  }
2904  if (run_power_dropout) {
2905  LOG_INFO("Power dropout detected, running power_dropout proc.");
2906  Jim_Eval(interp, "power_dropout");
2907  did_something = 1;
2908  }
2909  if (run_power_restore) {
2910  Jim_Eval(interp, "power_restore");
2911  did_something = 1;
2912  }
2913 
2914  if (did_something) {
2915  /* clear detect flags */
2916  sense_handler();
2917  }
2918 
2919  /* clear action flags */
2920 
2921  run_srst_asserted = 0;
2922  run_srst_deasserted = 0;
2923  run_power_restore = 0;
2924  run_power_dropout = 0;
2925 
2926  recursive = 0;
2927  }
2928 
2929  /* Poll targets for state changes unless that's globally disabled.
2930  * Skip targets that are currently disabled.
2931  */
2932  for (struct target *target = all_targets;
2934  target = target->next) {
2935 
2937  continue;
2938 
2939  if (!target->tap->enabled)
2940  continue;
2941 
2942  if (target->backoff.times > target->backoff.count) {
2943  /* do not poll this time as we failed previously */
2944  target->backoff.count++;
2945  continue;
2946  }
2947  target->backoff.count = 0;
2948 
2949  /* only poll target if we've got power and srst isn't asserted */
2950  if (!power_dropout && !srst_asserted) {
2951  /* polling may fail silently until the target has been examined */
2952  retval = target_poll(target);
2953  if (retval != ERROR_OK) {
2954  /* 100ms polling interval. Increase interval between polling up to 5000ms */
2955  if (target->backoff.times * polling_interval < 5000) {
2956  target->backoff.times *= 2;
2957  target->backoff.times++;
2958  }
2959 
2960  /* Tell GDB to halt the debugger. This allows the user to
2961  * run monitor commands to handle the situation.
2962  */
2964  }
2965  if (target->backoff.times > 0) {
2966  LOG_TARGET_ERROR(target, "Polling failed, trying to reexamine");
2968  retval = target_examine_one(target);
2969  if (retval != ERROR_OK) {
2970  LOG_TARGET_ERROR(target, "Examination failed, GDB will be halted. Polling again in %dms",
2972  return retval;
2973  }
2974  }
2975 
2976  /* Since we succeeded, we reset backoff count */
2977  target->backoff.times = 0;
2978  }
2979  }
2980 
2981  return retval;
2982 }
2983 
2984 COMMAND_HANDLER(handle_reg_command)
2985 {
2986  LOG_DEBUG("-");
2987 
2989  if (!target_was_examined(target)) {
2990  command_print(CMD, "Error: [%s] not examined", target_name(target));
2992  }
2993  struct reg *reg = NULL;
2994 
2995  /* list all available registers for the current target */
2996  if (CMD_ARGC == 0) {
2997  struct reg_cache *cache = target->reg_cache;
2998 
2999  unsigned int count = 0;
3000  while (cache) {
3001  unsigned int i;
3002 
3003  command_print(CMD, "===== %s", cache->name);
3004 
3005  for (i = 0, reg = cache->reg_list;
3006  i < cache->num_regs;
3007  i++, reg++, count++) {
3008  if (!reg->exist || reg->hidden)
3009  continue;
3010  /* only print cached values if they are valid */
3011  if (reg->valid) {
3012  char *value = buf_to_hex_str(reg->value,
3013  reg->size);
3015  "(%i) %s (/%" PRIu32 "): 0x%s%s",
3016  count, reg->name,
3017  reg->size, value,
3018  reg->dirty
3019  ? " (dirty)"
3020  : "");
3021  free(value);
3022  } else {
3023  command_print(CMD, "(%i) %s (/%" PRIu32 ")",
3024  count, reg->name,
3025  reg->size);
3026  }
3027  }
3028  cache = cache->next;
3029  }
3030 
3031  return ERROR_OK;
3032  }
3033 
3034  /* access a single register by its ordinal number */
3035  if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
3036  unsigned int num;
3037  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
3038 
3039  struct reg_cache *cache = target->reg_cache;
3040  unsigned int count = 0;
3041  while (cache) {
3042  unsigned int i;
3043  for (i = 0; i < cache->num_regs; i++) {
3044  if (count++ == num) {
3045  reg = &cache->reg_list[i];
3046  break;
3047  }
3048  }
3049  if (reg)
3050  break;
3051  cache = cache->next;
3052  }
3053 
3054  if (!reg) {
3055  command_print(CMD, "%i is out of bounds, the current target "
3056  "has only %i registers (0 - %i)", num, count, count - 1);
3057  return ERROR_FAIL;
3058  }
3059  } else {
3060  /* access a single register by its name */
3062 
3063  if (!reg)
3064  goto not_found;
3065  }
3066 
3067  assert(reg); /* give clang a hint that we *know* reg is != NULL here */
3068 
3069  if (!reg->exist)
3070  goto not_found;
3071 
3072  /* display a register */
3073  if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
3074  && (CMD_ARGV[1][0] <= '9')))) {
3075  if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
3076  reg->valid = false;
3077 
3078  if (!reg->valid) {
3079  int retval = reg->type->get(reg);
3080  if (retval != ERROR_OK) {
3081  LOG_ERROR("Could not read register '%s'", reg->name);
3082  return retval;
3083  }
3084  }
3085  char *value = buf_to_hex_str(reg->value, reg->size);
3086  command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3087  free(value);
3088  return ERROR_OK;
3089  }
3090 
3091  /* set register value */
3092  if (CMD_ARGC == 2) {
3093  uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
3094  if (!buf) {
3095  LOG_ERROR("Failed to allocate memory");
3096  return ERROR_FAIL;
3097  }
3098 
3099  int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[1], buf, reg->size);
3100  if (retval != ERROR_OK) {
3101  free(buf);
3102  return retval;
3103  }
3104 
3105  retval = reg->type->set(reg, buf);
3106  if (retval != ERROR_OK) {
3107  LOG_ERROR("Could not write to register '%s'", reg->name);
3108  } else {
3109  char *value = buf_to_hex_str(reg->value, reg->size);
3110  command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3111  free(value);
3112  }
3113 
3114  free(buf);
3115 
3116  return retval;
3117  }
3118 
3120 
3121 not_found:
3122  command_print(CMD, "register %s not found in current target", CMD_ARGV[0]);
3123  return ERROR_FAIL;
3124 }
3125 
3126 COMMAND_HANDLER(handle_poll_command)
3127 {
3128  int retval = ERROR_OK;
3130 
3131  if (CMD_ARGC == 0) {
3132  command_print(CMD, "background polling: %s",
3133  jtag_poll_get_enabled() ? "on" : "off");
3134  command_print(CMD, "TAP: %s (%s)",
3136  target->tap->enabled ? "enabled" : "disabled");
3137  if (!target->tap->enabled)
3138  return ERROR_OK;
3139  retval = target_poll(target);
3140  if (retval != ERROR_OK)
3141  return retval;
3142  retval = target_arch_state(target);
3143  if (retval != ERROR_OK)
3144  return retval;
3145  } else if (CMD_ARGC == 1) {
3146  bool enable;
3147  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
3148  jtag_poll_set_enabled(enable);
3149  } else
3151 
3152  return retval;
3153 }
3154 
3155 COMMAND_HANDLER(handle_wait_halt_command)
3156 {
3157  if (CMD_ARGC > 1)
3159 
3160  unsigned int ms = DEFAULT_HALT_TIMEOUT;
3161  if (1 == CMD_ARGC) {
3162  int retval = parse_uint(CMD_ARGV[0], &ms);
3163  if (retval != ERROR_OK)
3165  }
3166 
3168  return target_wait_state(target, TARGET_HALTED, ms);
3169 }
3170 
3171 /* wait for target state to change. The trick here is to have a low
3172  * latency for short waits and not to suck up all the CPU time
3173  * on longer waits.
3174  */
3175 int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
3176 {
3177  int retval;
3178  int64_t then = 0, cur;
3179  bool once = true;
3180 
3181  for (;;) {
3182  retval = target_poll(target);
3183  if (retval != ERROR_OK)
3184  return retval;
3185  if (target->state == state)
3186  break;
3187  cur = timeval_ms();
3188  if (once) {
3189  once = false;
3190  then = timeval_ms();
3191  LOG_DEBUG("waiting for target %s...",
3193  }
3194 
3195  keep_alive();
3197  return ERROR_SERVER_INTERRUPTED;
3198 
3199  if ((cur-then) > ms) {
3200  LOG_ERROR("timed out while waiting for target %s",
3202  return ERROR_FAIL;
3203  }
3204  }
3205 
3206  return ERROR_OK;
3207 }
3208 
3209 COMMAND_HANDLER(handle_halt_command)
3210 {
3211  LOG_DEBUG("-");
3212 
3214 
3215  target->verbose_halt_msg = true;
3216 
3217  int retval = target_halt(target);
3218  if (retval != ERROR_OK)
3219  return retval;
3220 
3221  if (CMD_ARGC == 1) {
3222  unsigned int wait_local;
3223  retval = parse_uint(CMD_ARGV[0], &wait_local);
3224  if (retval != ERROR_OK)
3226  if (!wait_local)
3227  return ERROR_OK;
3228  }
3229 
3230  return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3231 }
3232 
3233 COMMAND_HANDLER(handle_soft_reset_halt_command)
3234 {
3236 
3237  LOG_TARGET_INFO(target, "requesting target halt and executing a soft reset");
3238 
3240 
3241  return ERROR_OK;
3242 }
3243 
3244 COMMAND_HANDLER(handle_reset_command)
3245 {
3246  if (CMD_ARGC > 1)
3248 
3249  enum target_reset_mode reset_mode = RESET_RUN;
3250  if (CMD_ARGC == 1) {
3251  const struct nvp *n;
3253  if ((!n->name) || (n->value == RESET_UNKNOWN))
3255  reset_mode = n->value;
3256  }
3257 
3258  /* reset *all* targets */
3259  return target_process_reset(CMD, reset_mode);
3260 }
3261 
3262 
3263 COMMAND_HANDLER(handle_resume_command)
3264 {
3265  bool current = true;
3266  if (CMD_ARGC > 1)
3268 
3270 
3271  /* with no CMD_ARGV, resume from current pc, addr = 0,
3272  * with one arguments, addr = CMD_ARGV[0],
3273  * handle breakpoints, not debugging */
3274  target_addr_t addr = 0;
3275  if (CMD_ARGC == 1) {
3277  current = false;
3278  }
3279 
3280  return target_resume(target, current, addr, true, false);
3281 }
3282 
3283 COMMAND_HANDLER(handle_step_command)
3284 {
3285  if (CMD_ARGC > 1)
3287 
3288  LOG_DEBUG("-");
3289 
3290  /* with no CMD_ARGV, step from current pc, addr = 0,
3291  * with one argument addr = CMD_ARGV[0],
3292  * handle breakpoints, debugging */
3293  target_addr_t addr = 0;
3294  int current_pc = 1;
3295  if (CMD_ARGC == 1) {
3297  current_pc = 0;
3298  }
3299 
3301 
3302  return target_step(target, current_pc, addr, true);
3303 }
3304 
3306  struct target *target, target_addr_t address, unsigned int size,
3307  unsigned int count, const uint8_t *buffer, bool include_address)
3308 {
3309  const unsigned int line_bytecnt = 32;
3310  unsigned int line_modulo = line_bytecnt / size;
3311 
3312  char output[line_bytecnt * 4 + 1];
3313  unsigned int output_len = 0;
3314 
3315  const char *value_fmt;
3316  switch (size) {
3317  case 8:
3318  value_fmt = "%16.16"PRIx64" ";
3319  break;
3320  case 4:
3321  value_fmt = "%8.8"PRIx64" ";
3322  break;
3323  case 2:
3324  value_fmt = "%4.4"PRIx64" ";
3325  break;
3326  case 1:
3327  value_fmt = "%2.2"PRIx64" ";
3328  break;
3329  default:
3330  /* "can't happen", caller checked */
3331  LOG_ERROR("invalid memory read size: %u", size);
3332  return;
3333  }
3334 
3335  for (unsigned int i = 0; i < count; i++) {
3336  if (include_address && i % line_modulo == 0) {
3337  output_len += snprintf(output + output_len,
3338  sizeof(output) - output_len,
3339  TARGET_ADDR_FMT ": ",
3340  (address + (i * size)));
3341  }
3342 
3343  uint64_t value = 0;
3344  const uint8_t *value_ptr = buffer + i * size;
3345  switch (size) {
3346  case 8:
3347  value = target_buffer_get_u64(target, value_ptr);
3348  break;
3349  case 4:
3350  value = target_buffer_get_u32(target, value_ptr);
3351  break;
3352  case 2:
3353  value = target_buffer_get_u16(target, value_ptr);
3354  break;
3355  case 1:
3356  value = *value_ptr;
3357  }
3358  output_len += snprintf(output + output_len,
3359  sizeof(output) - output_len,
3360  value_fmt, value);
3361 
3362  if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3363  command_print(cmd, "%s", output);
3364  output_len = 0;
3365  }
3366  }
3367 }
3368 
3369 COMMAND_HANDLER(handle_md_command)
3370 {
3371  if (CMD_ARGC < 1)
3373 
3374  unsigned int size = 0;
3375  switch (CMD_NAME[2]) {
3376  case 'd':
3377  size = 8;
3378  break;
3379  case 'w':
3380  size = 4;
3381  break;
3382  case 'h':
3383  size = 2;
3384  break;
3385  case 'b':
3386  size = 1;
3387  break;
3388  default:
3390  }
3391 
3392  bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3393  int (*fn)(struct target *target,
3394  target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3395  if (physical) {
3396  CMD_ARGC--;
3397  CMD_ARGV++;
3399  } else
3400  fn = target_read_memory;
3401  if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3403 
3406 
3407  unsigned int count = 1;
3408  if (CMD_ARGC == 2)
3409  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3410 
3411  uint8_t *buffer = calloc(count, size);
3412  if (!buffer) {
3413  LOG_ERROR("Failed to allocate md read buffer");
3414  return ERROR_FAIL;
3415  }
3416 
3418  int retval = fn(target, address, size, count, buffer);
3419  if (retval == ERROR_OK)
3421 
3422  free(buffer);
3423 
3424  return retval;
3425 }
3426 
3427 typedef int (*target_write_fn)(struct target *target,
3428  target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3429 
3430 static int target_fill_mem(struct target *target,
3432  target_write_fn fn,
3433  unsigned int data_size,
3434  /* value */
3435  uint64_t b,
3436  /* count */
3437  unsigned int c)
3438 {
3439  /* We have to write in reasonably large chunks to be able
3440  * to fill large memory areas with any sane speed */
3441  const unsigned int chunk_size = 16384;
3442  uint8_t *target_buf = malloc(chunk_size * data_size);
3443  if (!target_buf) {
3444  LOG_ERROR("Out of memory");
3445  return ERROR_FAIL;
3446  }
3447 
3448  for (unsigned int i = 0; i < chunk_size; i++) {
3449  switch (data_size) {
3450  case 8:
3451  target_buffer_set_u64(target, target_buf + i * data_size, b);
3452  break;
3453  case 4:
3454  target_buffer_set_u32(target, target_buf + i * data_size, b);
3455  break;
3456  case 2:
3457  target_buffer_set_u16(target, target_buf + i * data_size, b);
3458  break;
3459  case 1:
3460  target_buffer_set_u8(target, target_buf + i * data_size, b);
3461  break;
3462  default:
3463  exit(-1);
3464  }
3465  }
3466 
3467  int retval = ERROR_OK;
3468 
3469  for (unsigned int x = 0; x < c; x += chunk_size) {
3470  unsigned int current;
3471  current = c - x;
3472  if (current > chunk_size)
3473  current = chunk_size;
3474  retval = fn(target, address + x * data_size, data_size, current, target_buf);
3475  if (retval != ERROR_OK)
3476  break;
3477  /* avoid GDB timeouts */
3478  keep_alive();
3479 
3481  retval = ERROR_SERVER_INTERRUPTED;
3482  break;
3483  }
3484  }
3485  free(target_buf);
3486 
3487  return retval;
3488 }
3489 
3490 
3491 COMMAND_HANDLER(handle_mw_command)
3492 {
3493  if (CMD_ARGC < 2)
3495  bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3496  target_write_fn fn;
3497  if (physical) {
3498  CMD_ARGC--;
3499  CMD_ARGV++;
3501  } else
3502  fn = target_write_memory;
3503  if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3505 
3508 
3509  uint64_t value;
3510  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], value);
3511 
3512  unsigned int count = 1;
3513  if (CMD_ARGC == 3)
3514  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3515 
3517  unsigned int wordsize;
3518  switch (CMD_NAME[2]) {
3519  case 'd':
3520  wordsize = 8;
3521  break;
3522  case 'w':
3523  wordsize = 4;
3524  break;
3525  case 'h':
3526  wordsize = 2;
3527  break;
3528  case 'b':
3529  wordsize = 1;
3530  break;
3531  default:
3533  }
3534 
3535  return target_fill_mem(target, address, fn, wordsize, value, count);
3536 }
3537 
3538 static COMMAND_HELPER(parse_load_image_command, struct image *image,
3539  target_addr_t *min_address, target_addr_t *max_address)
3540 {
3541  if (CMD_ARGC < 1 || CMD_ARGC > 5)
3543 
3544  /* a base address isn't always necessary,
3545  * default to 0x0 (i.e. don't relocate) */
3546  if (CMD_ARGC >= 2) {
3549  image->base_address = addr;
3550  image->base_address_set = true;
3551  } else
3552  image->base_address_set = false;
3553 
3554  image->start_address_set = false;
3555 
3556  if (CMD_ARGC >= 4)
3557  COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3558  if (CMD_ARGC == 5) {
3559  COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3560  /* use size (given) to find max (required) */
3561  *max_address += *min_address;
3562  }
3563 
3564  if (*min_address > *max_address)
3566 
3567  return ERROR_OK;
3568 }
3569 
3570 COMMAND_HANDLER(handle_load_image_command)
3571 {
3572  uint8_t *buffer;
3573  size_t buf_cnt;
3574  uint32_t image_size;
3575  target_addr_t min_address = 0;
3576  target_addr_t max_address = -1;
3577  struct image image;
3578 
3579  int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
3580  &image, &min_address, &max_address);
3581  if (retval != ERROR_OK)
3582  return retval;
3583 
3585 
3586  struct duration bench;
3587  duration_start(&bench);
3588 
3589  if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3590  return ERROR_FAIL;
3591 
3592  image_size = 0x0;
3593  retval = ERROR_OK;
3594  for (unsigned int i = 0; i < image.num_sections; i++) {
3595  buffer = malloc(image.sections[i].size);
3596  if (!buffer) {
3598  "error allocating buffer for section (%d bytes)",
3599  (int)(image.sections[i].size));
3600  retval = ERROR_FAIL;
3601  break;
3602  }
3603 
3604  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3605  if (retval != ERROR_OK) {
3606  free(buffer);
3607  break;
3608  }
3609 
3610  uint32_t offset = 0;
3611  uint32_t length = buf_cnt;
3612 
3613  /* DANGER!!! beware of unsigned comparison here!!! */
3614 
3615  if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3616  (image.sections[i].base_address < max_address)) {
3617 
3618  if (image.sections[i].base_address < min_address) {
3619  /* clip addresses below */
3620  offset += min_address-image.sections[i].base_address;
3621  length -= offset;
3622  }
3623 
3624  if (image.sections[i].base_address + buf_cnt > max_address)
3625  length -= (image.sections[i].base_address + buf_cnt)-max_address;
3626 
3627  retval = target_write_buffer(target,
3629  if (retval != ERROR_OK) {
3630  free(buffer);
3631  break;
3632  }
3633  image_size += length;
3634  command_print(CMD, "%u bytes written at address " TARGET_ADDR_FMT "",
3635  (unsigned int)length,
3637  }
3638 
3639  free(buffer);
3640  }
3641 
3642  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3643  command_print(CMD, "downloaded %" PRIu32 " bytes "
3644  "in %fs (%0.3f KiB/s)", image_size,
3645  duration_elapsed(&bench), duration_kbps(&bench, image_size));
3646  }
3647 
3648  image_close(&image);
3649 
3650  return retval;
3651 
3652 }
3653 
3654 COMMAND_HANDLER(handle_dump_image_command)
3655 {
3656  struct fileio *fileio;
3657  uint8_t *buffer;
3658  int retval, retvaltemp;
3660  struct duration bench;
3662 
3663  if (CMD_ARGC != 3)
3665 
3668 
3669  uint32_t buf_size = (size > 4096) ? 4096 : size;
3670  buffer = malloc(buf_size);
3671  if (!buffer)
3672  return ERROR_FAIL;
3673 
3675  if (retval != ERROR_OK) {
3676  free(buffer);
3677  return retval;
3678  }
3679 
3680  duration_start(&bench);
3681 
3682  while (size > 0) {
3683  size_t size_written;
3684  uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3685  retval = target_read_buffer(target, address, this_run_size, buffer);
3686  if (retval != ERROR_OK)
3687  break;
3688 
3689  retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3690  if (retval != ERROR_OK)
3691  break;
3692 
3693  size -= this_run_size;
3694  address += this_run_size;
3695  }
3696 
3697  free(buffer);
3698 
3699  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3700  size_t filesize;
3701  retval = fileio_size(fileio, &filesize);
3702  if (retval != ERROR_OK)
3703  return retval;
3705  "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3706  duration_elapsed(&bench), duration_kbps(&bench, filesize));
3707  }
3708 
3709  retvaltemp = fileio_close(fileio);
3710  if (retvaltemp != ERROR_OK)
3711  return retvaltemp;
3712 
3713  return retval;
3714 }
3715 
3720 };
3721 
3722 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3723 {
3724  uint8_t *buffer;
3725  size_t buf_cnt;
3726  uint32_t image_size;
3727  int retval;
3728  uint32_t checksum = 0;
3729  uint32_t mem_checksum = 0;
3730 
3731  struct image image;
3732 
3734 
3735  if (CMD_ARGC < 1)
3737 
3738  if (!target) {
3739  LOG_ERROR("no target selected");
3740  return ERROR_FAIL;
3741  }
3742 
3743  struct duration bench;
3744  duration_start(&bench);
3745 
3746  if (CMD_ARGC >= 2) {
3750  image.base_address_set = true;
3751  } else {
3752  image.base_address_set = false;
3753  image.base_address = 0x0;
3754  }
3755 
3756  image.start_address_set = false;
3757 
3758  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3759  if (retval != ERROR_OK)
3760  return retval;
3761 
3762  image_size = 0x0;
3763  int diffs = 0;
3764  retval = ERROR_OK;
3765  for (unsigned int i = 0; i < image.num_sections; i++) {
3766  buffer = malloc(image.sections[i].size);
3767  if (!buffer) {
3769  "error allocating buffer for section (%" PRIu32 " bytes)",
3770  image.sections[i].size);
3771  break;
3772  }
3773  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3774  if (retval != ERROR_OK) {
3775  free(buffer);
3776  break;
3777  }
3778 
3779  if (verify >= IMAGE_VERIFY) {
3780  /* calculate checksum of image */
3781  retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3782  if (retval != ERROR_OK) {
3783  free(buffer);
3784  break;
3785  }
3786 
3787  retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3788  if (retval != ERROR_OK) {
3789  free(buffer);
3790  break;
3791  }
3792  if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3793  LOG_ERROR("checksum mismatch");
3794  free(buffer);
3795  retval = ERROR_FAIL;
3796  goto done;
3797  }
3798  if (checksum != mem_checksum) {
3799  /* failed crc checksum, fall back to a binary compare */
3800  uint8_t *data;
3801 
3802  if (diffs == 0)
3803  LOG_ERROR("checksum mismatch - attempting binary compare");
3804 
3805  data = malloc(buf_cnt);
3806 
3807  retval = target_read_buffer(target, image.sections[i].base_address, buf_cnt, data);
3808  if (retval == ERROR_OK) {
3809  uint32_t t;
3810  for (t = 0; t < buf_cnt; t++) {
3811  if (data[t] != buffer[t]) {
3813  "diff %d address " TARGET_ADDR_FMT ". Was 0x%02" PRIx8 " instead of 0x%02" PRIx8,
3814  diffs,
3815  t + image.sections[i].base_address,
3816  data[t],
3817  buffer[t]);
3818  if (diffs++ >= 127) {
3819  command_print(CMD, "More than 128 errors, the rest are not printed.");
3820  free(data);
3821  free(buffer);
3822  goto done;
3823  }
3824  }
3825  keep_alive();
3827  retval = ERROR_SERVER_INTERRUPTED;
3828  free(data);
3829  free(buffer);
3830  goto done;
3831  }
3832  }
3833  }
3834  free(data);
3835  }
3836  } else {
3837  command_print(CMD, "address " TARGET_ADDR_FMT " length 0x%08zx",
3839  buf_cnt);
3840  }
3841 
3842  free(buffer);
3843  image_size += buf_cnt;
3844  }
3845  if (diffs > 0)
3846  command_print(CMD, "No more differences found.");
3847 done:
3848  if (diffs > 0)
3849  retval = ERROR_FAIL;
3850  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3851  command_print(CMD, "verified %" PRIu32 " bytes "
3852  "in %fs (%0.3f KiB/s)", image_size,
3853  duration_elapsed(&bench), duration_kbps(&bench, image_size));
3854  }
3855 
3856  image_close(&image);
3857 
3858  return retval;
3859 }
3860 
3861 COMMAND_HANDLER(handle_verify_image_checksum_command)
3862 {
3863  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3864 }
3865 
3866 COMMAND_HANDLER(handle_verify_image_command)
3867 {
3868  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3869 }
3870 
3871 COMMAND_HANDLER(handle_test_image_command)
3872 {
3873  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3874 }
3875 
3877 {
3878  struct target *target = get_current_target(cmd->ctx);
3880  while (breakpoint) {
3881  if (breakpoint->type == BKPT_SOFT) {
3882  char *buf = buf_to_hex_str(breakpoint->orig_instr,
3883  breakpoint->length * 8);
3884  command_print(cmd, "Software breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, orig_instr=0x%s",
3886  breakpoint->length,
3887  buf);
3888  free(buf);
3889  } else {
3890  if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3891  command_print(cmd, "Context breakpoint: asid=0x%8.8" PRIx32 ", len=0x%x, num=%u",
3892  breakpoint->asid,
3894  else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3895  command_print(cmd, "Hybrid breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
3898  command_print(cmd, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3899  breakpoint->asid);
3900  } else
3901  command_print(cmd, "Hardware breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
3904  }
3905 
3907  }
3908  return ERROR_OK;
3909 }
3910 
3912  target_addr_t addr, uint32_t asid, unsigned int length, int hw)
3913 {
3914  struct target *target = get_current_target(cmd->ctx);
3915  int retval;
3916 
3917  if (asid == 0) {
3918  retval = breakpoint_add(target, addr, length, hw);
3919  /* error is always logged in breakpoint_add(), do not print it again */
3920  if (retval == ERROR_OK)
3921  command_print(cmd, "breakpoint set at " TARGET_ADDR_FMT "", addr);
3922 
3923  } else if (addr == 0) {
3925  LOG_TARGET_ERROR(target, "Context breakpoint not available");
3927  }
3928  retval = context_breakpoint_add(target, asid, length, hw);
3929  /* error is always logged in context_breakpoint_add(), do not print it again */
3930  if (retval == ERROR_OK)
3931  command_print(cmd, "Context breakpoint set at 0x%8.8" PRIx32, asid);
3932 
3933  } else {
3935  LOG_TARGET_ERROR(target, "Hybrid breakpoint not available");
3937  }
3938  retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3939  /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3940  if (retval == ERROR_OK)
3941  command_print(cmd, "Hybrid breakpoint set at 0x%8.8" PRIx32, asid);
3942  }
3943  return retval;
3944 }
3945 
3946 COMMAND_HANDLER(handle_bp_command)
3947 {
3949  uint32_t asid;
3950  uint32_t length;
3951  int hw = BKPT_SOFT;
3952 
3953  switch (CMD_ARGC) {
3954  case 0:
3955  return handle_bp_command_list(CMD);
3956 
3957  case 2:
3958  asid = 0;
3961  return handle_bp_command_set(CMD, addr, asid, length, hw);
3962 
3963  case 3:
3964  if (strcmp(CMD_ARGV[2], "hw") == 0) {
3965  hw = BKPT_HARD;
3968  asid = 0;
3969  return handle_bp_command_set(CMD, addr, asid, length, hw);
3970  } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3971  hw = BKPT_HARD;
3972  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3974  addr = 0;
3975  return handle_bp_command_set(CMD, addr, asid, length, hw);
3976  }
3977  /* fallthrough */
3978  case 4:
3979  hw = BKPT_HARD;
3981  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3983  return handle_bp_command_set(CMD, addr, asid, length, hw);
3984 
3985  default:
3987  }
3988 }
3989 
3990 COMMAND_HANDLER(handle_rbp_command)
3991 {
3992  int retval;
3993 
3994  if (CMD_ARGC != 1)
3996 
3998 
3999  if (!strcmp(CMD_ARGV[0], "all")) {
4000  retval = breakpoint_remove_all(target);
4001 
4002  if (retval != ERROR_OK) {
4003  command_print(CMD, "Error encountered during removal of all breakpoints.");
4004  command_print(CMD, "Some breakpoints may have remained set.");
4005  }
4006  } else {
4009 
4010  retval = breakpoint_remove(target, addr);
4011 
4012  if (retval != ERROR_OK)
4013  command_print(CMD, "Error during removal of breakpoint at address " TARGET_ADDR_FMT, addr);
4014  }
4015 
4016  return retval;
4017 }
4018 
4019 COMMAND_HANDLER(handle_wp_command)
4020 {
4022 
4023  if (CMD_ARGC == 0) {
4025 
4026  while (watchpoint) {
4027  char wp_type = (watchpoint->rw == WPT_READ ? 'r' : (watchpoint->rw == WPT_WRITE ? 'w' : 'a'));
4028  command_print(CMD, "address: " TARGET_ADDR_FMT
4029  ", len: 0x%8.8x"
4030  ", r/w/a: %c, value: 0x%8.8" PRIx64
4031  ", mask: 0x%8.8" PRIx64,
4033  watchpoint->length,
4034  wp_type,
4035  watchpoint->value,
4036  watchpoint->mask);
4038  }
4039  return ERROR_OK;
4040  }
4041 
4042  enum watchpoint_rw type = WPT_ACCESS;
4043  target_addr_t addr = 0;
4044  uint32_t length = 0;
4045  uint64_t data_value = 0x0;
4046  uint64_t data_mask = WATCHPOINT_IGNORE_DATA_VALUE_MASK;
4047  bool mask_specified = false;
4048 
4049  switch (CMD_ARGC) {
4050  case 5:
4051  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[4], data_mask);
4052  mask_specified = true;
4053  /* fall through */
4054  case 4:
4055  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[3], data_value);
4056  // if user specified only data value without mask - the mask should be 0
4057  if (!mask_specified)
4058  data_mask = 0;
4059  /* fall through */
4060  case 3:
4061  switch (CMD_ARGV[2][0]) {
4062  case 'r':
4063  type = WPT_READ;
4064  break;
4065  case 'w':
4066  type = WPT_WRITE;
4067  break;
4068  case 'a':
4069  type = WPT_ACCESS;
4070  break;
4071  default:
4072  LOG_TARGET_ERROR(target, "invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
4074  }
4075  /* fall through */
4076  case 2:
4079  break;
4080 
4081  default:
4083  }
4084 
4085  int retval = watchpoint_add(target, addr, length, type,
4086  data_value, data_mask);
4087  if (retval != ERROR_OK)
4088  LOG_TARGET_ERROR(target, "Failure setting watchpoints");
4089 
4090  return retval;
4091 }
4092 
4093 COMMAND_HANDLER(handle_rwp_command)
4094 {
4095  int retval;
4096 
4097  if (CMD_ARGC != 1)
4099 
4101  if (!strcmp(CMD_ARGV[0], "all")) {
4102  retval = watchpoint_remove_all(target);
4103 
4104  if (retval != ERROR_OK) {
4105  command_print(CMD, "Error encountered during removal of all watchpoints.");
4106  command_print(CMD, "Some watchpoints may have remained set.");
4107  }
4108  } else {
4111 
4112  retval = watchpoint_remove(target, addr);
4113 
4114  if (retval != ERROR_OK)
4115  command_print(CMD, "Error during removal of watchpoint at address " TARGET_ADDR_FMT, addr);
4116  }
4117 
4118  return retval;
4119 }
4120 
4127 COMMAND_HANDLER(handle_virt2phys_command)
4128 {
4129  if (CMD_ARGC != 1)
4131 
4132  target_addr_t va;
4134  target_addr_t pa;
4135 
4137  int retval = target->type->virt2phys(target, va, &pa);
4138  if (retval == ERROR_OK)
4139  command_print(CMD, "Physical address " TARGET_ADDR_FMT "", pa);
4140 
4141  return retval;
4142 }
4143 
4144 static void write_data(FILE *f, const void *data, size_t len)
4145 {
4146  size_t written = fwrite(data, 1, len, f);
4147  if (written != len)
4148  LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
4149 }
4150 
4151 static void write_long(FILE *f, int l, struct target *target)
4152 {
4153  uint8_t val[4];
4154 
4155  target_buffer_set_u32(target, val, l);
4156  write_data(f, val, 4);
4157 }
4158 
4159 static void write_string(FILE *f, char *s)
4160 {
4161  write_data(f, s, strlen(s));
4162 }
4163 
4164 typedef unsigned char UNIT[2]; /* unit of profiling */
4165 
4166 /* Dump a gmon.out histogram file. */
4167 static void write_gmon(uint32_t *samples, uint32_t sample_num, const char *filename, bool with_range,
4168  uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
4169 {
4170  uint32_t i;
4171  FILE *f = fopen(filename, "wb");
4172  if (!f)
4173  return;
4174  write_string(f, "gmon");
4175  write_long(f, 0x00000001, target); /* Version */
4176  write_long(f, 0, target); /* padding */
4177  write_long(f, 0, target); /* padding */
4178  write_long(f, 0, target); /* padding */
4179 
4180  uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
4181  write_data(f, &zero, 1);
4182 
4183  /* figure out bucket size */
4184  uint32_t min;
4185  uint32_t max;
4186  if (with_range) {
4187  min = start_address;
4188  max = end_address;
4189  } else {
4190  min = samples[0];
4191  max = samples[0];
4192  for (i = 0; i < sample_num; i++) {
4193  if (min > samples[i])
4194  min = samples[i];
4195  if (max < samples[i])
4196  max = samples[i];
4197  }
4198 
4199  /* max should be (largest sample + 1)
4200  * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4201  if (max < UINT32_MAX)
4202  max++;
4203 
4204  /* gprof requires (max - min) >= 2 */
4205  while ((max - min) < 2) {
4206  if (max < UINT32_MAX)
4207  max++;
4208  else
4209  min--;
4210  }
4211  }
4212 
4213  uint32_t address_space = max - min;
4214 
4215  /* FIXME: What is the reasonable number of buckets?
4216  * The profiling result will be more accurate if there are enough buckets. */
4217  static const uint32_t max_buckets = 128 * 1024; /* maximum buckets. */
4218  uint32_t num_buckets = address_space / sizeof(UNIT);
4219  if (num_buckets > max_buckets)
4220  num_buckets = max_buckets;
4221  int *buckets = malloc(sizeof(int) * num_buckets);
4222  if (!buckets) {
4223  fclose(f);
4224  return;
4225  }
4226  memset(buckets, 0, sizeof(int) * num_buckets);
4227  for (i = 0; i < sample_num; i++) {
4228  uint32_t address = samples[i];
4229 
4230  if ((address < min) || (max <= address))
4231  continue;
4232 
4233  long long a = address - min;
4234  long long b = num_buckets;
4235  long long c = address_space;
4236  int index_t = (a * b) / c; /* danger!!!! int32 overflows */
4237  buckets[index_t]++;
4238  }
4239 
4240  /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4241  write_long(f, min, target); /* low_pc */
4242  write_long(f, max, target); /* high_pc */
4243  write_long(f, num_buckets, target); /* # of buckets */
4244  float sample_rate = sample_num / (duration_ms / 1000.0);
4245  write_long(f, sample_rate, target);
4246  write_string(f, "seconds");
4247  for (i = 0; i < (15-strlen("seconds")); i++)
4248  write_data(f, &zero, 1);
4249  write_string(f, "s");
4250 
4251  /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4252 
4253  char *data = malloc(2 * num_buckets);
4254  if (data) {
4255  for (i = 0; i < num_buckets; i++) {
4256  int val;
4257  val = buckets[i];
4258  if (val > 65535)
4259  val = 65535;
4260  data[i * 2] = val&0xff;
4261  data[i * 2 + 1] = (val >> 8) & 0xff;
4262  }
4263  free(buckets);
4264  write_data(f, data, num_buckets * 2);
4265  free(data);
4266  } else
4267  free(buckets);
4268 
4269  fclose(f);
4270 }
4271 
4272 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4273  * which will be used as a random sampling of PC */
4274 COMMAND_HANDLER(handle_profile_command)
4275 {
4277 
4278  if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
4280 
4281  const uint32_t MAX_PROFILE_SAMPLE_NUM = 1000000;
4282  uint32_t offset;
4283  uint32_t num_of_samples;
4284  int retval = ERROR_OK;
4285  bool halted_before_profiling = target->state == TARGET_HALTED;
4286 
4288 
4289  uint32_t start_address = 0;
4290  uint32_t end_address = 0;
4291  bool with_range = false;
4292  if (CMD_ARGC == 4) {
4293  with_range = true;
4294  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4295  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4296  if (start_address > end_address || (end_address - start_address) < 2) {
4297  command_print(CMD, "Error: end - start < 2");
4299  }
4300  }
4301 
4302  uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
4303  if (!samples) {
4304  LOG_ERROR("No memory to store samples.");
4305  return ERROR_FAIL;
4306  }
4307 
4308  uint64_t timestart_ms = timeval_ms();
4314  retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
4315  &num_of_samples, offset);
4316  if (retval != ERROR_OK) {
4317  free(samples);
4318  return retval;
4319  }
4320  uint32_t duration_ms = timeval_ms() - timestart_ms;
4321 
4322  assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
4323 
4324  retval = target_poll(target);
4325  if (retval != ERROR_OK) {
4326  free(samples);
4327  return retval;
4328  }
4329 
4330  if (target->state == TARGET_RUNNING && halted_before_profiling) {
4331  /* The target was halted before we started and is running now. Halt it,
4332  * for consistency. */
4333  retval = target_halt(target);
4334  if (retval != ERROR_OK) {
4335  free(samples);
4336  return retval;
4337  }
4338  } else if (target->state == TARGET_HALTED && !halted_before_profiling) {
4339  /* The target was running before we started and is halted now. Resume
4340  * it, for consistency. */
4341  retval = target_resume(target, true, 0, false, false);
4342  if (retval != ERROR_OK) {
4343  free(samples);
4344  return retval;
4345  }
4346  }
4347 
4348  retval = target_poll(target);
4349  if (retval != ERROR_OK) {
4350  free(samples);
4351  return retval;
4352  }
4353 
4354  write_gmon(samples, num_of_samples, CMD_ARGV[1],
4355  with_range, start_address, end_address, target, duration_ms);
4356  command_print(CMD, "Wrote %s", CMD_ARGV[1]);
4357 
4358  free(samples);
4359  return retval;
4360 }
4361 
4362 COMMAND_HANDLER(handle_target_read_memory)
4363 {
4364  /*
4365  * CMD_ARGV[0] = memory address
4366  * CMD_ARGV[1] = desired element width in bits
4367  * CMD_ARGV[2] = number of elements to read
4368  * CMD_ARGV[3] = optional "phys"
4369  */
4370 
4371  if (CMD_ARGC < 3 || CMD_ARGC > 4)
4373 
4374  /* Arg 1: Memory address. */
4377 
4378  /* Arg 2: Bit width of one element. */
4379  unsigned int width_bits;
4380  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
4381 
4382  /* Arg 3: Number of elements to read. */
4383  unsigned int count;
4384  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
4385 
4386  /* Arg 4: Optional 'phys'. */
4387  bool is_phys = false;
4388  if (CMD_ARGC == 4) {
4389  if (strcmp(CMD_ARGV[3], "phys")) {
4390  command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
4392  }
4393 
4394  is_phys = true;
4395  }
4396 
4397  switch (width_bits) {
4398  case 8:
4399  case 16:
4400  case 32:
4401  case 64:
4402  break;
4403  default:
4404  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
4406  }
4407 
4408  if (count > 65536) {
4409  command_print(CMD, "too large read request, exceeds 64K elements");
4411  }
4412 
4413  const unsigned int width = width_bits / 8;
4414  /* -1 is needed to handle cases when (addr + count * width) results in zero
4415  * due to overflow.
4416  */
4417  if ((addr + count * width - 1) < addr) {
4418  command_print(CMD, "memory region wraps over address zero");
4420  }
4421 
4423 
4424  const size_t buffersize = 4096;
4425  uint8_t *buffer = malloc(buffersize);
4426 
4427  if (!buffer) {
4428  LOG_ERROR("Failed to allocate memory");
4429  return ERROR_FAIL;
4430  }
4431 
4432  char *separator = "";
4433  while (count > 0) {
4434  const unsigned int max_chunk_len = buffersize / width;
4435  const size_t chunk_len = MIN(count, max_chunk_len);
4436 
4437  int retval;
4438 
4439  if (is_phys)
4440  retval = target_read_phys_memory(target, addr, width, chunk_len, buffer);
4441  else
4442  retval = target_read_memory(target, addr, width, chunk_len, buffer);
4443 
4444  if (retval != ERROR_OK) {
4445  LOG_DEBUG("read at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4446  addr, width_bits, chunk_len);
4447  /*
4448  * FIXME: we append the errmsg to the list of value already read.
4449  * Add a way to flush and replace old output, but LOG_DEBUG() it
4450  */
4451  command_print(CMD, "failed to read memory");
4452  free(buffer);
4453  return retval;
4454  }
4455 
4456  for (size_t i = 0; i < chunk_len ; i++) {
4457  uint64_t v = 0;
4458 
4459  switch (width) {
4460  case 8:
4462  break;
4463  case 4:
4465  break;
4466  case 2:
4468  break;
4469  case 1:
4470  v = buffer[i];
4471  break;
4472  }
4473 
4474  command_print_sameline(CMD, "%s0x%" PRIx64, separator, v);
4475  separator = " ";
4476  }
4477 
4478  count -= chunk_len;
4479  addr += chunk_len * width;
4480  }
4481 
4482  free(buffer);
4483 
4484  return ERROR_OK;
4485 }
4486 
4487 COMMAND_HANDLER(handle_target_write_memory)
4488 {
4489  /*
4490  * CMD_ARGV[0] = memory address
4491  * CMD_ARGV[1] = desired element width in bits
4492  * CMD_ARGV[2] = list of data to write
4493  * CMD_ARGV[3] = optional "phys"
4494  */
4495 
4496  if (CMD_ARGC < 3 || CMD_ARGC > 4)
4498 
4499  /* Arg 1: Memory address. */
4502 
4503  /* Arg 2: Bit width of one element. */
4504  unsigned int width_bits;
4505  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
4506 
4507  /* Arg 3: Elements to write. */
4508  size_t count = Jim_ListLength(CMD_CTX->interp, CMD_JIMTCL_ARGV[2]);
4509 
4510  /* Arg 4: Optional 'phys'. */
4511  bool is_phys = false;
4512 
4513  if (CMD_ARGC == 4) {
4514  if (strcmp(CMD_ARGV[3], "phys")) {
4515  command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
4517  }
4518 
4519  is_phys = true;
4520  }
4521 
4522  switch (width_bits) {
4523  case 8:
4524  case 16:
4525  case 32:
4526  case 64:
4527  break;
4528  default:
4529  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
4531  }
4532 
4533  if (count > 65536) {
4534  command_print(CMD, "too large memory write request, exceeds 64K elements");
4536  }
4537 
4538  const unsigned int width = width_bits / 8;
4539  /* -1 is needed to handle cases when (addr + count * width) results in zero
4540  * due to overflow.
4541  */
4542  if ((addr + count * width - 1) < addr) {
4543  command_print(CMD, "memory region wraps over address zero");
4545  }
4546 
4548 
4549  const size_t buffersize = 4096;
4550  uint8_t *buffer = malloc(buffersize);
4551 
4552  if (!buffer) {
4553  LOG_ERROR("Failed to allocate memory");
4554  return ERROR_FAIL;
4555  }
4556 
4557  size_t j = 0;
4558 
4559  while (count > 0) {
4560  const unsigned int max_chunk_len = buffersize / width;
4561  const size_t chunk_len = MIN(count, max_chunk_len);
4562 
4563  for (size_t i = 0; i < chunk_len; i++, j++) {
4564  Jim_Obj *tmp = Jim_ListGetIndex(CMD_CTX->interp, CMD_JIMTCL_ARGV[2], j);
4565  jim_wide element_wide;
4566  int jimretval = Jim_GetWide(CMD_CTX->interp, tmp, &element_wide);
4567  if (jimretval != JIM_OK) {
4568  command_print(CMD, "invalid value \"%s\"", Jim_GetString(tmp, NULL));
4569  free(buffer);
4571  }
4572 
4573  const uint64_t v = element_wide;
4574 
4575  switch (width) {
4576  case 8:
4578  break;
4579  case 4:
4581  break;
4582  case 2:
4584  break;
4585  case 1:
4586  buffer[i] = v & 0x0ff;
4587  break;
4588  }
4589  }
4590 
4591  count -= chunk_len;
4592 
4593  int retval;
4594 
4595  if (is_phys)
4596  retval = target_write_phys_memory(target, addr, width, chunk_len, buffer);
4597  else
4598  retval = target_write_memory(target, addr, width, chunk_len, buffer);
4599 
4600  if (retval != ERROR_OK) {
4601  LOG_DEBUG("write at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4602  addr, width_bits, chunk_len);
4603  command_print(CMD, "failed to write memory");
4604  free(buffer);
4605  return retval;
4606  }
4607 
4608  addr += chunk_len * width;
4609  }
4610 
4611  free(buffer);
4612 
4613  return ERROR_OK;
4614 }
4615 
4616 /* FIX? should we propagate errors here rather than printing them
4617  * and continuing?
4618  */
4620 {
4621  struct target_event_action *teap, *tmp;
4622  int retval;
4623 
4625  if (teap->event == e) {
4626  /*
4627  * The event can be destroyed by its own handler.
4628  * Make a local copy and use it in place of the original.
4629  */
4630  struct target_event_action local_teap = *teap;
4631  teap = &local_teap;
4632 
4633  LOG_DEBUG("target: %s (%s) event: %d (%s) action: %s",
4636  e,
4637  target_event_name(e),
4638  Jim_GetString(teap->body, NULL));
4639 
4640  /* Override current target by the target an event
4641  * is issued from (lot of scripts need it).
4642  * Return back to previous override as soon
4643  * as the handler processing is done */
4644  struct command_context *cmd_ctx = current_command_context(teap->interp);
4645  struct target *saved_target_override = cmd_ctx->current_target_override;
4646  cmd_ctx->current_target_override = target;
4647 
4648  /*
4649  * The event can be destroyed by its own handler.
4650  * Prevent the body to get deallocated by Jim.
4651  */
4652  Jim_IncrRefCount(teap->body);
4653  retval = Jim_EvalObj(teap->interp, teap->body);
4654  Jim_DecrRefCount(teap->interp, teap->body);
4655 
4656  cmd_ctx->current_target_override = saved_target_override;
4657 
4658  if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
4659  return;
4660 
4661  if (retval == JIM_RETURN)
4662  retval = teap->interp->returnCode;
4663 
4664  if (retval != JIM_OK) {
4665  Jim_MakeErrorMessage(teap->interp);
4666  LOG_TARGET_ERROR(target, "Execution of event %s failed:\n%s",
4667  target_event_name(e),
4668  Jim_GetString(Jim_GetResult(teap->interp), NULL));
4669  /* clean both error code and stacktrace before return */
4670  Jim_Eval(teap->interp, "error \"\" \"\"");
4671  }
4672  }
4673  }
4674 }
4675 
4676 COMMAND_HANDLER(handle_target_get_reg)
4677 {
4678  if (CMD_ARGC < 1 || CMD_ARGC > 2)
4680 
4681  bool force = false;
4682  Jim_Obj *next_argv = CMD_JIMTCL_ARGV[0];
4683 
4684  if (CMD_ARGC == 2) {
4685  if (strcmp(CMD_ARGV[0], "-force")) {
4686  command_print(CMD, "invalid argument '%s', must be '-force'", CMD_ARGV[0]);
4688  }
4689 
4690  force = true;
4691  next_argv = CMD_JIMTCL_ARGV[1];
4692  }
4693 
4694  const int length = Jim_ListLength(CMD_CTX->interp, next_argv);
4695 
4696  const struct target *target = get_current_target(CMD_CTX);
4697  if (target->state != TARGET_HALTED) {
4698  command_print(CMD, "Error: [%s] not halted", target_name(target));
4699  return ERROR_TARGET_NOT_HALTED;
4700  }
4701 
4702  for (int i = 0; i < length; i++) {
4703  Jim_Obj *elem = Jim_ListGetIndex(CMD_CTX->interp, next_argv, i);
4704 
4705  const char *reg_name = Jim_String(elem);
4706 
4707  struct reg *reg = register_get_by_name(target->reg_cache, reg_name, true);
4708 
4709  if (!reg || !reg->exist) {
4710  command_print(CMD, "unknown register '%s'", reg_name);
4712  }
4713 
4714  if (force || !reg->valid) {
4715  int retval = reg->type->get(reg);
4716 
4717  if (retval != ERROR_OK) {
4718  command_print(CMD, "failed to read register '%s'", reg_name);
4719  return retval;
4720  }
4721  }
4722 
4723  char *reg_value = buf_to_hex_str(reg->value, reg->size);
4724 
4725  if (!reg_value) {
4726  LOG_ERROR("Failed to allocate memory");
4727  return ERROR_FAIL;
4728  }
4729 
4730  command_print(CMD, "%s 0x%s", reg_name, reg_value);
4731 
4732  free(reg_value);
4733  }
4734 
4735  return ERROR_OK;
4736 }
4737 
4738 COMMAND_HANDLER(handle_set_reg_command)
4739 {
4740  if (CMD_ARGC != 1)
4742 
4743  int tmp;
4744 #if JIM_VERSION >= 80
4745  Jim_Obj **dict = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &tmp);
4746 
4747  if (!dict)
4748  return ERROR_FAIL;
4749 #else
4750  Jim_Obj **dict;
4751  int ret = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &dict, &tmp);
4752 
4753  if (ret != JIM_OK)
4754  return ERROR_FAIL;
4755 #endif
4756 
4757  const unsigned int length = tmp;
4758 
4759  const struct target *target = get_current_target(CMD_CTX);
4760  assert(target);
4761  if (target->state != TARGET_HALTED) {
4762  command_print(CMD, "Error: [%s] not halted", target_name(target));
4763  return ERROR_TARGET_NOT_HALTED;
4764  }
4765 
4766 
4767  for (unsigned int i = 0; i < length; i += 2) {
4768  const char *reg_name = Jim_String(dict[i]);
4769  const char *reg_value = Jim_String(dict[i + 1]);
4770  struct reg *reg = register_get_by_name(target->reg_cache, reg_name, true);
4771 
4772  if (!reg || !reg->exist) {
4773  command_print(CMD, "unknown register '%s'", reg_name);
4774  return ERROR_FAIL;
4775  }
4776 
4777  uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
4778  if (!buf) {
4779  LOG_ERROR("Failed to allocate memory");
4780  return ERROR_FAIL;
4781  }
4782 
4783  int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, reg_value, buf, reg->size);
4784  if (retval != ERROR_OK) {
4785  free(buf);
4786  return retval;
4787  }
4788 
4789  retval = reg->type->set(reg, buf);
4790  free(buf);
4791 
4792  if (retval != ERROR_OK) {
4793  command_print(CMD, "failed to set '%s' to register '%s'",
4794  reg_value, reg_name);
4795  return retval;
4796  }
4797  }
4798 
4799  return ERROR_OK;
4800 }
4801 
4805 bool target_has_event_action(const struct target *target, enum target_event event)
4806 {
4807  struct target_event_action *teap;
4808 
4810  if (teap->event == event)
4811  return true;
4812  }
4813  return false;
4814 }
4815 
4831 };
4832 
4833 static struct nvp nvp_config_opts[] = {
4834  { .name = "-type", .value = TCFG_TYPE },
4835  { .name = "-event", .value = TCFG_EVENT },
4836  { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4837  { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4838  { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4839  { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4840  { .name = "-endian", .value = TCFG_ENDIAN },
4841  { .name = "-coreid", .value = TCFG_COREID },
4842  { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4843  { .name = "-dbgbase", .value = TCFG_DBGBASE },
4844  { .name = "-rtos", .value = TCFG_RTOS },
4845  { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
4846  { .name = "-gdb-port", .value = TCFG_GDB_PORT },
4847  { .name = "-gdb-max-connections", .value = TCFG_GDB_MAX_CONNECTIONS },
4848  { .name = NULL, .value = -1 }
4849 };
4850 
4851 static COMMAND_HELPER(target_configure, struct target *target, unsigned int index, bool is_configure)
4852 {
4853  const struct nvp *n;
4854  int retval;
4855 
4856  /* parse config or cget options ... */
4857  while (index < CMD_ARGC) {
4859  /* target defines a configure function */
4860  /* target gets first dibs on parameters */
4861  struct jim_getopt_info goi;
4862  jim_getopt_setup(&goi, CMD_CTX->interp, CMD_ARGC - index, CMD_JIMTCL_ARGV + index);
4863  goi.is_configure = is_configure;
4864  int e = (*target->type->target_jim_configure)(target, &goi);
4865  index = CMD_ARGC - goi.argc;
4866 
4867  int reslen;
4868  const char *result = Jim_GetString(Jim_GetResult(CMD_CTX->interp), &reslen);
4869  if (reslen > 0)
4870  command_print(CMD, "%s", result);
4871 
4872  if (e == JIM_OK) {
4873  /* more? */
4874  continue;
4875  }
4876  if (e == JIM_ERR) {
4877  /* An error */
4878  return ERROR_FAIL;
4879  }
4880  /* otherwise we 'continue' below */
4881  }
4883  if (!n->name) {
4886  }
4887  index++;
4888  switch (n->value) {
4889  case TCFG_TYPE:
4890  /* not settable */
4891  if (is_configure) {
4892  command_print(CMD, "not settable: %s", n->name);
4894  }
4895  if (index != CMD_ARGC)
4898  /* loop for more */
4899  break;
4900 
4901  case TCFG_EVENT:
4902  if (index == CMD_ARGC) {
4903  command_print(CMD, "expecting %s event-name event-body",
4904  CMD_ARGV[index - 1]);
4906  }
4907 
4909  if (!n->name) {
4912  }
4913  index++;
4914 
4915  if (is_configure) {
4916  if (index == CMD_ARGC) {
4917  command_print(CMD, "expecting %s %s event-body",
4918  CMD_ARGV[index - 2], CMD_ARGV[index - 1]);
4920  }
4921  }
4922 
4923  {
4924  struct target_event_action *teap;
4925 
4926  /* replace existing? */
4928  if (teap->event == (enum target_event)n->value)
4929  break;
4930 
4931  /* not found! */
4932  if (&teap->list == &target->events_action)
4933  teap = NULL;
4934 
4935  if (is_configure) {
4936  /* START_DEPRECATED_TPIU */
4937  if (n->value == TARGET_EVENT_TRACE_CONFIG)
4938  LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n->name);
4939  /* END_DEPRECATED_TPIU */
4940 
4941  if (strlen(CMD_ARGV[index]) == 0) {
4942  /* empty action, drop existing one */
4943  if (teap) {
4944  list_del(&teap->list);
4945  Jim_DecrRefCount(teap->interp, teap->body);
4946  free(teap);
4947  }
4948  index++;
4949  break;
4950  }
4951 
4952  bool replace = true;
4953  if (!teap) {
4954  /* create new */
4955  teap = calloc(1, sizeof(*teap));
4956  replace = false;
4957  }
4958  teap->event = n->value;
4959  teap->interp = CMD_CTX->interp;
4960  if (teap->body)
4961  Jim_DecrRefCount(teap->interp, teap->body);
4962  /* use jim object to keep its reference on tcl file and line */
4963  /* TODO: need duplicate? isn't IncrRefCount enough? */
4964  teap->body = Jim_DuplicateObj(teap->interp, CMD_JIMTCL_ARGV[index++]);
4965  /*
4966  * FIXME:
4967  * Tcl/TK - "tk events" have a nice feature.
4968  * See the "BIND" command.
4969  * We should support that here.
4970  * You can specify %X and %Y in the event code.
4971  * The idea is: %T - target name.
4972  * The idea is: %N - target number
4973  * The idea is: %E - event name.
4974  */
4975  Jim_IncrRefCount(teap->body);
4976 
4977  if (!replace) {
4978  /* add to head of event list */
4979  list_add(&teap->list, &target->events_action);
4980  }
4981  } else {
4982  /* cget */
4983  if (index != CMD_ARGC)
4985 
4986  if (teap)
4987  command_print(CMD, "%s", Jim_GetString(teap->body, NULL));
4988  }
4989  }
4990  /* loop for more */
4991  break;
4992 
4993  case TCFG_WORK_AREA_VIRT:
4994  if (is_configure) {
4995  if (index == CMD_ARGC) {
4996  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
4998  }
5000  index++;
5003  } else {
5004  if (index != CMD_ARGC)
5007  }
5008  /* loop for more */
5009  break;
5010 
5011  case TCFG_WORK_AREA_PHYS:
5012  if (is_configure) {
5013  if (index == CMD_ARGC) {
5014  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5016  }
5018  index++;
5021  } else {
5022  if (index != CMD_ARGC)
5025  }
5026  /* loop for more */
5027  break;
5028 
5029  case TCFG_WORK_AREA_SIZE:
5030  if (is_configure) {
5031  if (index == CMD_ARGC) {
5032  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5034  }
5036  index++;
5038  } else {
5039  if (index != CMD_ARGC)
5041  command_print(CMD, "0x%08" PRIx32, target->working_area_size);
5042  }
5043  /* loop for more */
5044  break;
5045 
5046  case TCFG_WORK_AREA_BACKUP:
5047  if (is_configure) {
5048  if (index == CMD_ARGC) {
5049  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5051  }
5053  if (retval != ERROR_OK)
5054  return retval;
5055  index++;
5057  } else {
5058  if (index != CMD_ARGC)
5061  }
5062  /* loop for more */
5063  break;
5064 
5065  case TCFG_ENDIAN:
5066  if (is_configure) {
5067  if (index == CMD_ARGC) {
5068  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5070  }
5072  if (!n->name) {
5075  }
5076  index++;
5077  target->endianness = n->value;
5078  } else {
5079  if (index != CMD_ARGC)
5082  if (!n->name) {
5085  }
5086  command_print(CMD, "%s", n->name);
5087  }
5088  /* loop for more */
5089  break;
5090 
5091  case TCFG_COREID:
5092  if (is_configure) {
5093  if (index == CMD_ARGC) {
5094  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5096  }
5097  COMMAND_PARSE_NUMBER(s32, CMD_ARGV[index], target->coreid);
5098  index++;
5099  } else {
5100  if (index != CMD_ARGC)
5102  command_print(CMD, "%" PRIi32, target->coreid);
5103  }
5104  /* loop for more */
5105  break;
5106 
5107  case TCFG_CHAIN_POSITION:
5108  if (is_configure) {
5109  if (target->has_dap) {
5110  command_print(CMD, "target requires -dap parameter instead of -chain-position!");
5112  }
5113 
5114  if (index == CMD_ARGC) {
5115  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5117  }
5118  struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[index]);
5119  if (!tap) {
5120  command_print(CMD, "Tap '%s' could not be found", CMD_ARGV[index]);
5122  }
5123  index++;
5124  target->tap = tap;
5125  target->tap_configured = true;
5126  } else {
5127  if (index != CMD_ARGC)
5130  }
5131  /* loop for more */
5132  break;
5133 
5134  case TCFG_DBGBASE:
5135  if (is_configure) {
5136  if (index == CMD_ARGC) {
5137  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5139  }
5141  index++;
5142  target->dbgbase_set = true;
5143  } else {
5144  if (index != CMD_ARGC)
5146  command_print(CMD, "0x%08" PRIx32, target->dbgbase);
5147  }
5148  /* loop for more */
5149  break;
5150 
5151  case TCFG_RTOS:
5152  if (is_configure) {
5153  if (index == CMD_ARGC) {
5154  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5156  }
5157  retval = rtos_create(CMD, target, CMD_ARGV[index]);
5158  if (retval != ERROR_OK)
5159  return retval;
5160  index++;
5161  } else {
5162  if (index != CMD_ARGC)
5164  if (target->rtos)
5165  command_print(CMD, "%s", target->rtos->type->name);
5166  }
5167  /* loop for more */
5168  break;
5169 
5170  case TCFG_DEFER_EXAMINE:
5171  if (is_configure)
5172  target->defer_examine = true;
5173  else
5174  command_print(CMD, "%s", target->defer_examine ? "true" : "false");
5175  /* loop for more */
5176  break;
5177 
5178  case TCFG_GDB_PORT:
5179  if (is_configure) {
5180  if (index == CMD_ARGC) {
5181  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5183  }
5184 
5185  /* TODO: generalize test of COMMAND_CONFIG */
5186  if (CMD_CTX->mode != COMMAND_CONFIG) {
5187  command_print(CMD, "-gdb-port must be configured before 'init'");
5189  }
5190 
5191  char *s = strdup(CMD_ARGV[index]);
5192  if (!s) {
5193  LOG_ERROR("Out of memory");
5194  return ERROR_FAIL;
5195  }
5196  free(target->gdb_port_override);
5198  index++;
5199  } else {
5200  if (index != CMD_ARGC)
5203  }
5204  /* loop for more */
5205  break;
5206 
5208  if (is_configure) {
5209  if (index == CMD_ARGC) {
5210  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5212  }
5213 
5214  if (CMD_CTX->mode != COMMAND_CONFIG) {
5215  command_print(CMD, "-gdb-max-connections must be configured before 'init'");
5217  }
5218 
5220  index++;
5221  if (target->gdb_max_connections < 0)
5223  } else {
5224  if (index != CMD_ARGC)
5227  }
5228  /* loop for more */
5229  break;
5230  }
5231  }
5232 
5233  return ERROR_OK;
5234 }
5235 
5236 COMMAND_HANDLER(handle_target_configure)
5237 {
5238  if (!CMD_ARGC)
5240 
5241  bool is_configure = !strcmp(CMD_NAME, "configure");
5242 
5244 
5245  return CALL_COMMAND_HANDLER(target_configure, target, 0, is_configure);
5246 }
5247 
5248 COMMAND_HANDLER(handle_target_examine)
5249 {
5250  bool allow_defer = false;
5251 
5252  if (CMD_ARGC > 1)
5254 
5255  if (CMD_ARGC == 1) {
5256  if (strcmp(CMD_ARGV[0], "allow-defer"))
5258  allow_defer = true;
5259  }
5260 
5262  if (!target->tap->enabled) {
5263  command_print(CMD, "[TAP is disabled]");
5264  return ERROR_FAIL;
5265  }
5266 
5267  if (allow_defer && target->defer_examine) {
5268  LOG_INFO("Deferring arp_examine of %s", target_name(target));
5269  LOG_INFO("Use arp_examine command to examine it manually!");
5270  return ERROR_OK;
5271  }
5272 
5273  int retval = target->type->examine(target);
5274  if (retval != ERROR_OK) {
5276  return retval;
5277  }
5278 
5280 
5281  return ERROR_OK;
5282 }
5283 
5284 COMMAND_HANDLER(handle_target_was_examined)
5285 {
5286  if (CMD_ARGC != 0)
5288 
5290 
5291  command_print(CMD, "%d", target_was_examined(target) ? 1 : 0);
5292 
5293  return ERROR_OK;
5294 }
5295 
5296 COMMAND_HANDLER(handle_target_examine_deferred)
5297 {
5298  if (CMD_ARGC != 0)
5300 
5302 
5303  command_print(CMD, "%d", target->defer_examine ? 1 : 0);
5304 
5305  return ERROR_OK;
5306 }
5307 
5308 COMMAND_HANDLER(handle_target_halt_gdb)
5309 {
5310  if (CMD_ARGC != 0)
5312 
5314 
5316 }
5317 
5318 COMMAND_HANDLER(handle_target_poll)
5319 {
5320  if (CMD_ARGC != 0)
5322 
5324  if (!target->tap->enabled) {
5325  command_print(CMD, "[TAP is disabled]");
5326  return ERROR_FAIL;
5327  }
5328 
5329  if (!(target_was_examined(target)))
5331 
5332  return target->type->poll(target);
5333 }
5334 
5335 COMMAND_HANDLER(handle_target_reset)
5336 {
5337  if (CMD_ARGC != 2)
5339 
5340  const struct nvp *n = nvp_name2value(nvp_assert, CMD_ARGV[0]);
5341  if (!n->name) {
5344  }
5345 
5346  /* the halt or not param */
5347  int a;
5348  COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], a);
5349 
5351  if (!target->tap->enabled) {
5352  command_print(CMD, "[TAP is disabled]");
5353  return ERROR_FAIL;
5354  }
5355 
5357  command_print(CMD, "No target-specific reset for %s", target_name(target));
5358  return ERROR_FAIL;
5359  }
5360 
5361  /* determine if we should halt or not. */
5362  target->reset_halt = (a != 0);
5363  /* When this happens - all workareas are invalid. */
5365 
5366  /* do the assert */
5367  if (n->value == NVP_ASSERT) {
5368  int retval = target->type->assert_reset(target);
5369  if (target->defer_examine) {
5372  }
5373  return retval;
5374  }
5375 
5376  return target->type->deassert_reset(target);
5377 }
5378 
5379 COMMAND_HANDLER(handle_target_halt)
5380 {
5381  if (CMD_ARGC != 0)
5383 
5385  if (!target->tap->enabled) {
5386  command_print(CMD, "[TAP is disabled]");
5387  return ERROR_FAIL;
5388  }
5389 
5390  return target->type->halt(target);
5391 }
5392 
5393 COMMAND_HANDLER(handle_target_wait_state)
5394 {
5395  if (CMD_ARGC != 2)
5397 
5398  const struct nvp *n = nvp_name2value(nvp_target_state, CMD_ARGV[0]);
5399  if (!n->name) {
5402  }
5403 
5404  unsigned int a;
5405  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], a);
5406 
5408  if (!target->tap->enabled) {
5409  command_print(CMD, "[TAP is disabled]");
5410  return ERROR_FAIL;
5411  }
5412 
5413  int retval = target_wait_state(target, n->value, a);
5414  if (retval != ERROR_OK) {
5416  "target: %s wait %s fails (%d) %s",
5417  target_name(target), n->name,
5418  retval, target_strerror_safe(retval));
5419  return retval;
5420  }
5421  return ERROR_OK;
5422 }
5423 /* List for human, Events defined for this target.
5424  * scripts/programs should use 'name cget -event NAME'
5425  */
5426 COMMAND_HANDLER(handle_target_event_list)
5427 {
5429  struct target_event_action *teap;
5430 
5431  command_print(CMD, "Event actions for target %s\n",
5432  target_name(target));
5433  command_print(CMD, "%-25s | Body", "Event");
5434  command_print(CMD, "------------------------- | "
5435  "----------------------------------------");
5436 
5438  command_print(CMD, "%-25s | %s",
5439  target_event_name(teap->event),
5440  Jim_GetString(teap->body, NULL));
5441 
5442  command_print(CMD, "***END***");
5443  return ERROR_OK;
5444 }
5445 
5446 COMMAND_HANDLER(handle_target_current_state)
5447 {
5448  if (CMD_ARGC != 0)
5450 
5452 
5454 
5455  return ERROR_OK;
5456 }
5457 
5458 COMMAND_HANDLER(handle_target_debug_reason)
5459 {
5460  if (CMD_ARGC != 0)
5462 
5464 
5465 
5468 
5469  if (!debug_reason) {
5470  command_print(CMD, "bug: invalid debug reason (%d)",
5471  target->debug_reason);
5472  return ERROR_FAIL;
5473  }
5474 
5475  command_print(CMD, "%s", debug_reason);
5476 
5477  return ERROR_OK;
5478 }
5479 
5480 COMMAND_HANDLER(handle_target_invoke_event)
5481 {
5482  if (CMD_ARGC != 1)
5484 
5485  const struct nvp *n = nvp_name2value(nvp_target_event, CMD_ARGV[0]);
5486  if (!n->name) {
5489  }
5490 
5493  return ERROR_OK;
5494 }
5495 
5497  {
5498  .name = "configure",
5499  .mode = COMMAND_ANY,
5500  .handler = handle_target_configure,
5501  .help = "configure a new target for use",
5502  .usage = "[target_attribute ...]",
5503  },
5504  {
5505  .name = "cget",
5506  .mode = COMMAND_ANY,
5507  .handler = handle_target_configure,
5508  .help = "returns the specified target attribute",
5509  .usage = "target_attribute",
5510  },
5511  {
5512  .name = "mwd",
5513  .handler = handle_mw_command,
5514  .mode = COMMAND_EXEC,
5515  .help = "Write 64-bit word(s) to target memory",
5516  .usage = "address data [count]",
5517  },
5518  {
5519  .name = "mww",
5520  .handler = handle_mw_command,
5521  .mode = COMMAND_EXEC,
5522  .help = "Write 32-bit word(s) to target memory",
5523  .usage = "address data [count]",
5524  },
5525  {
5526  .name = "mwh",
5527  .handler = handle_mw_command,
5528  .mode = COMMAND_EXEC,
5529  .help = "Write 16-bit half-word(s) to target memory",
5530  .usage = "address data [count]",
5531  },
5532  {
5533  .name = "mwb",
5534  .handler = handle_mw_command,
5535  .mode = COMMAND_EXEC,
5536  .help = "Write byte(s) to target memory",
5537  .usage = "address data [count]",
5538  },
5539  {
5540  .name = "mdd",
5541  .handler = handle_md_command,
5542  .mode = COMMAND_EXEC,
5543  .help = "Display target memory as 64-bit words",
5544  .usage = "address [count]",
5545  },
5546  {
5547  .name = "mdw",
5548  .handler = handle_md_command,
5549  .mode = COMMAND_EXEC,
5550  .help = "Display target memory as 32-bit words",
5551  .usage = "address [count]",
5552  },
5553  {
5554  .name = "mdh",
5555  .handler = handle_md_command,
5556  .mode = COMMAND_EXEC,
5557  .help = "Display target memory as 16-bit half-words",
5558  .usage = "address [count]",
5559  },
5560  {
5561  .name = "mdb",
5562  .handler = handle_md_command,
5563  .mode = COMMAND_EXEC,
5564  .help = "Display target memory as 8-bit bytes",
5565  .usage = "address [count]",
5566  },
5567  {
5568  .name = "get_reg",
5569  .mode = COMMAND_EXEC,
5570  .handler = handle_target_get_reg,
5571  .help = "Get register values from the target",
5572  .usage = "[-force] list",
5573  },
5574  {
5575  .name = "set_reg",
5576  .mode = COMMAND_EXEC,
5577  .handler = handle_set_reg_command,
5578  .help = "Set target register values",
5579  .usage = "dict",
5580  },
5581  {
5582  .name = "read_memory",
5583  .mode = COMMAND_EXEC,
5584  .handler = handle_target_read_memory,
5585  .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
5586  .usage = "address width count ['phys']",
5587  },
5588  {
5589  .name = "write_memory",
5590  .mode = COMMAND_EXEC,
5591  .handler = handle_target_write_memory,
5592  .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
5593  .usage = "address width data ['phys']",
5594  },
5595  {
5596  .name = "eventlist",
5597  .handler = handle_target_event_list,
5598  .mode = COMMAND_EXEC,
5599  .help = "displays a table of events defined for this target",
5600  .usage = "",
5601  },
5602  {
5603  .name = "curstate",
5604  .mode = COMMAND_EXEC,
5605  .handler = handle_target_current_state,
5606  .help = "displays the current state of this target",
5607  .usage = "",
5608  },
5609  {
5610  .name = "debug_reason",
5611  .mode = COMMAND_EXEC,
5612  .handler = handle_target_debug_reason,
5613  .help = "displays the debug reason of this target",
5614  .usage = "",
5615  },
5616  {
5617  .name = "arp_examine",
5618  .mode = COMMAND_EXEC,
5619  .handler = handle_target_examine,
5620  .help = "used internally for reset processing",
5621  .usage = "['allow-defer']",
5622  },
5623  {
5624  .name = "was_examined",
5625  .mode = COMMAND_EXEC,
5626  .handler = handle_target_was_examined,
5627  .help = "used internally for reset processing",
5628  .usage = "",
5629  },
5630  {
5631  .name = "examine_deferred",
5632  .mode = COMMAND_EXEC,
5633  .handler = handle_target_examine_deferred,
5634  .help = "used internally for reset processing",
5635  .usage = "",
5636  },
5637  {
5638  .name = "arp_halt_gdb",
5639  .mode = COMMAND_EXEC,
5640  .handler = handle_target_halt_gdb,
5641  .help = "used internally for reset processing to halt GDB",
5642  .usage = "",
5643  },
5644  {
5645  .name = "arp_poll",
5646  .mode = COMMAND_EXEC,
5647  .handler = handle_target_poll,
5648  .help = "used internally for reset processing",
5649  .usage = "",
5650  },
5651  {
5652  .name = "arp_reset",
5653  .mode = COMMAND_EXEC,
5654  .handler = handle_target_reset,
5655  .help = "used internally for reset processing",
5656  .usage = "'assert'|'deassert' halt",
5657  },
5658  {
5659  .name = "arp_halt",
5660  .mode = COMMAND_EXEC,
5661  .handler = handle_target_halt,
5662  .help = "used internally for reset processing",
5663  .usage = "",
5664  },
5665  {
5666  .name = "arp_waitstate",
5667  .mode = COMMAND_EXEC,
5668  .handler = handle_target_wait_state,
5669  .help = "used internally for reset processing",
5670  .usage = "statename timeoutmsecs",
5671  },
5672  {
5673  .name = "invoke-event",
5674  .mode = COMMAND_EXEC,
5675  .handler = handle_target_invoke_event,
5676  .help = "invoke handler for specified event",
5677  .usage = "event_name",
5678  },
5680 };
5681 
5682 COMMAND_HANDLER(handle_target_create)
5683 {
5684  int retval = ERROR_OK;
5685 
5686  if (CMD_ARGC < 2)
5688 
5689  /* check if the target name clashes with an existing command name */
5690  Jim_Cmd *jimcmd = Jim_GetCommand(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], JIM_NONE);
5691  if (jimcmd) {
5692  command_print(CMD, "Command/target: %s Exists", CMD_ARGV[0]);
5693  return ERROR_FAIL;
5694  }
5695 
5696  /* TYPE */
5697  const char *cp = CMD_ARGV[1];
5698  struct transport *tr = get_current_transport();
5699  if (tr && tr->override_target) {
5700  retval = tr->override_target(&cp);
5701  if (retval != ERROR_OK) {
5702  command_print(CMD, "The selected transport doesn't support this target");
5703  return retval;
5704  }
5705  LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5706  }
5707  /* now does target type exist */
5708  size_t x;
5709  for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
5710  if (strcmp(cp, target_types[x]->name) == 0) {
5711  /* found */
5712  break;
5713  }
5714  }
5715  if (x == ARRAY_SIZE(target_types)) {
5716  char *all = NULL;
5717  for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
5718  char *prev = all;
5719  if (all)
5720  all = alloc_printf("%s, %s", all, target_types[x]->name);
5721  else
5722  all = alloc_printf("%s", target_types[x]->name);
5723  free(prev);
5724  if (!all) {
5725  LOG_ERROR("Out of memory");
5726  return ERROR_FAIL;
5727  }
5728  }
5729  command_print(CMD, "Unknown target type %s, try one of %s", cp, all);
5730  free(all);
5731  return ERROR_FAIL;
5732  }
5733 
5734  /* Create it */
5735  struct target *target = calloc(1, sizeof(struct target));
5736  if (!target) {
5737  LOG_ERROR("Out of memory");
5738  return ERROR_FAIL;
5739  }
5740 
5741  /* set empty smp cluster */
5742  target->smp_targets = &empty_smp_targets;
5743 
5744  /* allocate memory for each unique target type */
5745  target->type = malloc(sizeof(struct target_type));
5746  if (!target->type) {
5747  LOG_ERROR("Out of memory");
5748  free(target);
5749  return ERROR_FAIL;
5750  }
5751 
5752  memcpy(target->type, target_types[x], sizeof(struct target_type));
5753 
5754  /* default to first core, override with -coreid */
5755  target->coreid = 0;
5756 
5757  target->working_area = 0x0;
5758  target->working_area_size = 0x0;
5760  target->backup_working_area = false;
5761 
5764  target->reg_cache = NULL;
5765  target->breakpoints = NULL;
5766  target->watchpoints = NULL;
5767  target->next = NULL;
5768  target->arch_info = NULL;
5769 
5770  target->verbose_halt_msg = true;
5771 
5772  target->halt_issued = false;
5773 
5775 
5776  /* initialize trace information */
5777  target->trace_info = calloc(1, sizeof(struct trace));
5778  if (!target->trace_info) {
5779  LOG_ERROR("Out of memory");
5780  free(target->type);
5781  free(target);
5782  return ERROR_FAIL;
5783  }
5784 
5785  target->dbgmsg = NULL;
5786  target->dbg_msg_enabled = false;
5787 
5789 
5790  target->rtos = NULL;
5791  target->rtos_auto_detect = false;
5792 
5795 
5796  target->cmd_name = strdup(CMD_ARGV[0]);
5797  if (!target->cmd_name) {
5798  LOG_ERROR("Out of memory");
5799  free(target->trace_info);
5800  free(target->type);
5801  free(target);
5802  return ERROR_FAIL;
5803  }
5804 
5805  /* Do the rest as "configure" options */
5806  bool is_configure = true;
5807  retval = CALL_COMMAND_HANDLER(target_configure, target, 2, is_configure);
5808  if (retval == ERROR_OK) {
5809  if (target->has_dap) {
5810  if (!target->dap_configured) {
5811  command_print(CMD, "-dap ?name? required when creating target");
5813  }
5814  } else {
5815  if (!target->tap_configured) {
5816  command_print(CMD, "-chain-position ?name? required when creating target");
5818  }
5819  }
5820  /* tap must be set after target was configured */
5821  if (!target->tap)
5823  }
5824 
5825  if (retval != ERROR_OK) {
5827  free(target->gdb_port_override);
5828  free(target->trace_info);
5829  free(target->type);
5830  free(target->private_config);
5831  free(target);
5832  return retval;
5833  }
5834 
5836  /* default endian to little if not specified */
5838  }
5839 
5840  if (target->type->target_create) {
5841  retval = (*target->type->target_create)(target);
5842  if (retval != ERROR_OK) {
5843  LOG_DEBUG("target_create failed");
5844  free(target->cmd_name);
5846  free(target->gdb_port_override);
5847  free(target->trace_info);
5848  free(target->type);
5849  free(target->private_config);
5850  free(target);
5851  return retval;
5852  }
5853  }
5854 
5855  /* create the target specific commands */
5856  if (target->type->commands) {
5858  if (retval != ERROR_OK)
5859  LOG_ERROR("unable to register '%s' commands", CMD_ARGV[0]);
5860  }
5861 
5862  /* now - create the new target name command */
5863  const struct command_registration target_subcommands[] = {
5864  {
5866  },
5867  {
5868  .chain = target->type->commands,
5869  },
5871  };
5872  const struct command_registration target_commands[] = {
5873  {
5874  .name = CMD_ARGV[0],
5875  .mode = COMMAND_ANY,
5876  .help = "target command group",
5877  .usage = "",
5878  .chain = target_subcommands,
5879  },
5881  };
5882  retval = register_commands_override_target(CMD_CTX, NULL, target_commands, target);
5883  if (retval != ERROR_OK) {
5884  if (target->type->deinit_target)
5886  free(target->cmd_name);
5888  free(target->gdb_port_override);
5889  free(target->trace_info);
5890  free(target->type);
5891  free(target);
5892  return retval;
5893  }
5894 
5895  /* append to end of list */
5897 
5898  CMD_CTX->current_target = target;
5899  return ERROR_OK;
5900 }
5901 
5902 COMMAND_HANDLER(handle_target_current)
5903 {
5904  if (CMD_ARGC != 0)
5906 
5908  if (target)
5910 
5911  return ERROR_OK;
5912 }
5913 
5914 COMMAND_HANDLER(handle_target_types)
5915 {
5916  if (CMD_ARGC != 0)
5918 
5919  for (size_t x = 0; x < ARRAY_SIZE(target_types); x++)
5920  command_print(CMD, "%s", target_types[x]->name);
5921 
5922  return ERROR_OK;
5923 }
5924 
5925 COMMAND_HANDLER(handle_target_names)
5926 {
5927  if (CMD_ARGC != 0)
5929 
5930  struct target *target = all_targets;
5931  while (target) {
5933  target = target->next;
5934  }
5935 
5936  return ERROR_OK;
5937 }
5938 
5939 static struct target_list *
5940 __attribute__((warn_unused_result))
5941 create_target_list_node(const char *targetname)
5942 {
5943  struct target *target = get_target(targetname);
5944  LOG_DEBUG("%s ", targetname);
5945  if (!target)
5946  return NULL;
5947 
5948  struct target_list *new = malloc(sizeof(struct target_list));
5949  if (!new) {
5950  LOG_ERROR("Out of memory");
5951  return new;
5952  }
5953 
5954  new->target = target;
5955  return new;
5956 }
5957 
5959  struct list_head *lh, struct target **result)
5960 {
5961  struct target *target = NULL;
5962  struct target_list *curr;
5963  foreach_smp_target(curr, lh) {
5964  struct rtos *curr_rtos = curr->target->rtos;
5965  if (curr_rtos) {
5966  if (target && target->rtos && target->rtos->type != curr_rtos->type) {
5967  command_print(cmd, "Different rtos types in members of one smp target!");
5968  return ERROR_FAIL;
5969  }
5970  target = curr->target;
5971  }
5972  }
5973  *result = target;
5974  return ERROR_OK;
5975 }
5976 
5977 COMMAND_HANDLER(handle_target_smp)
5978 {
5979  static unsigned int smp_group = 1;
5980 
5981  if (CMD_ARGC == 0) {
5982  LOG_DEBUG("Empty SMP target");
5983  return ERROR_OK;
5984  }
5985  LOG_DEBUG("%d", CMD_ARGC);
5986  /* CMD_ARGC[0] = target to associate in smp
5987  * CMD_ARGC[1] = target to associate in smp
5988  * CMD_ARGC[2] ...
5989  */
5990 
5991  struct list_head *lh = malloc(sizeof(*lh));
5992  if (!lh) {
5993  LOG_ERROR("Out of memory");
5994  return ERROR_FAIL;
5995  }
5996  INIT_LIST_HEAD(lh);
5997 
5998  for (unsigned int i = 0; i < CMD_ARGC; i++) {
5999  struct target_list *new = create_target_list_node(CMD_ARGV[i]);
6000  if (new)
6001  list_add_tail(&new->lh, lh);
6002  }
6003  /* now parse the list of cpu and put the target in smp mode*/
6004  struct target_list *curr;
6005  foreach_smp_target(curr, lh) {
6006  struct target *target = curr->target;
6007  target->smp = smp_group;
6008  target->smp_targets = lh;
6009  }
6010  smp_group++;
6011 
6012  struct target *rtos_target;
6013  int retval = get_target_with_common_rtos_type(CMD, lh, &rtos_target);
6014  if (retval == ERROR_OK && rtos_target)
6015  retval = rtos_smp_init(rtos_target);
6016 
6017  return retval;
6018 }
6019 
6020 static const struct command_registration target_subcommand_handlers[] = {
6021  {
6022  .name = "init",
6023  .mode = COMMAND_CONFIG,
6024  .handler = handle_target_init_command,
6025  .help = "initialize targets",
6026  .usage = "",
6027  },
6028  {
6029  .name = "create",
6030  .mode = COMMAND_CONFIG,
6031  .handler = handle_target_create,
6032  .usage = "name type [options ...]",
6033  .help = "Creates and selects a new target",
6034  },
6035  {
6036  .name = "current",
6037  .mode = COMMAND_ANY,
6038  .handler = handle_target_current,
6039  .help = "Returns the currently selected target",
6040  .usage = "",
6041  },
6042  {
6043  .name = "types",
6044  .mode = COMMAND_ANY,
6045  .handler = handle_target_types,
6046  .help = "Returns the available target types as "
6047  "a list of strings",
6048  .usage = "",
6049  },
6050  {
6051  .name = "names",
6052  .mode = COMMAND_ANY,
6053  .handler = handle_target_names,
6054  .help = "Returns the names of all targets as a list of strings",
6055  .usage = "",
6056  },
6057  {
6058  .name = "smp",
6059  .mode = COMMAND_ANY,
6060  .handler = handle_target_smp,
6061  .usage = "targetname1 targetname2 ...",
6062  .help = "gather several target in a smp list"
6063  },
6064 
6066 };
6067 
6068 struct fast_load {
6070  uint8_t *data;
6071  int length;
6072 
6073 };
6074 
6075 static int fastload_num;
6076 static struct fast_load *fastload;
6077 
6078 static void free_fastload(void)
6079 {
6080  if (fastload) {
6081  for (int i = 0; i < fastload_num; i++)
6082  free(fastload[i].data);
6083  free(fastload);
6084  fastload = NULL;
6085  }
6086 }
6087 
6088 COMMAND_HANDLER(handle_fast_load_image_command)
6089 {
6090  uint8_t *buffer;
6091  size_t buf_cnt;
6092  uint32_t image_size;
6093  target_addr_t min_address = 0;
6094  target_addr_t max_address = -1;
6095 
6096  struct image image;
6097 
6098  int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
6099  &image, &min_address, &max_address);
6100  if (retval != ERROR_OK)
6101  return retval;
6102 
6103  struct duration bench;
6104  duration_start(&bench);
6105 
6106  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
6107  if (retval != ERROR_OK)
6108  return retval;
6109 
6110  image_size = 0x0;
6111  retval = ERROR_OK;
6113  fastload = malloc(sizeof(struct fast_load)*image.num_sections);
6114  if (!fastload) {
6115  command_print(CMD, "out of memory");
6116  image_close(&image);
6117  return ERROR_FAIL;
6118  }
6119  memset(fastload, 0, sizeof(struct fast_load)*image.num_sections);
6120  for (unsigned int i = 0; i < image.num_sections; i++) {
6121  buffer = malloc(image.sections[i].size);
6122  if (!buffer) {
6123  command_print(CMD, "error allocating buffer for section (%d bytes)",
6124  (int)(image.sections[i].size));
6125  retval = ERROR_FAIL;
6126  break;
6127  }
6128 
6129  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
6130  if (retval != ERROR_OK) {
6131  free(buffer);
6132  break;
6133  }
6134 
6135  uint32_t offset = 0;
6136  uint32_t length = buf_cnt;
6137 
6138  /* DANGER!!! beware of unsigned comparison here!!! */
6139 
6140  if ((image.sections[i].base_address + buf_cnt >= min_address) &&
6141  (image.sections[i].base_address < max_address)) {
6142  if (image.sections[i].base_address < min_address) {
6143  /* clip addresses below */
6144  offset += min_address-image.sections[i].base_address;
6145  length -= offset;
6146  }
6147 
6148  if (image.sections[i].base_address + buf_cnt > max_address)
6149  length -= (image.sections[i].base_address + buf_cnt)-max_address;
6150 
6152  fastload[i].data = malloc(length);
6153  if (!fastload[i].data) {
6154  free(buffer);
6155  command_print(CMD, "error allocating buffer for section (%" PRIu32 " bytes)",
6156  length);
6157  retval = ERROR_FAIL;
6158  break;
6159  }
6160  memcpy(fastload[i].data, buffer + offset, length);
6161  fastload[i].length = length;
6162 
6163  image_size += length;
6164  command_print(CMD, "%u bytes written at address 0x%8.8x",
6165  (unsigned int)length,
6166  ((unsigned int)(image.sections[i].base_address + offset)));
6167  }
6168 
6169  free(buffer);
6170  }
6171 
6172  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
6173  command_print(CMD, "Loaded %" PRIu32 " bytes "
6174  "in %fs (%0.3f KiB/s)", image_size,
6175  duration_elapsed(&bench), duration_kbps(&bench, image_size));
6176 
6178  "WARNING: image has not been loaded to target!"
6179  "You can issue a 'fast_load' to finish loading.");
6180  }
6181 
6182  image_close(&image);
6183 
6184  if (retval != ERROR_OK)
6185  free_fastload();
6186 
6187  return retval;
6188 }
6189 
6190 COMMAND_HANDLER(handle_fast_load_command)
6191 {
6192  if (CMD_ARGC > 0)
6194  if (!fastload) {
6195  LOG_ERROR("No image in memory");
6196  return ERROR_FAIL;
6197  }
6198  int i;
6199  int64_t ms = timeval_ms();
6200  int size = 0;
6201  int retval = ERROR_OK;
6202  for (i = 0; i < fastload_num; i++) {
6204  command_print(CMD, "Write to 0x%08x, length 0x%08x",
6205  (unsigned int)(fastload[i].address),
6206  (unsigned int)(fastload[i].length));
6207  retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
6208  if (retval != ERROR_OK)
6209  break;
6210  size += fastload[i].length;
6211  }
6212  if (retval == ERROR_OK) {
6213  int64_t after = timeval_ms();
6214  command_print(CMD, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
6215  }
6216  return retval;
6217 }
6218 
6219 static const struct command_registration target_command_handlers[] = {
6220  {
6221  .name = "targets",
6222  .handler = handle_targets_command,
6223  .mode = COMMAND_ANY,
6224  .help = "change current default target (one parameter) "
6225  "or prints table of all targets (no parameters)",
6226  .usage = "[target]",
6227  },
6228  {
6229  .name = "target",
6230  .mode = COMMAND_CONFIG,
6231  .help = "configure target",
6232  .chain = target_subcommand_handlers,
6233  .usage = "",
6234  },
6236 };
6237 
6239 {
6241 }
6242 
6243 static bool target_reset_nag = true;
6244 
6246 {
6247  return target_reset_nag;
6248 }
6249 
6250 COMMAND_HANDLER(handle_target_reset_nag)
6251 {
6252  return CALL_COMMAND_HANDLER(handle_command_parse_bool,
6253  &target_reset_nag, "Nag after each reset about options to improve "
6254  "performance");
6255 }
6256 
6257 COMMAND_HANDLER(handle_ps_command)
6258 {
6260  char *display;
6261  if (target->state != TARGET_HALTED) {
6262  command_print(CMD, "Error: [%s] not halted", target_name(target));
6263  return ERROR_TARGET_NOT_HALTED;
6264  }
6265 
6266  if ((target->rtos) && (target->rtos->type)
6267  && (target->rtos->type->ps_command)) {
6268  display = target->rtos->type->ps_command(target);
6269  command_print(CMD, "%s", display);
6270  free(display);
6271  return ERROR_OK;
6272  } else {
6273  LOG_INFO("failed");
6274  return ERROR_TARGET_FAILURE;
6275  }
6276 }
6277 
6278 static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
6279 {
6280  if (text)
6281  command_print_sameline(cmd, "%s", text);
6282  for (int i = 0; i < size; i++)
6283  command_print_sameline(cmd, " %02x", buf[i]);
6284  command_print(cmd, " ");
6285 }
6286 
6287 COMMAND_HANDLER(handle_test_mem_access_command)
6288 {
6290  uint32_t test_size;
6291  int retval = ERROR_OK;
6292 
6293  if (target->state != TARGET_HALTED) {
6294  command_print(CMD, "Error: [%s] not halted", target_name(target));
6295  return ERROR_TARGET_NOT_HALTED;
6296  }
6297 
6298  if (CMD_ARGC != 1)
6300 
6301  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
6302 
6303  /* Test reads */
6304  size_t num_bytes = test_size + 4;
6305 
6306  struct working_area *wa = NULL;
6307  retval = target_alloc_working_area(target, num_bytes, &wa);
6308  if (retval != ERROR_OK) {
6309  LOG_ERROR("Not enough working area");
6310  return ERROR_FAIL;
6311  }
6312 
6313  uint8_t *test_pattern = malloc(num_bytes);
6314 
6315  for (size_t i = 0; i < num_bytes; i++)
6316  test_pattern[i] = rand();
6317 
6318  retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6319  if (retval != ERROR_OK) {
6320  LOG_ERROR("Test pattern write failed");
6321  goto out;
6322  }
6323 
6324  for (int host_offset = 0; host_offset <= 1; host_offset++) {
6325  for (int size = 1; size <= 4; size *= 2) {
6326  for (int offset = 0; offset < 4; offset++) {
6327  uint32_t count = test_size / size;
6328  size_t host_bufsiz = (count + 2) * size + host_offset;
6329  uint8_t *read_ref = malloc(host_bufsiz);
6330  uint8_t *read_buf = malloc(host_bufsiz);
6331 
6332  for (size_t i = 0; i < host_bufsiz; i++) {
6333  read_ref[i] = rand();
6334  read_buf[i] = read_ref[i];
6335  }
6337  "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6338  size, offset, host_offset ? "un" : "");
6339 
6340  struct duration bench;
6341  duration_start(&bench);
6342 
6343  retval = target_read_memory(target, wa->address + offset, size, count,
6344  read_buf + size + host_offset);
6345 
6346  duration_measure(&bench);
6347 
6348  if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6349  command_print(CMD, "Unsupported alignment");
6350  goto next;
6351  } else if (retval != ERROR_OK) {
6352  command_print(CMD, "Memory read failed");
6353  goto next;
6354  }
6355 
6356  /* replay on host */
6357  memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6358 
6359  /* check result */
6360  int result = memcmp(read_ref, read_buf, host_bufsiz);
6361  if (result == 0) {
6362  command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6363  duration_elapsed(&bench),
6364  duration_kbps(&bench, count * size));
6365  } else {
6366  command_print(CMD, "Compare failed");
6367  binprint(CMD, "ref:", read_ref, host_bufsiz);
6368  binprint(CMD, "buf:", read_buf, host_bufsiz);
6369  }
6370 next:
6371  free(read_ref);
6372  free(read_buf);
6373  }
6374  }
6375  }
6376 
6377 out:
6378  free(test_pattern);
6379 
6381 
6382  /* Test writes */
6383  num_bytes = test_size + 4 + 4 + 4;
6384 
6385  retval = target_alloc_working_area(target, num_bytes, &wa);
6386  if (retval != ERROR_OK) {
6387  LOG_ERROR("Not enough working area");
6388  return ERROR_FAIL;
6389  }
6390 
6391  test_pattern = malloc(num_bytes);
6392 
6393  for (size_t i = 0; i < num_bytes; i++)
6394  test_pattern[i] = rand();
6395 
6396  for (int host_offset = 0; host_offset <= 1; host_offset++) {
6397  for (int size = 1; size <= 4; size *= 2) {
6398  for (int offset = 0; offset < 4; offset++) {
6399  uint32_t count = test_size / size;
6400  size_t host_bufsiz = count * size + host_offset;
6401  uint8_t *read_ref = malloc(num_bytes);
6402  uint8_t *read_buf = malloc(num_bytes);
6403  uint8_t *write_buf = malloc(host_bufsiz);
6404 
6405  for (size_t i = 0; i < host_bufsiz; i++)
6406  write_buf[i] = rand();
6408  "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6409  size, offset, host_offset ? "un" : "");
6410 
6411  retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6412  if (retval != ERROR_OK) {
6413  command_print(CMD, "Test pattern write failed");
6414  goto nextw;
6415  }
6416 
6417  /* replay on host */
6418  memcpy(read_ref, test_pattern, num_bytes);
6419  memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6420 
6421  struct duration bench;
6422  duration_start(&bench);
6423 
6424  retval = target_write_memory(target, wa->address + size + offset, size, count,
6425  write_buf + host_offset);
6426 
6427  duration_measure(&bench);
6428 
6429  if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6430  command_print(CMD, "Unsupported alignment");
6431  goto nextw;
6432  } else if (retval != ERROR_OK) {
6433  command_print(CMD, "Memory write failed");
6434  goto nextw;
6435  }
6436 
6437  /* read back */
6438  retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6439  if (retval != ERROR_OK) {
6440  command_print(CMD, "Test pattern write failed");
6441  goto nextw;
6442  }
6443 
6444  /* check result */
6445  int result = memcmp(read_ref, read_buf, num_bytes);
6446  if (result == 0) {
6447  command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6448  duration_elapsed(&bench),
6449  duration_kbps(&bench, count * size));
6450  } else {
6451  command_print(CMD, "Compare failed");
6452  binprint(CMD, "ref:", read_ref, num_bytes);
6453  binprint(CMD, "buf:", read_buf, num_bytes);
6454  }
6455 nextw:
6456  free(read_ref);
6457  free(read_buf);
6458  }
6459  }
6460  }
6461 
6462  free(test_pattern);
6463 
6465  return retval;
6466 }
6467 
6468 static const struct command_registration target_exec_command_handlers[] = {
6469  {
6470  .name = "fast_load_image",
6471  .handler = handle_fast_load_image_command,
6472  .mode = COMMAND_ANY,
6473  .help = "Load image into server memory for later use by "
6474  "fast_load; primarily for profiling",
6475  .usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
6476  "[min_address [max_length]]]]",
6477  },
6478  {
6479  .name = "fast_load",
6480  .handler = handle_fast_load_command,
6481  .mode = COMMAND_EXEC,
6482  .help = "loads active fast load image to current target "
6483  "- mainly for profiling purposes",
6484  .usage = "",
6485  },
6486  {
6487  .name = "profile",
6488  .handler = handle_profile_command,
6489  .mode = COMMAND_EXEC,
6490  .usage = "seconds filename [start end]",
6491  .help = "profiling samples the CPU PC",
6492  },
6494  {
6495  .name = "virt2phys",
6496  .handler = handle_virt2phys_command,
6497  .mode = COMMAND_ANY,
6498  .help = "translate a virtual address into a physical address",
6499  .usage = "virtual_address",
6500  },
6501  {
6502  .name = "reg",
6503  .handler = handle_reg_command,
6504  .mode = COMMAND_EXEC,
6505  .help = "display (reread from target with \"force\") or set a register; "
6506  "with no arguments, displays all registers and their values",
6507  .usage = "[(register_number|register_name) [(value|'force')]]",
6508  },
6509  {
6510  .name = "poll",
6511  .handler = handle_poll_command,
6512  .mode = COMMAND_EXEC,
6513  .help = "poll target state; or reconfigure background polling",
6514  .usage = "['on'|'off']",
6515  },
6516  {
6517  .name = "wait_halt",
6518  .handler = handle_wait_halt_command,
6519  .mode = COMMAND_EXEC,
6520  .help = "wait up to the specified number of milliseconds "
6521  "(default 5000) for a previously requested halt",
6522  .usage = "[milliseconds]",
6523  },
6524  {
6525  .name = "halt",
6526  .handler = handle_halt_command,
6527  .mode = COMMAND_EXEC,
6528  .help = "request target to halt, then wait up to the specified "
6529  "number of milliseconds (default 5000) for it to complete",
6530  .usage = "[milliseconds]",
6531  },
6532  {
6533  .name = "resume",
6534  .handler = handle_resume_command,
6535  .mode = COMMAND_EXEC,
6536  .help = "resume target execution from current PC or address",
6537  .usage = "[address]",
6538  },
6539  {
6540  .name = "reset",
6541  .handler = handle_reset_command,
6542  .mode = COMMAND_EXEC,
6543  .usage = "[run|halt|init]",
6544  .help = "Reset all targets into the specified mode. "
6545  "Default reset mode is run, if not given.",
6546  },
6547  {
6548  .name = "soft_reset_halt",
6549  .handler = handle_soft_reset_halt_command,
6550  .mode = COMMAND_EXEC,
6551  .usage = "",
6552  .help = "halt the target and do a soft reset",
6553  },
6554  {
6555  .name = "step",
6556  .handler = handle_step_command,
6557  .mode = COMMAND_EXEC,
6558  .help = "step one instruction from current PC or address",
6559  .usage = "[address]",
6560  },
6561  {
6562  .name = "mdd",
6563  .handler = handle_md_command,
6564  .mode = COMMAND_EXEC,
6565  .help = "display memory double-words",
6566  .usage = "['phys'] address [count]",
6567  },
6568  {
6569  .name = "mdw",
6570  .handler = handle_md_command,
6571  .mode = COMMAND_EXEC,
6572  .help = "display memory words",
6573  .usage = "['phys'] address [count]",
6574  },
6575  {
6576  .name = "mdh",
6577  .handler = handle_md_command,
6578  .mode = COMMAND_EXEC,
6579  .help = "display memory half-words",
6580  .usage = "['phys'] address [count]",
6581  },
6582  {
6583  .name = "mdb",
6584  .handler = handle_md_command,
6585  .mode = COMMAND_EXEC,
6586  .help = "display memory bytes",
6587  .usage = "['phys'] address [count]",
6588  },
6589  {
6590  .name = "mwd",
6591  .handler = handle_mw_command,
6592  .mode = COMMAND_EXEC,
6593  .help = "write memory double-word",
6594  .usage = "['phys'] address value [count]",
6595  },
6596  {
6597  .name = "mww",
6598  .handler = handle_mw_command,
6599  .mode = COMMAND_EXEC,
6600  .help = "write memory word",
6601  .usage = "['phys'] address value [count]",
6602  },
6603  {
6604  .name = "mwh",
6605  .handler = handle_mw_command,
6606  .mode = COMMAND_EXEC,
6607  .help = "write memory half-word",
6608  .usage = "['phys'] address value [count]",
6609  },
6610  {
6611  .name = "mwb",
6612  .handler = handle_mw_command,
6613  .mode = COMMAND_EXEC,
6614  .help = "write memory byte",
6615  .usage = "['phys'] address value [count]",
6616  },
6617  {
6618  .name = "bp",
6619  .handler = handle_bp_command,
6620  .mode = COMMAND_EXEC,
6621  .help = "list or set hardware or software breakpoint",
6622  .usage = "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6623  },
6624  {
6625  .name = "rbp",
6626  .handler = handle_rbp_command,
6627  .mode = COMMAND_EXEC,
6628  .help = "remove breakpoint",
6629  .usage = "'all' | address",
6630  },
6631  {
6632  .name = "wp",
6633  .handler = handle_wp_command,
6634  .mode = COMMAND_EXEC,
6635  .help = "list (no params) or create watchpoints",
6636  .usage = "[address length [('r'|'w'|'a') [value [mask]]]]",
6637  },
6638  {
6639  .name = "rwp",
6640  .handler = handle_rwp_command,
6641  .mode = COMMAND_EXEC,
6642  .help = "remove watchpoint",
6643  .usage = "'all' | address",
6644  },
6645  {
6646  .name = "load_image",
6647  .handler = handle_load_image_command,
6648  .mode = COMMAND_EXEC,
6649  .usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
6650  "[min_address [max_length]]]]",
6651  },
6652  {
6653  .name = "dump_image",
6654  .handler = handle_dump_image_command,
6655  .mode = COMMAND_EXEC,
6656  .usage = "filename address size",
6657  },
6658  {
6659  .name = "verify_image_checksum",
6660  .handler = handle_verify_image_checksum_command,
6661  .mode = COMMAND_EXEC,
6662  .usage = "filename [offset [type]]",
6663  },
6664  {
6665  .name = "verify_image",
6666  .handler = handle_verify_image_command,
6667  .mode = COMMAND_EXEC,
6668  .usage = "filename [offset [type]]",
6669  },
6670  {
6671  .name = "test_image",
6672  .handler = handle_test_image_command,
6673  .mode = COMMAND_EXEC,
6674  .usage = "filename [offset [type]]",
6675  },
6676  {
6677  .name = "get_reg",
6678  .mode = COMMAND_EXEC,
6679  .handler = handle_target_get_reg,
6680  .help = "Get register values from the target",
6681  .usage = "[-force] list",
6682  },
6683  {
6684  .name = "set_reg",
6685  .mode = COMMAND_EXEC,
6686  .handler = handle_set_reg_command,
6687  .help = "Set target register values",
6688  .usage = "dict",
6689  },
6690  {
6691  .name = "read_memory",
6692  .mode = COMMAND_EXEC,
6693  .handler = handle_target_read_memory,
6694  .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6695  .usage = "address width count ['phys']",
6696  },
6697  {
6698  .name = "write_memory",
6699  .mode = COMMAND_EXEC,
6700  .handler = handle_target_write_memory,
6701  .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6702  .usage = "address width data ['phys']",
6703  },
6704  {
6705  .name = "debug_reason",
6706  .mode = COMMAND_EXEC,
6707  .handler = handle_target_debug_reason,
6708  .help = "displays the debug reason of this target",
6709  .usage = "",
6710  },
6711  {
6712  .name = "reset_nag",
6713  .handler = handle_target_reset_nag,
6714  .mode = COMMAND_ANY,
6715  .help = "Nag after each reset about options that could have been "
6716  "enabled to improve performance.",
6717  .usage = "['enable'|'disable']",
6718  },
6719  {
6720  .name = "ps",
6721  .handler = handle_ps_command,
6722  .mode = COMMAND_EXEC,
6723  .help = "list all tasks",
6724  .usage = "",
6725  },
6726  {
6727  .name = "test_mem_access",
6728  .handler = handle_test_mem_access_command,
6729  .mode = COMMAND_EXEC,
6730  .help = "Test the target's memory access functions",
6731  .usage = "size",
6732  },
6733 
6735 };
6737 {
6738  int retval = ERROR_OK;
6739  retval = target_request_register_commands(cmd_ctx);
6740  if (retval != ERROR_OK)
6741  return retval;
6742 
6743  retval = trace_register_commands(cmd_ctx);
6744  if (retval != ERROR_OK)
6745  return retval;
6746 
6747 
6749 }
6750 
6752 {
6753  switch (reason) {
6754  case DBG_REASON_DBGRQ:
6755  return "DBGRQ";
6756  case DBG_REASON_BREAKPOINT:
6757  return "BREAKPOINT";
6758  case DBG_REASON_WATCHPOINT:
6759  return "WATCHPOINT";
6760  case DBG_REASON_WPTANDBKPT:
6761  return "WPTANDBKPT";
6762  case DBG_REASON_SINGLESTEP:
6763  return "SINGLESTEP";
6764  case DBG_REASON_NOTHALTED:
6765  return "NOTHALTED";
6766  case DBG_REASON_EXIT:
6767  return "EXIT";
6768  case DBG_REASON_EXC_CATCH:
6769  return "EXC_CATCH";
6770  case DBG_REASON_UNDEFINED:
6771  return "UNDEFINED";
6772  default:
6773  return "UNKNOWN!";
6774  }
6775 }
struct target_type aarch64_target
Definition: aarch64.c:3279
struct target_type armv8r_target
Definition: aarch64.c:3320
#define IS_ALIGNED(x, a)
Definition: align.h:22
#define IS_PWR_OF_2(x)
Definition: align.h:24
#define ALIGN_DOWN(x, a)
Definition: align.h:21
#define ALIGN_UP(x, a)
Definition: align.h:20
struct target_type arcv2_target
Definition: arc.c:2319
struct target_type arm11_target
Holds methods for ARM11xx targets.
Definition: arm11.c:1339
struct target_type arm720t_target
Holds methods for ARM720 targets.
Definition: arm720t.c:462
struct target_type arm7tdmi_target
Holds methods for ARM7TDMI targets.
Definition: arm7tdmi.c:684
struct target_type arm920t_target
Holds methods for ARM920 targets.
Definition: arm920t.c:1592
struct target_type arm926ejs_target
Holds methods for ARM926 targets.
Definition: arm926ejs.c:789
struct target_type arm946e_target
Holds methods for ARM946 targets.
Definition: arm946e.c:738
struct target_type arm966e_target
Holds methods for ARM966 targets.
Definition: arm966e.c:245
struct target_type arm9tdmi_target
Holds methods for ARM9TDMI targets.
Definition: arm9tdmi.c:888
const char * name
Definition: armv4_5.c:76
struct target_type avr32_ap7k_target
Definition: avr32_ap7k.c:580
struct target_type avr_target
Definition: avrt.c:39
char * buf_to_hex_str(const void *_buf, unsigned int buf_len)
Definition: binarybuffer.c:178
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
int watchpoint_add(struct target *target, target_addr_t address, unsigned int length, enum watchpoint_rw rw, uint64_t value, uint64_t mask)
Definition: breakpoints.c:551
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:346
int watchpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:588
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:216
int context_breakpoint_add(struct target *target, uint32_t asid, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:236
int watchpoint_remove_all(struct target *target)
Definition: breakpoints.c:467
int breakpoint_remove_all(struct target *target)
Definition: breakpoints.c:462
int hybrid_breakpoint_add(struct target *target, target_addr_t address, uint32_t asid, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:257
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
watchpoint_rw
Definition: breakpoints.h:22
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
struct command_context * current_command_context(Jim_Interp *interp)
Definition: command.c:86
int command_parse_bool_arg(const char *in, bool *out)
Definition: command.c:1306
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
int command_run_line(struct command_context *context, char *line)
Definition: command.c:497
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:171
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define COMMAND_PARSE_ADDRESS(in, out)
Definition: command.h:455
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:533
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
static int register_commands_override_target(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds, struct target *target)
Register one or more commands, as register_commands(), plus specify that command should override the ...
Definition: command.h:296
#define ERROR_COMMAND_CLOSE_CONNECTION
Definition: command.h:404
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define CMD_JIMTCL_ARGV
Use this macro to access the jimtcl arguments for the command being handled, rather than accessing th...
Definition: command.h:166
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
struct target_type cortexr4_target
Definition: cortex_a.c:3552
struct target_type cortexa_target
Definition: cortex_a.c:3472
struct target_type cortexm_target
Definition: cortex_m.c:3463
struct target_type dsp563xx_target
Holds methods for DSP563XX targets.
Definition: dsp563xx.c:2246
struct target_type dsp5680xx_target
Holds methods for dsp5680xx targets.
Definition: dsp5680xx.c:2278
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
unsigned short width
Definition: embeddedice.c:47
struct target_type esirisc_target
Definition: esirisc.c:1832
struct target_type esp32_target
Holds methods for Xtensa targets.
Definition: esp32.c:461
struct target_type esp32s2_target
Definition: esp32s2.c:498
struct target_type esp32s3_target
Holds methods for Xtensa targets.
Definition: esp32s3.c:382
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
uint8_t length
Definition: esp_usb_jtag.c:1
struct target_type fa526_target
Holds methods for FA526 targets.
Definition: fa526.c:350
struct target_type dragonite_target
Definition: feroceon.c:730
struct target_type feroceon_target
Definition: feroceon.c:691
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
static uint16_t output
Definition: ftdi.c:162
int fileio_write(struct fileio *fileio, size_t size, const void *buffer, size_t *size_written)
int fileio_close(struct fileio *fileio)
int fileio_size(struct fileio *fileio, size_t *size)
FIX!!!!
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
@ FILEIO_WRITE
Definition: helper/fileio.h:29
@ FILEIO_BINARY
Definition: helper/fileio.h:23
struct target_type hla_target
Definition: hla_target.c:645
void image_close(struct image *image)
Definition: image.c:1210
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
Definition: image.c:1078
int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *checksum)
Definition: image.c:1267
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:956
int jim_getopt_setup(struct jim_getopt_info *p, Jim_Interp *interp, int argc, Jim_Obj *const *argv)
GetOpt - how to.
Definition: jim-nvp.c:149
int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
Definition: jtag/core.c:307
void jtag_poll_unmask(bool saved)
Restore saved mask for polling.
Definition: jtag/core.c:181
struct jtag_tap * jtag_tap_by_string(const char *s)
Definition: jtag/core.c:241
void jtag_poll_set_enabled(bool value)
Assign flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:169
int jtag_srst_asserted(int *srst_asserted)
Definition: jtag/core.c:1736
bool is_jtag_poll_safe(void)
Return true if it's safe for a background polling task to access the JTAG scan chain.
Definition: jtag/core.c:146
int jtag_power_dropout(int *dropout)
Definition: jtag/core.c:1721
int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
Definition: jtag/core.c:286
bool jtag_poll_get_enabled(void)
Return flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:164
bool jtag_poll_mask(void)
Mask (disable) polling and return the current mask status that should be feed to jtag_poll_unmask() t...
Definition: jtag/core.c:174
The JTAG interface can be implemented with a software or hardware fifo.
jtag_event
Definition: jtag.h:179
@ JTAG_TAP_EVENT_ENABLE
Definition: jtag.h:182
static void list_add(struct list_head *new, struct list_head *head)
Definition: list.h:197
static void list_add_tail(struct list_head *new, struct list_head *head)
Definition: list.h:203
#define list_for_each_entry_safe(p, n, h, field)
Definition: list.h:159
#define list_for_each_entry(p, h, field)
Definition: list.h:155
static void list_del(struct list_head *entry)
Definition: list.h:88
static void INIT_LIST_HEAD(struct list_head *list)
Definition: list.h:54
void alive_sleep(uint64_t ms)
Definition: log.c:478
void keep_alive(void)
Definition: log.c:437
char * alloc_printf(const char *format,...)
Definition: log.c:386
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:164
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
struct target_type ls1_sap_target
Definition: ls1_sap.c:216
struct target_type mem_ap_target
Definition: mem_ap.c:264
#define zero
Definition: mips32.c:181
struct target_type mips_m4k_target
Definition: mips_m4k.c:1451
struct target_type mips_mips64_target
Definition: mips_mips64.c:1151
Upper level NOR flash interfaces.
void nvp_unknown_command_print(struct command_invocation *cmd, const struct nvp *nvp, const char *param_name, const char *param_value)
Definition: nvp.c:49
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
Definition: nvp.c:29
const struct nvp * nvp_value2name(const struct nvp *p, int value)
Definition: nvp.c:39
static uint32_t lh(unsigned int rd, unsigned int base, int16_t offset) __attribute__((unused))
Definition: opcodes.h:172
struct target_type or1k_target
Definition: or1k.c:1414
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
struct target_type quark_d20xx_target
Definition: quark_d20xx.c:79
struct target_type quark_x10xx_target
Definition: quark_x10xx.c:57
struct reg * register_get_by_name(struct reg_cache *first, const char *name, bool search_all)
Definition: register.c:50
#define MIN(a, b)
Definition: replacements.h:22
int gettimeofday(struct timeval *tv, struct timezone *tz)
struct target_type riscv_target
Definition: riscv.c:5930
int rtos_create(struct command_invocation *cmd, struct target *target, const char *rtos_name)
Definition: rtos.c:100
void rtos_destroy(struct target *target)
Definition: rtos.c:145
int rtos_smp_init(struct target *target)
Definition: rtos.c:40
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct target * target
Definition: rtt/rtt.c:26
bool openocd_is_shutdown_pending(void)
Definition: server.c:761
#define CONNECTION_LIMIT_UNLIMITED
Definition: server.h:34
#define ERROR_SERVER_INTERRUPTED
Definition: server.h:123
#define foreach_smp_target(pos, head)
Definition: smp.h:15
struct target_type stm8_target
Definition: stm8.c:2200
struct breakpoint * next
Definition: breakpoints.h:34
unsigned int length
Definition: breakpoints.h:29
uint8_t * orig_instr
Definition: breakpoints.h:33
enum breakpoint_type type
Definition: breakpoints.h:30
unsigned int number
Definition: breakpoints.h:32
uint32_t asid
Definition: breakpoints.h:28
target_addr_t address
Definition: breakpoints.h:27
Jim_Interp * interp
Definition: command.h:53
struct target * current_target_override
Definition: command.h:57
struct target * current_target
Definition: command.h:55
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:239
const struct command_registration * chain
If non-NULL, the commands in chain will be registered in the same context and scope of this registrat...
Definition: command.h:252
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
int length
Definition: target.c:6071
uint8_t * data
Definition: target.c:6070
target_addr_t address
Definition: target.c:6069
Definition: image.h:48
unsigned int num_sections
Definition: image.h:51
bool start_address_set
Definition: image.h:55
struct imagesection * sections
Definition: image.h:52
long long base_address
Definition: image.h:54
bool base_address_set
Definition: image.h:53
target_addr_t base_address
Definition: image.h:42
uint32_t size
Definition: image.h:43
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:136
bool is_configure
Definition: jim-nvp.h:140
Definition: jtag.h:101
bool enabled
Is this TAP currently enabled?
Definition: jtag.h:109
char * dotted_name
Definition: jtag.h:104
Definition: list.h:41
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
int value
Definition: nvp.h:63
const char * name
Definition: nvp.h:62
int(* get)(struct reg *reg)
Definition: register.h:152
int(* set)(struct reg *reg, uint8_t *buf)
Definition: register.h:153
const char * name
Definition: register.h:145
unsigned int num_regs
Definition: register.h:148
struct reg * reg_list
Definition: register.h:147
struct reg_cache * next
Definition: register.h:146
Definition: register.h:111
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
bool hidden
Definition: register.h:130
bool dirty
Definition: register.h:124
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
const char * name
Definition: rtos.h:60
char *(* ps_command)(struct target *target)
Definition: rtos.h:74
Definition: rtos.h:36
const struct rtos_type * type
Definition: rtos.h:37
char * basedir
Base directory for semihosting I/O operations.
Jim_Interp * interp
Definition: target.c:58
Jim_Obj * body
Definition: target.c:59
struct list_head list
Definition: target.c:60
enum target_event event
Definition: target.c:57
int(* callback)(struct target *target, enum target_event event, void *priv)
Definition: target.h:311
struct target_event_callback * next
Definition: target.h:313
struct list_head lh
Definition: target.h:223
struct target * target
Definition: target.h:224
int(* callback)(struct target *target, enum target_reset_mode reset_mode, void *priv)
Definition: target.h:319
struct list_head list
Definition: target.h:317
int(* callback)(void *priv)
Definition: target.h:334
struct target_timer_callback * next
Definition: target.h:340
unsigned int time_ms
Definition: target.h:335
enum target_timer_type type
Definition: target.h:336
int(* callback)(struct target *target, size_t len, uint8_t *data, void *priv)
Definition: target.h:325
struct list_head list
Definition: target.h:323
This holds methods shared between all instances of a given target type.
Definition: target_type.h:26
int(* add_context_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:160
int(* add_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:159
int(* write_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Target memory write callback.
Definition: target_type.h:130
int(* hit_watchpoint)(struct target *target, struct watchpoint **hit_watchpoint)
Definition: target_type.h:181
const char * name
Name of this type of target.
Definition: target_type.h:31
int(* deassert_reset)(struct target *target)
The implementation is responsible for polling the target such that target->state reflects the state c...
Definition: target_type.h:76
int(* get_gdb_reg_list)(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Target register access for GDB.
Definition: target_type.h:99
int(* resume)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Definition: target_type.h:45
int(* blank_check_memory)(struct target *target, struct target_memory_check_block *blocks, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
Definition: target_type.h:143
void(* deinit_target)(struct target *target)
Free all the resources allocated by the target.
Definition: target_type.h:249
int(* halt)(struct target *target)
Definition: target_type.h:43
int(* check_reset)(struct target *target)
Definition: target_type.h:281
int(* gdb_fileio_end)(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
Definition: target_type.h:289
int(* assert_reset)(struct target *target)
Definition: target_type.h:64
int(* run_algorithm)(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Target algorithm support.
Definition: target_type.h:187
int(* wait_algorithm)(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Definition: target_type.h:195
const struct command_registration * commands
Definition: target_type.h:200
int(* profiling)(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: target_type.h:302
int(* soft_reset_halt)(struct target *target)
Definition: target_type.h:77
const char *(* get_gdb_arch)(const struct target *target)
Target architecture for GDB.
Definition: target_type.h:86
int(* arch_state)(struct target *target)
Definition: target_type.h:37
unsigned int(* address_bits)(struct target *target)
Definition: target_type.h:308
int(* read_memory)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Target memory read callback.
Definition: target_type.h:124
int(* get_gdb_fileio_info)(struct target *target, struct gdb_fileio_info *fileio_info)
Definition: target_type.h:285
unsigned int(* data_bits)(struct target *target)
Definition: target_type.h:313
int(* target_jim_configure)(struct target *target, struct jim_getopt_info *goi)
Definition: target_type.h:208
int(* step)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: target_type.h:47
int(* read_phys_memory)(struct target *target, target_addr_t phys_address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: target_type.h:264
int(* get_gdb_reg_list_noread)(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Same as get_gdb_reg_list, but doesn't read the register values.
Definition: target_type.h:105
int(* start_algorithm)(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, void *arch_info)
Definition: target_type.h:191
int(* read_buffer)(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target_type.h:134
int(* add_watchpoint)(struct target *target, struct watchpoint *watchpoint)
Definition: target_type.h:170
int(* target_create)(struct target *target)
Definition: target_type.h:203
bool(* memory_ready)(struct target *target)
Returns true if target memory is read to read/write.
Definition: target_type.h:119
int(* write_buffer)(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target_type.h:138
int(* poll)(struct target *target)
Definition: target_type.h:34
int(* mmu)(struct target *target, bool *enabled)
Definition: target_type.h:273
int(* add_hybrid_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:161
int(* examine)(struct target *target)
This method is used to perform target setup that requires JTAG access.
Definition: target_type.h:224
int(* write_phys_memory)(struct target *target, target_addr_t phys_address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: target_type.h:270
int(* remove_breakpoint)(struct target *target, struct breakpoint *breakpoint)
Definition: target_type.h:167
int(* virt2phys)(struct target *target, target_addr_t address, target_addr_t *physical)
Definition: target_type.h:254
int(* checksum_memory)(struct target *target, target_addr_t address, uint32_t count, uint32_t *checksum)
Definition: target_type.h:141
int(* remove_watchpoint)(struct target *target, struct watchpoint *watchpoint)
Definition: target_type.h:176
Definition: target.h:119
int32_t coreid
Definition: target.h:123
struct semihosting * semihosting
Definition: target.h:219
target_addr_t working_area
Definition: target.h:155
target_addr_t working_area_virt
Definition: target.h:158
uint32_t working_area_size
Definition: target.h:161
struct jtag_tap * tap
Definition: target.h:122
bool dbgbase_set
Definition: target.h:184
struct trace * trace_info
Definition: target.h:171
bool dbg_msg_enabled
Definition: target.h:173
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
bool active_polled
The flag is set after a successful examine call.
Definition: target.h:141
uint32_t dbgbase
Definition: target.h:185
void * private_config
Definition: target.h:175
char * gdb_port_override
Definition: target.h:214
enum target_endianness endianness
Definition: target.h:165
struct reg_cache * reg_cache
Definition: target.h:168
bool backup_working_area
Definition: target.h:162
bool halt_issued
Definition: target.h:180
struct list_head * smp_targets
Definition: target.h:198
struct breakpoint * breakpoints
Definition: target.h:169
struct working_area * working_areas
Definition: target.h:163
bool verbose_halt_msg
Definition: target.h:178
bool dap_configured
Definition: target.h:189
struct rtos * rtos
Definition: target.h:193
struct gdb_fileio_info * fileio_info
Definition: target.h:212
struct debug_msg_receiver * dbgmsg
Definition: target.h:172
bool rtos_auto_detect
Definition: target.h:194
int64_t halt_issued_time
Definition: target.h:181
unsigned int smp
Definition: target.h:197
struct list_head events_action
Definition: target.h:152
struct target_type * type
Definition: target.h:120
struct backoff_timer backoff
Definition: target.h:196
target_addr_t working_area_phys
Definition: target.h:160
bool has_dap
Definition: target.h:188
bool tap_configured
Definition: target.h:190
struct watchpoint * watchpoints
Definition: target.h:170
bool working_area_phys_spec
Definition: target.h:159
bool running_alg
true if the target is currently running a downloaded "algorithm" instead of arbitrary user code.
Definition: target.h:150
void * arch_info
Definition: target.h:174
int gdb_max_connections
Definition: target.h:216
bool working_area_virt_spec
Definition: target.h:157
bool reset_halt
Definition: target.h:154
bool examined
Indicates whether this target has been examined, remembers the last result of examine call.
Definition: target.h:135
char * cmd_name
Definition: target.h:121
bool defer_examine
Should we defer examine to later.
Definition: target.h:126
struct target * next
Definition: target.h:176
Definition: psoc6.c:83
Definition: trace.h:21
Wrapper for transport lifecycle operations.
Definition: transport.h:55
int(* override_target)(const char **targetname)
Optional.
Definition: transport.h:85
uint64_t mask
Definition: breakpoints.h:44
enum watchpoint_rw rw
Definition: breakpoints.h:46
struct watchpoint * next
Definition: breakpoints.h:49
unsigned int length
Definition: breakpoints.h:43
uint64_t value
Definition: breakpoints.h:45
target_addr_t address
Definition: breakpoints.h:42
uint32_t size
Definition: target.h:90
bool free
Definition: target.h:91
struct working_area * next
Definition: target.h:94
target_addr_t address
Definition: target.h:89
struct working_area ** user
Definition: target.h:93
uint8_t * backup
Definition: target.h:92
COMMAND_HANDLER(handle_target_init_command)
Definition: target.c:1591
static bool target_reset_nag
Definition: target.c:6243
void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
Definition: target.c:410
void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:370
int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
Obtain file-I/O information from target for GDB to do syscall.
Definition: target.c:1454
unsigned char UNIT[2]
Definition: target.c:4164
static int run_srst_deasserted
Definition: target.c:2820
int target_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
Add the watchpoint for target.
Definition: target.c:1351
static int target_call_timer_callback(struct target_timer_callback *cb, int64_t *now)
Definition: target.c:1848
static int no_mmu(struct target *target, bool *enabled)
Definition: target.c:652
struct target * all_targets
Definition: target.c:115
static int target_get_gdb_fileio_info_default(struct target *target, struct gdb_fileio_info *fileio_info)
Definition: target.c:2302
int target_run_read_async_algorithm(struct target *target, uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
This routine is a wrapper for asynchronous algorithms.
Definition: target.c:1097
int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2701
uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
Definition: target.c:316
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1793
struct target * get_target(const char *id)
Definition: target.c:442
void target_free_all_working_areas(struct target *target)
Definition: target.c:2179
int target_unregister_reset_callback(int(*callback)(struct target *target, enum target_reset_mode reset_mode, void *priv), void *priv)
Definition: target.c:1739
int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
Definition: target.c:2684
static const struct nvp nvp_target_event[]
Definition: target.c:165
static OOCD_LIST_HEAD(target_reset_callback_list)
static int target_write_buffer_default(struct target *target, target_addr_t address, uint32_t count, const uint8_t *buffer)
Definition: target.c:2394
int target_unregister_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1716
static void write_long(FILE *f, int l, struct target *target)
Definition: target.c:4151
int target_read_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Definition: target.c:1273
static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
Definition: target.c:6278
int target_register_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1621
static const struct command_registration target_command_handlers[]
Definition: target.c:6219
int target_write_phys_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: target.c:1301
static int run_power_restore
Definition: target.c:2817
int target_halt(struct target *target)
Definition: target.c:516
static struct target_timer_callback * target_timer_callbacks
Definition: target.c:117
int target_get_gdb_reg_list_noread(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Obtain the registers for GDB, but don't read register values from the target.
Definition: target.c:1413
bool target_supports_gdb_connection(const struct target *target)
Check if target allows GDB connections.
Definition: target.c:1424
int target_arch_state(struct target *target)
Definition: target.c:2287
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
Definition: target.c:379
static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
Definition: target.c:2115
int target_call_timer_callbacks_now(void)
Invoke this to ensure that e.g.
Definition: target.c:1913
int target_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
Remove the breakpoint for target.
Definition: target.c:1345
static int target_profiling(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: target.c:1495
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:361
int target_register_commands(struct command_context *cmd_ctx)
Definition: target.c:6238
static const struct nvp nvp_target_debug_reason[]
Definition: target.c:227
static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
Definition: target.c:2459
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
Definition: target.c:2495
static void target_merge_working_areas(struct target *target)
Definition: target.c:1967
static const struct nvp nvp_target_state[]
Definition: target.c:217
static int handle_bp_command_list(struct command_invocation *cmd)
Definition: target.c:3876
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2370
static int get_target_with_common_rtos_type(struct command_invocation *cmd, struct list_head *lh, struct target **result)
Definition: target.c:5958
int target_add_hybrid_breakpoint(struct target *target, struct breakpoint *breakpoint)
Add the ContextID & IVA breakpoint for target.
Definition: target.c:1335
static int default_examine(struct target *target)
Definition: target.c:671
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2670
int target_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
Add the breakpoint for target.
Definition: target.c:1315
target_addr_t target_address_max(struct target *target)
Return the highest accessible address for this target.
Definition: target.c:1472
int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2653
int target_unregister_timer_callback(int(*callback)(void *priv), void *priv)
Definition: target.c:1777
int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
Pass GDB file-I/O response to target after finishing host syscall.
Definition: target.c:1463
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2435
int target_unregister_trace_callback(int(*callback)(struct target *target, size_t len, uint8_t *data, void *priv), void *priv)
Definition: target.c:1758
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2602
static void write_string(FILE *f, char *s)
Definition: target.c:4159
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:785
static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
Definition: target.c:388
unsigned int target_address_bits(struct target *target)
Return the number of address bits this target supports.
Definition: target.c:1481
static struct target_list * __attribute__((warn_unused_result))
Definition: target.c:5940
int target_profiling_default(struct target *target, uint32_t *samples, uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
Definition: target.c:2318
static const struct command_registration target_subcommand_handlers[]
Definition: target.c:6020
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1287
static int jtag_enable_callback(enum jtag_event event, void *priv)
Definition: target.c:706
int target_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Obtain the registers for GDB.
Definition: target.c:1390
static const struct nvp nvp_error_target[]
Definition: target.c:139
int target_call_timer_callbacks(void)
Definition: target.c:1907
int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
Definition: target.c:2619
static struct target_event_callback * target_event_callbacks
Definition: target.c:116
static COMMAND_HELPER(parse_load_image_command, struct image *image, target_addr_t *min_address, target_addr_t *max_address)
Definition: target.c:3538
struct target * get_current_target_or_null(struct command_context *cmd_ctx)
Definition: target.c:479
static void target_split_working_area(struct working_area *area, uint32_t size)
Definition: target.c:1937
const char * target_debug_reason_str(enum target_debug_reason reason)
Definition: target.c:6751
static int target_init(struct command_context *cmd_ctx)
Definition: target.c:1565
int target_hit_watchpoint(struct target *target, struct watchpoint **hit_watchpoint)
Find out the just hit watchpoint for target.
Definition: target.c:1365
int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
Definition: target.c:1818
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2193
target_cfg_param
Definition: target.c:4816
@ TCFG_GDB_MAX_CONNECTIONS
Definition: target.c:4830
@ TCFG_CHAIN_POSITION
Definition: target.c:4825
@ TCFG_GDB_PORT
Definition: target.c:4829
@ TCFG_WORK_AREA_VIRT
Definition: target.c:4819
@ TCFG_TYPE
Definition: target.c:4817
@ TCFG_WORK_AREA_BACKUP
Definition: target.c:4822
@ TCFG_RTOS
Definition: target.c:4827
@ TCFG_DBGBASE
Definition: target.c:4826
@ TCFG_WORK_AREA_PHYS
Definition: target.c:4820
@ TCFG_ENDIAN
Definition: target.c:4823
@ TCFG_WORK_AREA_SIZE
Definition: target.c:4821
@ TCFG_EVENT
Definition: target.c:4818
@ TCFG_DEFER_EXAMINE
Definition: target.c:4828
@ TCFG_COREID
Definition: target.c:4824
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2089
static const struct command_registration target_instance_command_handlers[]
Definition: target.c:5496
bool get_target_reset_nag(void)
Definition: target.c:6245
unsigned int target_data_bits(struct target *target)
Return the number of data bits this target supports.
Definition: target.c:1488
static int find_target(struct command_invocation *cmd, const char *name)
Definition: target.c:2749
int target_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
Remove the watchpoint for target.
Definition: target.c:1360
const char * target_event_name(enum target_event event)
Return the name of a target event enumeration value.
Definition: target.c:284
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2636
int target_examine_one(struct target *target)
Examine the specified target, letting it perform any Initialisation that requires JTAG access.
Definition: target.c:684
static int power_dropout
Definition: target.c:2814
bool target_memory_ready(struct target *target)
Returns true if target memory is ready to read/write.
Definition: target.c:1251
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:269
int(* target_write_fn)(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Definition: target.c:3427
static void print_wa_layout(struct target *target)
Definition: target.c:1924
#define DEFAULT_HALT_TIMEOUT
Definition: target.c:54
int target_poll(struct target *target)
Definition: target.c:486
static int target_call_timer_callbacks_check_time(int checktime)
Definition: target.c:1859
static int sense_handler(void)
Definition: target.c:2822
static int target_timer_callback_periodic_restart(struct target_timer_callback *cb, int64_t *now)
Definition: target.c:1841
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2147
static int srst_asserted
Definition: target.c:2815
static int fastload_num
Definition: target.c:6075
static const struct nvp nvp_target_endian[]
Definition: target.c:240
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:1995
int target_register_timer_callback(int(*callback)(void *priv), unsigned int time_ms, enum target_timer_type type, void *priv)
The period is very approximate, the callback can happen much more often or much more rarely than spec...
Definition: target.c:1687
static struct nvp nvp_config_opts[]
Definition: target.c:4833
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2582
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
Definition: target.c:944
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
Definition: target.c:352
static const int polling_interval
Definition: target.c:121
void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
Definition: target.c:418
static void target_reset_examined(struct target *target)
Reset the examined flag for the given target.
Definition: target.c:661
int target_add_context_breakpoint(struct target *target, struct breakpoint *breakpoint)
Add the ContextID breakpoint for target.
Definition: target.c:1325
static int target_init_one(struct command_context *cmd_ctx, struct target *target)
Definition: target.c:1504
static int run_power_dropout
Definition: target.c:2818
int target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:565
int target_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, unsigned int num_blocks, uint8_t erased_value, unsigned int *checked)
Definition: target.c:2526
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2562
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
Definition: target.c:343
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1259
bool target_has_event_action(const struct target *target, enum target_event event)
Returns true only if the target has a handler for the specified event.
Definition: target.c:4805
static const struct command_registration target_exec_command_handlers[]
Definition: target.c:6468
void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
Definition: target.c:394
static const struct nvp nvp_reset_modes[]
Definition: target.c:248
const char * debug_reason_name(const struct target *t)
Definition: target.c:256
static int default_check_reset(struct target *target)
Definition: target.c:677
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
Definition: target.c:434
static struct fast_load * fastload
Definition: target.c:6076
int target_register_reset_callback(int(*callback)(struct target *target, enum target_reset_mode reset_mode, void *priv), void *priv)
Definition: target.c:1643
uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
Definition: target.c:334
static int run_srst_asserted
Definition: target.c:2819
void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
Definition: target.c:402
int64_t target_timer_next_event(void)
Returns when the next registered event will take place.
Definition: target.c:1918
verify_mode
Definition: target.c:3716
@ IMAGE_TEST
Definition: target.c:3717
@ IMAGE_VERIFY
Definition: target.c:3718
@ IMAGE_CHECKSUM_ONLY
Definition: target.c:3719
int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2718
static void free_fastload(void)
Definition: target.c:6078
static int handle_target(void *priv)
Definition: target.c:2875
const char * target_get_gdb_arch(const struct target *target)
Obtain the architecture for GDB.
Definition: target.c:1383
static int target_restore_working_area(struct target *target, struct working_area *area)
Definition: target.c:2100
nvp_assert
Definition: target.c:124
@ NVP_ASSERT
Definition: target.c:126
@ NVP_DEASSERT
Definition: target.c:125
static int target_gdb_fileio_end_default(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
Definition: target.c:2312
int target_wait_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Waits for an algorithm started with target_start_algorithm() to complete.
Definition: target.c:872
static int target_fill_mem(struct target *target, target_addr_t address, target_write_fn fn, unsigned int data_size, uint64_t b, unsigned int c)
Definition: target.c:3430
static void target_destroy(struct target *target)
Definition: target.c:2211
static void target_reset_active_polled(struct target *target)
Definition: target.c:666
int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
Definition: target.c:3175
int target_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Step the target.
Definition: target.c:1433
int target_examine(void)
Definition: target.c:723
int target_register_trace_callback(int(*callback)(struct target *target, size_t len, uint8_t *data, void *priv), void *priv)
Definition: target.c:1665
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:467
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
Definition: target.c:426
static int handle_bp_command_set(struct command_invocation *cmd, target_addr_t addr, uint32_t asid, unsigned int length, int hw)
Definition: target.c:3911
static void write_gmon(uint32_t *samples, uint32_t sample_num, const char *filename, bool with_range, uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
Definition: target.c:4167
static int identity_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
Definition: target.c:645
static int target_register_user_commands(struct command_context *cmd_ctx)
Definition: target.c:6736
static void append_to_list_all_targets(struct target *target)
Definition: target.c:306
void target_handle_event(struct target *target, enum target_event e)
Definition: target.c:4619
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:325
static int target_soft_reset_halt(struct target *target)
Definition: target.c:753
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:748
int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
Definition: target.c:1831
static int target_process_reset(struct command_invocation *cmd, enum target_reset_mode reset_mode)
Definition: target.c:602
static const char * target_strerror_safe(int err)
Definition: target.c:154
static int64_t target_timer_next_event_value
Definition: target.c:118
int target_start_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, target_addr_t entry_point, target_addr_t exit_point, void *arch_info)
Executes a target-specific native code algorithm and leaves it running.
Definition: target.c:827
static void write_data(FILE *f, const void *data, size_t len)
Definition: target.c:4144
void target_quit(void)
Free all the resources allocated by targets and the target layer.
Definition: target.c:2258
int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2735
static struct target_type * target_types[]
Definition: target.c:73
void target_handle_md_output(struct command_invocation *cmd, struct target *target, target_addr_t address, unsigned int size, unsigned int count, const uint8_t *buffer, bool include_address)
Definition: target.c:3305
int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
Definition: target.c:2542
const char * target_reset_mode_name(enum target_reset_mode reset_mode)
Return the name of a target reset reason enumeration value.
Definition: target.c:295
static void target_free_all_working_areas_restore(struct target *target, int restore)
Definition: target.c:2155
target_debug_reason
Definition: target.h:71
@ DBG_REASON_WPTANDBKPT
Definition: target.h:75
@ DBG_REASON_UNDEFINED
Definition: target.h:80
@ DBG_REASON_EXIT
Definition: target.h:78
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_EXC_CATCH
Definition: target.h:79
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
target_reset_mode
Definition: target.h:64
@ RESET_RUN
Definition: target.h:66
@ RESET_HALT
Definition: target.h:67
@ RESET_UNKNOWN
Definition: target.h:65
@ RESET_INIT
Definition: target.h:68
target_register_class
Definition: target.h:113
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:814
#define ERROR_TARGET_INIT_FAILED
Definition: target.h:812
static bool target_was_examined(const struct target *target)
Definition: target.h:440
static bool target_active_polled(const struct target *target)
Definition: target.h:448
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:816
#define ERROR_TARGET_INVALID
Definition: target.h:811
target_timer_type
Definition: target.h:328
@ TARGET_TIMER_TYPE_PERIODIC
Definition: target.h:330
target_event
Definition: target.h:250
@ TARGET_EVENT_DEBUG_RESUMED
Definition: target.h:282
@ TARGET_EVENT_EXAMINE_START
Definition: target.h:284
@ TARGET_EVENT_RESET_START
Definition: target.h:272
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X106
Definition: target.h:304
@ TARGET_EVENT_GDB_FLASH_WRITE_END
Definition: target.h:294
@ TARGET_EVENT_RESET_END
Definition: target.h:279
@ TARGET_EVENT_RESET_ASSERT_POST
Definition: target.h:275
@ TARGET_EVENT_RESET_DEASSERT_POST
Definition: target.h:277
@ TARGET_EVENT_HALTED
Definition: target.h:262
@ TARGET_EVENT_RESUMED
Definition: target.h:263
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X102
Definition: target.h:300
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X107
Definition: target.h:305
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X105
Definition: target.h:303
@ TARGET_EVENT_EXAMINE_FAIL
Definition: target.h:285
@ TARGET_EVENT_GDB_START
Definition: target.h:269
@ TARGET_EVENT_EXAMINE_END
Definition: target.h:286
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X104
Definition: target.h:302
@ TARGET_EVENT_RESET_INIT
Definition: target.h:278
@ TARGET_EVENT_GDB_END
Definition: target.h:270
@ TARGET_EVENT_RESET_DEASSERT_PRE
Definition: target.h:276
@ TARGET_EVENT_GDB_FLASH_ERASE_START
Definition: target.h:291
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X103
Definition: target.h:301
@ TARGET_EVENT_DEBUG_HALTED
Definition: target.h:281
@ TARGET_EVENT_RESET_ASSERT_PRE
Definition: target.h:273
@ TARGET_EVENT_RESET_ASSERT
Definition: target.h:274
@ TARGET_EVENT_GDB_FLASH_WRITE_START
Definition: target.h:293
@ TARGET_EVENT_RESUME_START
Definition: target.h:264
@ TARGET_EVENT_STEP_END
Definition: target.h:267
@ TARGET_EVENT_STEP_START
Definition: target.h:266
@ TARGET_EVENT_GDB_ATTACH
Definition: target.h:288
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X100
Definition: target.h:298
@ TARGET_EVENT_SEMIHOSTING_USER_CMD_0X101
Definition: target.h:299
@ TARGET_EVENT_RESUME_END
Definition: target.h:265
@ TARGET_EVENT_GDB_FLASH_ERASE_END
Definition: target.h:292
@ TARGET_EVENT_GDB_DETACH
Definition: target.h:289
@ TARGET_EVENT_TRACE_CONFIG
Definition: target.h:296
@ TARGET_EVENT_GDB_HALT
Definition: target.h:261
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:243
target_state
Definition: target.h:55
@ TARGET_RESET
Definition: target.h:59
@ TARGET_DEBUG_RUNNING
Definition: target.h:60
@ TARGET_UNKNOWN
Definition: target.h:56
@ TARGET_UNAVAILABLE
Definition: target.h:61
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:821
@ TARGET_BIG_ENDIAN
Definition: target.h:85
@ TARGET_ENDIAN_UNKNOWN
Definition: target.h:84
@ TARGET_LITTLE_ENDIAN
Definition: target.h:85
#define TARGET_DEFAULT_POLLING_INTERVAL
Definition: target.h:829
#define ERROR_TARGET_TIMEOUT
Definition: target.h:813
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:818
static void target_set_examined(struct target *target)
Sets the examined and active_polled flags for the given target.
Definition: target.h:457
#define ERROR_TARGET_NOT_RUNNING
Definition: target.h:820
#define ERROR_TARGET_DATA_ABORT
Definition: target.h:817
#define ERROR_TARGET_FAILURE
Definition: target.h:815
#define ERROR_TARGET_TRANSLATION_FAULT
Definition: target.h:819
int target_request_register_commands(struct command_context *cmd_ctx)
struct target_type testee_target
Definition: testee.c:53
struct target_type xtensa_chip_target
Methods for generic example of Xtensa-based chip-level targets.
Definition: xtensa_chip.c:151
struct target_type xscale_target
Definition: xscale.c:3701
float duration_elapsed(const struct duration *duration)
Definition: time_support.c:83
int timeval_compare(const struct timeval *x, const struct timeval *y)
Definition: time_support.c:55
int timeval_add_time(struct timeval *result, long sec, long usec)
Definition: time_support.c:41
int duration_measure(struct duration *duration)
Update the duration->elapsed field to finish the duration measurement.
Definition: time_support.c:74
int duration_start(struct duration *duration)
Update the duration->start field to start the duration measurement.
Definition: time_support.c:69
float duration_kbps(const struct duration *duration, size_t count)
Definition: time_support.c:90
int64_t timeval_ms(void)
int trace_register_commands(struct command_context *cmd_ctx)
Definition: trace.c:159
struct transport * get_current_transport(void)
Returns the transport currently being used by this debug or programming session.
Definition: transport.c:252
static void h_u32_to_be(uint8_t *buf, uint32_t val)
Definition: types.h:186
static uint64_t le_to_h_u64(const uint8_t *buf)
Definition: types.h:100
static uint32_t be_to_h_u24(const uint8_t *buf)
Definition: types.h:144
static void h_u16_to_be(uint8_t *buf, uint16_t val)
Definition: types.h:214
static uint64_t be_to_h_u64(const uint8_t *buf)
Definition: types.h:127
static uint16_t le_to_h_u16(const uint8_t *buf)
Definition: types.h:122
static uint32_t le_to_h_u24(const uint8_t *buf)
Definition: types.h:117
#define TARGET_ADDR_FMT
Definition: types.h:286
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static uint32_t be_to_h_u32(const uint8_t *buf)
Definition: types.h:139
uint64_t target_addr_t
Definition: types.h:279
static void h_u24_to_le(uint8_t *buf, unsigned int val)
Definition: types.h:194
static void h_u24_to_be(uint8_t *buf, unsigned int val)
Definition: types.h:201
static uint16_t be_to_h_u16(const uint8_t *buf)
Definition: types.h:149
static void h_u16_to_le(uint8_t *buf, uint16_t val)
Definition: types.h:208
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
static void h_u64_to_be(uint8_t *buf, uint64_t val)
Definition: types.h:166
static void h_u64_to_le(uint8_t *buf, uint64_t val)
Definition: types.h:154
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22