debug.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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[13];
  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. snprintf(rtc_time_str, sizeof(rtc_time_str), "Current RTC time:[%d/%d/%d %d:%d:%d]\n",
  83. tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  84. _dputc('\n');//let log store do init first
  85. _dputs(rtc_time_str);
  86. }
  87. snprintf(ts_buf, sizeof(ts_buf), "[%u] ", (unsigned int)get_timer(boot_time));
  88. dputs(ALWAYS, ts_buf);
  89. va_list ap;
  90. va_start(ap, fmt);
  91. err = vsnprintf(buf, sizeof(buf), fmt, ap);
  92. va_end(ap);
  93. dputs(ALWAYS, buf);
  94. return err;
  95. }
  96. int _dvprintf(const char *fmt, va_list ap)
  97. {
  98. //char buf[256];
  99. int err;
  100. err = vsnprintf(buf, sizeof(buf), fmt, ap);
  101. dputs(ALWAYS, buf);
  102. return err;
  103. }
  104. void hexdump(const void *ptr, size_t len)
  105. {
  106. addr_t address = (addr_t)ptr;
  107. size_t count;
  108. int i;
  109. for (count = 0 ; count < len; count += 16) {
  110. printf("0x%08lx: ", address);
  111. printf("%08x %08x %08x %08x |", *(const uint32_t *)address, *(const uint32_t *)(address + 4), *(const uint32_t *)(address + 8), *(const uint32_t *)(address + 12));
  112. for (i=0; i < 16; i++) {
  113. char c = *(const char *)(address + i);
  114. if (isalpha(c)) {
  115. printf("%c", c);
  116. } else {
  117. printf(".");
  118. }
  119. }
  120. printf("|\n");
  121. address += 16;
  122. }
  123. }
  124. void hexdump8(const void *ptr, size_t len)
  125. {
  126. addr_t address = (addr_t)ptr;
  127. size_t count;
  128. int i;
  129. for (count = 0 ; count < len; count += 16) {
  130. printf("0x%08lx: ", address);
  131. for (i=0; i < 16; i++) {
  132. printf("0x%02hhx ", *(const uint8_t *)(address + i));
  133. }
  134. printf("\n");
  135. address += 16;
  136. }
  137. }
  138. #ifdef WITH_LIB_CONSOLE
  139. #include <lib/console.h>
  140. static int cmd_display_mem(int argc, const cmd_args *argv);
  141. static int cmd_modify_mem(int argc, const cmd_args *argv);
  142. static int cmd_fill_mem(int argc, const cmd_args *argv);
  143. static int cmd_reset(int argc, const cmd_args *argv);
  144. static int cmd_memtest(int argc, const cmd_args *argv);
  145. static int cmd_copy_mem(int argc, const cmd_args *argv);
  146. STATIC_COMMAND_START
  147. #if DEBUGLEVEL > 0
  148. { "dw", "display memory in words", &cmd_display_mem },
  149. { "dh", "display memory in halfwords", &cmd_display_mem },
  150. { "db", "display memory in bytes", &cmd_display_mem },
  151. { "mw", "modify word of memory", &cmd_modify_mem },
  152. { "mh", "modify halfword of memory", &cmd_modify_mem },
  153. { "mb", "modify byte of memory", &cmd_modify_mem },
  154. { "fw", "fill range of memory by word", &cmd_fill_mem },
  155. { "fh", "fill range of memory by halfword", &cmd_fill_mem },
  156. { "fb", "fill range of memory by byte", &cmd_fill_mem },
  157. { "mc", "copy a range of memory", &cmd_copy_mem },
  158. #endif
  159. #if DEBUGLEVEL > 1
  160. { "mtest", "simple memory test", &cmd_memtest },
  161. #endif
  162. STATIC_COMMAND_END(mem);
  163. static int cmd_display_mem(int argc, const cmd_args *argv)
  164. {
  165. int size;
  166. if (argc < 3) {
  167. printf("not enough arguments\n");
  168. printf("%s <address> <length>\n", argv[0].str);
  169. return -1;
  170. }
  171. if (strcmp(argv[0].str, "dw") == 0) {
  172. size = 4;
  173. } else if (strcmp(argv[0].str, "dh") == 0) {
  174. size = 2;
  175. } else {
  176. size = 1;
  177. }
  178. unsigned long address = argv[1].u;
  179. size_t len = argv[2].u;
  180. unsigned long stop = address + len;
  181. int count = 0;
  182. if ((address & (size - 1)) != 0) {
  183. printf("unaligned address, cannot display\n");
  184. return -1;
  185. }
  186. for ( ; address < stop; address += size) {
  187. if (count == 0)
  188. printf("0x%08lx: ", address);
  189. switch (size) {
  190. case 4:
  191. printf("%08x ", *(uint32_t *)address);
  192. break;
  193. case 2:
  194. printf("%04hx ", *(uint16_t *)address);
  195. break;
  196. case 1:
  197. printf("%02hhx ", *(uint8_t *)address);
  198. break;
  199. }
  200. count += size;
  201. if (count == 16) {
  202. printf("\n");
  203. count = 0;
  204. }
  205. }
  206. if (count != 0)
  207. printf("\n");
  208. return 0;
  209. }
  210. static int cmd_modify_mem(int argc, const cmd_args *argv)
  211. {
  212. int size;
  213. if (argc < 3) {
  214. printf("not enough arguments\n");
  215. printf("%s <address> <val>\n", argv[0].str);
  216. return -1;
  217. }
  218. if (strcmp(argv[0].str, "mw") == 0) {
  219. size = 4;
  220. } else if (strcmp(argv[0].str, "mh") == 0) {
  221. size = 2;
  222. } else {
  223. size = 1;
  224. }
  225. unsigned long address = argv[1].u;
  226. unsigned int val = argv[2].u;
  227. if ((address & (size - 1)) != 0) {
  228. printf("unaligned address, cannot modify\n");
  229. return -1;
  230. }
  231. switch (size) {
  232. case 4:
  233. *(uint32_t *)address = (uint32_t)val;
  234. break;
  235. case 2:
  236. *(uint16_t *)address = (uint16_t)val;
  237. break;
  238. case 1:
  239. *(uint8_t *)address = (uint8_t)val;
  240. break;
  241. }
  242. return 0;
  243. }
  244. static int cmd_fill_mem(int argc, const cmd_args *argv)
  245. {
  246. int size;
  247. if (argc < 4) {
  248. printf("not enough arguments\n");
  249. printf("%s <address> <len> <val>\n", argv[0].str);
  250. return -1;
  251. }
  252. if (strcmp(argv[0].str, "fw") == 0) {
  253. size = 4;
  254. } else if (strcmp(argv[0].str, "fh") == 0) {
  255. size = 2;
  256. } else {
  257. size = 1;
  258. }
  259. unsigned long address = argv[1].u;
  260. unsigned long len = argv[2].u;
  261. unsigned long stop = address + len;
  262. unsigned int val = argv[3].u;
  263. if ((address & (size - 1)) != 0) {
  264. printf("unaligned address, cannot modify\n");
  265. return -1;
  266. }
  267. for ( ; address < stop; address += size) {
  268. switch (size) {
  269. case 4:
  270. *(uint32_t *)address = (uint32_t)val;
  271. break;
  272. case 2:
  273. *(uint16_t *)address = (uint16_t)val;
  274. break;
  275. case 1:
  276. *(uint8_t *)address = (uint8_t)val;
  277. break;
  278. }
  279. }
  280. return 0;
  281. }
  282. static int cmd_copy_mem(int argc, const cmd_args *argv)
  283. {
  284. if (argc < 4) {
  285. printf("not enough arguments\n");
  286. printf("%s <source address> <target address> <len>\n", argv[0].str);
  287. return -1;
  288. }
  289. addr_t source = argv[1].u;
  290. addr_t target = argv[2].u;
  291. size_t len = argv[3].u;
  292. memcpy((void *)target, (const void *)source, len);
  293. return 0;
  294. }
  295. static int cmd_memtest(int argc, const cmd_args *argv)
  296. {
  297. if (argc < 3) {
  298. printf("not enough arguments\n");
  299. printf("%s <base> <len>\n", argv[0].str);
  300. return -1;
  301. }
  302. uint32_t *ptr;
  303. size_t len;
  304. ptr = (uint32_t *)argv[1].u;
  305. len = (size_t)argv[2].u;
  306. size_t i;
  307. // write out
  308. printf("writing first pass...");
  309. for (i = 0; i < len / 4; i++) {
  310. ptr[i] = i;
  311. }
  312. printf("done\n");
  313. // verify
  314. printf("verifying...");
  315. for (i = 0; i < len / 4; i++) {
  316. if (ptr[i] != i)
  317. printf("error at %p\n", &ptr[i]);
  318. }
  319. printf("done\n");
  320. return 0;
  321. }
  322. #endif