You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1892 lines
49 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <string.h>
  24. #include "etm.h"
  25. #include "etb.h"
  26. #include "armv4_5.h"
  27. #include "arm7_9_common.h"
  28. #include "arm_disassembler.h"
  29. #include "arm_simulator.h"
  30. #include "log.h"
  31. #include "arm_jtag.h"
  32. #include "types.h"
  33. #include "binarybuffer.h"
  34. #include "target.h"
  35. #include "register.h"
  36. #include "jtag.h"
  37. #include "fileio.h"
  38. #include <stdlib.h>
  39. /* ETM register access functionality
  40. *
  41. */
  42. bitfield_desc_t etm_comms_ctrl_bitfield_desc[] =
  43. {
  44. {"R", 1},
  45. {"W", 1},
  46. {"reserved", 26},
  47. {"version", 4}
  48. };
  49. int etm_reg_arch_info[] =
  50. {
  51. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  52. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  53. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  54. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  55. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  56. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  57. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  58. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  59. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  60. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  61. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  62. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  63. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x67,
  64. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  65. };
  66. int etm_reg_arch_size_info[] =
  67. {
  68. 32, 32, 17, 8, 3, 9, 32, 16,
  69. 17, 26, 25, 8, 17, 32, 32, 17,
  70. 32, 32, 32, 32, 32, 32, 32, 32,
  71. 32, 32, 32, 32, 32, 32, 32, 32,
  72. 7, 7, 7, 7, 7, 7, 7, 7,
  73. 7, 7, 7, 7, 7, 7, 7, 7,
  74. 32, 32, 32, 32, 32, 32, 32, 32,
  75. 32, 32, 32, 32, 32, 32, 32, 32,
  76. 32, 32, 32, 32, 32, 32, 32, 32,
  77. 32, 32, 32, 32, 32, 32, 32, 32,
  78. 16, 16, 16, 16, 18, 18, 18, 18,
  79. 17, 17, 17, 17, 16, 16, 16, 16,
  80. 17, 17, 17, 17, 17, 17, 2,
  81. 17, 17, 17, 17, 32, 32, 32, 32
  82. };
  83. char* etm_reg_list[] =
  84. {
  85. "ETM_CTRL",
  86. "ETM_CONFIG",
  87. "ETM_TRIG_EVENT",
  88. "ETM_MMD_CTRL",
  89. "ETM_STATUS",
  90. "ETM_SYS_CONFIG",
  91. "ETM_TRACE_RESOURCE_CTRL",
  92. "ETM_TRACE_EN_CTRL2",
  93. "ETM_TRACE_EN_EVENT",
  94. "ETM_TRACE_EN_CTRL1",
  95. "ETM_FIFOFULL_REGION",
  96. "ETM_FIFOFULL_LEVEL",
  97. "ETM_VIEWDATA_EVENT",
  98. "ETM_VIEWDATA_CTRL1",
  99. "ETM_VIEWDATA_CTRL2",
  100. "ETM_VIEWDATA_CTRL3",
  101. "ETM_ADDR_COMPARATOR_VALUE1",
  102. "ETM_ADDR_COMPARATOR_VALUE2",
  103. "ETM_ADDR_COMPARATOR_VALUE3",
  104. "ETM_ADDR_COMPARATOR_VALUE4",
  105. "ETM_ADDR_COMPARATOR_VALUE5",
  106. "ETM_ADDR_COMPARATOR_VALUE6",
  107. "ETM_ADDR_COMPARATOR_VALUE7",
  108. "ETM_ADDR_COMPARATOR_VALUE8",
  109. "ETM_ADDR_COMPARATOR_VALUE9",
  110. "ETM_ADDR_COMPARATOR_VALUE10",
  111. "ETM_ADDR_COMPARATOR_VALUE11",
  112. "ETM_ADDR_COMPARATOR_VALUE12",
  113. "ETM_ADDR_COMPARATOR_VALUE13",
  114. "ETM_ADDR_COMPARATOR_VALUE14",
  115. "ETM_ADDR_COMPARATOR_VALUE15",
  116. "ETM_ADDR_COMPARATOR_VALUE16",
  117. "ETM_ADDR_ACCESS_TYPE1",
  118. "ETM_ADDR_ACCESS_TYPE2",
  119. "ETM_ADDR_ACCESS_TYPE3",
  120. "ETM_ADDR_ACCESS_TYPE4",
  121. "ETM_ADDR_ACCESS_TYPE5",
  122. "ETM_ADDR_ACCESS_TYPE6",
  123. "ETM_ADDR_ACCESS_TYPE7",
  124. "ETM_ADDR_ACCESS_TYPE8",
  125. "ETM_ADDR_ACCESS_TYPE9",
  126. "ETM_ADDR_ACCESS_TYPE10",
  127. "ETM_ADDR_ACCESS_TYPE11",
  128. "ETM_ADDR_ACCESS_TYPE12",
  129. "ETM_ADDR_ACCESS_TYPE13",
  130. "ETM_ADDR_ACCESS_TYPE14",
  131. "ETM_ADDR_ACCESS_TYPE15",
  132. "ETM_ADDR_ACCESS_TYPE16",
  133. "ETM_DATA_COMPARATOR_VALUE1",
  134. "ETM_DATA_COMPARATOR_VALUE2",
  135. "ETM_DATA_COMPARATOR_VALUE3",
  136. "ETM_DATA_COMPARATOR_VALUE4",
  137. "ETM_DATA_COMPARATOR_VALUE5",
  138. "ETM_DATA_COMPARATOR_VALUE6",
  139. "ETM_DATA_COMPARATOR_VALUE7",
  140. "ETM_DATA_COMPARATOR_VALUE8",
  141. "ETM_DATA_COMPARATOR_VALUE9",
  142. "ETM_DATA_COMPARATOR_VALUE10",
  143. "ETM_DATA_COMPARATOR_VALUE11",
  144. "ETM_DATA_COMPARATOR_VALUE12",
  145. "ETM_DATA_COMPARATOR_VALUE13",
  146. "ETM_DATA_COMPARATOR_VALUE14",
  147. "ETM_DATA_COMPARATOR_VALUE15",
  148. "ETM_DATA_COMPARATOR_VALUE16",
  149. "ETM_DATA_COMPARATOR_MASK1",
  150. "ETM_DATA_COMPARATOR_MASK2",
  151. "ETM_DATA_COMPARATOR_MASK3",
  152. "ETM_DATA_COMPARATOR_MASK4",
  153. "ETM_DATA_COMPARATOR_MASK5",
  154. "ETM_DATA_COMPARATOR_MASK6",
  155. "ETM_DATA_COMPARATOR_MASK7",
  156. "ETM_DATA_COMPARATOR_MASK8",
  157. "ETM_DATA_COMPARATOR_MASK9",
  158. "ETM_DATA_COMPARATOR_MASK10",
  159. "ETM_DATA_COMPARATOR_MASK11",
  160. "ETM_DATA_COMPARATOR_MASK12",
  161. "ETM_DATA_COMPARATOR_MASK13",
  162. "ETM_DATA_COMPARATOR_MASK14",
  163. "ETM_DATA_COMPARATOR_MASK15",
  164. "ETM_DATA_COMPARATOR_MASK16",
  165. "ETM_COUNTER_INITAL_VALUE1",
  166. "ETM_COUNTER_INITAL_VALUE2",
  167. "ETM_COUNTER_INITAL_VALUE3",
  168. "ETM_COUNTER_INITAL_VALUE4",
  169. "ETM_COUNTER_ENABLE1",
  170. "ETM_COUNTER_ENABLE2",
  171. "ETM_COUNTER_ENABLE3",
  172. "ETM_COUNTER_ENABLE4",
  173. "ETM_COUNTER_RELOAD_VALUE1",
  174. "ETM_COUNTER_RELOAD_VALUE2",
  175. "ETM_COUNTER_RELOAD_VALUE3",
  176. "ETM_COUNTER_RELOAD_VALUE4",
  177. "ETM_COUNTER_VALUE1",
  178. "ETM_COUNTER_VALUE2",
  179. "ETM_COUNTER_VALUE3",
  180. "ETM_COUNTER_VALUE4",
  181. "ETM_SEQUENCER_CTRL1",
  182. "ETM_SEQUENCER_CTRL2",
  183. "ETM_SEQUENCER_CTRL3",
  184. "ETM_SEQUENCER_CTRL4",
  185. "ETM_SEQUENCER_CTRL5",
  186. "ETM_SEQUENCER_CTRL6",
  187. "ETM_SEQUENCER_STATE",
  188. "ETM_EXTERNAL_OUTPUT1",
  189. "ETM_EXTERNAL_OUTPUT2",
  190. "ETM_EXTERNAL_OUTPUT3",
  191. "ETM_EXTERNAL_OUTPUT4",
  192. "ETM_CONTEXTID_COMPARATOR_VALUE1",
  193. "ETM_CONTEXTID_COMPARATOR_VALUE2",
  194. "ETM_CONTEXTID_COMPARATOR_VALUE3",
  195. "ETM_CONTEXTID_COMPARATOR_MASK"
  196. };
  197. int etm_reg_arch_type = -1;
  198. int etm_get_reg(reg_t *reg);
  199. int etm_set_reg(reg_t *reg, u32 value);
  200. int etm_set_reg_w_exec(reg_t *reg, u8 *buf);
  201. int etm_write_reg(reg_t *reg, u32 value);
  202. int etm_read_reg(reg_t *reg);
  203. command_t *etm_cmd = NULL;
  204. reg_cache_t* etm_build_reg_cache(target_t *target, arm_jtag_t *jtag_info, etm_context_t *etm_ctx)
  205. {
  206. reg_cache_t *reg_cache = malloc(sizeof(reg_cache_t));
  207. reg_t *reg_list = NULL;
  208. etm_reg_t *arch_info = NULL;
  209. int num_regs = sizeof(etm_reg_arch_info)/sizeof(int);
  210. int i;
  211. /* register a register arch-type for etm registers only once */
  212. if (etm_reg_arch_type == -1)
  213. etm_reg_arch_type = register_reg_arch_type(etm_get_reg, etm_set_reg_w_exec);
  214. /* the actual registers are kept in two arrays */
  215. reg_list = calloc(num_regs, sizeof(reg_t));
  216. arch_info = calloc(num_regs, sizeof(etm_reg_t));
  217. /* fill in values for the reg cache */
  218. reg_cache->name = "etm registers";
  219. reg_cache->next = NULL;
  220. reg_cache->reg_list = reg_list;
  221. reg_cache->num_regs = num_regs;
  222. /* set up registers */
  223. for (i = 0; i < num_regs; i++)
  224. {
  225. reg_list[i].name = etm_reg_list[i];
  226. reg_list[i].size = 32;
  227. reg_list[i].dirty = 0;
  228. reg_list[i].valid = 0;
  229. reg_list[i].bitfield_desc = NULL;
  230. reg_list[i].num_bitfields = 0;
  231. reg_list[i].value = calloc(1, 4);
  232. reg_list[i].arch_info = &arch_info[i];
  233. reg_list[i].arch_type = etm_reg_arch_type;
  234. reg_list[i].size = etm_reg_arch_size_info[i];
  235. arch_info[i].addr = etm_reg_arch_info[i];
  236. arch_info[i].jtag_info = jtag_info;
  237. }
  238. /* the ETM might have an ETB connected */
  239. if (strcmp(etm_ctx->capture_driver->name, "etb") == 0)
  240. {
  241. etb_t *etb = etm_ctx->capture_driver_priv;
  242. if (!etb)
  243. {
  244. LOG_ERROR("etb selected as etm capture driver, but no ETB configured");
  245. return ERROR_OK;
  246. }
  247. reg_cache->next = etb_build_reg_cache(etb);
  248. etb->reg_cache = reg_cache->next;
  249. }
  250. return reg_cache;
  251. }
  252. int etm_setup(target_t *target)
  253. {
  254. int retval;
  255. u32 etm_ctrl_value;
  256. armv4_5_common_t *armv4_5 = target->arch_info;
  257. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  258. etm_context_t *etm_ctx = arm7_9->etm_ctx;
  259. reg_t *etm_ctrl_reg = &arm7_9->etm_ctx->reg_cache->reg_list[ETM_CTRL];
  260. /* initialize some ETM control register settings */
  261. etm_get_reg(etm_ctrl_reg);
  262. etm_ctrl_value = buf_get_u32(etm_ctrl_reg->value, 0, etm_ctrl_reg->size);
  263. /* clear the ETM powerdown bit (0) */
  264. etm_ctrl_value &= ~0x1;
  265. /* configure port width (6:4), mode (17:16) and clocking (13) */
  266. etm_ctrl_value = (etm_ctrl_value &
  267. ~ETM_PORT_WIDTH_MASK & ~ETM_PORT_MODE_MASK & ~ETM_PORT_CLOCK_MASK)
  268. | etm_ctx->portmode;
  269. buf_set_u32(etm_ctrl_reg->value, 0, etm_ctrl_reg->size, etm_ctrl_value);
  270. etm_store_reg(etm_ctrl_reg);
  271. if ((retval=jtag_execute_queue())!=ERROR_OK)
  272. return retval;
  273. if ((retval=etm_ctx->capture_driver->init(etm_ctx)) != ERROR_OK)
  274. {
  275. LOG_ERROR("ETM capture driver initialization failed");
  276. return retval;
  277. }
  278. return ERROR_OK;
  279. }
  280. int etm_get_reg(reg_t *reg)
  281. {
  282. int retval;
  283. if ((retval = etm_read_reg(reg)) != ERROR_OK)
  284. {
  285. LOG_ERROR("BUG: error scheduling etm register read");
  286. return retval;
  287. }
  288. if ((retval = jtag_execute_queue()) != ERROR_OK)
  289. {
  290. LOG_ERROR("register read failed");
  291. return retval;
  292. }
  293. return ERROR_OK;
  294. }
  295. int etm_read_reg_w_check(reg_t *reg, u8* check_value, u8* check_mask)
  296. {
  297. etm_reg_t *etm_reg = reg->arch_info;
  298. u8 reg_addr = etm_reg->addr & 0x7f;
  299. scan_field_t fields[3];
  300. LOG_DEBUG("%i", etm_reg->addr);
  301. jtag_add_end_state(TAP_IDLE);
  302. arm_jtag_scann(etm_reg->jtag_info, 0x6);
  303. arm_jtag_set_instr(etm_reg->jtag_info, etm_reg->jtag_info->intest_instr, NULL);
  304. fields[0].tap = etm_reg->jtag_info->tap;
  305. fields[0].num_bits = 32;
  306. fields[0].out_value = reg->value;
  307. fields[0].out_mask = NULL;
  308. fields[0].in_value = NULL;
  309. fields[0].in_check_value = NULL;
  310. fields[0].in_check_mask = NULL;
  311. fields[0].in_handler = NULL;
  312. fields[0].in_handler_priv = NULL;
  313. fields[1].tap = etm_reg->jtag_info->tap;
  314. fields[1].num_bits = 7;
  315. fields[1].out_value = malloc(1);
  316. buf_set_u32(fields[1].out_value, 0, 7, reg_addr);
  317. fields[1].out_mask = NULL;
  318. fields[1].in_value = NULL;
  319. fields[1].in_check_value = NULL;
  320. fields[1].in_check_mask = NULL;
  321. fields[1].in_handler = NULL;
  322. fields[1].in_handler_priv = NULL;
  323. fields[2].tap = etm_reg->jtag_info->tap;
  324. fields[2].num_bits = 1;
  325. fields[2].out_value = malloc(1);
  326. buf_set_u32(fields[2].out_value, 0, 1, 0);
  327. fields[2].out_mask = NULL;
  328. fields[2].in_value = NULL;
  329. fields[2].in_check_value = NULL;
  330. fields[2].in_check_mask = NULL;
  331. fields[2].in_handler = NULL;
  332. fields[2].in_handler_priv = NULL;
  333. jtag_add_dr_scan(3, fields, -1);
  334. fields[0].in_value = reg->value;
  335. jtag_set_check_value(fields+0, check_value, check_mask, NULL);
  336. jtag_add_dr_scan(3, fields, -1);
  337. free(fields[1].out_value);
  338. free(fields[2].out_value);
  339. return ERROR_OK;
  340. }
  341. int etm_read_reg(reg_t *reg)
  342. {
  343. return etm_read_reg_w_check(reg, NULL, NULL);
  344. }
  345. int etm_set_reg(reg_t *reg, u32 value)
  346. {
  347. int retval;
  348. if ((retval = etm_write_reg(reg, value)) != ERROR_OK)
  349. {
  350. LOG_ERROR("BUG: error scheduling etm register write");
  351. return retval;
  352. }
  353. buf_set_u32(reg->value, 0, reg->size, value);
  354. reg->valid = 1;
  355. reg->dirty = 0;
  356. return ERROR_OK;
  357. }
  358. int etm_set_reg_w_exec(reg_t *reg, u8 *buf)
  359. {
  360. int retval;
  361. etm_set_reg(reg, buf_get_u32(buf, 0, reg->size));
  362. if ((retval = jtag_execute_queue()) != ERROR_OK)
  363. {
  364. LOG_ERROR("register write failed");
  365. return retval;
  366. }
  367. return ERROR_OK;
  368. }
  369. int etm_write_reg(reg_t *reg, u32 value)
  370. {
  371. etm_reg_t *etm_reg = reg->arch_info;
  372. u8 reg_addr = etm_reg->addr & 0x7f;
  373. scan_field_t fields[3];
  374. LOG_DEBUG("%i: 0x%8.8x", etm_reg->addr, value);
  375. jtag_add_end_state(TAP_IDLE);
  376. arm_jtag_scann(etm_reg->jtag_info, 0x6);
  377. arm_jtag_set_instr(etm_reg->jtag_info, etm_reg->jtag_info->intest_instr, NULL);
  378. fields[0].tap = etm_reg->jtag_info->tap;
  379. fields[0].num_bits = 32;
  380. fields[0].out_value = malloc(4);
  381. buf_set_u32(fields[0].out_value, 0, 32, value);
  382. fields[0].out_mask = NULL;
  383. fields[0].in_value = NULL;
  384. fields[0].in_check_value = NULL;
  385. fields[0].in_check_mask = NULL;
  386. fields[0].in_handler = NULL;
  387. fields[0].in_handler_priv = NULL;
  388. fields[1].tap = etm_reg->jtag_info->tap;
  389. fields[1].num_bits = 7;
  390. fields[1].out_value = malloc(1);
  391. buf_set_u32(fields[1].out_value, 0, 7, reg_addr);
  392. fields[1].out_mask = NULL;
  393. fields[1].in_value = NULL;
  394. fields[1].in_check_value = NULL;
  395. fields[1].in_check_mask = NULL;
  396. fields[1].in_handler = NULL;
  397. fields[1].in_handler_priv = NULL;
  398. fields[2].tap = etm_reg->jtag_info->tap;
  399. fields[2].num_bits = 1;
  400. fields[2].out_value = malloc(1);
  401. buf_set_u32(fields[2].out_value, 0, 1, 1);
  402. fields[2].out_mask = NULL;
  403. fields[2].in_value = NULL;
  404. fields[2].in_check_value = NULL;
  405. fields[2].in_check_mask = NULL;
  406. fields[2].in_handler = NULL;
  407. fields[2].in_handler_priv = NULL;
  408. jtag_add_dr_scan(3, fields, -1);
  409. free(fields[0].out_value);
  410. free(fields[1].out_value);
  411. free(fields[2].out_value);
  412. return ERROR_OK;
  413. }
  414. int etm_store_reg(reg_t *reg)
  415. {
  416. return etm_write_reg(reg, buf_get_u32(reg->value, 0, reg->size));
  417. }
  418. /* ETM trace analysis functionality
  419. *
  420. */
  421. extern etm_capture_driver_t etb_capture_driver;
  422. extern etm_capture_driver_t etm_dummy_capture_driver;
  423. #if BUILD_OOCD_TRACE == 1
  424. extern etm_capture_driver_t oocd_trace_capture_driver;
  425. #endif
  426. etm_capture_driver_t *etm_capture_drivers[] =
  427. {
  428. &etb_capture_driver,
  429. &etm_dummy_capture_driver,
  430. #if BUILD_OOCD_TRACE == 1
  431. &oocd_trace_capture_driver,
  432. #endif
  433. NULL
  434. };
  435. char *etmv1v1_branch_reason_strings[] =
  436. {
  437. "normal PC change",
  438. "tracing enabled",
  439. "trace restarted after overflow",
  440. "exit from debug",
  441. "periodic synchronization",
  442. "reserved",
  443. "reserved",
  444. "reserved",
  445. };
  446. int etm_read_instruction(etm_context_t *ctx, arm_instruction_t *instruction)
  447. {
  448. int i;
  449. int section = -1;
  450. u32 size_read;
  451. u32 opcode;
  452. int retval;
  453. if (!ctx->image)
  454. return ERROR_TRACE_IMAGE_UNAVAILABLE;
  455. /* search for the section the current instruction belongs to */
  456. for (i = 0; i < ctx->image->num_sections; i++)
  457. {
  458. if ((ctx->image->sections[i].base_address <= ctx->current_pc) &&
  459. (ctx->image->sections[i].base_address + ctx->image->sections[i].size > ctx->current_pc))
  460. {
  461. section = i;
  462. break;
  463. }
  464. }
  465. if (section == -1)
  466. {
  467. /* current instruction couldn't be found in the image */
  468. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  469. }
  470. if (ctx->core_state == ARMV4_5_STATE_ARM)
  471. {
  472. u8 buf[4];
  473. if ((retval = image_read_section(ctx->image, section,
  474. ctx->current_pc - ctx->image->sections[section].base_address,
  475. 4, buf, &size_read)) != ERROR_OK)
  476. {
  477. LOG_ERROR("error while reading instruction: %i", retval);
  478. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  479. }
  480. opcode = target_buffer_get_u32(ctx->target, buf);
  481. arm_evaluate_opcode(opcode, ctx->current_pc, instruction);
  482. }
  483. else if (ctx->core_state == ARMV4_5_STATE_THUMB)
  484. {
  485. u8 buf[2];
  486. if ((retval = image_read_section(ctx->image, section,
  487. ctx->current_pc - ctx->image->sections[section].base_address,
  488. 2, buf, &size_read)) != ERROR_OK)
  489. {
  490. LOG_ERROR("error while reading instruction: %i", retval);
  491. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  492. }
  493. opcode = target_buffer_get_u16(ctx->target, buf);
  494. thumb_evaluate_opcode(opcode, ctx->current_pc, instruction);
  495. }
  496. else if (ctx->core_state == ARMV4_5_STATE_JAZELLE)
  497. {
  498. LOG_ERROR("BUG: tracing of jazelle code not supported");
  499. exit(-1);
  500. }
  501. else
  502. {
  503. LOG_ERROR("BUG: unknown core state encountered");
  504. exit(-1);
  505. }
  506. return ERROR_OK;
  507. }
  508. int etmv1_next_packet(etm_context_t *ctx, u8 *packet, int apo)
  509. {
  510. while (ctx->data_index < ctx->trace_depth)
  511. {
  512. /* if the caller specified an address packet offset, skip until the
  513. * we reach the n-th cycle marked with tracesync */
  514. if (apo > 0)
  515. {
  516. if (ctx->trace_data[ctx->data_index].flags & ETMV1_TRACESYNC_CYCLE)
  517. apo--;
  518. if (apo > 0)
  519. {
  520. ctx->data_index++;
  521. ctx->data_half = 0;
  522. }
  523. continue;
  524. }
  525. /* no tracedata output during a TD cycle
  526. * or in a trigger cycle */
  527. if ((ctx->trace_data[ctx->data_index].pipestat == STAT_TD)
  528. || (ctx->trace_data[ctx->data_index].flags & ETMV1_TRIGGER_CYCLE))
  529. {
  530. ctx->data_index++;
  531. ctx->data_half = 0;
  532. continue;
  533. }
  534. if ((ctx->portmode & ETM_PORT_WIDTH_MASK) == ETM_PORT_16BIT)
  535. {
  536. if (ctx->data_half == 0)
  537. {
  538. *packet = ctx->trace_data[ctx->data_index].packet & 0xff;
  539. ctx->data_half = 1;
  540. }
  541. else
  542. {
  543. *packet = (ctx->trace_data[ctx->data_index].packet & 0xff00) >> 8;
  544. ctx->data_half = 0;
  545. ctx->data_index++;
  546. }
  547. }
  548. else if ((ctx->portmode & ETM_PORT_WIDTH_MASK) == ETM_PORT_8BIT)
  549. {
  550. *packet = ctx->trace_data[ctx->data_index].packet & 0xff;
  551. ctx->data_index++;
  552. }
  553. else
  554. {
  555. /* on a 4-bit port, a packet will be output during two consecutive cycles */
  556. if (ctx->data_index > (ctx->trace_depth - 2))
  557. return -1;
  558. *packet = ctx->trace_data[ctx->data_index].packet & 0xf;
  559. *packet |= (ctx->trace_data[ctx->data_index + 1].packet & 0xf) << 4;
  560. ctx->data_index += 2;
  561. }
  562. return 0;
  563. }
  564. return -1;
  565. }
  566. int etmv1_branch_address(etm_context_t *ctx)
  567. {
  568. int retval;
  569. u8 packet;
  570. int shift = 0;
  571. int apo;
  572. int i;
  573. /* quit analysis if less than two cycles are left in the trace
  574. * because we can't extract the APO */
  575. if (ctx->data_index > (ctx->trace_depth - 2))
  576. return -1;
  577. /* a BE could be output during an APO cycle, skip the current
  578. * and continue with the new one */
  579. if (ctx->trace_data[ctx->pipe_index + 1].pipestat & 0x4)
  580. return 1;
  581. if (ctx->trace_data[ctx->pipe_index + 2].pipestat & 0x4)
  582. return 2;
  583. /* address packet offset encoded in the next two cycles' pipestat bits */
  584. apo = ctx->trace_data[ctx->pipe_index + 1].pipestat & 0x3;
  585. apo |= (ctx->trace_data[ctx->pipe_index + 2].pipestat & 0x3) << 2;
  586. /* count number of tracesync cycles between current pipe_index and data_index
  587. * i.e. the number of tracesyncs that data_index already passed by
  588. * to subtract them from the APO */
  589. for (i = ctx->pipe_index; i < ctx->data_index; i++)
  590. {
  591. if (ctx->trace_data[ctx->pipe_index + 1].pipestat & ETMV1_TRACESYNC_CYCLE)
  592. apo--;
  593. }
  594. /* extract up to four 7-bit packets */
  595. do {
  596. if ((retval = etmv1_next_packet(ctx, &packet, (shift == 0) ? apo + 1 : 0)) != 0)
  597. return -1;
  598. ctx->last_branch &= ~(0x7f << shift);
  599. ctx->last_branch |= (packet & 0x7f) << shift;
  600. shift += 7;
  601. } while ((packet & 0x80) && (shift < 28));
  602. /* one last packet holding 4 bits of the address, plus the branch reason code */
  603. if ((shift == 28) && (packet & 0x80))
  604. {
  605. if ((retval = etmv1_next_packet(ctx, &packet, 0)) != 0)
  606. return -1;
  607. ctx->last_branch &= 0x0fffffff;
  608. ctx->last_branch |= (packet & 0x0f) << 28;
  609. ctx->last_branch_reason = (packet & 0x70) >> 4;
  610. shift += 4;
  611. }
  612. else
  613. {
  614. ctx->last_branch_reason = 0;
  615. }
  616. if (shift == 32)
  617. {
  618. ctx->pc_ok = 1;
  619. }
  620. /* if a full address was output, we might have branched into Jazelle state */
  621. if ((shift == 32) && (packet & 0x80))
  622. {
  623. ctx->core_state = ARMV4_5_STATE_JAZELLE;
  624. }
  625. else
  626. {
  627. /* if we didn't branch into Jazelle state, the current processor state is
  628. * encoded in bit 0 of the branch target address */
  629. if (ctx->last_branch & 0x1)
  630. {
  631. ctx->core_state = ARMV4_5_STATE_THUMB;
  632. ctx->last_branch &= ~0x1;
  633. }
  634. else
  635. {
  636. ctx->core_state = ARMV4_5_STATE_ARM;
  637. ctx->last_branch &= ~0x3;
  638. }
  639. }
  640. return 0;
  641. }
  642. int etmv1_data(etm_context_t *ctx, int size, u32 *data)
  643. {
  644. int j;
  645. u8 buf[4];
  646. int retval;
  647. for (j = 0; j < size; j++)
  648. {
  649. if ((retval = etmv1_next_packet(ctx, &buf[j], 0)) != 0)
  650. return -1;
  651. }
  652. if (size == 8)
  653. {
  654. LOG_ERROR("TODO: add support for 64-bit values");
  655. return -1;
  656. }
  657. else if (size == 4)
  658. *data = target_buffer_get_u32(ctx->target, buf);
  659. else if (size == 2)
  660. *data = target_buffer_get_u16(ctx->target, buf);
  661. else if (size == 1)
  662. *data = buf[0];
  663. else
  664. return -1;
  665. return 0;
  666. }
  667. int etmv1_analyze_trace(etm_context_t *ctx, struct command_context_s *cmd_ctx)
  668. {
  669. int retval;
  670. arm_instruction_t instruction;
  671. /* read the trace data if it wasn't read already */
  672. if (ctx->trace_depth == 0)
  673. ctx->capture_driver->read_trace(ctx);
  674. /* start at the beginning of the captured trace */
  675. ctx->pipe_index = 0;
  676. ctx->data_index = 0;
  677. ctx->data_half = 0;
  678. /* neither the PC nor the data pointer are valid */
  679. ctx->pc_ok = 0;
  680. ctx->ptr_ok = 0;
  681. while (ctx->pipe_index < ctx->trace_depth)
  682. {
  683. u8 pipestat = ctx->trace_data[ctx->pipe_index].pipestat;
  684. u32 next_pc = ctx->current_pc;
  685. u32 old_data_index = ctx->data_index;
  686. u32 old_data_half = ctx->data_half;
  687. u32 old_index = ctx->pipe_index;
  688. u32 last_instruction = ctx->last_instruction;
  689. u32 cycles = 0;
  690. int current_pc_ok = ctx->pc_ok;
  691. if (ctx->trace_data[ctx->pipe_index].flags & ETMV1_TRIGGER_CYCLE)
  692. {
  693. command_print(cmd_ctx, "--- trigger ---");
  694. }
  695. /* instructions execute in IE/D or BE/D cycles */
  696. if ((pipestat == STAT_IE) || (pipestat == STAT_ID))
  697. ctx->last_instruction = ctx->pipe_index;
  698. /* if we don't have a valid pc skip until we reach an indirect branch */
  699. if ((!ctx->pc_ok) && (pipestat != STAT_BE))
  700. {
  701. ctx->pipe_index++;
  702. continue;
  703. }
  704. /* any indirect branch could have interrupted instruction flow
  705. * - the branch reason code could indicate a trace discontinuity
  706. * - a branch to the exception vectors indicates an exception
  707. */
  708. if ((pipestat == STAT_BE) || (pipestat == STAT_BD))
  709. {
  710. /* backup current data index, to be able to consume the branch address
  711. * before examining data address and values
  712. */
  713. old_data_index = ctx->data_index;
  714. old_data_half = ctx->data_half;
  715. ctx->last_instruction = ctx->pipe_index;
  716. if ((retval = etmv1_branch_address(ctx)) != 0)
  717. {
  718. /* negative return value from etmv1_branch_address means we ran out of packets,
  719. * quit analysing the trace */
  720. if (retval < 0)
  721. break;
  722. /* a positive return values means the current branch was abandoned,
  723. * and a new branch was encountered in cycle ctx->pipe_index + retval;
  724. */
  725. LOG_WARNING("abandoned branch encountered, correctnes of analysis uncertain");
  726. ctx->pipe_index += retval;
  727. continue;
  728. }
  729. /* skip over APO cycles */
  730. ctx->pipe_index += 2;
  731. switch (ctx->last_branch_reason)
  732. {
  733. case 0x0: /* normal PC change */
  734. next_pc = ctx->last_branch;
  735. break;
  736. case 0x1: /* tracing enabled */
  737. command_print(cmd_ctx, "--- tracing enabled at 0x%8.8x ---", ctx->last_branch);
  738. ctx->current_pc = ctx->last_branch;
  739. ctx->pipe_index++;
  740. continue;
  741. break;
  742. case 0x2: /* trace restarted after FIFO overflow */
  743. command_print(cmd_ctx, "--- trace restarted after FIFO overflow at 0x%8.8x ---", ctx->last_branch);
  744. ctx->current_pc = ctx->last_branch;
  745. ctx->pipe_index++;
  746. continue;
  747. break;
  748. case 0x3: /* exit from debug state */
  749. command_print(cmd_ctx, "--- exit from debug state at 0x%8.8x ---", ctx->last_branch);
  750. ctx->current_pc = ctx->last_branch;
  751. ctx->pipe_index++;
  752. continue;
  753. break;
  754. case 0x4: /* periodic synchronization point */
  755. next_pc = ctx->last_branch;
  756. /* if we had no valid PC prior to this synchronization point,
  757. * we have to move on with the next trace cycle
  758. */
  759. if (!current_pc_ok)
  760. {
  761. command_print(cmd_ctx, "--- periodic synchronization point at 0x%8.8x ---", next_pc);
  762. ctx->current_pc = next_pc;
  763. ctx->pipe_index++;
  764. continue;
  765. }
  766. break;
  767. default: /* reserved */
  768. LOG_ERROR("BUG: branch reason code 0x%x is reserved", ctx->last_branch_reason);
  769. exit(-1);
  770. break;
  771. }
  772. /* if we got here the branch was a normal PC change
  773. * (or a periodic synchronization point, which means the same for that matter)
  774. * if we didn't accquire a complete PC continue with the next cycle
  775. */
  776. if (!ctx->pc_ok)
  777. continue;
  778. /* indirect branch to the exception vector means an exception occured */
  779. if (((ctx->last_branch >= 0x0) && (ctx->last_branch <= 0x20))
  780. || ((ctx->last_branch >= 0xffff0000) && (ctx->last_branch <= 0xffff0020)))
  781. {
  782. if ((ctx->last_branch & 0xff) == 0x10)
  783. {
  784. command_print(cmd_ctx, "data abort");
  785. }
  786. else
  787. {
  788. command_print(cmd_ctx, "exception vector 0x%2.2x", ctx->last_branch);
  789. ctx->current_pc = ctx->last_branch;
  790. ctx->pipe_index++;
  791. continue;
  792. }
  793. }
  794. }
  795. /* an instruction was executed (or not, depending on the condition flags)
  796. * retrieve it from the image for displaying */
  797. if (ctx->pc_ok && (pipestat != STAT_WT) && (pipestat != STAT_TD) &&
  798. !(((pipestat == STAT_BE) || (pipestat == STAT_BD)) &&
  799. ((ctx->last_branch_reason != 0x0) && (ctx->last_branch_reason != 0x4))))
  800. {
  801. if ((retval = etm_read_instruction(ctx, &instruction)) != ERROR_OK)
  802. {
  803. /* can't continue tracing with no image available */
  804. if (retval == ERROR_TRACE_IMAGE_UNAVAILABLE)
  805. {
  806. return retval;
  807. }
  808. else if (retval == ERROR_TRACE_INSTRUCTION_UNAVAILABLE)
  809. {
  810. /* TODO: handle incomplete images
  811. * for now we just quit the analsysis*/
  812. return retval;
  813. }
  814. }
  815. cycles = old_index - last_instruction;
  816. }
  817. if ((pipestat == STAT_ID) || (pipestat == STAT_BD))
  818. {
  819. u32 new_data_index = ctx->data_index;
  820. u32 new_data_half = ctx->data_half;
  821. /* in case of a branch with data, the branch target address was consumed before
  822. * we temporarily go back to the saved data index */
  823. if (pipestat == STAT_BD)
  824. {
  825. ctx->data_index = old_data_index;
  826. ctx->data_half = old_data_half;
  827. }
  828. if (ctx->tracemode & ETMV1_TRACE_ADDR)
  829. {
  830. u8 packet;
  831. int shift = 0;
  832. do {
  833. if ((retval = etmv1_next_packet(ctx, &packet, 0)) != 0)
  834. return ERROR_ETM_ANALYSIS_FAILED;
  835. ctx->last_ptr &= ~(0x7f << shift);
  836. ctx->last_ptr |= (packet & 0x7f) << shift;
  837. shift += 7;
  838. } while ((packet & 0x80) && (shift < 32));
  839. if (shift >= 32)
  840. ctx->ptr_ok = 1;
  841. if (ctx->ptr_ok)
  842. {
  843. command_print(cmd_ctx, "address: 0x%8.8x", ctx->last_ptr);
  844. }
  845. }
  846. if (ctx->tracemode & ETMV1_TRACE_DATA)
  847. {
  848. if ((instruction.type == ARM_LDM) || (instruction.type == ARM_STM))
  849. {
  850. int i;
  851. for (i = 0; i < 16; i++)
  852. {
  853. if (instruction.info.load_store_multiple.register_list & (1 << i))
  854. {
  855. u32 data;
  856. if (etmv1_data(ctx, 4, &data) != 0)
  857. return ERROR_ETM_ANALYSIS_FAILED;
  858. command_print(cmd_ctx, "data: 0x%8.8x", data);
  859. }
  860. }
  861. }
  862. else if ((instruction.type >= ARM_LDR) && (instruction.type <= ARM_STRH))
  863. {
  864. u32 data;
  865. if (etmv1_data(ctx, arm_access_size(&instruction), &data) != 0)
  866. return ERROR_ETM_ANALYSIS_FAILED;
  867. command_print(cmd_ctx, "data: 0x%8.8x", data);
  868. }
  869. }
  870. /* restore data index after consuming BD address and data */
  871. if (pipestat == STAT_BD)
  872. {
  873. ctx->data_index = new_data_index;
  874. ctx->data_half = new_data_half;
  875. }
  876. }
  877. /* adjust PC */
  878. if ((pipestat == STAT_IE) || (pipestat == STAT_ID))
  879. {
  880. if (((instruction.type == ARM_B) ||
  881. (instruction.type == ARM_BL) ||
  882. (instruction.type == ARM_BLX)) &&
  883. (instruction.info.b_bl_bx_blx.target_address != -1))
  884. {
  885. next_pc = instruction.info.b_bl_bx_blx.target_address;
  886. }
  887. else
  888. {
  889. next_pc += (ctx->core_state == ARMV4_5_STATE_ARM) ? 4 : 2;
  890. }
  891. }
  892. else if (pipestat == STAT_IN)
  893. {
  894. next_pc += (ctx->core_state == ARMV4_5_STATE_ARM) ? 4 : 2;
  895. }
  896. if ((pipestat != STAT_TD) && (pipestat != STAT_WT))
  897. {
  898. char cycles_text[32] = "";
  899. /* if the trace was captured with cycle accurate tracing enabled,
  900. * output the number of cycles since the last executed instruction
  901. */
  902. if (ctx->tracemode & ETMV1_CYCLE_ACCURATE)
  903. {
  904. snprintf(cycles_text, 32, " (%i %s)",
  905. cycles,
  906. (cycles == 1) ? "cycle" : "cycles");
  907. }
  908. command_print(cmd_ctx, "%s%s%s",
  909. instruction.text,
  910. (pipestat == STAT_IN) ? " (not executed)" : "",
  911. cycles_text);
  912. ctx->current_pc = next_pc;
  913. /* packets for an instruction don't start on or before the preceding
  914. * functional pipestat (i.e. other than WT or TD)
  915. */
  916. if (ctx->data_index <= ctx->pipe_index)
  917. {
  918. ctx->data_index = ctx->pipe_index + 1;
  919. ctx->data_half = 0;
  920. }
  921. }
  922. ctx->pipe_index += 1;
  923. }
  924. return ERROR_OK;
  925. }
  926. int handle_etm_tracemode_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  927. {
  928. target_t *target;
  929. armv4_5_common_t *armv4_5;
  930. arm7_9_common_t *arm7_9;
  931. etmv1_tracemode_t tracemode;
  932. target = get_current_target(cmd_ctx);
  933. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  934. {
  935. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  936. return ERROR_OK;
  937. }
  938. if (!arm7_9->etm_ctx)
  939. {
  940. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  941. return ERROR_OK;
  942. }
  943. tracemode = arm7_9->etm_ctx->tracemode;
  944. if (argc == 4)
  945. {
  946. if (strcmp(args[0], "none") == 0)
  947. {
  948. tracemode = ETMV1_TRACE_NONE;
  949. }
  950. else if (strcmp(args[0], "data") == 0)
  951. {
  952. tracemode = ETMV1_TRACE_DATA;
  953. }
  954. else if (strcmp(args[0], "address") == 0)
  955. {
  956. tracemode = ETMV1_TRACE_ADDR;
  957. }
  958. else if (strcmp(args[0], "all") == 0)
  959. {
  960. tracemode = ETMV1_TRACE_DATA | ETMV1_TRACE_ADDR;
  961. }
  962. else
  963. {
  964. command_print(cmd_ctx, "invalid option '%s'", args[0]);
  965. return ERROR_OK;
  966. }
  967. switch (strtol(args[1], NULL, 0))
  968. {
  969. case 0:
  970. tracemode |= ETMV1_CONTEXTID_NONE;
  971. break;
  972. case 8:
  973. tracemode |= ETMV1_CONTEXTID_8;
  974. break;
  975. case 16:
  976. tracemode |= ETMV1_CONTEXTID_16;
  977. break;
  978. case 32:
  979. tracemode |= ETMV1_CONTEXTID_32;
  980. break;
  981. default:
  982. command_print(cmd_ctx, "invalid option '%s'", args[1]);
  983. return ERROR_OK;
  984. }
  985. if (strcmp(args[2], "enable") == 0)
  986. {
  987. tracemode |= ETMV1_CYCLE_ACCURATE;
  988. }
  989. else if (strcmp(args[2], "disable") == 0)
  990. {
  991. tracemode |= 0;
  992. }
  993. else
  994. {
  995. command_print(cmd_ctx, "invalid option '%s'", args[2]);
  996. return ERROR_OK;
  997. }
  998. if (strcmp(args[3], "enable") == 0)
  999. {
  1000. tracemode |= ETMV1_BRANCH_OUTPUT;
  1001. }
  1002. else if (strcmp(args[3], "disable") == 0)
  1003. {
  1004. tracemode |= 0;
  1005. }
  1006. else
  1007. {
  1008. command_print(cmd_ctx, "invalid option '%s'", args[2]);
  1009. return ERROR_OK;
  1010. }
  1011. }
  1012. else if (argc != 0)
  1013. {
  1014. command_print(cmd_ctx, "usage: configure trace mode <none|data|address|all> <context id bits> <cycle accurate> <branch output>");
  1015. return ERROR_OK;
  1016. }
  1017. command_print(cmd_ctx, "current tracemode configuration:");
  1018. switch (tracemode & ETMV1_TRACE_MASK)
  1019. {
  1020. case ETMV1_TRACE_NONE:
  1021. command_print(cmd_ctx, "data tracing: none");
  1022. break;
  1023. case ETMV1_TRACE_DATA:
  1024. command_print(cmd_ctx, "data tracing: data only");
  1025. break;
  1026. case ETMV1_TRACE_ADDR:
  1027. command_print(cmd_ctx, "data tracing: address only");
  1028. break;
  1029. case ETMV1_TRACE_DATA | ETMV1_TRACE_ADDR:
  1030. command_print(cmd_ctx, "data tracing: address and data");
  1031. break;
  1032. }
  1033. switch (tracemode & ETMV1_CONTEXTID_MASK)
  1034. {
  1035. case ETMV1_CONTEXTID_NONE:
  1036. command_print(cmd_ctx, "contextid tracing: none");
  1037. break;
  1038. case ETMV1_CONTEXTID_8:
  1039. command_print(cmd_ctx, "contextid tracing: 8 bit");
  1040. break;
  1041. case ETMV1_CONTEXTID_16:
  1042. command_print(cmd_ctx, "contextid tracing: 16 bit");
  1043. break;
  1044. case ETMV1_CONTEXTID_32:
  1045. command_print(cmd_ctx, "contextid tracing: 32 bit");
  1046. break;
  1047. }
  1048. if (tracemode & ETMV1_CYCLE_ACCURATE)
  1049. {
  1050. command_print(cmd_ctx, "cycle-accurate tracing enabled");
  1051. }
  1052. else
  1053. {
  1054. command_print(cmd_ctx, "cycle-accurate tracing disabled");
  1055. }
  1056. if (tracemode & ETMV1_BRANCH_OUTPUT)
  1057. {
  1058. command_print(cmd_ctx, "full branch address output enabled");
  1059. }
  1060. else
  1061. {
  1062. command_print(cmd_ctx, "full branch address output disabled");
  1063. }
  1064. /* only update ETM_CTRL register if tracemode changed */
  1065. if (arm7_9->etm_ctx->tracemode != tracemode)
  1066. {
  1067. reg_t *etm_ctrl_reg = &arm7_9->etm_ctx->reg_cache->reg_list[ETM_CTRL];
  1068. etm_get_reg(etm_ctrl_reg);
  1069. buf_set_u32(etm_ctrl_reg->value, 2, 2, tracemode & ETMV1_TRACE_MASK);
  1070. buf_set_u32(etm_ctrl_reg->value, 14, 2, (tracemode & ETMV1_CONTEXTID_MASK) >> 4);
  1071. buf_set_u32(etm_ctrl_reg->value, 12, 1, (tracemode & ETMV1_CYCLE_ACCURATE) >> 8);
  1072. buf_set_u32(etm_ctrl_reg->value, 8, 1, (tracemode & ETMV1_BRANCH_OUTPUT) >> 9);
  1073. etm_store_reg(etm_ctrl_reg);
  1074. arm7_9->etm_ctx->tracemode = tracemode;
  1075. /* invalidate old trace data */
  1076. arm7_9->etm_ctx->capture_status = TRACE_IDLE;
  1077. if (arm7_9->etm_ctx->trace_depth > 0)
  1078. {
  1079. free(arm7_9->etm_ctx->trace_data);
  1080. arm7_9->etm_ctx->trace_data = NULL;
  1081. }
  1082. arm7_9->etm_ctx->trace_depth = 0;
  1083. }
  1084. return ERROR_OK;
  1085. }
  1086. int handle_etm_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1087. {
  1088. target_t *target;
  1089. armv4_5_common_t *armv4_5;
  1090. arm7_9_common_t *arm7_9;
  1091. etm_portmode_t portmode = 0x0;
  1092. etm_context_t *etm_ctx = malloc(sizeof(etm_context_t));
  1093. int i;
  1094. if (argc != 5)
  1095. {
  1096. return ERROR_COMMAND_SYNTAX_ERROR;
  1097. }
  1098. target = get_target_by_num(strtoul(args[0], NULL, 0));
  1099. if (!target)
  1100. {
  1101. LOG_ERROR("target number '%s' not defined", args[0]);
  1102. return ERROR_FAIL;
  1103. }
  1104. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1105. {
  1106. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1107. return ERROR_FAIL;
  1108. }
  1109. switch (strtoul(args[1], NULL, 0))
  1110. {
  1111. case 4:
  1112. portmode |= ETM_PORT_4BIT;
  1113. break;
  1114. case 8:
  1115. portmode |= ETM_PORT_8BIT;
  1116. break;
  1117. case 16:
  1118. portmode |= ETM_PORT_16BIT;
  1119. break;
  1120. default:
  1121. command_print(cmd_ctx, "unsupported ETM port width '%s', must be 4, 8 or 16", args[1]);
  1122. return ERROR_FAIL;
  1123. }
  1124. if (strcmp("normal", args[2]) == 0)
  1125. {
  1126. portmode |= ETM_PORT_NORMAL;
  1127. }
  1128. else if (strcmp("multiplexed", args[2]) == 0)
  1129. {
  1130. portmode |= ETM_PORT_MUXED;
  1131. }
  1132. else if (strcmp("demultiplexed", args[2]) == 0)
  1133. {
  1134. portmode |= ETM_PORT_DEMUXED;
  1135. }
  1136. else
  1137. {
  1138. command_print(cmd_ctx, "unsupported ETM port mode '%s', must be 'normal', 'multiplexed' or 'demultiplexed'", args[2]);
  1139. return ERROR_FAIL;
  1140. }
  1141. if (strcmp("half", args[3]) == 0)
  1142. {
  1143. portmode |= ETM_PORT_HALF_CLOCK;
  1144. }
  1145. else if (strcmp("full", args[3]) == 0)
  1146. {
  1147. portmode |= ETM_PORT_FULL_CLOCK;
  1148. }
  1149. else
  1150. {
  1151. command_print(cmd_ctx, "unsupported ETM port clocking '%s', must be 'full' or 'half'", args[3]);
  1152. return ERROR_FAIL;
  1153. }
  1154. for (i=0; etm_capture_drivers[i]; i++)
  1155. {
  1156. if (strcmp(args[4], etm_capture_drivers[i]->name) == 0)
  1157. {
  1158. int retval;
  1159. if ((retval=etm_capture_drivers[i]->register_commands(cmd_ctx)) != ERROR_OK)
  1160. {
  1161. free(etm_ctx);
  1162. return retval;
  1163. }
  1164. etm_ctx->capture_driver = etm_capture_drivers[i];
  1165. break;
  1166. }
  1167. }
  1168. if (!etm_capture_drivers[i])
  1169. {
  1170. /* no supported capture driver found, don't register an ETM */
  1171. free(etm_ctx);
  1172. LOG_ERROR("trace capture driver '%s' not found", args[4]);
  1173. return ERROR_FAIL;
  1174. }
  1175. etm_ctx->target = target;
  1176. etm_ctx->trigger_percent = 50;
  1177. etm_ctx->trace_data = NULL;
  1178. etm_ctx->trace_depth = 0;
  1179. etm_ctx->portmode = portmode;
  1180. etm_ctx->tracemode = 0x0;
  1181. etm_ctx->core_state = ARMV4_5_STATE_ARM;
  1182. etm_ctx->image = NULL;
  1183. etm_ctx->pipe_index = 0;
  1184. etm_ctx->data_index = 0;
  1185. etm_ctx->current_pc = 0x0;
  1186. etm_ctx->pc_ok = 0;
  1187. etm_ctx->last_branch = 0x0;
  1188. etm_ctx->last_branch_reason = 0x0;
  1189. etm_ctx->last_ptr = 0x0;
  1190. etm_ctx->ptr_ok = 0x0;
  1191. etm_ctx->context_id = 0x0;
  1192. etm_ctx->last_instruction = 0;
  1193. arm7_9->etm_ctx = etm_ctx;
  1194. return etm_register_user_commands(cmd_ctx);
  1195. }
  1196. int handle_etm_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1197. {
  1198. target_t *target;
  1199. armv4_5_common_t *armv4_5;
  1200. arm7_9_common_t *arm7_9;
  1201. reg_t *etm_config_reg;
  1202. reg_t *etm_sys_config_reg;
  1203. int max_port_size;
  1204. target = get_current_target(cmd_ctx);
  1205. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1206. {
  1207. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1208. return ERROR_OK;
  1209. }
  1210. if (!arm7_9->etm_ctx)
  1211. {
  1212. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1213. return ERROR_OK;
  1214. }
  1215. etm_config_reg = &arm7_9->etm_ctx->reg_cache->reg_list[ETM_CONFIG];
  1216. etm_sys_config_reg = &arm7_9->etm_ctx->reg_cache->reg_list[ETM_SYS_CONFIG];
  1217. etm_get_reg(etm_config_reg);
  1218. command_print(cmd_ctx, "pairs of address comparators: %i", buf_get_u32(etm_config_reg->value, 0, 4));
  1219. command_print(cmd_ctx, "pairs of data comparators: %i", buf_get_u32(etm_config_reg->value, 4, 4));
  1220. command_print(cmd_ctx, "memory map decoders: %i", buf_get_u32(etm_config_reg->value, 8, 5));
  1221. command_print(cmd_ctx, "number of counters: %i", buf_get_u32(etm_config_reg->value, 13, 3));
  1222. command_print(cmd_ctx, "sequencer %spresent",
  1223. (buf_get_u32(etm_config_reg->value, 16, 1) == 1) ? "" : "not ");
  1224. command_print(cmd_ctx, "number of ext. inputs: %i", buf_get_u32(etm_config_reg->value, 17, 3));
  1225. command_print(cmd_ctx, "number of ext. outputs: %i", buf_get_u32(etm_config_reg->value, 20, 3));
  1226. command_print(cmd_ctx, "FIFO full %spresent",
  1227. (buf_get_u32(etm_config_reg->value, 23, 1) == 1) ? "" : "not ");
  1228. command_print(cmd_ctx, "protocol version: %i", buf_get_u32(etm_config_reg->value, 28, 3));
  1229. etm_get_reg(etm_sys_config_reg);
  1230. switch (buf_get_u32(etm_sys_config_reg->value, 0, 3))
  1231. {
  1232. case 0:
  1233. max_port_size = 4;
  1234. break;
  1235. case 1:
  1236. max_port_size = 8;
  1237. break;
  1238. case 2:
  1239. max_port_size = 16;
  1240. break;
  1241. default:
  1242. LOG_ERROR("Illegal max_port_size");
  1243. exit(-1);
  1244. }
  1245. command_print(cmd_ctx, "max. port size: %i", max_port_size);
  1246. command_print(cmd_ctx, "half-rate clocking %ssupported",
  1247. (buf_get_u32(etm_sys_config_reg->value, 3, 1) == 1) ? "" : "not ");
  1248. command_print(cmd_ctx, "full-rate clocking %ssupported",
  1249. (buf_get_u32(etm_sys_config_reg->value, 4, 1) == 1) ? "" : "not ");
  1250. command_print(cmd_ctx, "normal trace format %ssupported",
  1251. (buf_get_u32(etm_sys_config_reg->value, 5, 1) == 1) ? "" : "not ");
  1252. command_print(cmd_ctx, "multiplex trace format %ssupported",
  1253. (buf_get_u32(etm_sys_config_reg->value, 6, 1) == 1) ? "" : "not ");
  1254. command_print(cmd_ctx, "demultiplex trace format %ssupported",
  1255. (buf_get_u32(etm_sys_config_reg->value, 7, 1) == 1) ? "" : "not ");
  1256. command_print(cmd_ctx, "FIFO full %ssupported",
  1257. (buf_get_u32(etm_sys_config_reg->value, 8, 1) == 1) ? "" : "not ");
  1258. return ERROR_OK;
  1259. }
  1260. int handle_etm_status_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1261. {
  1262. target_t *target;
  1263. armv4_5_common_t *armv4_5;
  1264. arm7_9_common_t *arm7_9;
  1265. trace_status_t trace_status;
  1266. target = get_current_target(cmd_ctx);
  1267. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1268. {
  1269. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1270. return ERROR_OK;
  1271. }
  1272. if (!arm7_9->etm_ctx)
  1273. {
  1274. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1275. return ERROR_OK;
  1276. }
  1277. trace_status = arm7_9->etm_ctx->capture_driver->status(arm7_9->etm_ctx);
  1278. if (trace_status == TRACE_IDLE)
  1279. {
  1280. command_print(cmd_ctx, "tracing is idle");
  1281. }
  1282. else
  1283. {
  1284. static char *completed = " completed";
  1285. static char *running = " is running";
  1286. static char *overflowed = ", trace overflowed";
  1287. static char *triggered = ", trace triggered";
  1288. command_print(cmd_ctx, "trace collection%s%s%s",
  1289. (trace_status & TRACE_RUNNING) ? running : completed,
  1290. (trace_status & TRACE_OVERFLOWED) ? overflowed : "",
  1291. (trace_status & TRACE_TRIGGERED) ? triggered : "");
  1292. if (arm7_9->etm_ctx->trace_depth > 0)
  1293. {
  1294. command_print(cmd_ctx, "%i frames of trace data read", arm7_9->etm_ctx->trace_depth);
  1295. }
  1296. }
  1297. return ERROR_OK;
  1298. }
  1299. int handle_etm_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1300. {
  1301. target_t *target;
  1302. armv4_5_common_t *armv4_5;
  1303. arm7_9_common_t *arm7_9;
  1304. etm_context_t *etm_ctx;
  1305. if (argc < 1)
  1306. {
  1307. command_print(cmd_ctx, "usage: etm image <file> [base address] [type]");
  1308. return ERROR_OK;
  1309. }
  1310. target = get_current_target(cmd_ctx);
  1311. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1312. {
  1313. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1314. return ERROR_OK;
  1315. }
  1316. if (!(etm_ctx = arm7_9->etm_ctx))
  1317. {
  1318. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1319. return ERROR_OK;
  1320. }
  1321. if (etm_ctx->image)
  1322. {
  1323. image_close(etm_ctx->image);
  1324. free(etm_ctx->image);
  1325. command_print(cmd_ctx, "previously loaded image found and closed");
  1326. }
  1327. etm_ctx->image = malloc(sizeof(image_t));
  1328. etm_ctx->image->base_address_set = 0;
  1329. etm_ctx->image->start_address_set = 0;
  1330. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  1331. if (argc >= 2)
  1332. {
  1333. etm_ctx->image->base_address_set = 1;
  1334. etm_ctx->image->base_address = strtoul(args[1], NULL, 0);
  1335. }
  1336. else
  1337. {
  1338. etm_ctx->image->base_address_set = 0;
  1339. }
  1340. if (image_open(etm_ctx->image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
  1341. {
  1342. free(etm_ctx->image);
  1343. etm_ctx->image = NULL;
  1344. return ERROR_OK;
  1345. }
  1346. return ERROR_OK;
  1347. }
  1348. int handle_etm_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1349. {
  1350. fileio_t file;
  1351. target_t *target;
  1352. armv4_5_common_t *armv4_5;
  1353. arm7_9_common_t *arm7_9;
  1354. etm_context_t *etm_ctx;
  1355. int i;
  1356. if (argc != 1)
  1357. {
  1358. command_print(cmd_ctx, "usage: etm dump <file>");
  1359. return ERROR_OK;
  1360. }
  1361. target = get_current_target(cmd_ctx);
  1362. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1363. {
  1364. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1365. return ERROR_OK;
  1366. }
  1367. if (!(etm_ctx = arm7_9->etm_ctx))
  1368. {
  1369. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1370. return ERROR_OK;
  1371. }
  1372. if (etm_ctx->capture_driver->status == TRACE_IDLE)
  1373. {
  1374. command_print(cmd_ctx, "trace capture wasn't enabled, no trace data captured");
  1375. return ERROR_OK;
  1376. }
  1377. if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING)
  1378. {
  1379. /* TODO: if on-the-fly capture is to be supported, this needs to be changed */
  1380. command_print(cmd_ctx, "trace capture not completed");
  1381. return ERROR_OK;
  1382. }
  1383. /* read the trace data if it wasn't read already */
  1384. if (etm_ctx->trace_depth == 0)
  1385. etm_ctx->capture_driver->read_trace(etm_ctx);
  1386. if (fileio_open(&file, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1387. {
  1388. return ERROR_OK;
  1389. }
  1390. fileio_write_u32(&file, etm_ctx->capture_status);
  1391. fileio_write_u32(&file, etm_ctx->portmode);
  1392. fileio_write_u32(&file, etm_ctx->tracemode);
  1393. fileio_write_u32(&file, etm_ctx->trace_depth);
  1394. for (i = 0; i < etm_ctx->trace_depth; i++)
  1395. {
  1396. fileio_write_u32(&file, etm_ctx->trace_data[i].pipestat);
  1397. fileio_write_u32(&file, etm_ctx->trace_data[i].packet);
  1398. fileio_write_u32(&file, etm_ctx->trace_data[i].flags);
  1399. }
  1400. fileio_close(&file);
  1401. return ERROR_OK;
  1402. }
  1403. int handle_etm_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1404. {
  1405. fileio_t file;
  1406. target_t *target;
  1407. armv4_5_common_t *armv4_5;
  1408. arm7_9_common_t *arm7_9;
  1409. etm_context_t *etm_ctx;
  1410. int i;
  1411. if (argc != 1)
  1412. {
  1413. command_print(cmd_ctx, "usage: etm load <file>");
  1414. return ERROR_OK;
  1415. }
  1416. target = get_current_target(cmd_ctx);
  1417. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1418. {
  1419. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1420. return ERROR_OK;
  1421. }
  1422. if (!(etm_ctx = arm7_9->etm_ctx))
  1423. {
  1424. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1425. return ERROR_OK;
  1426. }
  1427. if (etm_ctx->capture_driver->status(etm_ctx) & TRACE_RUNNING)
  1428. {
  1429. command_print(cmd_ctx, "trace capture running, stop first");
  1430. return ERROR_OK;
  1431. }
  1432. if (fileio_open(&file, args[0], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  1433. {
  1434. return ERROR_OK;
  1435. }
  1436. if (file.size % 4)
  1437. {
  1438. command_print(cmd_ctx, "size isn't a multiple of 4, no valid trace data");
  1439. fileio_close(&file);
  1440. return ERROR_OK;
  1441. }
  1442. if (etm_ctx->trace_depth > 0)
  1443. {
  1444. free(etm_ctx->trace_data);
  1445. etm_ctx->trace_data = NULL;
  1446. }
  1447. fileio_read_u32(&file, &etm_ctx->capture_status);
  1448. fileio_read_u32(&file, &etm_ctx->portmode);
  1449. fileio_read_u32(&file, &etm_ctx->tracemode);
  1450. fileio_read_u32(&file, &etm_ctx->trace_depth);
  1451. etm_ctx->trace_data = malloc(sizeof(etmv1_trace_data_t) * etm_ctx->trace_depth);
  1452. if(etm_ctx->trace_data == NULL)
  1453. {
  1454. command_print(cmd_ctx, "not enough memory to perform operation");
  1455. fileio_close(&file);
  1456. return ERROR_OK;
  1457. }
  1458. for (i = 0; i < etm_ctx->trace_depth; i++)
  1459. {
  1460. u32 pipestat, packet, flags;
  1461. fileio_read_u32(&file, &pipestat);
  1462. fileio_read_u32(&file, &packet);
  1463. fileio_read_u32(&file, &flags);
  1464. etm_ctx->trace_data[i].pipestat = pipestat & 0xff;
  1465. etm_ctx->trace_data[i].packet = packet & 0xffff;
  1466. etm_ctx->trace_data[i].flags = flags;
  1467. }
  1468. fileio_close(&file);
  1469. return ERROR_OK;
  1470. }
  1471. int handle_etm_trigger_percent_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1472. {
  1473. target_t *target;
  1474. armv4_5_common_t *armv4_5;
  1475. arm7_9_common_t *arm7_9;
  1476. etm_context_t *etm_ctx;
  1477. target = get_current_target(cmd_ctx);
  1478. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1479. {
  1480. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1481. return ERROR_OK;
  1482. }
  1483. if (!(etm_ctx = arm7_9->etm_ctx))
  1484. {
  1485. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1486. return ERROR_OK;
  1487. }
  1488. if (argc > 0)
  1489. {
  1490. u32 new_value = strtoul(args[0], NULL, 0);
  1491. if ((new_value < 2) || (new_value > 100))
  1492. {
  1493. command_print(cmd_ctx, "valid settings are 2% to 100%");
  1494. }
  1495. else
  1496. {
  1497. etm_ctx->trigger_percent = new_value;
  1498. }
  1499. }
  1500. command_print(cmd_ctx, "%i percent of the tracebuffer reserved for after the trigger", etm_ctx->trigger_percent);
  1501. return ERROR_OK;
  1502. }
  1503. int handle_etm_start_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1504. {
  1505. target_t *target;
  1506. armv4_5_common_t *armv4_5;
  1507. arm7_9_common_t *arm7_9;
  1508. etm_context_t *etm_ctx;
  1509. reg_t *etm_ctrl_reg;
  1510. target = get_current_target(cmd_ctx);
  1511. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1512. {
  1513. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1514. return ERROR_OK;
  1515. }
  1516. if (!(etm_ctx = arm7_9->etm_ctx))
  1517. {
  1518. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1519. return ERROR_OK;
  1520. }
  1521. /* invalidate old tracing data */
  1522. arm7_9->etm_ctx->capture_status = TRACE_IDLE;
  1523. if (arm7_9->etm_ctx->trace_depth > 0)
  1524. {
  1525. free(arm7_9->etm_ctx->trace_data);
  1526. arm7_9->etm_ctx->trace_data = NULL;
  1527. }
  1528. arm7_9->etm_ctx->trace_depth = 0;
  1529. etm_ctrl_reg = &arm7_9->etm_ctx->reg_cache->reg_list[ETM_CTRL];
  1530. etm_get_reg(etm_ctrl_reg);
  1531. /* Clear programming bit (10), set port selection bit (11) */
  1532. buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x2);
  1533. etm_store_reg(etm_ctrl_reg);
  1534. jtag_execute_queue();
  1535. etm_ctx->capture_driver->start_capture(etm_ctx);
  1536. return ERROR_OK;
  1537. }
  1538. int handle_etm_stop_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1539. {
  1540. target_t *target;
  1541. armv4_5_common_t *armv4_5;
  1542. arm7_9_common_t *arm7_9;
  1543. etm_context_t *etm_ctx;
  1544. reg_t *etm_ctrl_reg;
  1545. target = get_current_target(cmd_ctx);
  1546. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1547. {
  1548. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1549. return ERROR_OK;
  1550. }
  1551. if (!(etm_ctx = arm7_9->etm_ctx))
  1552. {
  1553. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1554. return ERROR_OK;
  1555. }
  1556. etm_ctrl_reg = &arm7_9->etm_ctx->reg_cache->reg_list[ETM_CTRL];
  1557. etm_get_reg(etm_ctrl_reg);
  1558. /* Set programming bit (10), clear port selection bit (11) */
  1559. buf_set_u32(etm_ctrl_reg->value, 10, 2, 0x1);
  1560. etm_store_reg(etm_ctrl_reg);
  1561. jtag_execute_queue();
  1562. etm_ctx->capture_driver->stop_capture(etm_ctx);
  1563. return ERROR_OK;
  1564. }
  1565. int handle_etm_analyze_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1566. {
  1567. target_t *target;
  1568. armv4_5_common_t *armv4_5;
  1569. arm7_9_common_t *arm7_9;
  1570. etm_context_t *etm_ctx;
  1571. int retval;
  1572. target = get_current_target(cmd_ctx);
  1573. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  1574. {
  1575. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  1576. return ERROR_OK;
  1577. }
  1578. if (!(etm_ctx = arm7_9->etm_ctx))
  1579. {
  1580. command_print(cmd_ctx, "current target doesn't have an ETM configured");
  1581. return ERROR_OK;
  1582. }
  1583. if ((retval = etmv1_analyze_trace(etm_ctx, cmd_ctx)) != ERROR_OK)
  1584. {
  1585. switch(retval)
  1586. {
  1587. case ERROR_ETM_ANALYSIS_FAILED:
  1588. command_print(cmd_ctx, "further analysis failed (corrupted trace data or just end of data");
  1589. break;
  1590. case ERROR_TRACE_INSTRUCTION_UNAVAILABLE:
  1591. command_print(cmd_ctx, "no instruction for current address available, analysis aborted");
  1592. break;
  1593. case ERROR_TRACE_IMAGE_UNAVAILABLE:
  1594. command_print(cmd_ctx, "no image available for trace analysis");
  1595. break;
  1596. default:
  1597. command_print(cmd_ctx, "unknown error: %i", retval);
  1598. }
  1599. }
  1600. return ERROR_OK;
  1601. }
  1602. int etm_register_commands(struct command_context_s *cmd_ctx)
  1603. {
  1604. etm_cmd = register_command(cmd_ctx, NULL, "etm", NULL, COMMAND_ANY, "Embedded Trace Macrocell");
  1605. register_command(cmd_ctx, etm_cmd, "config", handle_etm_config_command, COMMAND_CONFIG, "etm config <target> <port_width> <port_mode> <clocking> <capture_driver>");
  1606. return ERROR_OK;
  1607. }
  1608. int etm_register_user_commands(struct command_context_s *cmd_ctx)
  1609. {
  1610. register_command(cmd_ctx, etm_cmd, "tracemode", handle_etm_tracemode_command,
  1611. COMMAND_EXEC, "configure trace mode <none|data|address|all> <context id bits> <cycle accurate> <branch output");
  1612. register_command(cmd_ctx, etm_cmd, "info", handle_etm_info_command,
  1613. COMMAND_EXEC, "display info about the current target's ETM");
  1614. register_command(cmd_ctx, etm_cmd, "trigger_percent <percent>", handle_etm_trigger_percent_command,
  1615. COMMAND_EXEC, "amount (<percent>) of trace buffer to be filled after the trigger occured");
  1616. register_command(cmd_ctx, etm_cmd, "status", handle_etm_status_command,
  1617. COMMAND_EXEC, "display current target's ETM status");
  1618. register_command(cmd_ctx, etm_cmd, "start", handle_etm_start_command,
  1619. COMMAND_EXEC, "start ETM trace collection");
  1620. register_command(cmd_ctx, etm_cmd, "stop", handle_etm_stop_command,
  1621. COMMAND_EXEC, "stop ETM trace collection");
  1622. register_command(cmd_ctx, etm_cmd, "analyze", handle_etm_analyze_command,
  1623. COMMAND_EXEC, "anaylze collected ETM trace");
  1624. register_command(cmd_ctx, etm_cmd, "image", handle_etm_image_command,
  1625. COMMAND_EXEC, "load image from <file> [base address]");
  1626. register_command(cmd_ctx, etm_cmd, "dump", handle_etm_dump_command,
  1627. COMMAND_EXEC, "dump captured trace data <file>");
  1628. register_command(cmd_ctx, etm_cmd, "load", handle_etm_load_command,
  1629. COMMAND_EXEC, "load trace data for analysis <file>");
  1630. return ERROR_OK;
  1631. }