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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <stdarg.h>
  23. #include <ipxe/console.h>
  24. /**
  25. * Print debug message
  26. *
  27. * @v fmt Format string
  28. * @v ... Arguments
  29. */
  30. void dbg_printf ( const char *fmt, ... ) {
  31. int saved_usage;
  32. va_list args;
  33. /* Mark console as in use for debugging messages */
  34. saved_usage = console_set_usage ( CONSOLE_USAGE_DEBUG );
  35. /* Print message */
  36. va_start ( args, fmt );
  37. vprintf ( fmt, args );
  38. va_end ( args );
  39. /* Restore console usage */
  40. console_set_usage ( saved_usage );
  41. }
  42. /**
  43. * Pause until a key is pressed
  44. *
  45. */
  46. void dbg_pause ( void ) {
  47. dbg_printf ( "\nPress a key..." );
  48. getchar();
  49. dbg_printf ( "\r \r" );
  50. }
  51. /**
  52. * Indicate more data to follow and pause until a key is pressed
  53. *
  54. */
  55. void dbg_more ( void ) {
  56. dbg_printf ( "---more---" );
  57. getchar();
  58. dbg_printf ( "\r \r" );
  59. }
  60. /**
  61. * Print row of a hex dump with specified display address
  62. *
  63. * @v dispaddr Display address
  64. * @v data Data to print
  65. * @v len Length of data
  66. * @v offset Starting offset within data
  67. */
  68. static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
  69. unsigned long len, unsigned int offset ) {
  70. const uint8_t *bytes = data;
  71. unsigned int i;
  72. uint8_t byte;
  73. dbg_printf ( "%08lx :", ( dispaddr + offset ) );
  74. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  75. if ( i >= len ) {
  76. dbg_printf ( " " );
  77. continue;
  78. }
  79. dbg_printf ( "%c%02x",
  80. ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
  81. }
  82. dbg_printf ( " : " );
  83. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  84. if ( i >= len ) {
  85. dbg_printf ( " " );
  86. continue;
  87. }
  88. byte = bytes[i];
  89. if ( ( byte < 0x20 ) || ( byte >= 0x7f ) )
  90. byte = '.';
  91. dbg_printf ( "%c", byte );
  92. }
  93. dbg_printf ( "\n" );
  94. }
  95. /**
  96. * Print hex dump with specified display address
  97. *
  98. * @v dispaddr Display address
  99. * @v data Data to print
  100. * @v len Length of data
  101. */
  102. void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
  103. unsigned long len ) {
  104. unsigned int offset;
  105. for ( offset = 0 ; offset < len ; offset += 16 ) {
  106. dbg_hex_dump_da_row ( dispaddr, data, len, offset );
  107. }
  108. }
  109. /**
  110. * Maximum number of separately coloured message streams
  111. *
  112. * Six is the realistic maximum; there are 8 basic ANSI colours, one
  113. * of which will be the terminal default and one of which will be
  114. * invisible on the terminal because it matches the background colour.
  115. */
  116. #define NUM_AUTO_COLOURS 6
  117. /** A colour assigned to an autocolourised debug message stream */
  118. struct autocolour {
  119. /** Message stream ID */
  120. unsigned long stream;
  121. /** Last recorded usage */
  122. unsigned long last_used;
  123. };
  124. /**
  125. * Choose colour index for debug autocolourisation
  126. *
  127. * @v stream Message stream ID
  128. * @ret colour Colour ID
  129. */
  130. static int dbg_autocolour ( unsigned long stream ) {
  131. static struct autocolour acs[NUM_AUTO_COLOURS];
  132. static unsigned long use;
  133. unsigned int i;
  134. unsigned int oldest;
  135. unsigned int oldest_last_used;
  136. /* Increment usage iteration counter */
  137. use++;
  138. /* Scan through list for a currently assigned colour */
  139. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  140. if ( acs[i].stream == stream ) {
  141. acs[i].last_used = use;
  142. return i;
  143. }
  144. }
  145. /* No colour found; evict the oldest from the list */
  146. oldest = 0;
  147. oldest_last_used = use;
  148. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  149. if ( acs[i].last_used < oldest_last_used ) {
  150. oldest_last_used = acs[i].last_used;
  151. oldest = i;
  152. }
  153. }
  154. acs[oldest].stream = stream;
  155. acs[oldest].last_used = use;
  156. return oldest;
  157. }
  158. /**
  159. * Select automatic colour for debug messages
  160. *
  161. * @v stream Message stream ID
  162. */
  163. void dbg_autocolourise ( unsigned long stream ) {
  164. dbg_printf ( "\033[%dm",
  165. ( stream ? ( 31 + dbg_autocolour ( stream ) ) : 0 ) );
  166. }
  167. /**
  168. * Revert to normal colour
  169. *
  170. */
  171. void dbg_decolourise ( void ) {
  172. dbg_printf ( "\033[0m" );
  173. }