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.6KB

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