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.

debug.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <stdint.h>
  2. #include <stdarg.h>
  3. #include <io.h>
  4. #include <console.h>
  5. void pause ( void ) {
  6. printf ( "\nPress a key" );
  7. getchar();
  8. printf ( "\r \r" );
  9. }
  10. void more ( void ) {
  11. printf ( "---more---" );
  12. getchar();
  13. printf ( "\r \r" );
  14. }
  15. /**
  16. * Print row of a hex dump with specified display address
  17. *
  18. * @v dispaddr Display address
  19. * @v data Data to print
  20. * @v len Length of data
  21. * @v offset Starting offset within data
  22. */
  23. static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
  24. unsigned long len, unsigned int offset ) {
  25. const uint8_t *bytes = data;
  26. unsigned int i;
  27. uint8_t byte;
  28. printf ( "%08lx :", ( dispaddr + offset ) );
  29. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  30. if ( i >= len ) {
  31. printf ( " " );
  32. continue;
  33. }
  34. printf ( " %02x", bytes[i] );
  35. }
  36. printf ( " : " );
  37. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  38. if ( i >= len ) {
  39. printf ( " " );
  40. continue;
  41. }
  42. byte = bytes[i];
  43. if ( ( byte < 0x20 ) || ( byte >= 0x7f ) )
  44. byte = '.';
  45. printf ( "%c", byte );
  46. }
  47. printf ( "\n" );
  48. }
  49. /**
  50. * Print hex dump with specified display address
  51. *
  52. * @v dispaddr Display address
  53. * @v data Data to print
  54. * @v len Length of data
  55. */
  56. void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
  57. unsigned long len ) {
  58. unsigned int offset;
  59. for ( offset = 0 ; offset < len ; offset += 16 ) {
  60. dbg_hex_dump_da_row ( dispaddr, data, len, offset );
  61. }
  62. }
  63. #define GUARD_SYMBOL ( ( 'M' << 24 ) | ( 'I' << 16 ) | ( 'N' << 8 ) | 'E' )
  64. /* Fill a region with guard markers. We use a 4-byte pattern to make
  65. * it less likely that check_region will find spurious 1-byte regions
  66. * of non-corruption.
  67. */
  68. void guard_region ( void *region, size_t len ) {
  69. uint32_t offset = 0;
  70. len &= ~0x03;
  71. for ( offset = 0; offset < len ; offset += 4 ) {
  72. *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  73. }
  74. }
  75. /* Check a region that has been guarded with guard_region() for
  76. * corruption.
  77. */
  78. int check_region ( void *region, size_t len ) {
  79. uint8_t corrupted = 0;
  80. uint8_t in_corruption = 0;
  81. uint32_t offset = 0;
  82. uint32_t test = 0;
  83. len &= ~0x03;
  84. for ( offset = 0; offset < len ; offset += 4 ) {
  85. test = *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  86. if ( ( in_corruption == 0 ) &&
  87. ( test != GUARD_SYMBOL ) ) {
  88. /* Start of corruption */
  89. if ( corrupted == 0 ) {
  90. corrupted = 1;
  91. printf ( "Region %p-%p (physical %#lx-%#lx) "
  92. "corrupted\n",
  93. region, region + len,
  94. virt_to_phys ( region ),
  95. virt_to_phys ( region + len ) );
  96. }
  97. in_corruption = 1;
  98. printf ( "--- offset %#lx ", offset );
  99. } else if ( ( in_corruption != 0 ) &&
  100. ( test == GUARD_SYMBOL ) ) {
  101. /* End of corruption */
  102. in_corruption = 0;
  103. printf ( "to offset %#lx", offset );
  104. }
  105. }
  106. if ( in_corruption != 0 ) {
  107. printf ( "to offset %#x (end of region)\n", len-1 );
  108. }
  109. return corrupted;
  110. }
  111. /**
  112. * Maximum number of separately coloured message streams
  113. *
  114. * Six is the realistic maximum; there are 8 basic ANSI colours, one
  115. * of which will be the terminal default and one of which will be
  116. * invisible on the terminal because it matches the background colour.
  117. */
  118. #define NUM_AUTO_COLOURS 6
  119. /** A colour assigned to an autocolourised debug message stream */
  120. struct autocolour {
  121. /** Message stream ID */
  122. unsigned long stream;
  123. /** Last recorded usage */
  124. unsigned long last_used;
  125. };
  126. /**
  127. * Choose colour index for debug autocolourisation
  128. *
  129. * @v stream Message stream ID
  130. * @ret colour Colour ID
  131. */
  132. static int dbg_autocolour ( unsigned long stream ) {
  133. static struct autocolour acs[NUM_AUTO_COLOURS];
  134. static unsigned long use;
  135. unsigned int i;
  136. unsigned int oldest;
  137. unsigned int oldest_last_used;
  138. /* Increment usage iteration counter */
  139. use++;
  140. /* Scan through list for a currently assigned colour */
  141. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  142. if ( acs[i].stream == stream ) {
  143. acs[i].last_used = use;
  144. return i;
  145. }
  146. }
  147. /* No colour found; evict the oldest from the list */
  148. oldest = 0;
  149. oldest_last_used = use;
  150. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  151. if ( acs[i].last_used < oldest_last_used ) {
  152. oldest_last_used = acs[i].last_used;
  153. oldest = i;
  154. }
  155. }
  156. acs[oldest].stream = stream;
  157. acs[oldest].last_used = use;
  158. return oldest;
  159. }
  160. /**
  161. * Select automatic colour for debug messages
  162. *
  163. * @v stream Message stream ID
  164. */
  165. void dbg_autocolourise ( unsigned long stream ) {
  166. printf ( "\033[%dm",
  167. ( stream ? ( 31 + dbg_autocolour ( stream ) ) : 0 ) );
  168. }
  169. /**
  170. * Revert to normal colour
  171. *
  172. */
  173. void dbg_decolourise ( void ) {
  174. printf ( "\033[0m" );
  175. }