debug.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef __DEBUG_H__
  2. #define __DEBUG_H__
  3. #include <stdio.h>
  4. #define DEBUG(enable, tagged, ...) \
  5. do \
  6. { \
  7. if (enable) \
  8. { \
  9. if (tagged) \
  10. fprintf(stderr, "[ %25s() ] ", __func__); \
  11. fprintf(stderr, __VA_ARGS__); \
  12. } \
  13. } while( 0)
  14. #define ERROR(...) DEBUG(1, 1, "ERROR:"__VA_ARGS__)
  15. #define KASSERT(cond) \
  16. { \
  17. if (!(cond)) \
  18. { \
  19. ERROR("Failed assertion in %s:\n" \
  20. "%s at %s\n" \
  21. "line %d\n" \
  22. "RA=%lx\n", \
  23. __func__, \
  24. #cond, \
  25. __FILE__, \
  26. __LINE__, \
  27. (unsigned long)__builtin_return_address(0)); \
  28. \
  29. while (1) \
  30. ; \
  31. } \
  32. }
  33. #define KPANIC(args, ...) \
  34. { \
  35. ERROR(args, __VA_ARGS__); \
  36. while (1) ; \
  37. }
  38. static inline void dump_mem(const void *mem, int count)
  39. {
  40. const unsigned char *p = mem;
  41. int i = 0;
  42. for(i = 0; i < count; i++)
  43. {
  44. if( i % 16 == 0)
  45. DEBUG(1, 0, "\n");
  46. DEBUG(1, 0, "%02x ", p[i]);
  47. }
  48. }
  49. /* help to trace back */
  50. static inline void dump_stack(void)
  51. {
  52. unsigned long *stack;
  53. unsigned long addr;
  54. __asm__ __volatile__ ("\tori\t%0, $sp, #0\n" : "=r" (stack));
  55. printf("Call Trace:\n");
  56. addr = *stack;
  57. while (addr)
  58. {
  59. addr = *stack++;
  60. printf("[<%08lx>] ", addr);
  61. }
  62. printf("\n");
  63. return;
  64. }
  65. #endif /* __DEBUG_H__ */