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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdarg.h>
  4. #include <ipxe/io.h>
  5. #include <console.h>
  6. /**
  7. * Pause until a key is pressed
  8. *
  9. */
  10. void dbg_pause ( void ) {
  11. printf ( "\nPress a key..." );
  12. getchar();
  13. printf ( "\r \r" );
  14. }
  15. /**
  16. * Indicate more data to follow and pause until a key is pressed
  17. *
  18. */
  19. void dbg_more ( void ) {
  20. printf ( "---more---" );
  21. getchar();
  22. printf ( "\r \r" );
  23. }
  24. /**
  25. * Print row of a hex dump with specified display address
  26. *
  27. * @v dispaddr Display address
  28. * @v data Data to print
  29. * @v len Length of data
  30. * @v offset Starting offset within data
  31. */
  32. static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
  33. unsigned long len, unsigned int offset ) {
  34. const uint8_t *bytes = data;
  35. unsigned int i;
  36. uint8_t byte;
  37. printf ( "%08lx :", ( dispaddr + offset ) );
  38. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  39. if ( i >= len ) {
  40. printf ( " " );
  41. continue;
  42. }
  43. printf ( "%c%02x",
  44. ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
  45. }
  46. printf ( " : " );
  47. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  48. if ( i >= len ) {
  49. printf ( " " );
  50. continue;
  51. }
  52. byte = bytes[i];
  53. if ( ( byte < 0x20 ) || ( byte >= 0x7f ) )
  54. byte = '.';
  55. printf ( "%c", byte );
  56. }
  57. printf ( "\n" );
  58. }
  59. /**
  60. * Print hex dump with specified display address
  61. *
  62. * @v dispaddr Display address
  63. * @v data Data to print
  64. * @v len Length of data
  65. */
  66. void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
  67. unsigned long len ) {
  68. unsigned int offset;
  69. for ( offset = 0 ; offset < len ; offset += 16 ) {
  70. dbg_hex_dump_da_row ( dispaddr, data, len, offset );
  71. }
  72. }
  73. /**
  74. * Maximum number of separately coloured message streams
  75. *
  76. * Six is the realistic maximum; there are 8 basic ANSI colours, one
  77. * of which will be the terminal default and one of which will be
  78. * invisible on the terminal because it matches the background colour.
  79. */
  80. #define NUM_AUTO_COLOURS 6
  81. /** A colour assigned to an autocolourised debug message stream */
  82. struct autocolour {
  83. /** Message stream ID */
  84. unsigned long stream;
  85. /** Last recorded usage */
  86. unsigned long last_used;
  87. };
  88. /**
  89. * Choose colour index for debug autocolourisation
  90. *
  91. * @v stream Message stream ID
  92. * @ret colour Colour ID
  93. */
  94. static int dbg_autocolour ( unsigned long stream ) {
  95. static struct autocolour acs[NUM_AUTO_COLOURS];
  96. static unsigned long use;
  97. unsigned int i;
  98. unsigned int oldest;
  99. unsigned int oldest_last_used;
  100. /* Increment usage iteration counter */
  101. use++;
  102. /* Scan through list for a currently assigned colour */
  103. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  104. if ( acs[i].stream == stream ) {
  105. acs[i].last_used = use;
  106. return i;
  107. }
  108. }
  109. /* No colour found; evict the oldest from the list */
  110. oldest = 0;
  111. oldest_last_used = use;
  112. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  113. if ( acs[i].last_used < oldest_last_used ) {
  114. oldest_last_used = acs[i].last_used;
  115. oldest = i;
  116. }
  117. }
  118. acs[oldest].stream = stream;
  119. acs[oldest].last_used = use;
  120. return oldest;
  121. }
  122. /**
  123. * Select automatic colour for debug messages
  124. *
  125. * @v stream Message stream ID
  126. */
  127. void dbg_autocolourise ( unsigned long stream ) {
  128. printf ( "\033[%dm",
  129. ( stream ? ( 31 + dbg_autocolour ( stream ) ) : 0 ) );
  130. }
  131. /**
  132. * Revert to normal colour
  133. *
  134. */
  135. void dbg_decolourise ( void ) {
  136. printf ( "\033[0m" );
  137. }