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