debug.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <ctype.h>
  24. #include <debug.h>
  25. #include <stdlib.h>
  26. #include <printf.h>
  27. #include <list.h>
  28. #include <string.h>
  29. #include <arch/ops.h>
  30. #include <platform.h>
  31. #include <platform/debug.h>
  32. #include <kernel/thread.h>
  33. #include <kernel/timer.h>
  34. #include <platform/mt_gpt.h> // for get_timer()
  35. #include <platform/mt_rtc.h> // for rtc_get_time()
  36. #include <platform/ram_console.h>
  37. #ifndef PRINT_BUFFER_LEN
  38. #define PRINT_BUFFER_LEN 1024
  39. #endif
  40. static char buf[PRINT_BUFFER_LEN];
  41. extern unsigned int boot_time;
  42. void spin(uint32_t usecs)
  43. {
  44. bigtime_t start = current_time_hires();
  45. while ((current_time_hires() - start) < usecs)
  46. ;
  47. }
  48. void halt(void)
  49. {
  50. enter_critical_section(); // disable ints
  51. platform_halt();
  52. }
  53. void _panic(void *caller, const char *fmt, ...)
  54. {
  55. dprintf(ALWAYS, "panic (caller %p): ", caller);
  56. ram_console_set_exp_type(AEE_EXP_TYPE_LK_CRASH);
  57. va_list ap;
  58. va_start(ap, fmt);
  59. _dvprintf(fmt, ap);
  60. va_end(ap);
  61. halt();
  62. }
  63. int _dputs(const char *str)
  64. {
  65. while(*str != 0) {
  66. _dputc(*str++);
  67. }
  68. return 0;
  69. }
  70. int _dprintf(const char *fmt, ...)
  71. {
  72. //char buf[256];
  73. static int print_once;
  74. char ts_buf[16];
  75. int err;
  76. if (!print_once) {
  77. struct rtc_time tm;
  78. char rtc_time_str[64];
  79. print_once = 1;
  80. rtc_get_time(&tm);
  81. memset(rtc_time_str, 0x0, sizeof(rtc_time_str));
  82. err = snprintf(rtc_time_str, sizeof(rtc_time_str),
  83. "Current RTC time:[%d/%d/%d %d:%d:%d]\n",
  84. tm.tm_year, tm.tm_mon, tm.tm_mday,
  85. tm.tm_hour, tm.tm_min, tm.tm_sec);
  86. if (err < 0 || (unsigned)err >= sizeof(rtc_time_str))
  87. strncpy(rtc_time_str, "unknown error", sizeof(rtc_time_str));
  88. _dputc('\n');//let log store do init first
  89. _dputs(rtc_time_str);
  90. }
  91. err = snprintf(ts_buf, sizeof(ts_buf), "[%u] ", (unsigned int)get_timer(boot_time));
  92. if (err < 0 || (unsigned)err >= sizeof(ts_buf))
  93. strncpy(ts_buf, "unknown error", sizeof(ts_buf));
  94. dputs(ALWAYS, ts_buf);
  95. va_list ap;
  96. va_start(ap, fmt);
  97. err = vsnprintf(buf, sizeof(buf), fmt, ap);
  98. va_end(ap);
  99. if (err < 0 || (unsigned)err >= sizeof(buf))
  100. strncpy(buf, "unknown error", sizeof(buf));
  101. dputs(ALWAYS, buf);
  102. return err;
  103. }
  104. int _dvprintf(const char *fmt, va_list ap)
  105. {
  106. //char buf[256];
  107. int err;
  108. err = vsnprintf(buf, sizeof(buf), fmt, ap);
  109. if (err < 0 || (unsigned)err >= sizeof(buf))
  110. strncpy(buf, "unknown error", sizeof(buf));
  111. dputs(ALWAYS, buf);
  112. return err;
  113. }
  114. void hexdump(const void *ptr, size_t len)
  115. {
  116. addr_t address = (addr_t)ptr;
  117. size_t count;
  118. int i;
  119. for (count = 0 ; count < len; count += 16) {
  120. printf("0x%08lx: ", address);
  121. printf("%08x %08x %08x %08x |", *(const uint32_t *)address, *(const uint32_t *)(address + 4), *(const uint32_t *)(address + 8), *(const uint32_t *)(address + 12));
  122. for (i=0; i < 16; i++) {
  123. char c = *(const char *)(address + i);
  124. if (isalpha(c)) {
  125. printf("%c", c);
  126. } else {
  127. printf(".");
  128. }
  129. }
  130. printf("|\n");
  131. address += 16;
  132. }
  133. }
  134. void hexdump8(const void *ptr, size_t len)
  135. {
  136. addr_t address = (addr_t)ptr;
  137. size_t count;
  138. int i;
  139. for (count = 0 ; count < len; count += 16) {
  140. printf("0x%08lx: ", address);
  141. for (i=0; i < 16; i++) {
  142. printf("0x%02hhx ", *(const uint8_t *)(address + i));
  143. }
  144. printf("\n");
  145. address += 16;
  146. }
  147. }
  148. #ifdef WITH_LIB_CONSOLE
  149. #include <lib/console.h>
  150. static int cmd_display_mem(int argc, const cmd_args *argv);
  151. static int cmd_modify_mem(int argc, const cmd_args *argv);
  152. static int cmd_fill_mem(int argc, const cmd_args *argv);
  153. static int cmd_reset(int argc, const cmd_args *argv);
  154. static int cmd_memtest(int argc, const cmd_args *argv);
  155. static int cmd_copy_mem(int argc, const cmd_args *argv);
  156. STATIC_COMMAND_START
  157. #if DEBUGLEVEL > 0
  158. { "dw", "display memory in words", &cmd_display_mem },
  159. { "dh", "display memory in halfwords", &cmd_display_mem },
  160. { "db", "display memory in bytes", &cmd_display_mem },
  161. { "mw", "modify word of memory", &cmd_modify_mem },
  162. { "mh", "modify halfword of memory", &cmd_modify_mem },
  163. { "mb", "modify byte of memory", &cmd_modify_mem },
  164. { "fw", "fill range of memory by word", &cmd_fill_mem },
  165. { "fh", "fill range of memory by halfword", &cmd_fill_mem },
  166. { "fb", "fill range of memory by byte", &cmd_fill_mem },
  167. { "mc", "copy a range of memory", &cmd_copy_mem },
  168. #endif
  169. #if DEBUGLEVEL > 1
  170. { "mtest", "simple memory test", &cmd_memtest },
  171. #endif
  172. STATIC_COMMAND_END(mem);
  173. static int cmd_display_mem(int argc, const cmd_args *argv)
  174. {
  175. int size;
  176. if (argc < 3) {
  177. printf("not enough arguments\n");
  178. printf("%s <address> <length>\n", argv[0].str);
  179. return -1;
  180. }
  181. if (strcmp(argv[0].str, "dw") == 0) {
  182. size = 4;
  183. } else if (strcmp(argv[0].str, "dh") == 0) {
  184. size = 2;
  185. } else {
  186. size = 1;
  187. }
  188. unsigned long address = argv[1].u;
  189. size_t len = argv[2].u;
  190. unsigned long stop = address + len;
  191. int count = 0;
  192. if ((address & (size - 1)) != 0) {
  193. printf("unaligned address, cannot display\n");
  194. return -1;
  195. }
  196. for ( ; address < stop; address += size) {
  197. if (count == 0)
  198. printf("0x%08lx: ", address);
  199. switch (size) {
  200. case 4:
  201. printf("%08x ", *(uint32_t *)address);
  202. break;
  203. case 2:
  204. printf("%04hx ", *(uint16_t *)address);
  205. break;
  206. case 1:
  207. printf("%02hhx ", *(uint8_t *)address);
  208. break;
  209. }
  210. count += size;
  211. if (count == 16) {
  212. printf("\n");
  213. count = 0;
  214. }
  215. }
  216. if (count != 0)
  217. printf("\n");
  218. return 0;
  219. }
  220. static int cmd_modify_mem(int argc, const cmd_args *argv)
  221. {
  222. int size;
  223. if (argc < 3) {
  224. printf("not enough arguments\n");
  225. printf("%s <address> <val>\n", argv[0].str);
  226. return -1;
  227. }
  228. if (strcmp(argv[0].str, "mw") == 0) {
  229. size = 4;
  230. } else if (strcmp(argv[0].str, "mh") == 0) {
  231. size = 2;
  232. } else {
  233. size = 1;
  234. }
  235. unsigned long address = argv[1].u;
  236. unsigned int val = argv[2].u;
  237. if ((address & (size - 1)) != 0) {
  238. printf("unaligned address, cannot modify\n");
  239. return -1;
  240. }
  241. switch (size) {
  242. case 4:
  243. *(uint32_t *)address = (uint32_t)val;
  244. break;
  245. case 2:
  246. *(uint16_t *)address = (uint16_t)val;
  247. break;
  248. case 1:
  249. *(uint8_t *)address = (uint8_t)val;
  250. break;
  251. }
  252. return 0;
  253. }
  254. static int cmd_fill_mem(int argc, const cmd_args *argv)
  255. {
  256. int size;
  257. if (argc < 4) {
  258. printf("not enough arguments\n");
  259. printf("%s <address> <len> <val>\n", argv[0].str);
  260. return -1;
  261. }
  262. if (strcmp(argv[0].str, "fw") == 0) {
  263. size = 4;
  264. } else if (strcmp(argv[0].str, "fh") == 0) {
  265. size = 2;
  266. } else {
  267. size = 1;
  268. }
  269. unsigned long address = argv[1].u;
  270. unsigned long len = argv[2].u;
  271. unsigned long stop = address + len;
  272. unsigned int val = argv[3].u;
  273. if ((address & (size - 1)) != 0) {
  274. printf("unaligned address, cannot modify\n");
  275. return -1;
  276. }
  277. for ( ; address < stop; address += size) {
  278. switch (size) {
  279. case 4:
  280. *(uint32_t *)address = (uint32_t)val;
  281. break;
  282. case 2:
  283. *(uint16_t *)address = (uint16_t)val;
  284. break;
  285. case 1:
  286. *(uint8_t *)address = (uint8_t)val;
  287. break;
  288. }
  289. }
  290. return 0;
  291. }
  292. static int cmd_copy_mem(int argc, const cmd_args *argv)
  293. {
  294. if (argc < 4) {
  295. printf("not enough arguments\n");
  296. printf("%s <source address> <target address> <len>\n", argv[0].str);
  297. return -1;
  298. }
  299. addr_t source = argv[1].u;
  300. addr_t target = argv[2].u;
  301. size_t len = argv[3].u;
  302. memcpy((void *)target, (const void *)source, len);
  303. return 0;
  304. }
  305. static int cmd_memtest(int argc, const cmd_args *argv)
  306. {
  307. if (argc < 3) {
  308. printf("not enough arguments\n");
  309. printf("%s <base> <len>\n", argv[0].str);
  310. return -1;
  311. }
  312. uint32_t *ptr;
  313. size_t len;
  314. ptr = (uint32_t *)argv[1].u;
  315. len = (size_t)argv[2].u;
  316. size_t i;
  317. // write out
  318. printf("writing first pass...");
  319. for (i = 0; i < len / 4; i++) {
  320. ptr[i] = i;
  321. }
  322. printf("done\n");
  323. // verify
  324. printf("verifying...");
  325. for (i = 0; i < len / 4; i++) {
  326. if (ptr[i] != i)
  327. printf("error at %p\n", &ptr[i]);
  328. }
  329. printf("done\n");
  330. return 0;
  331. }
  332. #endif