Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

debug.c 4.6KB

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