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.
 
 
 
 
 
 

531 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) ST-Ericsson SA 2011 *
  6. * michel.jaouen@stericsson.com : smp minimum support *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "target.h"
  27. #include <helper/log.h>
  28. #include "breakpoints.h"
  29. static char *breakpoint_type_strings[] =
  30. {
  31. "hardware",
  32. "software"
  33. };
  34. static char *watchpoint_rw_strings[] =
  35. {
  36. "read",
  37. "write",
  38. "access"
  39. };
  40. // monotonic counter/id-number for breakpoints and watch points
  41. static int bpwp_unique_id;
  42. int breakpoint_add_internal(struct target *target, uint32_t address, uint32_t length, enum breakpoint_type type)
  43. {
  44. struct breakpoint *breakpoint = target->breakpoints;
  45. struct breakpoint **breakpoint_p = &target->breakpoints;
  46. char *reason;
  47. int retval;
  48. int n;
  49. n = 0;
  50. while (breakpoint)
  51. {
  52. n++;
  53. if (breakpoint->address == address) {
  54. /* FIXME don't assume "same address" means "same
  55. * breakpoint" ... check all the parameters before
  56. * succeeding.
  57. */
  58. LOG_DEBUG("Duplicate Breakpoint address: 0x%08" PRIx32 " (BP %d)",
  59. address, breakpoint->unique_id);
  60. return ERROR_OK;
  61. }
  62. breakpoint_p = &breakpoint->next;
  63. breakpoint = breakpoint->next;
  64. }
  65. (*breakpoint_p) = malloc(sizeof(struct breakpoint));
  66. (*breakpoint_p)->address = address;
  67. (*breakpoint_p)->asid = 0;
  68. (*breakpoint_p)->length = length;
  69. (*breakpoint_p)->type = type;
  70. (*breakpoint_p)->set = 0;
  71. (*breakpoint_p)->orig_instr = malloc(length);
  72. (*breakpoint_p)->next = NULL;
  73. (*breakpoint_p)->unique_id = bpwp_unique_id++;
  74. retval = target_add_breakpoint(target, *breakpoint_p);
  75. switch (retval) {
  76. case ERROR_OK:
  77. break;
  78. case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
  79. reason = "resource not available";
  80. goto fail;
  81. case ERROR_TARGET_NOT_HALTED:
  82. reason = "target running";
  83. goto fail;
  84. default:
  85. reason = "unknown reason";
  86. fail:
  87. LOG_ERROR("can't add breakpoint: %s", reason);
  88. free((*breakpoint_p)->orig_instr);
  89. free(*breakpoint_p);
  90. *breakpoint_p = NULL;
  91. return retval;
  92. }
  93. LOG_DEBUG("added %s breakpoint at 0x%8.8" PRIx32 " of length 0x%8.8x, (BPID: %d)",
  94. breakpoint_type_strings[(*breakpoint_p)->type],
  95. (*breakpoint_p)->address, (*breakpoint_p)->length,
  96. (*breakpoint_p)->unique_id);
  97. return ERROR_OK;
  98. }
  99. int context_breakpoint_add_internal(struct target *target, uint32_t asid, uint32_t length, enum breakpoint_type type)
  100. {
  101. struct breakpoint *breakpoint = target->breakpoints;
  102. struct breakpoint **breakpoint_p = &target->breakpoints;
  103. int retval;
  104. int n;
  105. n = 0;
  106. while (breakpoint)
  107. {
  108. n++;
  109. if (breakpoint->asid == asid)
  110. {
  111. /* FIXME don't assume "same address" means "same
  112. * breakpoint" ... check all the parameters before
  113. * succeeding.
  114. */
  115. LOG_DEBUG("Duplicate Breakpoint asid: 0x%08" PRIx32 " (BP %d)",
  116. asid, breakpoint->unique_id);
  117. return -1;
  118. }
  119. breakpoint_p = &breakpoint->next;
  120. breakpoint = breakpoint->next;
  121. }
  122. (*breakpoint_p) = malloc(sizeof(struct breakpoint));
  123. (*breakpoint_p)->address = 0;
  124. (*breakpoint_p)->asid = asid;
  125. (*breakpoint_p)->length = length;
  126. (*breakpoint_p)->type = type;
  127. (*breakpoint_p)->set = 0;
  128. (*breakpoint_p)->orig_instr = malloc(length);
  129. (*breakpoint_p)->next = NULL;
  130. (*breakpoint_p)->unique_id = bpwp_unique_id++;
  131. retval = target_add_context_breakpoint(target, *breakpoint_p);
  132. if (retval != ERROR_OK)
  133. {
  134. LOG_ERROR("could not add breakpoint");
  135. free((*breakpoint_p)->orig_instr);
  136. free(*breakpoint_p);
  137. *breakpoint_p = NULL;
  138. return retval;
  139. }
  140. LOG_DEBUG("added %s Context breakpoint at 0x%8.8" PRIx32 " of length 0x%8.8x, (BPID: %d)",
  141. breakpoint_type_strings[(*breakpoint_p)->type],
  142. (*breakpoint_p)->asid, (*breakpoint_p)->length,
  143. (*breakpoint_p)->unique_id);
  144. return ERROR_OK;
  145. }
  146. int hybrid_breakpoint_add_internal(struct target *target, uint32_t address, uint32_t asid, uint32_t length, enum breakpoint_type type)
  147. {
  148. struct breakpoint *breakpoint = target->breakpoints;
  149. struct breakpoint **breakpoint_p = &target->breakpoints;
  150. int retval;
  151. int n;
  152. n = 0;
  153. while (breakpoint)
  154. {
  155. n++;
  156. if ((breakpoint->asid == asid) && (breakpoint->address == address)) {
  157. /* FIXME don't assume "same address" means "same
  158. * breakpoint" ... check all the parameters before
  159. * succeeding.
  160. */
  161. LOG_DEBUG("Duplicate Hybrid Breakpoint asid: 0x%08" PRIx32 " (BP %d)",
  162. asid, breakpoint->unique_id);
  163. return -1;
  164. }
  165. else if ((breakpoint->address == address) && (breakpoint->asid == 0))
  166. {
  167. LOG_DEBUG("Duplicate Breakpoint IVA: 0x%08" PRIx32 " (BP %d)",
  168. address, breakpoint->unique_id);
  169. return -1;
  170. }
  171. breakpoint_p = &breakpoint->next;
  172. breakpoint = breakpoint->next;
  173. }
  174. (*breakpoint_p) = malloc(sizeof(struct breakpoint));
  175. (*breakpoint_p)->address = address;
  176. (*breakpoint_p)->asid = asid;
  177. (*breakpoint_p)->length = length;
  178. (*breakpoint_p)->type = type;
  179. (*breakpoint_p)->set = 0;
  180. (*breakpoint_p)->orig_instr = malloc(length);
  181. (*breakpoint_p)->next = NULL;
  182. (*breakpoint_p)->unique_id = bpwp_unique_id++;
  183. retval = target_add_hybrid_breakpoint(target, *breakpoint_p);
  184. if (retval != ERROR_OK)
  185. {
  186. LOG_ERROR("could not add breakpoint");
  187. free((*breakpoint_p)->orig_instr);
  188. free(*breakpoint_p);
  189. *breakpoint_p = NULL;
  190. return retval;
  191. }
  192. LOG_DEBUG("added %s Hybrid breakpoint at address 0x%8.8" PRIx32 " of length 0x%8.8x, (BPID: %d)",
  193. breakpoint_type_strings[(*breakpoint_p)->type],
  194. (*breakpoint_p)->address, (*breakpoint_p)->length,
  195. (*breakpoint_p)->unique_id);
  196. return ERROR_OK;
  197. }
  198. int breakpoint_add(struct target *target, uint32_t address, uint32_t length, enum breakpoint_type type)
  199. {
  200. int retval = ERROR_OK;
  201. if (target->smp)
  202. {
  203. struct target_list *head;
  204. struct target *curr;
  205. head = target->head;
  206. if (type == BKPT_SOFT)
  207. return(breakpoint_add_internal(head->target, address,length, type));
  208. while(head != (struct target_list*)NULL)
  209. {
  210. curr = head->target;
  211. retval = breakpoint_add_internal(curr, address,length, type);
  212. if (retval != ERROR_OK) return retval;
  213. head = head->next;
  214. }
  215. return retval;
  216. }
  217. else
  218. return(breakpoint_add_internal(target, address, length, type));
  219. }
  220. int context_breakpoint_add(struct target *target, uint32_t asid, uint32_t length, enum breakpoint_type type)
  221. {
  222. int retval = ERROR_OK;
  223. if (target->smp)
  224. {
  225. struct target_list *head;
  226. struct target *curr;
  227. head = target->head;
  228. while(head != (struct target_list*)NULL)
  229. {
  230. curr = head->target;
  231. retval = context_breakpoint_add_internal(curr, asid,length, type);
  232. if (retval != ERROR_OK) return retval;
  233. head = head->next;
  234. }
  235. return retval;
  236. }
  237. else
  238. return(context_breakpoint_add_internal(target, asid, length, type));
  239. }
  240. int hybrid_breakpoint_add(struct target *target, uint32_t address, uint32_t asid, uint32_t length, enum breakpoint_type type)
  241. {
  242. int retval = ERROR_OK;
  243. if (target->smp)
  244. {
  245. struct target_list *head;
  246. struct target *curr;
  247. head = target->head;
  248. while(head != (struct target_list*)NULL)
  249. {
  250. curr = head->target;
  251. retval = hybrid_breakpoint_add_internal(curr, address, asid, length, type);
  252. if (retval != ERROR_OK) return retval;
  253. head = head->next;
  254. }
  255. return retval;
  256. }
  257. else
  258. return(hybrid_breakpoint_add_internal(target, address, asid, length, type));
  259. }
  260. /* free up a breakpoint */
  261. static void breakpoint_free(struct target *target, struct breakpoint *breakpoint_to_remove)
  262. {
  263. struct breakpoint *breakpoint = target->breakpoints;
  264. struct breakpoint **breakpoint_p = &target->breakpoints;
  265. int retval;
  266. while (breakpoint)
  267. {
  268. if (breakpoint == breakpoint_to_remove)
  269. break;
  270. breakpoint_p = &breakpoint->next;
  271. breakpoint = breakpoint->next;
  272. }
  273. if (breakpoint == NULL)
  274. return;
  275. retval = target_remove_breakpoint(target, breakpoint);
  276. LOG_DEBUG("free BPID: %d --> %d", breakpoint->unique_id, retval);
  277. (*breakpoint_p) = breakpoint->next;
  278. free(breakpoint->orig_instr);
  279. free(breakpoint);
  280. }
  281. int breakpoint_remove_internal(struct target *target, uint32_t address)
  282. {
  283. struct breakpoint *breakpoint = target->breakpoints;
  284. while (breakpoint)
  285. {
  286. if ((breakpoint->address == address) && (breakpoint->asid == 0))
  287. break;
  288. else if ((breakpoint->address == 0) && (breakpoint->asid == address))
  289. break;
  290. else if ((breakpoint->address == address) && (breakpoint->asid != 0))
  291. break;
  292. breakpoint = breakpoint->next;
  293. }
  294. if (breakpoint)
  295. {
  296. breakpoint_free(target, breakpoint);
  297. return 1;
  298. }
  299. else
  300. {
  301. if (!target->smp)
  302. LOG_ERROR("no breakpoint at address 0x%8.8" PRIx32 " found", address);
  303. return 0;
  304. }
  305. }
  306. void breakpoint_remove(struct target *target, uint32_t address)
  307. {
  308. int found = 0;
  309. if (target->smp)
  310. {
  311. struct target_list *head;
  312. struct target *curr;
  313. head = target->head;
  314. while(head != (struct target_list*)NULL)
  315. {
  316. curr = head->target;
  317. found += breakpoint_remove_internal(curr, address);
  318. head = head->next;
  319. }
  320. if (found == 0)
  321. LOG_ERROR("no breakpoint at address 0x%8.8" PRIx32 " found", address);
  322. }
  323. else breakpoint_remove_internal(target, address);
  324. }
  325. void breakpoint_clear_target_internal(struct target *target)
  326. {
  327. struct breakpoint *breakpoint;
  328. LOG_DEBUG("Delete all breakpoints for target: %s",
  329. target_name(target));
  330. while ((breakpoint = target->breakpoints) != NULL)
  331. {
  332. breakpoint_free(target, breakpoint);
  333. }
  334. }
  335. void breakpoint_clear_target(struct target *target)
  336. {
  337. if (target->smp)
  338. {
  339. struct target_list *head;
  340. struct target *curr;
  341. head = target->head;
  342. while(head != (struct target_list*)NULL)
  343. {
  344. curr = head->target;
  345. breakpoint_clear_target_internal(curr);
  346. head = head->next;
  347. }
  348. }
  349. else breakpoint_clear_target_internal(target);
  350. }
  351. struct breakpoint* breakpoint_find(struct target *target, uint32_t address)
  352. {
  353. struct breakpoint *breakpoint = target->breakpoints;
  354. while (breakpoint)
  355. {
  356. if (breakpoint->address == address)
  357. return breakpoint;
  358. breakpoint = breakpoint->next;
  359. }
  360. return NULL;
  361. }
  362. int watchpoint_add(struct target *target, uint32_t address, uint32_t length,
  363. enum watchpoint_rw rw, uint32_t value, uint32_t mask)
  364. {
  365. struct watchpoint *watchpoint = target->watchpoints;
  366. struct watchpoint **watchpoint_p = &target->watchpoints;
  367. int retval;
  368. char *reason;
  369. while (watchpoint)
  370. {
  371. if (watchpoint->address == address) {
  372. if (watchpoint->length != length
  373. || watchpoint->value != value
  374. || watchpoint->mask != mask
  375. || watchpoint->rw != rw) {
  376. LOG_ERROR("address 0x%8.8" PRIx32
  377. "already has watchpoint %d",
  378. address, watchpoint->unique_id);
  379. return ERROR_FAIL;
  380. }
  381. /* ignore duplicate watchpoint */
  382. return ERROR_OK;
  383. }
  384. watchpoint_p = &watchpoint->next;
  385. watchpoint = watchpoint->next;
  386. }
  387. (*watchpoint_p) = calloc(1, sizeof(struct watchpoint));
  388. (*watchpoint_p)->address = address;
  389. (*watchpoint_p)->length = length;
  390. (*watchpoint_p)->value = value;
  391. (*watchpoint_p)->mask = mask;
  392. (*watchpoint_p)->rw = rw;
  393. (*watchpoint_p)->unique_id = bpwp_unique_id++;
  394. retval = target_add_watchpoint(target, *watchpoint_p);
  395. switch (retval) {
  396. case ERROR_OK:
  397. break;
  398. case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
  399. reason = "resource not available";
  400. goto bye;
  401. case ERROR_TARGET_NOT_HALTED:
  402. reason = "target running";
  403. goto bye;
  404. default:
  405. reason = "unrecognized error";
  406. bye:
  407. LOG_ERROR("can't add %s watchpoint at 0x%8.8" PRIx32 ", %s",
  408. watchpoint_rw_strings[(*watchpoint_p)->rw],
  409. address, reason);
  410. free (*watchpoint_p);
  411. *watchpoint_p = NULL;
  412. return retval;
  413. }
  414. LOG_DEBUG("added %s watchpoint at 0x%8.8" PRIx32
  415. " of length 0x%8.8" PRIx32 " (WPID: %d)",
  416. watchpoint_rw_strings[(*watchpoint_p)->rw],
  417. (*watchpoint_p)->address,
  418. (*watchpoint_p)->length,
  419. (*watchpoint_p)->unique_id);
  420. return ERROR_OK;
  421. }
  422. static void watchpoint_free(struct target *target, struct watchpoint *watchpoint_to_remove)
  423. {
  424. struct watchpoint *watchpoint = target->watchpoints;
  425. struct watchpoint **watchpoint_p = &target->watchpoints;
  426. int retval;
  427. while (watchpoint)
  428. {
  429. if (watchpoint == watchpoint_to_remove)
  430. break;
  431. watchpoint_p = &watchpoint->next;
  432. watchpoint = watchpoint->next;
  433. }
  434. if (watchpoint == NULL)
  435. return;
  436. retval = target_remove_watchpoint(target, watchpoint);
  437. LOG_DEBUG("free WPID: %d --> %d", watchpoint->unique_id, retval);
  438. (*watchpoint_p) = watchpoint->next;
  439. free(watchpoint);
  440. }
  441. void watchpoint_remove(struct target *target, uint32_t address)
  442. {
  443. struct watchpoint *watchpoint = target->watchpoints;
  444. while (watchpoint)
  445. {
  446. if (watchpoint->address == address)
  447. break;
  448. watchpoint = watchpoint->next;
  449. }
  450. if (watchpoint)
  451. {
  452. watchpoint_free(target, watchpoint);
  453. }
  454. else
  455. {
  456. LOG_ERROR("no watchpoint at address 0x%8.8" PRIx32 " found", address);
  457. }
  458. }
  459. void watchpoint_clear_target(struct target *target)
  460. {
  461. struct watchpoint *watchpoint;
  462. LOG_DEBUG("Delete all watchpoints for target: %s",
  463. target_name(target));
  464. while ((watchpoint = target->watchpoints) != NULL)
  465. {
  466. watchpoint_free(target, watchpoint);
  467. }
  468. }