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, int num_blocks,
2517  uint8_t erased_value)
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, erased_value);
2528 }
2529 
2531 {
2532  uint8_t value_buf[8];
2533 
2534  int retval = target_read_memory(target, address, 8, 1, value_buf);
2535 
2536  if (retval == ERROR_OK) {
2537  *value = target_buffer_get_u64(target, value_buf);
2538  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2539  address,
2540  *value);
2541  } else {
2542  *value = 0x0;
2543  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2544  address);
2545  }
2546 
2547  return retval;
2548 }
2549 
2551 {
2552  uint8_t value_buf[4];
2553 
2554  int retval = target_read_memory(target, address, 4, 1, value_buf);
2555 
2556  if (retval == ERROR_OK) {
2557  *value = target_buffer_get_u32(target, value_buf);
2558  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2559  address,
2560  *value);
2561  } else {
2562  *value = 0x0;
2563  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2564  address);
2565  }
2566 
2567  return retval;
2568 }
2569 
2571 {
2572  uint8_t value_buf[2];
2573 
2574  int retval = target_read_memory(target, address, 2, 1, value_buf);
2575 
2576  if (retval == ERROR_OK) {
2577  *value = target_buffer_get_u16(target, value_buf);
2578  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2579  address,
2580  *value);
2581  } else {
2582  *value = 0x0;
2583  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2584  address);
2585  }
2586 
2587  return retval;
2588 }
2589 
2591 {
2592  int retval = target_read_memory(target, address, 1, 1, value);
2593 
2594  if (retval == ERROR_OK) {
2595  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2596  address,
2597  *value);
2598  } else {
2599  *value = 0x0;
2600  LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2601  address);
2602  }
2603 
2604  return retval;
2605 }
2606 
2608 {
2609  int retval;
2610  uint8_t value_buf[8];
2611 
2612  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2613  address,
2614  value);
2615 
2616  target_buffer_set_u64(target, value_buf, value);
2617  retval = target_write_memory(target, address, 8, 1, value_buf);
2618  if (retval != ERROR_OK)
2619  LOG_DEBUG("failed: %i", retval);
2620 
2621  return retval;
2622 }
2623 
2625 {
2626  int retval;
2627  uint8_t value_buf[4];
2628 
2629  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2630  address,
2631  value);
2632 
2633  target_buffer_set_u32(target, value_buf, value);
2634  retval = target_write_memory(target, address, 4, 1, value_buf);
2635  if (retval != ERROR_OK)
2636  LOG_DEBUG("failed: %i", retval);
2637 
2638  return retval;
2639 }
2640 
2642 {
2643  int retval;
2644  uint8_t value_buf[2];
2645 
2646  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2647  address,
2648  value);
2649 
2650  target_buffer_set_u16(target, value_buf, value);
2651  retval = target_write_memory(target, address, 2, 1, value_buf);
2652  if (retval != ERROR_OK)
2653  LOG_DEBUG("failed: %i", retval);
2654 
2655  return retval;
2656 }
2657 
2659 {
2660  int retval;
2661 
2662  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2663  address, value);
2664 
2665  retval = target_write_memory(target, address, 1, 1, &value);
2666  if (retval != ERROR_OK)
2667  LOG_DEBUG("failed: %i", retval);
2668 
2669  return retval;
2670 }
2671 
2673 {
2674  int retval;
2675  uint8_t value_buf[8];
2676 
2677  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64,
2678  address,
2679  value);
2680 
2681  target_buffer_set_u64(target, value_buf, value);
2682  retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2683  if (retval != ERROR_OK)
2684  LOG_DEBUG("failed: %i", retval);
2685 
2686  return retval;
2687 }
2688 
2690 {
2691  int retval;
2692  uint8_t value_buf[4];
2693 
2694  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32,
2695  address,
2696  value);
2697 
2698  target_buffer_set_u32(target, value_buf, value);
2699  retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2700  if (retval != ERROR_OK)
2701  LOG_DEBUG("failed: %i", retval);
2702 
2703  return retval;
2704 }
2705 
2707 {
2708  int retval;
2709  uint8_t value_buf[2];
2710 
2711  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2712  address,
2713  value);
2714 
2715  target_buffer_set_u16(target, value_buf, value);
2716  retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2717  if (retval != ERROR_OK)
2718  LOG_DEBUG("failed: %i", retval);
2719 
2720  return retval;
2721 }
2722 
2724 {
2725  int retval;
2726 
2727  LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2728  address, value);
2729 
2730  retval = target_write_phys_memory(target, address, 1, 1, &value);
2731  if (retval != ERROR_OK)
2732  LOG_DEBUG("failed: %i", retval);
2733 
2734  return retval;
2735 }
2736 
2737 static int find_target(struct command_invocation *cmd, const char *name)
2738 {
2739  struct target *target = get_target(name);
2740  if (!target) {
2741  command_print(cmd, "Target: %s is unknown, try one of:\n", name);
2742  return ERROR_FAIL;
2743  }
2744  if (!target->tap->enabled) {
2745  command_print(cmd, "Target: TAP %s is disabled, "
2746  "can't be the current target\n",
2747  target->tap->dotted_name);
2748  return ERROR_FAIL;
2749  }
2750 
2751  cmd->ctx->current_target = target;
2752  if (cmd->ctx->current_target_override)
2753  cmd->ctx->current_target_override = target;
2754 
2755  return ERROR_OK;
2756 }
2757 
2758 
2759 COMMAND_HANDLER(handle_targets_command)
2760 {
2761  int retval = ERROR_OK;
2762  if (CMD_ARGC == 1) {
2763  retval = find_target(CMD, CMD_ARGV[0]);
2764  if (retval == ERROR_OK) {
2765  /* we're done! */
2766  return retval;
2767  }
2768  }
2769 
2770  unsigned int index = 0;
2771  command_print(CMD, " TargetName Type Endian TapName State ");
2772  command_print(CMD, "-- ------------------ ---------- ------ ------------------ ------------");
2773  for (struct target *target = all_targets; target; target = target->next, ++index) {
2774  const char *state;
2775  char marker = ' ';
2776 
2777  if (target->tap->enabled)
2779  else
2780  state = "tap-disabled";
2781 
2782  if (CMD_CTX->current_target == target)
2783  marker = '*';
2784 
2785  /* keep columns lined up to match the headers above */
2787  "%2d%c %-18s %-10s %-6s %-18s %s",
2788  index,
2789  marker,
2794  state);
2795  }
2796 
2797  return retval;
2798 }
2799 
2800 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2801 
2802 static int power_dropout;
2803 static int srst_asserted;
2804 
2809 
2810 static int sense_handler(void)
2811 {
2812  static int prev_srst_asserted;
2813  static int prev_power_dropout;
2814 
2815  int retval = jtag_power_dropout(&power_dropout);
2816  if (retval != ERROR_OK)
2817  return retval;
2818 
2819  int power_restored;
2820  power_restored = prev_power_dropout && !power_dropout;
2821  if (power_restored)
2822  run_power_restore = 1;
2823 
2824  int64_t current = timeval_ms();
2825  static int64_t last_power;
2826  bool wait_more = last_power + 2000 > current;
2827  if (power_dropout && !wait_more) {
2828  run_power_dropout = 1;
2829  last_power = current;
2830  }
2831 
2832  retval = jtag_srst_asserted(&srst_asserted);
2833  if (retval != ERROR_OK)
2834  return retval;
2835 
2836  int srst_deasserted;
2837  srst_deasserted = prev_srst_asserted && !srst_asserted;
2838 
2839  static int64_t last_srst;
2840  wait_more = last_srst + 2000 > current;
2841  if (srst_deasserted && !wait_more) {
2842  run_srst_deasserted = 1;
2843  last_srst = current;
2844  }
2845 
2846  if (!prev_srst_asserted && srst_asserted)
2847  run_srst_asserted = 1;
2848 
2849  prev_srst_asserted = srst_asserted;
2850  prev_power_dropout = power_dropout;
2851 
2852  if (srst_deasserted || power_restored) {
2853  /* Other than logging the event we can't do anything here.
2854  * Issuing a reset is a particularly bad idea as we might
2855  * be inside a reset already.
2856  */
2857  }
2858 
2859  return ERROR_OK;
2860 }
2861 
2862 /* process target state changes */
2863 static int handle_target(void *priv)
2864 {
2865  Jim_Interp *interp = (Jim_Interp *)priv;
2866  int retval = ERROR_OK;
2867 
2868  if (!is_jtag_poll_safe()) {
2869  /* polling is disabled currently */
2870  return ERROR_OK;
2871  }
2872 
2873  /* we do not want to recurse here... */
2874  static int recursive;
2875  if (!recursive) {
2876  recursive = 1;
2877  sense_handler();
2878  /* danger! running these procedures can trigger srst assertions and power dropouts.
2879  * We need to avoid an infinite loop/recursion here and we do that by
2880  * clearing the flags after running these events.
2881  */
2882  int did_something = 0;
2883  if (run_srst_asserted) {
2884  LOG_INFO("srst asserted detected, running srst_asserted proc.");
2885  Jim_Eval(interp, "srst_asserted");
2886  did_something = 1;
2887  }
2888  if (run_srst_deasserted) {
2889  Jim_Eval(interp, "srst_deasserted");
2890  did_something = 1;
2891  }
2892  if (run_power_dropout) {
2893  LOG_INFO("Power dropout detected, running power_dropout proc.");
2894  Jim_Eval(interp, "power_dropout");
2895  did_something = 1;
2896  }
2897  if (run_power_restore) {
2898  Jim_Eval(interp, "power_restore");
2899  did_something = 1;
2900  }
2901 
2902  if (did_something) {
2903  /* clear detect flags */
2904  sense_handler();
2905  }
2906 
2907  /* clear action flags */
2908 
2909  run_srst_asserted = 0;
2910  run_srst_deasserted = 0;
2911  run_power_restore = 0;
2912  run_power_dropout = 0;
2913 
2914  recursive = 0;
2915  }
2916 
2917  /* Poll targets for state changes unless that's globally disabled.
2918  * Skip targets that are currently disabled.
2919  */
2920  for (struct target *target = all_targets;
2922  target = target->next) {
2923 
2925  continue;
2926 
2927  if (!target->tap->enabled)
2928  continue;
2929 
2930  if (target->backoff.times > target->backoff.count) {
2931  /* do not poll this time as we failed previously */
2932  target->backoff.count++;
2933  continue;
2934  }
2935  target->backoff.count = 0;
2936 
2937  /* only poll target if we've got power and srst isn't asserted */
2938  if (!power_dropout && !srst_asserted) {
2939  /* polling may fail silently until the target has been examined */
2940  retval = target_poll(target);
2941  if (retval != ERROR_OK) {
2942  /* 100ms polling interval. Increase interval between polling up to 5000ms */
2943  if (target->backoff.times * polling_interval < 5000) {
2944  target->backoff.times *= 2;
2945  target->backoff.times++;
2946  }
2947 
2948  /* Tell GDB to halt the debugger. This allows the user to
2949  * run monitor commands to handle the situation.
2950  */
2952  }
2953  if (target->backoff.times > 0) {
2954  LOG_TARGET_ERROR(target, "Polling failed, trying to reexamine");
2956  retval = target_examine_one(target);
2957  /* Target examination could have failed due to unstable connection,
2958  * but we set the examined flag anyway to repoll it later */
2959  if (retval != ERROR_OK) {
2961  LOG_TARGET_ERROR(target, "Examination failed, GDB will be halted. Polling again in %dms",
2963  return retval;
2964  }
2965  }
2966 
2967  /* Since we succeeded, we reset backoff count */
2968  target->backoff.times = 0;
2969  }
2970  }
2971 
2972  return retval;
2973 }
2974 
2975 COMMAND_HANDLER(handle_reg_command)
2976 {
2977  LOG_DEBUG("-");
2978 
2980  if (!target_was_examined(target)) {
2981  LOG_ERROR("Target not examined yet");
2983  }
2984  struct reg *reg = NULL;
2985 
2986  /* list all available registers for the current target */
2987  if (CMD_ARGC == 0) {
2988  struct reg_cache *cache = target->reg_cache;
2989 
2990  unsigned int count = 0;
2991  while (cache) {
2992  unsigned int i;
2993 
2994  command_print(CMD, "===== %s", cache->name);
2995 
2996  for (i = 0, reg = cache->reg_list;
2997  i < cache->num_regs;
2998  i++, reg++, count++) {
2999  if (!reg->exist || reg->hidden)
3000  continue;
3001  /* only print cached values if they are valid */
3002  if (reg->valid) {
3003  char *value = buf_to_hex_str(reg->value,
3004  reg->size);
3006  "(%i) %s (/%" PRIu32 "): 0x%s%s",
3007  count, reg->name,
3008  reg->size, value,
3009  reg->dirty
3010  ? " (dirty)"
3011  : "");
3012  free(value);
3013  } else {
3014  command_print(CMD, "(%i) %s (/%" PRIu32 ")",
3015  count, reg->name,
3016  reg->size);
3017  }
3018  }
3019  cache = cache->next;
3020  }
3021 
3022  return ERROR_OK;
3023  }
3024 
3025  /* access a single register by its ordinal number */
3026  if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
3027  unsigned int num;
3028  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
3029 
3030  struct reg_cache *cache = target->reg_cache;
3031  unsigned int count = 0;
3032  while (cache) {
3033  unsigned int i;
3034  for (i = 0; i < cache->num_regs; i++) {
3035  if (count++ == num) {
3036  reg = &cache->reg_list[i];
3037  break;
3038  }
3039  }
3040  if (reg)
3041  break;
3042  cache = cache->next;
3043  }
3044 
3045  if (!reg) {
3046  command_print(CMD, "%i is out of bounds, the current target "
3047  "has only %i registers (0 - %i)", num, count, count - 1);
3048  return ERROR_FAIL;
3049  }
3050  } else {
3051  /* access a single register by its name */
3053 
3054  if (!reg)
3055  goto not_found;
3056  }
3057 
3058  assert(reg); /* give clang a hint that we *know* reg is != NULL here */
3059 
3060  if (!reg->exist)
3061  goto not_found;
3062 
3063  /* display a register */
3064  if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
3065  && (CMD_ARGV[1][0] <= '9')))) {
3066  if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
3067  reg->valid = false;
3068 
3069  if (!reg->valid) {
3070  int retval = reg->type->get(reg);
3071  if (retval != ERROR_OK) {
3072  LOG_ERROR("Could not read register '%s'", reg->name);
3073  return retval;
3074  }
3075  }
3076  char *value = buf_to_hex_str(reg->value, reg->size);
3077  command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3078  free(value);
3079  return ERROR_OK;
3080  }
3081 
3082  /* set register value */
3083  if (CMD_ARGC == 2) {
3084  uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
3085  if (!buf) {
3086  LOG_ERROR("Failed to allocate memory");
3087  return ERROR_FAIL;
3088  }
3089 
3090  int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, CMD_ARGV[1], buf, reg->size);
3091  if (retval != ERROR_OK) {
3092  free(buf);
3093  return retval;
3094  }
3095 
3096  retval = reg->type->set(reg, buf);
3097  if (retval != ERROR_OK) {
3098  LOG_ERROR("Could not write to register '%s'", reg->name);
3099  } else {
3100  char *value = buf_to_hex_str(reg->value, reg->size);
3101  command_print(CMD, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
3102  free(value);
3103  }
3104 
3105  free(buf);
3106 
3107  return retval;
3108  }
3109 
3111 
3112 not_found:
3113  command_print(CMD, "register %s not found in current target", CMD_ARGV[0]);
3114  return ERROR_FAIL;
3115 }
3116 
3117 COMMAND_HANDLER(handle_poll_command)
3118 {
3119  int retval = ERROR_OK;
3121 
3122  if (CMD_ARGC == 0) {
3123  command_print(CMD, "background polling: %s",
3124  jtag_poll_get_enabled() ? "on" : "off");
3125  command_print(CMD, "TAP: %s (%s)",
3127  target->tap->enabled ? "enabled" : "disabled");
3128  if (!target->tap->enabled)
3129  return ERROR_OK;
3130  retval = target_poll(target);
3131  if (retval != ERROR_OK)
3132  return retval;
3133  retval = target_arch_state(target);
3134  if (retval != ERROR_OK)
3135  return retval;
3136  } else if (CMD_ARGC == 1) {
3137  bool enable;
3138  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
3139  jtag_poll_set_enabled(enable);
3140  } else
3142 
3143  return retval;
3144 }
3145 
3146 COMMAND_HANDLER(handle_wait_halt_command)
3147 {
3148  if (CMD_ARGC > 1)
3150 
3151  unsigned int ms = DEFAULT_HALT_TIMEOUT;
3152  if (1 == CMD_ARGC) {
3153  int retval = parse_uint(CMD_ARGV[0], &ms);
3154  if (retval != ERROR_OK)
3156  }
3157 
3159  return target_wait_state(target, TARGET_HALTED, ms);
3160 }
3161 
3162 /* wait for target state to change. The trick here is to have a low
3163  * latency for short waits and not to suck up all the CPU time
3164  * on longer waits.
3165  */
3166 int target_wait_state(struct target *target, enum target_state state, unsigned int ms)
3167 {
3168  int retval;
3169  int64_t then = 0, cur;
3170  bool once = true;
3171 
3172  for (;;) {
3173  retval = target_poll(target);
3174  if (retval != ERROR_OK)
3175  return retval;
3176  if (target->state == state)
3177  break;
3178  cur = timeval_ms();
3179  if (once) {
3180  once = false;
3181  then = timeval_ms();
3182  LOG_DEBUG("waiting for target %s...",
3184  }
3185 
3186  keep_alive();
3188  return ERROR_SERVER_INTERRUPTED;
3189 
3190  if ((cur-then) > ms) {
3191  LOG_ERROR("timed out while waiting for target %s",
3193  return ERROR_FAIL;
3194  }
3195  }
3196 
3197  return ERROR_OK;
3198 }
3199 
3200 COMMAND_HANDLER(handle_halt_command)
3201 {
3202  LOG_DEBUG("-");
3203 
3205 
3206  target->verbose_halt_msg = true;
3207 
3208  int retval = target_halt(target);
3209  if (retval != ERROR_OK)
3210  return retval;
3211 
3212  if (CMD_ARGC == 1) {
3213  unsigned int wait_local;
3214  retval = parse_uint(CMD_ARGV[0], &wait_local);
3215  if (retval != ERROR_OK)
3217  if (!wait_local)
3218  return ERROR_OK;
3219  }
3220 
3221  return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3222 }
3223 
3224 COMMAND_HANDLER(handle_soft_reset_halt_command)
3225 {
3227 
3228  LOG_TARGET_INFO(target, "requesting target halt and executing a soft reset");
3229 
3231 
3232  return ERROR_OK;
3233 }
3234 
3235 COMMAND_HANDLER(handle_reset_command)
3236 {
3237  if (CMD_ARGC > 1)
3239 
3240  enum target_reset_mode reset_mode = RESET_RUN;
3241  if (CMD_ARGC == 1) {
3242  const struct nvp *n;
3244  if ((!n->name) || (n->value == RESET_UNKNOWN))
3246  reset_mode = n->value;
3247  }
3248 
3249  /* reset *all* targets */
3250  return target_process_reset(CMD, reset_mode);
3251 }
3252 
3253 
3254 COMMAND_HANDLER(handle_resume_command)
3255 {
3256  bool current = true;
3257  if (CMD_ARGC > 1)
3259 
3261 
3262  /* with no CMD_ARGV, resume from current pc, addr = 0,
3263  * with one arguments, addr = CMD_ARGV[0],
3264  * handle breakpoints, not debugging */
3265  target_addr_t addr = 0;
3266  if (CMD_ARGC == 1) {
3268  current = false;
3269  }
3270 
3271  return target_resume(target, current, addr, true, false);
3272 }
3273 
3274 COMMAND_HANDLER(handle_step_command)
3275 {
3276  if (CMD_ARGC > 1)
3278 
3279  LOG_DEBUG("-");
3280 
3281  /* with no CMD_ARGV, step from current pc, addr = 0,
3282  * with one argument addr = CMD_ARGV[0],
3283  * handle breakpoints, debugging */
3284  target_addr_t addr = 0;
3285  int current_pc = 1;
3286  if (CMD_ARGC == 1) {
3288  current_pc = 0;
3289  }
3290 
3292 
3293  return target_step(target, current_pc, addr, true);
3294 }
3295 
3297  struct target *target, target_addr_t address, unsigned int size,
3298  unsigned int count, const uint8_t *buffer, bool include_address)
3299 {
3300  const unsigned int line_bytecnt = 32;
3301  unsigned int line_modulo = line_bytecnt / size;
3302 
3303  char output[line_bytecnt * 4 + 1];
3304  unsigned int output_len = 0;
3305 
3306  const char *value_fmt;
3307  switch (size) {
3308  case 8:
3309  value_fmt = "%16.16"PRIx64" ";
3310  break;
3311  case 4:
3312  value_fmt = "%8.8"PRIx64" ";
3313  break;
3314  case 2:
3315  value_fmt = "%4.4"PRIx64" ";
3316  break;
3317  case 1:
3318  value_fmt = "%2.2"PRIx64" ";
3319  break;
3320  default:
3321  /* "can't happen", caller checked */
3322  LOG_ERROR("invalid memory read size: %u", size);
3323  return;
3324  }
3325 
3326  for (unsigned int i = 0; i < count; i++) {
3327  if (include_address && i % line_modulo == 0) {
3328  output_len += snprintf(output + output_len,
3329  sizeof(output) - output_len,
3330  TARGET_ADDR_FMT ": ",
3331  (address + (i * size)));
3332  }
3333 
3334  uint64_t value = 0;
3335  const uint8_t *value_ptr = buffer + i * size;
3336  switch (size) {
3337  case 8:
3338  value = target_buffer_get_u64(target, value_ptr);
3339  break;
3340  case 4:
3341  value = target_buffer_get_u32(target, value_ptr);
3342  break;
3343  case 2:
3344  value = target_buffer_get_u16(target, value_ptr);
3345  break;
3346  case 1:
3347  value = *value_ptr;
3348  }
3349  output_len += snprintf(output + output_len,
3350  sizeof(output) - output_len,
3351  value_fmt, value);
3352 
3353  if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3354  command_print(cmd, "%s", output);
3355  output_len = 0;
3356  }
3357  }
3358 }
3359 
3360 COMMAND_HANDLER(handle_md_command)
3361 {
3362  if (CMD_ARGC < 1)
3364 
3365  unsigned int size = 0;
3366  switch (CMD_NAME[2]) {
3367  case 'd':
3368  size = 8;
3369  break;
3370  case 'w':
3371  size = 4;
3372  break;
3373  case 'h':
3374  size = 2;
3375  break;
3376  case 'b':
3377  size = 1;
3378  break;
3379  default:
3381  }
3382 
3383  bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3384  int (*fn)(struct target *target,
3385  target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3386  if (physical) {
3387  CMD_ARGC--;
3388  CMD_ARGV++;
3390  } else
3391  fn = target_read_memory;
3392  if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3394 
3397 
3398  unsigned int count = 1;
3399  if (CMD_ARGC == 2)
3400  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3401 
3402  uint8_t *buffer = calloc(count, size);
3403  if (!buffer) {
3404  LOG_ERROR("Failed to allocate md read buffer");
3405  return ERROR_FAIL;
3406  }
3407 
3409  int retval = fn(target, address, size, count, buffer);
3410  if (retval == ERROR_OK)
3412 
3413  free(buffer);
3414 
3415  return retval;
3416 }
3417 
3418 typedef int (*target_write_fn)(struct target *target,
3419  target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3420 
3421 static int target_fill_mem(struct target *target,
3423  target_write_fn fn,
3424  unsigned int data_size,
3425  /* value */
3426  uint64_t b,
3427  /* count */
3428  unsigned int c)
3429 {
3430  /* We have to write in reasonably large chunks to be able
3431  * to fill large memory areas with any sane speed */
3432  const unsigned int chunk_size = 16384;
3433  uint8_t *target_buf = malloc(chunk_size * data_size);
3434  if (!target_buf) {
3435  LOG_ERROR("Out of memory");
3436  return ERROR_FAIL;
3437  }
3438 
3439  for (unsigned int i = 0; i < chunk_size; i++) {
3440  switch (data_size) {
3441  case 8:
3442  target_buffer_set_u64(target, target_buf + i * data_size, b);
3443  break;
3444  case 4:
3445  target_buffer_set_u32(target, target_buf + i * data_size, b);
3446  break;
3447  case 2:
3448  target_buffer_set_u16(target, target_buf + i * data_size, b);
3449  break;
3450  case 1:
3451  target_buffer_set_u8(target, target_buf + i * data_size, b);
3452  break;
3453  default:
3454  exit(-1);
3455  }
3456  }
3457 
3458  int retval = ERROR_OK;
3459 
3460  for (unsigned int x = 0; x < c; x += chunk_size) {
3461  unsigned int current;
3462  current = c - x;
3463  if (current > chunk_size)
3464  current = chunk_size;
3465  retval = fn(target, address + x * data_size, data_size, current, target_buf);
3466  if (retval != ERROR_OK)
3467  break;
3468  /* avoid GDB timeouts */
3469  keep_alive();
3470 
3472  retval = ERROR_SERVER_INTERRUPTED;
3473  break;
3474  }
3475  }
3476  free(target_buf);
3477 
3478  return retval;
3479 }
3480 
3481 
3482 COMMAND_HANDLER(handle_mw_command)
3483 {
3484  if (CMD_ARGC < 2)
3486  bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3487  target_write_fn fn;
3488  if (physical) {
3489  CMD_ARGC--;
3490  CMD_ARGV++;
3492  } else
3493  fn = target_write_memory;
3494  if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3496 
3499 
3500  uint64_t value;
3501  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[1], value);
3502 
3503  unsigned int count = 1;
3504  if (CMD_ARGC == 3)
3505  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3506 
3508  unsigned int wordsize;
3509  switch (CMD_NAME[2]) {
3510  case 'd':
3511  wordsize = 8;
3512  break;
3513  case 'w':
3514  wordsize = 4;
3515  break;
3516  case 'h':
3517  wordsize = 2;
3518  break;
3519  case 'b':
3520  wordsize = 1;
3521  break;
3522  default:
3524  }
3525 
3526  return target_fill_mem(target, address, fn, wordsize, value, count);
3527 }
3528 
3529 static COMMAND_HELPER(parse_load_image_command, struct image *image,
3530  target_addr_t *min_address, target_addr_t *max_address)
3531 {
3532  if (CMD_ARGC < 1 || CMD_ARGC > 5)
3534 
3535  /* a base address isn't always necessary,
3536  * default to 0x0 (i.e. don't relocate) */
3537  if (CMD_ARGC >= 2) {
3540  image->base_address = addr;
3541  image->base_address_set = true;
3542  } else
3543  image->base_address_set = false;
3544 
3545  image->start_address_set = false;
3546 
3547  if (CMD_ARGC >= 4)
3548  COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3549  if (CMD_ARGC == 5) {
3550  COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3551  /* use size (given) to find max (required) */
3552  *max_address += *min_address;
3553  }
3554 
3555  if (*min_address > *max_address)
3557 
3558  return ERROR_OK;
3559 }
3560 
3561 COMMAND_HANDLER(handle_load_image_command)
3562 {
3563  uint8_t *buffer;
3564  size_t buf_cnt;
3565  uint32_t image_size;
3566  target_addr_t min_address = 0;
3567  target_addr_t max_address = -1;
3568  struct image image;
3569 
3570  int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
3571  &image, &min_address, &max_address);
3572  if (retval != ERROR_OK)
3573  return retval;
3574 
3576 
3577  struct duration bench;
3578  duration_start(&bench);
3579 
3580  if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3581  return ERROR_FAIL;
3582 
3583  image_size = 0x0;
3584  retval = ERROR_OK;
3585  for (unsigned int i = 0; i < image.num_sections; i++) {
3586  buffer = malloc(image.sections[i].size);
3587  if (!buffer) {
3589  "error allocating buffer for section (%d bytes)",
3590  (int)(image.sections[i].size));
3591  retval = ERROR_FAIL;
3592  break;
3593  }
3594 
3595  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3596  if (retval != ERROR_OK) {
3597  free(buffer);
3598  break;
3599  }
3600 
3601  uint32_t offset = 0;
3602  uint32_t length = buf_cnt;
3603 
3604  /* DANGER!!! beware of unsigned comparison here!!! */
3605 
3606  if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3607  (image.sections[i].base_address < max_address)) {
3608 
3609  if (image.sections[i].base_address < min_address) {
3610  /* clip addresses below */
3611  offset += min_address-image.sections[i].base_address;
3612  length -= offset;
3613  }
3614 
3615  if (image.sections[i].base_address + buf_cnt > max_address)
3616  length -= (image.sections[i].base_address + buf_cnt)-max_address;
3617 
3618  retval = target_write_buffer(target,
3620  if (retval != ERROR_OK) {
3621  free(buffer);
3622  break;
3623  }
3624  image_size += length;
3625  command_print(CMD, "%u bytes written at address " TARGET_ADDR_FMT "",
3626  (unsigned int)length,
3628  }
3629 
3630  free(buffer);
3631  }
3632 
3633  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3634  command_print(CMD, "downloaded %" PRIu32 " bytes "
3635  "in %fs (%0.3f KiB/s)", image_size,
3636  duration_elapsed(&bench), duration_kbps(&bench, image_size));
3637  }
3638 
3639  image_close(&image);
3640 
3641  return retval;
3642 
3643 }
3644 
3645 COMMAND_HANDLER(handle_dump_image_command)
3646 {
3647  struct fileio *fileio;
3648  uint8_t *buffer;
3649  int retval, retvaltemp;
3651  struct duration bench;
3653 
3654  if (CMD_ARGC != 3)
3656 
3659 
3660  uint32_t buf_size = (size > 4096) ? 4096 : size;
3661  buffer = malloc(buf_size);
3662  if (!buffer)
3663  return ERROR_FAIL;
3664 
3666  if (retval != ERROR_OK) {
3667  free(buffer);
3668  return retval;
3669  }
3670 
3671  duration_start(&bench);
3672 
3673  while (size > 0) {
3674  size_t size_written;
3675  uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3676  retval = target_read_buffer(target, address, this_run_size, buffer);
3677  if (retval != ERROR_OK)
3678  break;
3679 
3680  retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3681  if (retval != ERROR_OK)
3682  break;
3683 
3684  size -= this_run_size;
3685  address += this_run_size;
3686  }
3687 
3688  free(buffer);
3689 
3690  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3691  size_t filesize;
3692  retval = fileio_size(fileio, &filesize);
3693  if (retval != ERROR_OK)
3694  return retval;
3696  "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3697  duration_elapsed(&bench), duration_kbps(&bench, filesize));
3698  }
3699 
3700  retvaltemp = fileio_close(fileio);
3701  if (retvaltemp != ERROR_OK)
3702  return retvaltemp;
3703 
3704  return retval;
3705 }
3706 
3711 };
3712 
3713 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3714 {
3715  uint8_t *buffer;
3716  size_t buf_cnt;
3717  uint32_t image_size;
3718  int retval;
3719  uint32_t checksum = 0;
3720  uint32_t mem_checksum = 0;
3721 
3722  struct image image;
3723 
3725 
3726  if (CMD_ARGC < 1)
3728 
3729  if (!target) {
3730  LOG_ERROR("no target selected");
3731  return ERROR_FAIL;
3732  }
3733 
3734  struct duration bench;
3735  duration_start(&bench);
3736 
3737  if (CMD_ARGC >= 2) {
3741  image.base_address_set = true;
3742  } else {
3743  image.base_address_set = false;
3744  image.base_address = 0x0;
3745  }
3746 
3747  image.start_address_set = false;
3748 
3749  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3750  if (retval != ERROR_OK)
3751  return retval;
3752 
3753  image_size = 0x0;
3754  int diffs = 0;
3755  retval = ERROR_OK;
3756  for (unsigned int i = 0; i < image.num_sections; i++) {
3757  buffer = malloc(image.sections[i].size);
3758  if (!buffer) {
3760  "error allocating buffer for section (%" PRIu32 " bytes)",
3761  image.sections[i].size);
3762  break;
3763  }
3764  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3765  if (retval != ERROR_OK) {
3766  free(buffer);
3767  break;
3768  }
3769 
3770  if (verify >= IMAGE_VERIFY) {
3771  /* calculate checksum of image */
3772  retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3773  if (retval != ERROR_OK) {
3774  free(buffer);
3775  break;
3776  }
3777 
3778  retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3779  if (retval != ERROR_OK) {
3780  free(buffer);
3781  break;
3782  }
3783  if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3784  LOG_ERROR("checksum mismatch");
3785  free(buffer);
3786  retval = ERROR_FAIL;
3787  goto done;
3788  }
3789  if (checksum != mem_checksum) {
3790  /* failed crc checksum, fall back to a binary compare */
3791  uint8_t *data;
3792 
3793  if (diffs == 0)
3794  LOG_ERROR("checksum mismatch - attempting binary compare");
3795 
3796  data = malloc(buf_cnt);
3797 
3798  retval = target_read_buffer(target, image.sections[i].base_address, buf_cnt, data);
3799  if (retval == ERROR_OK) {
3800  uint32_t t;
3801  for (t = 0; t < buf_cnt; t++) {
3802  if (data[t] != buffer[t]) {
3804  "diff %d address " TARGET_ADDR_FMT ". Was 0x%02" PRIx8 " instead of 0x%02" PRIx8,
3805  diffs,
3806  t + image.sections[i].base_address,
3807  data[t],
3808  buffer[t]);
3809  if (diffs++ >= 127) {
3810  command_print(CMD, "More than 128 errors, the rest are not printed.");
3811  free(data);
3812  free(buffer);
3813  goto done;
3814  }
3815  }
3816  keep_alive();
3818  retval = ERROR_SERVER_INTERRUPTED;
3819  free(data);
3820  free(buffer);
3821  goto done;
3822  }
3823  }
3824  }
3825  free(data);
3826  }
3827  } else {
3828  command_print(CMD, "address " TARGET_ADDR_FMT " length 0x%08zx",
3830  buf_cnt);
3831  }
3832 
3833  free(buffer);
3834  image_size += buf_cnt;
3835  }
3836  if (diffs > 0)
3837  command_print(CMD, "No more differences found.");
3838 done:
3839  if (diffs > 0)
3840  retval = ERROR_FAIL;
3841  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
3842  command_print(CMD, "verified %" PRIu32 " bytes "
3843  "in %fs (%0.3f KiB/s)", image_size,
3844  duration_elapsed(&bench), duration_kbps(&bench, image_size));
3845  }
3846 
3847  image_close(&image);
3848 
3849  return retval;
3850 }
3851 
3852 COMMAND_HANDLER(handle_verify_image_checksum_command)
3853 {
3854  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3855 }
3856 
3857 COMMAND_HANDLER(handle_verify_image_command)
3858 {
3859  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3860 }
3861 
3862 COMMAND_HANDLER(handle_test_image_command)
3863 {
3864  return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3865 }
3866 
3868 {
3869  struct target *target = get_current_target(cmd->ctx);
3871  while (breakpoint) {
3872  if (breakpoint->type == BKPT_SOFT) {
3873  char *buf = buf_to_hex_str(breakpoint->orig_instr,
3874  breakpoint->length * 8);
3875  command_print(cmd, "Software breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, orig_instr=0x%s",
3877  breakpoint->length,
3878  buf);
3879  free(buf);
3880  } else {
3881  if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3882  command_print(cmd, "Context breakpoint: asid=0x%8.8" PRIx32 ", len=0x%x, num=%u",
3883  breakpoint->asid,
3885  else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3886  command_print(cmd, "Hybrid breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
3889  command_print(cmd, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3890  breakpoint->asid);
3891  } else
3892  command_print(cmd, "Hardware breakpoint(IVA): addr=" TARGET_ADDR_FMT ", len=0x%x, num=%u",
3895  }
3896 
3898  }
3899  return ERROR_OK;
3900 }
3901 
3903  target_addr_t addr, uint32_t asid, unsigned int length, int hw)
3904 {
3905  struct target *target = get_current_target(cmd->ctx);
3906  int retval;
3907 
3908  if (asid == 0) {
3909  retval = breakpoint_add(target, addr, length, hw);
3910  /* error is always logged in breakpoint_add(), do not print it again */
3911  if (retval == ERROR_OK)
3912  command_print(cmd, "breakpoint set at " TARGET_ADDR_FMT "", addr);
3913 
3914  } else if (addr == 0) {
3916  LOG_TARGET_ERROR(target, "Context breakpoint not available");
3918  }
3919  retval = context_breakpoint_add(target, asid, length, hw);
3920  /* error is always logged in context_breakpoint_add(), do not print it again */
3921  if (retval == ERROR_OK)
3922  command_print(cmd, "Context breakpoint set at 0x%8.8" PRIx32, asid);
3923 
3924  } else {
3926  LOG_TARGET_ERROR(target, "Hybrid breakpoint not available");
3928  }
3929  retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3930  /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3931  if (retval == ERROR_OK)
3932  command_print(cmd, "Hybrid breakpoint set at 0x%8.8" PRIx32, asid);
3933  }
3934  return retval;
3935 }
3936 
3937 COMMAND_HANDLER(handle_bp_command)
3938 {
3940  uint32_t asid;
3941  uint32_t length;
3942  int hw = BKPT_SOFT;
3943 
3944  switch (CMD_ARGC) {
3945  case 0:
3946  return handle_bp_command_list(CMD);
3947 
3948  case 2:
3949  asid = 0;
3952  return handle_bp_command_set(CMD, addr, asid, length, hw);
3953 
3954  case 3:
3955  if (strcmp(CMD_ARGV[2], "hw") == 0) {
3956  hw = BKPT_HARD;
3959  asid = 0;
3960  return handle_bp_command_set(CMD, addr, asid, length, hw);
3961  } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3962  hw = BKPT_HARD;
3963  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3965  addr = 0;
3966  return handle_bp_command_set(CMD, addr, asid, length, hw);
3967  }
3968  /* fallthrough */
3969  case 4:
3970  hw = BKPT_HARD;
3972  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3974  return handle_bp_command_set(CMD, addr, asid, length, hw);
3975 
3976  default:
3978  }
3979 }
3980 
3981 COMMAND_HANDLER(handle_rbp_command)
3982 {
3983  int retval;
3984 
3985  if (CMD_ARGC != 1)
3987 
3989 
3990  if (!strcmp(CMD_ARGV[0], "all")) {
3991  retval = breakpoint_remove_all(target);
3992 
3993  if (retval != ERROR_OK) {
3994  command_print(CMD, "Error encountered during removal of all breakpoints.");
3995  command_print(CMD, "Some breakpoints may have remained set.");
3996  }
3997  } else {
4000 
4001  retval = breakpoint_remove(target, addr);
4002 
4003  if (retval != ERROR_OK)
4004  command_print(CMD, "Error during removal of breakpoint at address " TARGET_ADDR_FMT, addr);
4005  }
4006 
4007  return retval;
4008 }
4009 
4010 COMMAND_HANDLER(handle_wp_command)
4011 {
4013 
4014  if (CMD_ARGC == 0) {
4016 
4017  while (watchpoint) {
4018  char wp_type = (watchpoint->rw == WPT_READ ? 'r' : (watchpoint->rw == WPT_WRITE ? 'w' : 'a'));
4019  command_print(CMD, "address: " TARGET_ADDR_FMT
4020  ", len: 0x%8.8x"
4021  ", r/w/a: %c, value: 0x%8.8" PRIx64
4022  ", mask: 0x%8.8" PRIx64,
4024  watchpoint->length,
4025  wp_type,
4026  watchpoint->value,
4027  watchpoint->mask);
4029  }
4030  return ERROR_OK;
4031  }
4032 
4033  enum watchpoint_rw type = WPT_ACCESS;
4034  target_addr_t addr = 0;
4035  uint32_t length = 0;
4036  uint64_t data_value = 0x0;
4037  uint64_t data_mask = WATCHPOINT_IGNORE_DATA_VALUE_MASK;
4038  bool mask_specified = false;
4039 
4040  switch (CMD_ARGC) {
4041  case 5:
4042  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[4], data_mask);
4043  mask_specified = true;
4044  /* fall through */
4045  case 4:
4046  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[3], data_value);
4047  // if user specified only data value without mask - the mask should be 0
4048  if (!mask_specified)
4049  data_mask = 0;
4050  /* fall through */
4051  case 3:
4052  switch (CMD_ARGV[2][0]) {
4053  case 'r':
4054  type = WPT_READ;
4055  break;
4056  case 'w':
4057  type = WPT_WRITE;
4058  break;
4059  case 'a':
4060  type = WPT_ACCESS;
4061  break;
4062  default:
4063  LOG_TARGET_ERROR(target, "invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
4065  }
4066  /* fall through */
4067  case 2:
4070  break;
4071 
4072  default:
4074  }
4075 
4076  int retval = watchpoint_add(target, addr, length, type,
4077  data_value, data_mask);
4078  if (retval != ERROR_OK)
4079  LOG_TARGET_ERROR(target, "Failure setting watchpoints");
4080 
4081  return retval;
4082 }
4083 
4084 COMMAND_HANDLER(handle_rwp_command)
4085 {
4086  int retval;
4087 
4088  if (CMD_ARGC != 1)
4090 
4092  if (!strcmp(CMD_ARGV[0], "all")) {
4093  retval = watchpoint_remove_all(target);
4094 
4095  if (retval != ERROR_OK) {
4096  command_print(CMD, "Error encountered during removal of all watchpoints.");
4097  command_print(CMD, "Some watchpoints may have remained set.");
4098  }
4099  } else {
4102 
4103  retval = watchpoint_remove(target, addr);
4104 
4105  if (retval != ERROR_OK)
4106  command_print(CMD, "Error during removal of watchpoint at address " TARGET_ADDR_FMT, addr);
4107  }
4108 
4109  return retval;
4110 }
4111 
4118 COMMAND_HANDLER(handle_virt2phys_command)
4119 {
4120  if (CMD_ARGC != 1)
4122 
4123  target_addr_t va;
4125  target_addr_t pa;
4126 
4128  int retval = target->type->virt2phys(target, va, &pa);
4129  if (retval == ERROR_OK)
4130  command_print(CMD, "Physical address " TARGET_ADDR_FMT "", pa);
4131 
4132  return retval;
4133 }
4134 
4135 static void write_data(FILE *f, const void *data, size_t len)
4136 {
4137  size_t written = fwrite(data, 1, len, f);
4138  if (written != len)
4139  LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
4140 }
4141 
4142 static void write_long(FILE *f, int l, struct target *target)
4143 {
4144  uint8_t val[4];
4145 
4146  target_buffer_set_u32(target, val, l);
4147  write_data(f, val, 4);
4148 }
4149 
4150 static void write_string(FILE *f, char *s)
4151 {
4152  write_data(f, s, strlen(s));
4153 }
4154 
4155 typedef unsigned char UNIT[2]; /* unit of profiling */
4156 
4157 /* Dump a gmon.out histogram file. */
4158 static void write_gmon(uint32_t *samples, uint32_t sample_num, const char *filename, bool with_range,
4159  uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
4160 {
4161  uint32_t i;
4162  FILE *f = fopen(filename, "wb");
4163  if (!f)
4164  return;
4165  write_string(f, "gmon");
4166  write_long(f, 0x00000001, target); /* Version */
4167  write_long(f, 0, target); /* padding */
4168  write_long(f, 0, target); /* padding */
4169  write_long(f, 0, target); /* padding */
4170 
4171  uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
4172  write_data(f, &zero, 1);
4173 
4174  /* figure out bucket size */
4175  uint32_t min;
4176  uint32_t max;
4177  if (with_range) {
4178  min = start_address;
4179  max = end_address;
4180  } else {
4181  min = samples[0];
4182  max = samples[0];
4183  for (i = 0; i < sample_num; i++) {
4184  if (min > samples[i])
4185  min = samples[i];
4186  if (max < samples[i])
4187  max = samples[i];
4188  }
4189 
4190  /* max should be (largest sample + 1)
4191  * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
4192  if (max < UINT32_MAX)
4193  max++;
4194 
4195  /* gprof requires (max - min) >= 2 */
4196  while ((max - min) < 2) {
4197  if (max < UINT32_MAX)
4198  max++;
4199  else
4200  min--;
4201  }
4202  }
4203 
4204  uint32_t address_space = max - min;
4205 
4206  /* FIXME: What is the reasonable number of buckets?
4207  * The profiling result will be more accurate if there are enough buckets. */
4208  static const uint32_t max_buckets = 128 * 1024; /* maximum buckets. */
4209  uint32_t num_buckets = address_space / sizeof(UNIT);
4210  if (num_buckets > max_buckets)
4211  num_buckets = max_buckets;
4212  int *buckets = malloc(sizeof(int) * num_buckets);
4213  if (!buckets) {
4214  fclose(f);
4215  return;
4216  }
4217  memset(buckets, 0, sizeof(int) * num_buckets);
4218  for (i = 0; i < sample_num; i++) {
4219  uint32_t address = samples[i];
4220 
4221  if ((address < min) || (max <= address))
4222  continue;
4223 
4224  long long a = address - min;
4225  long long b = num_buckets;
4226  long long c = address_space;
4227  int index_t = (a * b) / c; /* danger!!!! int32 overflows */
4228  buckets[index_t]++;
4229  }
4230 
4231  /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4232  write_long(f, min, target); /* low_pc */
4233  write_long(f, max, target); /* high_pc */
4234  write_long(f, num_buckets, target); /* # of buckets */
4235  float sample_rate = sample_num / (duration_ms / 1000.0);
4236  write_long(f, sample_rate, target);
4237  write_string(f, "seconds");
4238  for (i = 0; i < (15-strlen("seconds")); i++)
4239  write_data(f, &zero, 1);
4240  write_string(f, "s");
4241 
4242  /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4243 
4244  char *data = malloc(2 * num_buckets);
4245  if (data) {
4246  for (i = 0; i < num_buckets; i++) {
4247  int val;
4248  val = buckets[i];
4249  if (val > 65535)
4250  val = 65535;
4251  data[i * 2] = val&0xff;
4252  data[i * 2 + 1] = (val >> 8) & 0xff;
4253  }
4254  free(buckets);
4255  write_data(f, data, num_buckets * 2);
4256  free(data);
4257  } else
4258  free(buckets);
4259 
4260  fclose(f);
4261 }
4262 
4263 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4264  * which will be used as a random sampling of PC */
4265 COMMAND_HANDLER(handle_profile_command)
4266 {
4268 
4269  if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
4271 
4272  const uint32_t MAX_PROFILE_SAMPLE_NUM = 1000000;
4273  uint32_t offset;
4274  uint32_t num_of_samples;
4275  int retval = ERROR_OK;
4276  bool halted_before_profiling = target->state == TARGET_HALTED;
4277 
4279 
4280  uint32_t start_address = 0;
4281  uint32_t end_address = 0;
4282  bool with_range = false;
4283  if (CMD_ARGC == 4) {
4284  with_range = true;
4285  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4286  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4287  if (start_address > end_address || (end_address - start_address) < 2) {
4288  command_print(CMD, "Error: end - start < 2");
4290  }
4291  }
4292 
4293  uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
4294  if (!samples) {
4295  LOG_ERROR("No memory to store samples.");
4296  return ERROR_FAIL;
4297  }
4298 
4299  uint64_t timestart_ms = timeval_ms();
4305  retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
4306  &num_of_samples, offset);
4307  if (retval != ERROR_OK) {
4308  free(samples);
4309  return retval;
4310  }
4311  uint32_t duration_ms = timeval_ms() - timestart_ms;
4312 
4313  assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
4314 
4315  retval = target_poll(target);
4316  if (retval != ERROR_OK) {
4317  free(samples);
4318  return retval;
4319  }
4320 
4321  if (target->state == TARGET_RUNNING && halted_before_profiling) {
4322  /* The target was halted before we started and is running now. Halt it,
4323  * for consistency. */
4324  retval = target_halt(target);
4325  if (retval != ERROR_OK) {
4326  free(samples);
4327  return retval;
4328  }
4329  } else if (target->state == TARGET_HALTED && !halted_before_profiling) {
4330  /* The target was running before we started and is halted now. Resume
4331  * it, for consistency. */
4332  retval = target_resume(target, true, 0, false, false);
4333  if (retval != ERROR_OK) {
4334  free(samples);
4335  return retval;
4336  }
4337  }
4338 
4339  retval = target_poll(target);
4340  if (retval != ERROR_OK) {
4341  free(samples);
4342  return retval;
4343  }
4344 
4345  write_gmon(samples, num_of_samples, CMD_ARGV[1],
4346  with_range, start_address, end_address, target, duration_ms);
4347  command_print(CMD, "Wrote %s", CMD_ARGV[1]);
4348 
4349  free(samples);
4350  return retval;
4351 }
4352 
4353 COMMAND_HANDLER(handle_target_read_memory)
4354 {
4355  /*
4356  * CMD_ARGV[0] = memory address
4357  * CMD_ARGV[1] = desired element width in bits
4358  * CMD_ARGV[2] = number of elements to read
4359  * CMD_ARGV[3] = optional "phys"
4360  */
4361 
4362  if (CMD_ARGC < 3 || CMD_ARGC > 4)
4364 
4365  /* Arg 1: Memory address. */
4368 
4369  /* Arg 2: Bit width of one element. */
4370  unsigned int width_bits;
4371  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
4372 
4373  /* Arg 3: Number of elements to read. */
4374  unsigned int count;
4375  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
4376 
4377  /* Arg 4: Optional 'phys'. */
4378  bool is_phys = false;
4379  if (CMD_ARGC == 4) {
4380  if (strcmp(CMD_ARGV[3], "phys")) {
4381  command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
4383  }
4384 
4385  is_phys = true;
4386  }
4387 
4388  switch (width_bits) {
4389  case 8:
4390  case 16:
4391  case 32:
4392  case 64:
4393  break;
4394  default:
4395  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
4397  }
4398 
4399  if (count > 65536) {
4400  command_print(CMD, "too large read request, exceeds 64K elements");
4402  }
4403 
4404  const unsigned int width = width_bits / 8;
4405  /* -1 is needed to handle cases when (addr + count * width) results in zero
4406  * due to overflow.
4407  */
4408  if ((addr + count * width - 1) < addr) {
4409  command_print(CMD, "memory region wraps over address zero");
4411  }
4412 
4414 
4415  const size_t buffersize = 4096;
4416  uint8_t *buffer = malloc(buffersize);
4417 
4418  if (!buffer) {
4419  LOG_ERROR("Failed to allocate memory");
4420  return ERROR_FAIL;
4421  }
4422 
4423  char *separator = "";
4424  while (count > 0) {
4425  const unsigned int max_chunk_len = buffersize / width;
4426  const size_t chunk_len = MIN(count, max_chunk_len);
4427 
4428  int retval;
4429 
4430  if (is_phys)
4431  retval = target_read_phys_memory(target, addr, width, chunk_len, buffer);
4432  else
4433  retval = target_read_memory(target, addr, width, chunk_len, buffer);
4434 
4435  if (retval != ERROR_OK) {
4436  LOG_DEBUG("read at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4437  addr, width_bits, chunk_len);
4438  /*
4439  * FIXME: we append the errmsg to the list of value already read.
4440  * Add a way to flush and replace old output, but LOG_DEBUG() it
4441  */
4442  command_print(CMD, "failed to read memory");
4443  free(buffer);
4444  return retval;
4445  }
4446 
4447  for (size_t i = 0; i < chunk_len ; i++) {
4448  uint64_t v = 0;
4449 
4450  switch (width) {
4451  case 8:
4453  break;
4454  case 4:
4456  break;
4457  case 2:
4459  break;
4460  case 1:
4461  v = buffer[i];
4462  break;
4463  }
4464 
4465  command_print_sameline(CMD, "%s0x%" PRIx64, separator, v);
4466  separator = " ";
4467  }
4468 
4469  count -= chunk_len;
4470  addr += chunk_len * width;
4471  }
4472 
4473  free(buffer);
4474 
4475  return ERROR_OK;
4476 }
4477 
4478 COMMAND_HANDLER(handle_target_write_memory)
4479 {
4480  /*
4481  * CMD_ARGV[0] = memory address
4482  * CMD_ARGV[1] = desired element width in bits
4483  * CMD_ARGV[2] = list of data to write
4484  * CMD_ARGV[3] = optional "phys"
4485  */
4486 
4487  if (CMD_ARGC < 3 || CMD_ARGC > 4)
4489 
4490  /* Arg 1: Memory address. */
4493 
4494  /* Arg 2: Bit width of one element. */
4495  unsigned int width_bits;
4496  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], width_bits);
4497 
4498  /* Arg 3: Elements to write. */
4499  size_t count = Jim_ListLength(CMD_CTX->interp, CMD_JIMTCL_ARGV[2]);
4500 
4501  /* Arg 4: Optional 'phys'. */
4502  bool is_phys = false;
4503 
4504  if (CMD_ARGC == 4) {
4505  if (strcmp(CMD_ARGV[3], "phys")) {
4506  command_print(CMD, "invalid argument '%s', must be 'phys'", CMD_ARGV[3]);
4508  }
4509 
4510  is_phys = true;
4511  }
4512 
4513  switch (width_bits) {
4514  case 8:
4515  case 16:
4516  case 32:
4517  case 64:
4518  break;
4519  default:
4520  command_print(CMD, "invalid width, must be 8, 16, 32 or 64");
4522  }
4523 
4524  if (count > 65536) {
4525  command_print(CMD, "too large memory write request, exceeds 64K elements");
4527  }
4528 
4529  const unsigned int width = width_bits / 8;
4530  /* -1 is needed to handle cases when (addr + count * width) results in zero
4531  * due to overflow.
4532  */
4533  if ((addr + count * width - 1) < addr) {
4534  command_print(CMD, "memory region wraps over address zero");
4536  }
4537 
4539 
4540  const size_t buffersize = 4096;
4541  uint8_t *buffer = malloc(buffersize);
4542 
4543  if (!buffer) {
4544  LOG_ERROR("Failed to allocate memory");
4545  return ERROR_FAIL;
4546  }
4547 
4548  size_t j = 0;
4549 
4550  while (count > 0) {
4551  const unsigned int max_chunk_len = buffersize / width;
4552  const size_t chunk_len = MIN(count, max_chunk_len);
4553 
4554  for (size_t i = 0; i < chunk_len; i++, j++) {
4555  Jim_Obj *tmp = Jim_ListGetIndex(CMD_CTX->interp, CMD_JIMTCL_ARGV[2], j);
4556  jim_wide element_wide;
4557  int jimretval = Jim_GetWide(CMD_CTX->interp, tmp, &element_wide);
4558  if (jimretval != JIM_OK) {
4559  command_print(CMD, "invalid value \"%s\"", Jim_GetString(tmp, NULL));
4560  free(buffer);
4562  }
4563 
4564  const uint64_t v = element_wide;
4565 
4566  switch (width) {
4567  case 8:
4569  break;
4570  case 4:
4572  break;
4573  case 2:
4575  break;
4576  case 1:
4577  buffer[i] = v & 0x0ff;
4578  break;
4579  }
4580  }
4581 
4582  count -= chunk_len;
4583 
4584  int retval;
4585 
4586  if (is_phys)
4587  retval = target_write_phys_memory(target, addr, width, chunk_len, buffer);
4588  else
4589  retval = target_write_memory(target, addr, width, chunk_len, buffer);
4590 
4591  if (retval != ERROR_OK) {
4592  LOG_DEBUG("write at " TARGET_ADDR_FMT " with width=%u and count=%zu failed",
4593  addr, width_bits, chunk_len);
4594  command_print(CMD, "failed to write memory");
4595  free(buffer);
4596  return retval;
4597  }
4598 
4599  addr += chunk_len * width;
4600  }
4601 
4602  free(buffer);
4603 
4604  return ERROR_OK;
4605 }
4606 
4607 /* FIX? should we propagate errors here rather than printing them
4608  * and continuing?
4609  */
4611 {
4612  struct target_event_action *teap, *tmp;
4613  int retval;
4614 
4616  if (teap->event == e) {
4617  /*
4618  * The event can be destroyed by its own handler.
4619  * Make a local copy and use it in place of the original.
4620  */
4621  struct target_event_action local_teap = *teap;
4622  teap = &local_teap;
4623 
4624  LOG_DEBUG("target: %s (%s) event: %d (%s) action: %s",
4627  e,
4628  target_event_name(e),
4629  Jim_GetString(teap->body, NULL));
4630 
4631  /* Override current target by the target an event
4632  * is issued from (lot of scripts need it).
4633  * Return back to previous override as soon
4634  * as the handler processing is done */
4635  struct command_context *cmd_ctx = current_command_context(teap->interp);
4636  struct target *saved_target_override = cmd_ctx->current_target_override;
4637  cmd_ctx->current_target_override = target;
4638 
4639  /*
4640  * The event can be destroyed by its own handler.
4641  * Prevent the body to get deallocated by Jim.
4642  */
4643  Jim_IncrRefCount(teap->body);
4644  retval = Jim_EvalObj(teap->interp, teap->body);
4645  Jim_DecrRefCount(teap->interp, teap->body);
4646 
4647  cmd_ctx->current_target_override = saved_target_override;
4648 
4649  if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
4650  return;
4651 
4652  if (retval == JIM_RETURN)
4653  retval = teap->interp->returnCode;
4654 
4655  if (retval != JIM_OK) {
4656  Jim_MakeErrorMessage(teap->interp);
4657  LOG_TARGET_ERROR(target, "Execution of event %s failed:\n%s",
4658  target_event_name(e),
4659  Jim_GetString(Jim_GetResult(teap->interp), NULL));
4660  /* clean both error code and stacktrace before return */
4661  Jim_Eval(teap->interp, "error \"\" \"\"");
4662  }
4663  }
4664  }
4665 }
4666 
4667 COMMAND_HANDLER(handle_target_get_reg)
4668 {
4669  if (CMD_ARGC < 1 || CMD_ARGC > 2)
4671 
4672  bool force = false;
4673  Jim_Obj *next_argv = CMD_JIMTCL_ARGV[0];
4674 
4675  if (CMD_ARGC == 2) {
4676  if (strcmp(CMD_ARGV[0], "-force")) {
4677  command_print(CMD, "invalid argument '%s', must be '-force'", CMD_ARGV[0]);
4679  }
4680 
4681  force = true;
4682  next_argv = CMD_JIMTCL_ARGV[1];
4683  }
4684 
4685  const int length = Jim_ListLength(CMD_CTX->interp, next_argv);
4686 
4687  const struct target *target = get_current_target(CMD_CTX);
4688 
4689  for (int i = 0; i < length; i++) {
4690  Jim_Obj *elem = Jim_ListGetIndex(CMD_CTX->interp, next_argv, i);
4691 
4692  const char *reg_name = Jim_String(elem);
4693 
4694  struct reg *reg = register_get_by_name(target->reg_cache, reg_name, true);
4695 
4696  if (!reg || !reg->exist) {
4697  command_print(CMD, "unknown register '%s'", reg_name);
4699  }
4700 
4701  if (force || !reg->valid) {
4702  int retval = reg->type->get(reg);
4703 
4704  if (retval != ERROR_OK) {
4705  command_print(CMD, "failed to read register '%s'", reg_name);
4706  return retval;
4707  }
4708  }
4709 
4710  char *reg_value = buf_to_hex_str(reg->value, reg->size);
4711 
4712  if (!reg_value) {
4713  LOG_ERROR("Failed to allocate memory");
4714  return ERROR_FAIL;
4715  }
4716 
4717  command_print(CMD, "%s 0x%s", reg_name, reg_value);
4718 
4719  free(reg_value);
4720  }
4721 
4722  return ERROR_OK;
4723 }
4724 
4725 COMMAND_HANDLER(handle_set_reg_command)
4726 {
4727  if (CMD_ARGC != 1)
4729 
4730  int tmp;
4731 #if JIM_VERSION >= 80
4732  Jim_Obj **dict = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &tmp);
4733 
4734  if (!dict)
4735  return ERROR_FAIL;
4736 #else
4737  Jim_Obj **dict;
4738  int ret = Jim_DictPairs(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], &dict, &tmp);
4739 
4740  if (ret != JIM_OK)
4741  return ERROR_FAIL;
4742 #endif
4743 
4744  const unsigned int length = tmp;
4745 
4746  const struct target *target = get_current_target(CMD_CTX);
4747  assert(target);
4748 
4749  for (unsigned int i = 0; i < length; i += 2) {
4750  const char *reg_name = Jim_String(dict[i]);
4751  const char *reg_value = Jim_String(dict[i + 1]);
4752  struct reg *reg = register_get_by_name(target->reg_cache, reg_name, true);
4753 
4754  if (!reg || !reg->exist) {
4755  command_print(CMD, "unknown register '%s'", reg_name);
4756  return ERROR_FAIL;
4757  }
4758 
4759  uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
4760  if (!buf) {
4761  LOG_ERROR("Failed to allocate memory");
4762  return ERROR_FAIL;
4763  }
4764 
4765  int retval = CALL_COMMAND_HANDLER(command_parse_str_to_buf, reg_value, buf, reg->size);
4766  if (retval != ERROR_OK) {
4767  free(buf);
4768  return retval;
4769  }
4770 
4771  retval = reg->type->set(reg, buf);
4772  free(buf);
4773 
4774  if (retval != ERROR_OK) {
4775  command_print(CMD, "failed to set '%s' to register '%s'",
4776  reg_value, reg_name);
4777  return retval;
4778  }
4779  }
4780 
4781  return ERROR_OK;
4782 }
4783 
4787 bool target_has_event_action(const struct target *target, enum target_event event)
4788 {
4789  struct target_event_action *teap;
4790 
4792  if (teap->event == event)
4793  return true;
4794  }
4795  return false;
4796 }
4797 
4813 };
4814 
4815 static struct nvp nvp_config_opts[] = {
4816  { .name = "-type", .value = TCFG_TYPE },
4817  { .name = "-event", .value = TCFG_EVENT },
4818  { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4819  { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4820  { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4821  { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4822  { .name = "-endian", .value = TCFG_ENDIAN },
4823  { .name = "-coreid", .value = TCFG_COREID },
4824  { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4825  { .name = "-dbgbase", .value = TCFG_DBGBASE },
4826  { .name = "-rtos", .value = TCFG_RTOS },
4827  { .name = "-defer-examine", .value = TCFG_DEFER_EXAMINE },
4828  { .name = "-gdb-port", .value = TCFG_GDB_PORT },
4829  { .name = "-gdb-max-connections", .value = TCFG_GDB_MAX_CONNECTIONS },
4830  { .name = NULL, .value = -1 }
4831 };
4832 
4833 static COMMAND_HELPER(target_configure, struct target *target, unsigned int index, bool is_configure)
4834 {
4835  const struct nvp *n;
4836  int retval;
4837 
4838  /* parse config or cget options ... */
4839  while (index < CMD_ARGC) {
4841  /* target defines a configure function */
4842  /* target gets first dibs on parameters */
4843  struct jim_getopt_info goi;
4844  jim_getopt_setup(&goi, CMD_CTX->interp, CMD_ARGC - index, CMD_JIMTCL_ARGV + index);
4845  goi.is_configure = is_configure;
4846  int e = (*target->type->target_jim_configure)(target, &goi);
4847  index = CMD_ARGC - goi.argc;
4848 
4849  int reslen;
4850  const char *result = Jim_GetString(Jim_GetResult(CMD_CTX->interp), &reslen);
4851  if (reslen > 0)
4852  command_print(CMD, "%s", result);
4853 
4854  if (e == JIM_OK) {
4855  /* more? */
4856  continue;
4857  }
4858  if (e == JIM_ERR) {
4859  /* An error */
4860  return ERROR_FAIL;
4861  }
4862  /* otherwise we 'continue' below */
4863  }
4865  if (!n->name) {
4868  }
4869  index++;
4870  switch (n->value) {
4871  case TCFG_TYPE:
4872  /* not settable */
4873  if (is_configure) {
4874  command_print(CMD, "not settable: %s", n->name);
4876  }
4877  if (index != CMD_ARGC)
4880  /* loop for more */
4881  break;
4882 
4883  case TCFG_EVENT:
4884  if (index == CMD_ARGC) {
4885  command_print(CMD, "expecting %s event-name event-body",
4886  CMD_ARGV[index - 1]);
4888  }
4889 
4891  if (!n->name) {
4894  }
4895  index++;
4896 
4897  if (is_configure) {
4898  if (index == CMD_ARGC) {
4899  command_print(CMD, "expecting %s %s event-body",
4900  CMD_ARGV[index - 2], CMD_ARGV[index - 1]);
4902  }
4903  }
4904 
4905  {
4906  struct target_event_action *teap;
4907 
4908  /* replace existing? */
4910  if (teap->event == (enum target_event)n->value)
4911  break;
4912 
4913  /* not found! */
4914  if (&teap->list == &target->events_action)
4915  teap = NULL;
4916 
4917  if (is_configure) {
4918  /* START_DEPRECATED_TPIU */
4919  if (n->value == TARGET_EVENT_TRACE_CONFIG)
4920  LOG_INFO("DEPRECATED target event %s; use TPIU events {pre,post}-{enable,disable}", n->name);
4921  /* END_DEPRECATED_TPIU */
4922 
4923  if (strlen(CMD_ARGV[index]) == 0) {
4924  /* empty action, drop existing one */
4925  if (teap) {
4926  list_del(&teap->list);
4927  Jim_DecrRefCount(teap->interp, teap->body);
4928  free(teap);
4929  }
4930  index++;
4931  break;
4932  }
4933 
4934  bool replace = true;
4935  if (!teap) {
4936  /* create new */
4937  teap = calloc(1, sizeof(*teap));
4938  replace = false;
4939  }
4940  teap->event = n->value;
4941  teap->interp = CMD_CTX->interp;
4942  if (teap->body)
4943  Jim_DecrRefCount(teap->interp, teap->body);
4944  /* use jim object to keep its reference on tcl file and line */
4945  /* TODO: need duplicate? isn't IncrRefCount enough? */
4946  teap->body = Jim_DuplicateObj(teap->interp, CMD_JIMTCL_ARGV[index++]);
4947  /*
4948  * FIXME:
4949  * Tcl/TK - "tk events" have a nice feature.
4950  * See the "BIND" command.
4951  * We should support that here.
4952  * You can specify %X and %Y in the event code.
4953  * The idea is: %T - target name.
4954  * The idea is: %N - target number
4955  * The idea is: %E - event name.
4956  */
4957  Jim_IncrRefCount(teap->body);
4958 
4959  if (!replace) {
4960  /* add to head of event list */
4961  list_add(&teap->list, &target->events_action);
4962  }
4963  } else {
4964  /* cget */
4965  if (index != CMD_ARGC)
4967 
4968  if (teap)
4969  command_print(CMD, "%s", Jim_GetString(teap->body, NULL));
4970  }
4971  }
4972  /* loop for more */
4973  break;
4974 
4975  case TCFG_WORK_AREA_VIRT:
4976  if (is_configure) {
4977  if (index == CMD_ARGC) {
4978  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
4980  }
4982  index++;
4985  } else {
4986  if (index != CMD_ARGC)
4989  }
4990  /* loop for more */
4991  break;
4992 
4993  case TCFG_WORK_AREA_PHYS:
4994  if (is_configure) {
4995  if (index == CMD_ARGC) {
4996  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
4998  }
5000  index++;
5003  } else {
5004  if (index != CMD_ARGC)
5007  }
5008  /* loop for more */
5009  break;
5010 
5011  case TCFG_WORK_AREA_SIZE:
5012  if (is_configure) {
5013  if (index == CMD_ARGC) {
5014  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5016  }
5018  index++;
5020  } else {
5021  if (index != CMD_ARGC)
5023  command_print(CMD, "0x%08" PRIx32, target->working_area_size);
5024  }
5025  /* loop for more */
5026  break;
5027 
5028  case TCFG_WORK_AREA_BACKUP:
5029  if (is_configure) {
5030  if (index == CMD_ARGC) {
5031  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5033  }
5035  if (retval != ERROR_OK)
5036  return retval;
5037  index++;
5039  } else {
5040  if (index != CMD_ARGC)
5043  }
5044  /* loop for more */
5045  break;
5046 
5047  case TCFG_ENDIAN:
5048  if (is_configure) {
5049  if (index == CMD_ARGC) {
5050  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5052  }
5054  if (!n->name) {
5057  }
5058  index++;
5059  target->endianness = n->value;
5060  } else {
5061  if (index != CMD_ARGC)
5064  if (!n->name) {
5067  }
5068  command_print(CMD, "%s", n->name);
5069  }
5070  /* loop for more */
5071  break;
5072 
5073  case TCFG_COREID:
5074  if (is_configure) {
5075  if (index == CMD_ARGC) {
5076  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5078  }
5079  COMMAND_PARSE_NUMBER(s32, CMD_ARGV[index], target->coreid);
5080  index++;
5081  } else {
5082  if (index != CMD_ARGC)
5084  command_print(CMD, "%" PRIi32, target->coreid);
5085  }
5086  /* loop for more */
5087  break;
5088 
5089  case TCFG_CHAIN_POSITION:
5090  if (is_configure) {
5091  if (target->has_dap) {
5092  command_print(CMD, "target requires -dap parameter instead of -chain-position!");
5094  }
5095 
5096  if (index == CMD_ARGC) {
5097  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5099  }
5100  struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[index]);
5101  if (!tap) {
5102  command_print(CMD, "Tap '%s' could not be found", CMD_ARGV[index]);
5104  }
5105  index++;
5106  target->tap = tap;
5107  target->tap_configured = true;
5108  } else {
5109  if (index != CMD_ARGC)
5112  }
5113  /* loop for more */
5114  break;
5115 
5116  case TCFG_DBGBASE:
5117  if (is_configure) {
5118  if (index == CMD_ARGC) {
5119  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5121  }
5123  index++;
5124  target->dbgbase_set = true;
5125  } else {
5126  if (index != CMD_ARGC)
5128  command_print(CMD, "0x%08" PRIx32, target->dbgbase);
5129  }
5130  /* loop for more */
5131  break;
5132 
5133  case TCFG_RTOS:
5134  if (is_configure) {
5135  if (index == CMD_ARGC) {
5136  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5138  }
5139  retval = rtos_create(CMD, target, CMD_ARGV[index]);
5140  if (retval != ERROR_OK)
5141  return retval;
5142  index++;
5143  } else {
5144  if (index != CMD_ARGC)
5146  if (target->rtos)
5147  command_print(CMD, "%s", target->rtos->type->name);
5148  }
5149  /* loop for more */
5150  break;
5151 
5152  case TCFG_DEFER_EXAMINE:
5153  if (is_configure)
5154  target->defer_examine = true;
5155  else
5156  command_print(CMD, "%s", target->defer_examine ? "true" : "false");
5157  /* loop for more */
5158  break;
5159 
5160  case TCFG_GDB_PORT:
5161  if (is_configure) {
5162  if (index == CMD_ARGC) {
5163  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5165  }
5166 
5167  /* TODO: generalize test of COMMAND_CONFIG */
5168  if (CMD_CTX->mode != COMMAND_CONFIG) {
5169  command_print(CMD, "-gdb-port must be configured before 'init'");
5171  }
5172 
5173  char *s = strdup(CMD_ARGV[index]);
5174  if (!s) {
5175  LOG_ERROR("Out of memory");
5176  return ERROR_FAIL;
5177  }
5178  free(target->gdb_port_override);
5180  index++;
5181  } else {
5182  if (index != CMD_ARGC)
5185  }
5186  /* loop for more */
5187  break;
5188 
5190  if (is_configure) {
5191  if (index == CMD_ARGC) {
5192  command_print(CMD, "missing argument to %s", CMD_ARGV[index - 1]);
5194  }
5195 
5196  if (CMD_CTX->mode != COMMAND_CONFIG) {
5197  command_print(CMD, "-gdb-max-connections must be configured before 'init'");
5199  }
5200 
5202  index++;
5203  if (target->gdb_max_connections < 0)
5205  } else {
5206  if (index != CMD_ARGC)
5209  }
5210  /* loop for more */
5211  break;
5212  }
5213  }
5214 
5215  return ERROR_OK;
5216 }
5217 
5218 COMMAND_HANDLER(handle_target_configure)
5219 {
5220  if (!CMD_ARGC)
5222 
5223  bool is_configure = !strcmp(CMD_NAME, "configure");
5224 
5226 
5227  return CALL_COMMAND_HANDLER(target_configure, target, 0, is_configure);
5228 }
5229 
5230 COMMAND_HANDLER(handle_target_examine)
5231 {
5232  bool allow_defer = false;
5233 
5234  if (CMD_ARGC > 1)
5236 
5237  if (CMD_ARGC == 1) {
5238  if (strcmp(CMD_ARGV[0], "allow-defer"))
5240  allow_defer = true;
5241  }
5242 
5244  if (!target->tap->enabled) {
5245  command_print(CMD, "[TAP is disabled]");
5246  return ERROR_FAIL;
5247  }
5248 
5249  if (allow_defer && target->defer_examine) {
5250  LOG_INFO("Deferring arp_examine of %s", target_name(target));
5251  LOG_INFO("Use arp_examine command to examine it manually!");
5252  return ERROR_OK;
5253  }
5254 
5255  int retval = target->type->examine(target);
5256  if (retval != ERROR_OK) {
5258  return retval;
5259  }
5260 
5262 
5263  return ERROR_OK;
5264 }
5265 
5266 COMMAND_HANDLER(handle_target_was_examined)
5267 {
5268  if (CMD_ARGC != 0)
5270 
5272 
5273  command_print(CMD, "%d", target_was_examined(target) ? 1 : 0);
5274 
5275  return ERROR_OK;
5276 }
5277 
5278 COMMAND_HANDLER(handle_target_examine_deferred)
5279 {
5280  if (CMD_ARGC != 0)
5282 
5284 
5285  command_print(CMD, "%d", target->defer_examine ? 1 : 0);
5286 
5287  return ERROR_OK;
5288 }
5289 
5290 COMMAND_HANDLER(handle_target_halt_gdb)
5291 {
5292  if (CMD_ARGC != 0)
5294 
5296 
5298 }
5299 
5300 COMMAND_HANDLER(handle_target_poll)
5301 {
5302  if (CMD_ARGC != 0)
5304 
5306  if (!target->tap->enabled) {
5307  command_print(CMD, "[TAP is disabled]");
5308  return ERROR_FAIL;
5309  }
5310 
5311  if (!(target_was_examined(target)))
5313 
5314  return target->type->poll(target);
5315 }
5316 
5317 COMMAND_HANDLER(handle_target_reset)
5318 {
5319  if (CMD_ARGC != 2)
5321 
5322  const struct nvp *n = nvp_name2value(nvp_assert, CMD_ARGV[0]);
5323  if (!n->name) {
5326  }
5327 
5328  /* the halt or not param */
5329  int a;
5330  COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], a);
5331 
5333  if (!target->tap->enabled) {
5334  command_print(CMD, "[TAP is disabled]");
5335  return ERROR_FAIL;
5336  }
5337 
5339  command_print(CMD, "No target-specific reset for %s", target_name(target));
5340  return ERROR_FAIL;
5341  }
5342 
5343  /* determine if we should halt or not. */
5344  target->reset_halt = (a != 0);
5345  /* When this happens - all workareas are invalid. */
5347 
5348  /* do the assert */
5349  if (n->value == NVP_ASSERT) {
5350  int retval = target->type->assert_reset(target);
5351  if (target->defer_examine)
5353  return retval;
5354  }
5355 
5356  return target->type->deassert_reset(target);
5357 }
5358 
5359 COMMAND_HANDLER(handle_target_halt)
5360 {
5361  if (CMD_ARGC != 0)
5363 
5365  if (!target->tap->enabled) {
5366  command_print(CMD, "[TAP is disabled]");
5367  return ERROR_FAIL;
5368  }
5369 
5370  return target->type->halt(target);
5371 }
5372 
5373 COMMAND_HANDLER(handle_target_wait_state)
5374 {
5375  if (CMD_ARGC != 2)
5377 
5378  const struct nvp *n = nvp_name2value(nvp_target_state, CMD_ARGV[0]);
5379  if (!n->name) {
5382  }
5383 
5384  unsigned int a;
5385  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], a);
5386 
5388  if (!target->tap->enabled) {
5389  command_print(CMD, "[TAP is disabled]");
5390  return ERROR_FAIL;
5391  }
5392 
5393  int retval = target_wait_state(target, n->value, a);
5394  if (retval != ERROR_OK) {
5396  "target: %s wait %s fails (%d) %s",
5397  target_name(target), n->name,
5398  retval, target_strerror_safe(retval));
5399  return retval;
5400  }
5401  return ERROR_OK;
5402 }
5403 /* List for human, Events defined for this target.
5404  * scripts/programs should use 'name cget -event NAME'
5405  */
5406 COMMAND_HANDLER(handle_target_event_list)
5407 {
5409  struct target_event_action *teap;
5410 
5411  command_print(CMD, "Event actions for target %s\n",
5412  target_name(target));
5413  command_print(CMD, "%-25s | Body", "Event");
5414  command_print(CMD, "------------------------- | "
5415  "----------------------------------------");
5416 
5418  command_print(CMD, "%-25s | %s",
5419  target_event_name(teap->event),
5420  Jim_GetString(teap->body, NULL));
5421 
5422  command_print(CMD, "***END***");
5423  return ERROR_OK;
5424 }
5425 
5426 COMMAND_HANDLER(handle_target_current_state)
5427 {
5428  if (CMD_ARGC != 0)
5430 
5432 
5434 
5435  return ERROR_OK;
5436 }
5437 
5438 COMMAND_HANDLER(handle_target_debug_reason)
5439 {
5440  if (CMD_ARGC != 0)
5442 
5444 
5445 
5448 
5449  if (!debug_reason) {
5450  command_print(CMD, "bug: invalid debug reason (%d)",
5451  target->debug_reason);
5452  return ERROR_FAIL;
5453  }
5454 
5455  command_print(CMD, "%s", debug_reason);
5456 
5457  return ERROR_OK;
5458 }
5459 
5460 COMMAND_HANDLER(handle_target_invoke_event)
5461 {
5462  if (CMD_ARGC != 1)
5464 
5465  const struct nvp *n = nvp_name2value(nvp_target_event, CMD_ARGV[0]);
5466  if (!n->name) {
5469  }
5470 
5473  return ERROR_OK;
5474 }
5475 
5477  {
5478  .name = "configure",
5479  .mode = COMMAND_ANY,
5480  .handler = handle_target_configure,
5481  .help = "configure a new target for use",
5482  .usage = "[target_attribute ...]",
5483  },
5484  {
5485  .name = "cget",
5486  .mode = COMMAND_ANY,
5487  .handler = handle_target_configure,
5488  .help = "returns the specified target attribute",
5489  .usage = "target_attribute",
5490  },
5491  {
5492  .name = "mwd",
5493  .handler = handle_mw_command,
5494  .mode = COMMAND_EXEC,
5495  .help = "Write 64-bit word(s) to target memory",
5496  .usage = "address data [count]",
5497  },
5498  {
5499  .name = "mww",
5500  .handler = handle_mw_command,
5501  .mode = COMMAND_EXEC,
5502  .help = "Write 32-bit word(s) to target memory",
5503  .usage = "address data [count]",
5504  },
5505  {
5506  .name = "mwh",
5507  .handler = handle_mw_command,
5508  .mode = COMMAND_EXEC,
5509  .help = "Write 16-bit half-word(s) to target memory",
5510  .usage = "address data [count]",
5511  },
5512  {
5513  .name = "mwb",
5514  .handler = handle_mw_command,
5515  .mode = COMMAND_EXEC,
5516  .help = "Write byte(s) to target memory",
5517  .usage = "address data [count]",
5518  },
5519  {
5520  .name = "mdd",
5521  .handler = handle_md_command,
5522  .mode = COMMAND_EXEC,
5523  .help = "Display target memory as 64-bit words",
5524  .usage = "address [count]",
5525  },
5526  {
5527  .name = "mdw",
5528  .handler = handle_md_command,
5529  .mode = COMMAND_EXEC,
5530  .help = "Display target memory as 32-bit words",
5531  .usage = "address [count]",
5532  },
5533  {
5534  .name = "mdh",
5535  .handler = handle_md_command,
5536  .mode = COMMAND_EXEC,
5537  .help = "Display target memory as 16-bit half-words",
5538  .usage = "address [count]",
5539  },
5540  {
5541  .name = "mdb",
5542  .handler = handle_md_command,
5543  .mode = COMMAND_EXEC,
5544  .help = "Display target memory as 8-bit bytes",
5545  .usage = "address [count]",
5546  },
5547  {
5548  .name = "get_reg",
5549  .mode = COMMAND_EXEC,
5550  .handler = handle_target_get_reg,
5551  .help = "Get register values from the target",
5552  .usage = "[-force] list",
5553  },
5554  {
5555  .name = "set_reg",
5556  .mode = COMMAND_EXEC,
5557  .handler = handle_set_reg_command,
5558  .help = "Set target register values",
5559  .usage = "dict",
5560  },
5561  {
5562  .name = "read_memory",
5563  .mode = COMMAND_EXEC,
5564  .handler = handle_target_read_memory,
5565  .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
5566  .usage = "address width count ['phys']",
5567  },
5568  {
5569  .name = "write_memory",
5570  .mode = COMMAND_EXEC,
5571  .handler = handle_target_write_memory,
5572  .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
5573  .usage = "address width data ['phys']",
5574  },
5575  {
5576  .name = "eventlist",
5577  .handler = handle_target_event_list,
5578  .mode = COMMAND_EXEC,
5579  .help = "displays a table of events defined for this target",
5580  .usage = "",
5581  },
5582  {
5583  .name = "curstate",
5584  .mode = COMMAND_EXEC,
5585  .handler = handle_target_current_state,
5586  .help = "displays the current state of this target",
5587  .usage = "",
5588  },
5589  {
5590  .name = "debug_reason",
5591  .mode = COMMAND_EXEC,
5592  .handler = handle_target_debug_reason,
5593  .help = "displays the debug reason of this target",
5594  .usage = "",
5595  },
5596  {
5597  .name = "arp_examine",
5598  .mode = COMMAND_EXEC,
5599  .handler = handle_target_examine,
5600  .help = "used internally for reset processing",
5601  .usage = "['allow-defer']",
5602  },
5603  {
5604  .name = "was_examined",
5605  .mode = COMMAND_EXEC,
5606  .handler = handle_target_was_examined,
5607  .help = "used internally for reset processing",
5608  .usage = "",
5609  },
5610  {
5611  .name = "examine_deferred",
5612  .mode = COMMAND_EXEC,
5613  .handler = handle_target_examine_deferred,
5614  .help = "used internally for reset processing",
5615  .usage = "",
5616  },
5617  {
5618  .name = "arp_halt_gdb",
5619  .mode = COMMAND_EXEC,
5620  .handler = handle_target_halt_gdb,
5621  .help = "used internally for reset processing to halt GDB",
5622  .usage = "",
5623  },
5624  {
5625  .name = "arp_poll",
5626  .mode = COMMAND_EXEC,
5627  .handler = handle_target_poll,
5628  .help = "used internally for reset processing",
5629  .usage = "",
5630  },
5631  {
5632  .name = "arp_reset",
5633  .mode = COMMAND_EXEC,
5634  .handler = handle_target_reset,
5635  .help = "used internally for reset processing",
5636  .usage = "'assert'|'deassert' halt",
5637  },
5638  {
5639  .name = "arp_halt",
5640  .mode = COMMAND_EXEC,
5641  .handler = handle_target_halt,
5642  .help = "used internally for reset processing",
5643  .usage = "",
5644  },
5645  {
5646  .name = "arp_waitstate",
5647  .mode = COMMAND_EXEC,
5648  .handler = handle_target_wait_state,
5649  .help = "used internally for reset processing",
5650  .usage = "statename timeoutmsecs",
5651  },
5652  {
5653  .name = "invoke-event",
5654  .mode = COMMAND_EXEC,
5655  .handler = handle_target_invoke_event,
5656  .help = "invoke handler for specified event",
5657  .usage = "event_name",
5658  },
5660 };
5661 
5662 COMMAND_HANDLER(handle_target_create)
5663 {
5664  int retval = ERROR_OK;
5665 
5666  if (CMD_ARGC < 2)
5668 
5669  /* check if the target name clashes with an existing command name */
5670  Jim_Cmd *jimcmd = Jim_GetCommand(CMD_CTX->interp, CMD_JIMTCL_ARGV[0], JIM_NONE);
5671  if (jimcmd) {
5672  command_print(CMD, "Command/target: %s Exists", CMD_ARGV[0]);
5673  return ERROR_FAIL;
5674  }
5675 
5676  /* TYPE */
5677  const char *cp = CMD_ARGV[1];
5678  struct transport *tr = get_current_transport();
5679  if (tr && tr->override_target) {
5680  retval = tr->override_target(&cp);
5681  if (retval != ERROR_OK) {
5682  command_print(CMD, "The selected transport doesn't support this target");
5683  return retval;
5684  }
5685  LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5686  }
5687  /* now does target type exist */
5688  size_t x;
5689  for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
5690  if (strcmp(cp, target_types[x]->name) == 0) {
5691  /* found */
5692  break;
5693  }
5694  }
5695  if (x == ARRAY_SIZE(target_types)) {
5696  char *all = NULL;
5697  for (x = 0 ; x < ARRAY_SIZE(target_types) ; x++) {
5698  char *prev = all;
5699  if (all)
5700  all = alloc_printf("%s, %s", all, target_types[x]->name);
5701  else
5702  all = alloc_printf("%s", target_types[x]->name);
5703  free(prev);
5704  if (!all) {
5705  LOG_ERROR("Out of memory");
5706  return ERROR_FAIL;
5707  }
5708  }
5709  command_print(CMD, "Unknown target type %s, try one of %s", cp, all);
5710  free(all);
5711  return ERROR_FAIL;
5712  }
5713 
5714  /* Create it */
5715  struct target *target = calloc(1, sizeof(struct target));
5716  if (!target) {
5717  LOG_ERROR("Out of memory");
5718  return ERROR_FAIL;
5719  }
5720 
5721  /* set empty smp cluster */
5722  target->smp_targets = &empty_smp_targets;
5723 
5724  /* allocate memory for each unique target type */
5725  target->type = malloc(sizeof(struct target_type));
5726  if (!target->type) {
5727  LOG_ERROR("Out of memory");
5728  free(target);
5729  return ERROR_FAIL;
5730  }
5731 
5732  memcpy(target->type, target_types[x], sizeof(struct target_type));
5733 
5734  /* default to first core, override with -coreid */
5735  target->coreid = 0;
5736 
5737  target->working_area = 0x0;
5738  target->working_area_size = 0x0;
5740  target->backup_working_area = false;
5741 
5744  target->reg_cache = NULL;
5745  target->breakpoints = NULL;
5746  target->watchpoints = NULL;
5747  target->next = NULL;
5748  target->arch_info = NULL;
5749 
5750  target->verbose_halt_msg = true;
5751 
5752  target->halt_issued = false;
5753 
5755 
5756  /* initialize trace information */
5757  target->trace_info = calloc(1, sizeof(struct trace));
5758  if (!target->trace_info) {
5759  LOG_ERROR("Out of memory");
5760  free(target->type);
5761  free(target);
5762  return ERROR_FAIL;
5763  }
5764 
5765  target->dbgmsg = NULL;
5766  target->dbg_msg_enabled = false;
5767 
5769 
5770  target->rtos = NULL;
5771  target->rtos_auto_detect = false;
5772 
5775 
5776  target->cmd_name = strdup(CMD_ARGV[0]);
5777  if (!target->cmd_name) {
5778  LOG_ERROR("Out of memory");
5779  free(target->trace_info);
5780  free(target->type);
5781  free(target);
5782  return ERROR_FAIL;
5783  }
5784 
5785  /* Do the rest as "configure" options */
5786  bool is_configure = true;
5787  retval = CALL_COMMAND_HANDLER(target_configure, target, 2, is_configure);
5788  if (retval == ERROR_OK) {
5789  if (target->has_dap) {
5790  if (!target->dap_configured) {
5791  command_print(CMD, "-dap ?name? required when creating target");
5793  }
5794  } else {
5795  if (!target->tap_configured) {
5796  command_print(CMD, "-chain-position ?name? required when creating target");
5798  }
5799  }
5800  /* tap must be set after target was configured */
5801  if (!target->tap)
5803  }
5804 
5805  if (retval != ERROR_OK) {
5807  free(target->gdb_port_override);
5808  free(target->trace_info);
5809  free(target->type);
5810  free(target->private_config);
5811  free(target);
5812  return retval;
5813  }
5814 
5816  /* default endian to little if not specified */
5818  }
5819 
5820  if (target->type->target_create) {
5821  retval = (*target->type->target_create)(target);
5822  if (retval != ERROR_OK) {
5823  LOG_DEBUG("target_create failed");
5824  free(target->cmd_name);
5826  free(target->gdb_port_override);
5827  free(target->trace_info);
5828  free(target->type);
5829  free(target->private_config);
5830  free(target);
5831  return retval;
5832  }
5833  }
5834 
5835  /* create the target specific commands */
5836  if (target->type->commands) {
5838  if (retval != ERROR_OK)
5839  LOG_ERROR("unable to register '%s' commands", CMD_ARGV[0]);
5840  }
5841 
5842  /* now - create the new target name command */
5843  const struct command_registration target_subcommands[] = {
5844  {
5846  },
5847  {
5848  .chain = target->type->commands,
5849  },
5851  };
5852  const struct command_registration target_commands[] = {
5853  {
5854  .name = CMD_ARGV[0],
5855  .mode = COMMAND_ANY,
5856  .help = "target command group",
5857  .usage = "",
5858  .chain = target_subcommands,
5859  },
5861  };
5862  retval = register_commands_override_target(CMD_CTX, NULL, target_commands, target);
5863  if (retval != ERROR_OK) {
5864  if (target->type->deinit_target)
5866  free(target->cmd_name);
5868  free(target->gdb_port_override);
5869  free(target->trace_info);
5870  free(target->type);
5871  free(target);
5872  return retval;
5873  }
5874 
5875  /* append to end of list */
5877 
5878  CMD_CTX->current_target = target;
5879  return ERROR_OK;
5880 }
5881 
5882 COMMAND_HANDLER(handle_target_current)
5883 {
5884  if (CMD_ARGC != 0)
5886 
5888  if (target)
5890 
5891  return ERROR_OK;
5892 }
5893 
5894 COMMAND_HANDLER(handle_target_types)
5895 {
5896  if (CMD_ARGC != 0)
5898 
5899  for (size_t x = 0; x < ARRAY_SIZE(target_types); x++)
5900  command_print(CMD, "%s", target_types[x]->name);
5901 
5902  return ERROR_OK;
5903 }
5904 
5905 COMMAND_HANDLER(handle_target_names)
5906 {
5907  if (CMD_ARGC != 0)
5909 
5910  struct target *target = all_targets;
5911  while (target) {
5913  target = target->next;
5914  }
5915 
5916  return ERROR_OK;
5917 }
5918 
5919 static struct target_list *
5920 __attribute__((warn_unused_result))
5921 create_target_list_node(const char *targetname)
5922 {
5923  struct target *target = get_target(targetname);
5924  LOG_DEBUG("%s ", targetname);
5925  if (!target)
5926  return NULL;
5927 
5928  struct target_list *new = malloc(sizeof(struct target_list));
5929  if (!new) {
5930  LOG_ERROR("Out of memory");
5931  return new;
5932  }
5933 
5934  new->target = target;
5935  return new;
5936 }
5937 
5939  struct list_head *lh, struct target **result)
5940 {
5941  struct target *target = NULL;
5942  struct target_list *curr;
5943  foreach_smp_target(curr, lh) {
5944  struct rtos *curr_rtos = curr->target->rtos;
5945  if (curr_rtos) {
5946  if (target && target->rtos && target->rtos->type != curr_rtos->type) {
5947  command_print(cmd, "Different rtos types in members of one smp target!");
5948  return ERROR_FAIL;
5949  }
5950  target = curr->target;
5951  }
5952  }
5953  *result = target;
5954  return ERROR_OK;
5955 }
5956 
5957 COMMAND_HANDLER(handle_target_smp)
5958 {
5959  static unsigned int smp_group = 1;
5960 
5961  if (CMD_ARGC == 0) {
5962  LOG_DEBUG("Empty SMP target");
5963  return ERROR_OK;
5964  }
5965  LOG_DEBUG("%d", CMD_ARGC);
5966  /* CMD_ARGC[0] = target to associate in smp
5967  * CMD_ARGC[1] = target to associate in smp
5968  * CMD_ARGC[2] ...
5969  */
5970 
5971  struct list_head *lh = malloc(sizeof(*lh));
5972  if (!lh) {
5973  LOG_ERROR("Out of memory");
5974  return ERROR_FAIL;
5975  }
5976  INIT_LIST_HEAD(lh);
5977 
5978  for (unsigned int i = 0; i < CMD_ARGC; i++) {
5979  struct target_list *new = create_target_list_node(CMD_ARGV[i]);
5980  if (new)
5981  list_add_tail(&new->lh, lh);
5982  }
5983  /* now parse the list of cpu and put the target in smp mode*/
5984  struct target_list *curr;
5985  foreach_smp_target(curr, lh) {
5986  struct target *target = curr->target;
5987  target->smp = smp_group;
5988  target->smp_targets = lh;
5989  }
5990  smp_group++;
5991 
5992  struct target *rtos_target;
5993  int retval = get_target_with_common_rtos_type(CMD, lh, &rtos_target);
5994  if (retval == ERROR_OK && rtos_target)
5995  retval = rtos_smp_init(rtos_target);
5996 
5997  return retval;
5998 }
5999 
6000 static const struct command_registration target_subcommand_handlers[] = {
6001  {
6002  .name = "init",
6003  .mode = COMMAND_CONFIG,
6004  .handler = handle_target_init_command,
6005  .help = "initialize targets",
6006  .usage = "",
6007  },
6008  {
6009  .name = "create",
6010  .mode = COMMAND_CONFIG,
6011  .handler = handle_target_create,
6012  .usage = "name type [options ...]",
6013  .help = "Creates and selects a new target",
6014  },
6015  {
6016  .name = "current",
6017  .mode = COMMAND_ANY,
6018  .handler = handle_target_current,
6019  .help = "Returns the currently selected target",
6020  .usage = "",
6021  },
6022  {
6023  .name = "types",
6024  .mode = COMMAND_ANY,
6025  .handler = handle_target_types,
6026  .help = "Returns the available target types as "
6027  "a list of strings",
6028  .usage = "",
6029  },
6030  {
6031  .name = "names",
6032  .mode = COMMAND_ANY,
6033  .handler = handle_target_names,
6034  .help = "Returns the names of all targets as a list of strings",
6035  .usage = "",
6036  },
6037  {
6038  .name = "smp",
6039  .mode = COMMAND_ANY,
6040  .handler = handle_target_smp,
6041  .usage = "targetname1 targetname2 ...",
6042  .help = "gather several target in a smp list"
6043  },
6044 
6046 };
6047 
6048 struct fast_load {
6050  uint8_t *data;
6051  int length;
6052 
6053 };
6054 
6055 static int fastload_num;
6056 static struct fast_load *fastload;
6057 
6058 static void free_fastload(void)
6059 {
6060  if (fastload) {
6061  for (int i = 0; i < fastload_num; i++)
6062  free(fastload[i].data);
6063  free(fastload);
6064  fastload = NULL;
6065  }
6066 }
6067 
6068 COMMAND_HANDLER(handle_fast_load_image_command)
6069 {
6070  uint8_t *buffer;
6071  size_t buf_cnt;
6072  uint32_t image_size;
6073  target_addr_t min_address = 0;
6074  target_addr_t max_address = -1;
6075 
6076  struct image image;
6077 
6078  int retval = CALL_COMMAND_HANDLER(parse_load_image_command,
6079  &image, &min_address, &max_address);
6080  if (retval != ERROR_OK)
6081  return retval;
6082 
6083  struct duration bench;
6084  duration_start(&bench);
6085 
6086  retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
6087  if (retval != ERROR_OK)
6088  return retval;
6089 
6090  image_size = 0x0;
6091  retval = ERROR_OK;
6093  fastload = malloc(sizeof(struct fast_load)*image.num_sections);
6094  if (!fastload) {
6095  command_print(CMD, "out of memory");
6096  image_close(&image);
6097  return ERROR_FAIL;
6098  }
6099  memset(fastload, 0, sizeof(struct fast_load)*image.num_sections);
6100  for (unsigned int i = 0; i < image.num_sections; i++) {
6101  buffer = malloc(image.sections[i].size);
6102  if (!buffer) {
6103  command_print(CMD, "error allocating buffer for section (%d bytes)",
6104  (int)(image.sections[i].size));
6105  retval = ERROR_FAIL;
6106  break;
6107  }
6108 
6109  retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
6110  if (retval != ERROR_OK) {
6111  free(buffer);
6112  break;
6113  }
6114 
6115  uint32_t offset = 0;
6116  uint32_t length = buf_cnt;
6117 
6118  /* DANGER!!! beware of unsigned comparison here!!! */
6119 
6120  if ((image.sections[i].base_address + buf_cnt >= min_address) &&
6121  (image.sections[i].base_address < max_address)) {
6122  if (image.sections[i].base_address < min_address) {
6123  /* clip addresses below */
6124  offset += min_address-image.sections[i].base_address;
6125  length -= offset;
6126  }
6127 
6128  if (image.sections[i].base_address + buf_cnt > max_address)
6129  length -= (image.sections[i].base_address + buf_cnt)-max_address;
6130 
6132  fastload[i].data = malloc(length);
6133  if (!fastload[i].data) {
6134  free(buffer);
6135  command_print(CMD, "error allocating buffer for section (%" PRIu32 " bytes)",
6136  length);
6137  retval = ERROR_FAIL;
6138  break;
6139  }
6140  memcpy(fastload[i].data, buffer + offset, length);
6141  fastload[i].length = length;
6142 
6143  image_size += length;
6144  command_print(CMD, "%u bytes written at address 0x%8.8x",
6145  (unsigned int)length,
6146  ((unsigned int)(image.sections[i].base_address + offset)));
6147  }
6148 
6149  free(buffer);
6150  }
6151 
6152  if ((retval == ERROR_OK) && (duration_measure(&bench) == ERROR_OK)) {
6153  command_print(CMD, "Loaded %" PRIu32 " bytes "
6154  "in %fs (%0.3f KiB/s)", image_size,
6155  duration_elapsed(&bench), duration_kbps(&bench, image_size));
6156 
6158  "WARNING: image has not been loaded to target!"
6159  "You can issue a 'fast_load' to finish loading.");
6160  }
6161 
6162  image_close(&image);
6163 
6164  if (retval != ERROR_OK)
6165  free_fastload();
6166 
6167  return retval;
6168 }
6169 
6170 COMMAND_HANDLER(handle_fast_load_command)
6171 {
6172  if (CMD_ARGC > 0)
6174  if (!fastload) {
6175  LOG_ERROR("No image in memory");
6176  return ERROR_FAIL;
6177  }
6178  int i;
6179  int64_t ms = timeval_ms();
6180  int size = 0;
6181  int retval = ERROR_OK;
6182  for (i = 0; i < fastload_num; i++) {
6184  command_print(CMD, "Write to 0x%08x, length 0x%08x",
6185  (unsigned int)(fastload[i].address),
6186  (unsigned int)(fastload[i].length));
6187  retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
6188  if (retval != ERROR_OK)
6189  break;
6190  size += fastload[i].length;
6191  }
6192  if (retval == ERROR_OK) {
6193  int64_t after = timeval_ms();
6194  command_print(CMD, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
6195  }
6196  return retval;
6197 }
6198 
6199 static const struct command_registration target_command_handlers[] = {
6200  {
6201  .name = "targets",
6202  .handler = handle_targets_command,
6203  .mode = COMMAND_ANY,
6204  .help = "change current default target (one parameter) "
6205  "or prints table of all targets (no parameters)",
6206  .usage = "[target]",
6207  },
6208  {
6209  .name = "target",
6210  .mode = COMMAND_CONFIG,
6211  .help = "configure target",
6212  .chain = target_subcommand_handlers,
6213  .usage = "",
6214  },
6216 };
6217 
6219 {
6221 }
6222 
6223 static bool target_reset_nag = true;
6224 
6226 {
6227  return target_reset_nag;
6228 }
6229 
6230 COMMAND_HANDLER(handle_target_reset_nag)
6231 {
6232  return CALL_COMMAND_HANDLER(handle_command_parse_bool,
6233  &target_reset_nag, "Nag after each reset about options to improve "
6234  "performance");
6235 }
6236 
6237 COMMAND_HANDLER(handle_ps_command)
6238 {
6240  char *display;
6241  if (target->state != TARGET_HALTED) {
6242  command_print(CMD, "Error: [%s] not halted", target_name(target));
6243  return ERROR_TARGET_NOT_HALTED;
6244  }
6245 
6246  if ((target->rtos) && (target->rtos->type)
6247  && (target->rtos->type->ps_command)) {
6248  display = target->rtos->type->ps_command(target);
6249  command_print(CMD, "%s", display);
6250  free(display);
6251  return ERROR_OK;
6252  } else {
6253  LOG_INFO("failed");
6254  return ERROR_TARGET_FAILURE;
6255  }
6256 }
6257 
6258 static void binprint(struct command_invocation *cmd, const char *text, const uint8_t *buf, int size)
6259 {
6260  if (text)
6261  command_print_sameline(cmd, "%s", text);
6262  for (int i = 0; i < size; i++)
6263  command_print_sameline(cmd, " %02x", buf[i]);
6264  command_print(cmd, " ");
6265 }
6266 
6267 COMMAND_HANDLER(handle_test_mem_access_command)
6268 {
6270  uint32_t test_size;
6271  int retval = ERROR_OK;
6272 
6273  if (target->state != TARGET_HALTED) {
6274  command_print(CMD, "Error: [%s] not halted", target_name(target));
6275  return ERROR_TARGET_NOT_HALTED;
6276  }
6277 
6278  if (CMD_ARGC != 1)
6280 
6281  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
6282 
6283  /* Test reads */
6284  size_t num_bytes = test_size + 4;
6285 
6286  struct working_area *wa = NULL;
6287  retval = target_alloc_working_area(target, num_bytes, &wa);
6288  if (retval != ERROR_OK) {
6289  LOG_ERROR("Not enough working area");
6290  return ERROR_FAIL;
6291  }
6292 
6293  uint8_t *test_pattern = malloc(num_bytes);
6294 
6295  for (size_t i = 0; i < num_bytes; i++)
6296  test_pattern[i] = rand();
6297 
6298  retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6299  if (retval != ERROR_OK) {
6300  LOG_ERROR("Test pattern write failed");
6301  goto out;
6302  }
6303 
6304  for (int host_offset = 0; host_offset <= 1; host_offset++) {
6305  for (int size = 1; size <= 4; size *= 2) {
6306  for (int offset = 0; offset < 4; offset++) {
6307  uint32_t count = test_size / size;
6308  size_t host_bufsiz = (count + 2) * size + host_offset;
6309  uint8_t *read_ref = malloc(host_bufsiz);
6310  uint8_t *read_buf = malloc(host_bufsiz);
6311 
6312  for (size_t i = 0; i < host_bufsiz; i++) {
6313  read_ref[i] = rand();
6314  read_buf[i] = read_ref[i];
6315  }
6317  "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6318  size, offset, host_offset ? "un" : "");
6319 
6320  struct duration bench;
6321  duration_start(&bench);
6322 
6323  retval = target_read_memory(target, wa->address + offset, size, count,
6324  read_buf + size + host_offset);
6325 
6326  duration_measure(&bench);
6327 
6328  if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6329  command_print(CMD, "Unsupported alignment");
6330  goto next;
6331  } else if (retval != ERROR_OK) {
6332  command_print(CMD, "Memory read failed");
6333  goto next;
6334  }
6335 
6336  /* replay on host */
6337  memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6338 
6339  /* check result */
6340  int result = memcmp(read_ref, read_buf, host_bufsiz);
6341  if (result == 0) {
6342  command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6343  duration_elapsed(&bench),
6344  duration_kbps(&bench, count * size));
6345  } else {
6346  command_print(CMD, "Compare failed");
6347  binprint(CMD, "ref:", read_ref, host_bufsiz);
6348  binprint(CMD, "buf:", read_buf, host_bufsiz);
6349  }
6350 next:
6351  free(read_ref);
6352  free(read_buf);
6353  }
6354  }
6355  }
6356 
6357 out:
6358  free(test_pattern);
6359 
6361 
6362  /* Test writes */
6363  num_bytes = test_size + 4 + 4 + 4;
6364 
6365  retval = target_alloc_working_area(target, num_bytes, &wa);
6366  if (retval != ERROR_OK) {
6367  LOG_ERROR("Not enough working area");
6368  return ERROR_FAIL;
6369  }
6370 
6371  test_pattern = malloc(num_bytes);
6372 
6373  for (size_t i = 0; i < num_bytes; i++)
6374  test_pattern[i] = rand();
6375 
6376  for (int host_offset = 0; host_offset <= 1; host_offset++) {
6377  for (int size = 1; size <= 4; size *= 2) {
6378  for (int offset = 0; offset < 4; offset++) {
6379  uint32_t count = test_size / size;
6380  size_t host_bufsiz = count * size + host_offset;
6381  uint8_t *read_ref = malloc(num_bytes);
6382  uint8_t *read_buf = malloc(num_bytes);
6383  uint8_t *write_buf = malloc(host_bufsiz);
6384 
6385  for (size_t i = 0; i < host_bufsiz; i++)
6386  write_buf[i] = rand();
6388  "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6389  size, offset, host_offset ? "un" : "");
6390 
6391  retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6392  if (retval != ERROR_OK) {
6393  command_print(CMD, "Test pattern write failed");
6394  goto nextw;
6395  }
6396 
6397  /* replay on host */
6398  memcpy(read_ref, test_pattern, num_bytes);
6399  memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6400 
6401  struct duration bench;
6402  duration_start(&bench);
6403 
6404  retval = target_write_memory(target, wa->address + size + offset, size, count,
6405  write_buf + host_offset);
6406 
6407  duration_measure(&bench);
6408 
6409  if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6410  command_print(CMD, "Unsupported alignment");
6411  goto nextw;
6412  } else if (retval != ERROR_OK) {
6413  command_print(CMD, "Memory write failed");
6414  goto nextw;
6415  }
6416 
6417  /* read back */
6418  retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6419  if (retval != ERROR_OK) {
6420  command_print(CMD, "Test pattern write failed");
6421  goto nextw;
6422  }
6423 
6424  /* check result */
6425  int result = memcmp(read_ref, read_buf, num_bytes);
6426  if (result == 0) {
6427  command_print(CMD, "Pass in %fs (%0.3f KiB/s)",
6428  duration_elapsed(&bench),
6429  duration_kbps(&bench, count * size));
6430  } else {
6431  command_print(CMD, "Compare failed");
6432  binprint(CMD, "ref:", read_ref, num_bytes);
6433  binprint(CMD, "buf:", read_buf, num_bytes);
6434  }
6435 nextw:
6436  free(read_ref);
6437  free(read_buf);
6438  }
6439  }
6440  }
6441 
6442  free(test_pattern);
6443 
6445  return retval;
6446 }
6447 
6448 static const struct command_registration target_exec_command_handlers[] = {
6449  {
6450  .name = "fast_load_image",
6451  .handler = handle_fast_load_image_command,
6452  .mode = COMMAND_ANY,
6453  .help = "Load image into server memory for later use by "
6454  "fast_load; primarily for profiling",
6455  .usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
6456  "[min_address [max_length]]]]",
6457  },
6458  {
6459  .name = "fast_load",
6460  .handler = handle_fast_load_command,
6461  .mode = COMMAND_EXEC,
6462  .help = "loads active fast load image to current target "
6463  "- mainly for profiling purposes",
6464  .usage = "",
6465  },
6466  {
6467  .name = "profile",
6468  .handler = handle_profile_command,
6469  .mode = COMMAND_EXEC,
6470  .usage = "seconds filename [start end]",
6471  .help = "profiling samples the CPU PC",
6472  },
6474  {
6475  .name = "virt2phys",
6476  .handler = handle_virt2phys_command,
6477  .mode = COMMAND_ANY,
6478  .help = "translate a virtual address into a physical address",
6479  .usage = "virtual_address",
6480  },
6481  {
6482  .name = "reg",
6483  .handler = handle_reg_command,
6484  .mode = COMMAND_EXEC,
6485  .help = "display (reread from target with \"force\") or set a register; "
6486  "with no arguments, displays all registers and their values",
6487  .usage = "[(register_number|register_name) [(value|'force')]]",
6488  },
6489  {
6490  .name = "poll",
6491  .handler = handle_poll_command,
6492  .mode = COMMAND_EXEC,
6493  .help = "poll target state; or reconfigure background polling",
6494  .usage = "['on'|'off']",
6495  },
6496  {
6497  .name = "wait_halt",
6498  .handler = handle_wait_halt_command,
6499  .mode = COMMAND_EXEC,
6500  .help = "wait up to the specified number of milliseconds "
6501  "(default 5000) for a previously requested halt",
6502  .usage = "[milliseconds]",
6503  },
6504  {
6505  .name = "halt",
6506  .handler = handle_halt_command,
6507  .mode = COMMAND_EXEC,
6508  .help = "request target to halt, then wait up to the specified "
6509  "number of milliseconds (default 5000) for it to complete",
6510  .usage = "[milliseconds]",
6511  },
6512  {
6513  .name = "resume",
6514  .handler = handle_resume_command,
6515  .mode = COMMAND_EXEC,
6516  .help = "resume target execution from current PC or address",
6517  .usage = "[address]",
6518  },
6519  {
6520  .name = "reset",
6521  .handler = handle_reset_command,
6522  .mode = COMMAND_EXEC,
6523  .usage = "[run|halt|init]",
6524  .help = "Reset all targets into the specified mode. "
6525  "Default reset mode is run, if not given.",
6526  },
6527  {
6528  .name = "soft_reset_halt",
6529  .handler = handle_soft_reset_halt_command,
6530  .mode = COMMAND_EXEC,
6531  .usage = "",
6532  .help = "halt the target and do a soft reset",
6533  },
6534  {
6535  .name = "step",
6536  .handler = handle_step_command,
6537  .mode = COMMAND_EXEC,
6538  .help = "step one instruction from current PC or address",
6539  .usage = "[address]",
6540  },
6541  {
6542  .name = "mdd",
6543  .handler = handle_md_command,
6544  .mode = COMMAND_EXEC,
6545  .help = "display memory double-words",
6546  .usage = "['phys'] address [count]",
6547  },
6548  {
6549  .name = "mdw",
6550  .handler = handle_md_command,
6551  .mode = COMMAND_EXEC,
6552  .help = "display memory words",
6553  .usage = "['phys'] address [count]",
6554  },
6555  {
6556  .name = "mdh",
6557  .handler = handle_md_command,
6558  .mode = COMMAND_EXEC,
6559  .help = "display memory half-words",
6560  .usage = "['phys'] address [count]",
6561  },
6562  {
6563  .name = "mdb",
6564  .handler = handle_md_command,
6565  .mode = COMMAND_EXEC,
6566  .help = "display memory bytes",
6567  .usage = "['phys'] address [count]",
6568  },
6569  {
6570  .name = "mwd",
6571  .handler = handle_mw_command,
6572  .mode = COMMAND_EXEC,
6573  .help = "write memory double-word",
6574  .usage = "['phys'] address value [count]",
6575  },
6576  {
6577  .name = "mww",
6578  .handler = handle_mw_command,
6579  .mode = COMMAND_EXEC,
6580  .help = "write memory word",
6581  .usage = "['phys'] address value [count]",
6582  },
6583  {
6584  .name = "mwh",
6585  .handler = handle_mw_command,
6586  .mode = COMMAND_EXEC,
6587  .help = "write memory half-word",
6588  .usage = "['phys'] address value [count]",
6589  },
6590  {
6591  .name = "mwb",
6592  .handler = handle_mw_command,
6593  .mode = COMMAND_EXEC,
6594  .help = "write memory byte",
6595  .usage = "['phys'] address value [count]",
6596  },
6597  {
6598  .name = "bp",
6599  .handler = handle_bp_command,
6600  .mode = COMMAND_EXEC,
6601  .help = "list or set hardware or software breakpoint",
6602  .usage = "[<address> [<asid>] <length> ['hw'|'hw_ctx']]",
6603  },
6604  {
6605  .name = "rbp",
6606  .handler = handle_rbp_command,
6607  .mode = COMMAND_EXEC,
6608  .help = "remove breakpoint",
6609  .usage = "'all' | address",
6610  },
6611  {
6612  .name = "wp",
6613  .handler = handle_wp_command,
6614  .mode = COMMAND_EXEC,
6615  .help = "list (no params) or create watchpoints",
6616  .usage = "[address length [('r'|'w'|'a') [value [mask]]]]",
6617  },
6618  {
6619  .name = "rwp",
6620  .handler = handle_rwp_command,
6621  .mode = COMMAND_EXEC,
6622  .help = "remove watchpoint",
6623  .usage = "'all' | address",
6624  },
6625  {
6626  .name = "load_image",
6627  .handler = handle_load_image_command,
6628  .mode = COMMAND_EXEC,
6629  .usage = "filename [address ['bin'|'ihex'|'elf'|'s19' "
6630  "[min_address [max_length]]]]",
6631  },
6632  {
6633  .name = "dump_image",
6634  .handler = handle_dump_image_command,
6635  .mode = COMMAND_EXEC,
6636  .usage = "filename address size",
6637  },
6638  {
6639  .name = "verify_image_checksum",
6640  .handler = handle_verify_image_checksum_command,
6641  .mode = COMMAND_EXEC,
6642  .usage = "filename [offset [type]]",
6643  },
6644  {
6645  .name = "verify_image",
6646  .handler = handle_verify_image_command,
6647  .mode = COMMAND_EXEC,
6648  .usage = "filename [offset [type]]",
6649  },
6650  {
6651  .name = "test_image",
6652  .handler = handle_test_image_command,
6653  .mode = COMMAND_EXEC,
6654  .usage = "filename [offset [type]]",
6655  },
6656  {
6657  .name = "get_reg",
6658  .mode = COMMAND_EXEC,
6659  .handler = handle_target_get_reg,
6660  .help = "Get register values from the target",
6661  .usage = "[-force] list",
6662  },
6663  {
6664  .name = "set_reg",
6665  .mode = COMMAND_EXEC,
6666  .handler = handle_set_reg_command,
6667  .help = "Set target register values",
6668  .usage = "dict",
6669  },
6670  {
6671  .name = "read_memory",
6672  .mode = COMMAND_EXEC,
6673  .handler = handle_target_read_memory,
6674  .help = "Read Tcl list of 8/16/32/64 bit numbers from target memory",
6675  .usage = "address width count ['phys']",
6676  },
6677  {
6678  .name = "write_memory",
6679  .mode = COMMAND_EXEC,
6680  .handler = handle_target_write_memory,
6681  .help = "Write Tcl list of 8/16/32/64 bit numbers to target memory",
6682  .usage = "address width data ['phys']",
6683  },
6684  {
6685  .name = "debug_reason",
6686  .mode = COMMAND_EXEC,
6687  .handler = handle_target_debug_reason,
6688  .help = "displays the debug reason of this target",
6689  .usage = "",
6690  },
6691  {
6692  .name = "reset_nag",
6693  .handler = handle_target_reset_nag,
6694  .mode = COMMAND_ANY,
6695  .help = "Nag after each reset about options that could have been "
6696  "enabled to improve performance.",
6697  .usage = "['enable'|'disable']",
6698  },
6699  {
6700  .name = "ps",
6701  .handler = handle_ps_command,
6702  .mode = COMMAND_EXEC,
6703  .help = "list all tasks",
6704  .usage = "",
6705  },
6706  {
6707  .name = "test_mem_access",
6708  .handler = handle_test_mem_access_command,
6709  .mode = COMMAND_EXEC,
6710  .help = "Test the target's memory access functions",
6711  .usage = "size",
6712  },
6713 
6715 };
6717 {
6718  int retval = ERROR_OK;
6719  retval = target_request_register_commands(cmd_ctx);
6720  if (retval != ERROR_OK)
6721  return retval;
6722 
6723  retval = trace_register_commands(cmd_ctx);
6724  if (retval != ERROR_OK)
6725  return retval;
6726 
6727 
6729 }
6730 
6732 {
6733  switch (reason) {
6734  case DBG_REASON_DBGRQ:
6735  return "DBGRQ";
6736  case DBG_REASON_BREAKPOINT:
6737  return "BREAKPOINT";
6738  case DBG_REASON_WATCHPOINT:
6739  return "WATCHPOINT";
6740  case DBG_REASON_WPTANDBKPT:
6741  return "WPTANDBKPT";
6742  case DBG_REASON_SINGLESTEP:
6743  return "SINGLESTEP";
6744  case DBG_REASON_NOTHALTED:
6745  return "NOTHALTED";
6746  case DBG_REASON_EXIT:
6747  return "EXIT";
6748  case DBG_REASON_EXC_CATCH:
6749  return "EXC_CATCH";
6750  case DBG_REASON_UNDEFINED:
6751  return "UNDEFINED";
6752  default:
6753  return "UNKNOWN!";
6754  }
6755 }
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:471
void keep_alive(void)
Definition: log.c:430
char * alloc_printf(const char *format,...)
Definition: log.c:379
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:154
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:179
#define LOG_WARNING(expr ...)
Definition: log.h:131
#define ERROR_FAIL
Definition: log.h:175
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:163
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:151
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_INFO(expr ...)
Definition: log.h:128
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
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:748
#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:6051
uint8_t * data
Definition: target.c:6050
target_addr_t address
Definition: target.c:6049
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
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(* blank_check_memory)(struct target *target, struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
Definition: target_type.h:143
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:6223
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:4155
static int run_srst_deasserted
Definition: target.c:2808
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:2689
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:2672
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:4142
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:6258
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:6199
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:2805
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:6218
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:3867
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:5938
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:2658
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:2641
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:2590
static void write_string(FILE *f, char *s)
Definition: target.c:4150
int target_blank_check_memory(struct target *target, struct target_memory_check_block *blocks, int num_blocks, uint8_t erased_value)
Definition: target.c:2515
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:5920
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:6000
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:2607
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:3529
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:6731
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:4798
@ TCFG_GDB_MAX_CONNECTIONS
Definition: target.c:4812
@ TCFG_CHAIN_POSITION
Definition: target.c:4807
@ TCFG_GDB_PORT
Definition: target.c:4811
@ TCFG_WORK_AREA_VIRT
Definition: target.c:4801
@ TCFG_TYPE
Definition: target.c:4799
@ TCFG_WORK_AREA_BACKUP
Definition: target.c:4804
@ TCFG_RTOS
Definition: target.c:4809
@ TCFG_DBGBASE
Definition: target.c:4808
@ TCFG_WORK_AREA_PHYS
Definition: target.c:4802
@ TCFG_ENDIAN
Definition: target.c:4805
@ TCFG_WORK_AREA_SIZE
Definition: target.c:4803
@ TCFG_EVENT
Definition: target.c:4800
@ TCFG_DEFER_EXAMINE
Definition: target.c:4810
@ TCFG_COREID
Definition: target.c:4806
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:5476
bool get_target_reset_nag(void)
Definition: target.c:6225
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:2737
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:2624
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:2802
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:3418
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:2810
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:2803
static int fastload_num
Definition: target.c:6055
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:4815
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2570
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:2806
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_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2550
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:4787
static const struct command_registration target_exec_command_handlers[]
Definition: target.c:6448
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:6056
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:2807
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:3707
@ IMAGE_TEST
Definition: target.c:3708
@ IMAGE_VERIFY
Definition: target.c:3709
@ IMAGE_CHECKSUM_ONLY
Definition: target.c:3710
int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
Definition: target.c:2706
static void free_fastload(void)
Definition: target.c:6058
static int handle_target(void *priv)
Definition: target.c:2863
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:3421
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:3166
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:3902
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:4158
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:6716
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:4610
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:4135
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:2723
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:3296
int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
Definition: target.c:2530
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