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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdarg.h>
  22. #include <ipxe/console.h>
  23. /**
  24. * Print debug message
  25. *
  26. * @v fmt Format string
  27. * @v ... Arguments
  28. */
  29. void dbg_printf ( const char *fmt, ... ) {
  30. int saved_usage;
  31. va_list args;
  32. /* Mark console as in use for debugging messages */
  33. saved_usage = console_set_usage ( CONSOLE_USAGE_DEBUG );
  34. /* Print message */
  35. va_start ( args, fmt );
  36. vprintf ( fmt, args );
  37. va_end ( args );
  38. /* Restore console usage */
  39. console_set_usage ( saved_usage );
  40. }
  41. /**
  42. * Pause until a key is pressed
  43. *
  44. */
  45. void dbg_pause ( void ) {
  46. dbg_printf ( "\nPress a key..." );
  47. getchar();
  48. dbg_printf ( "\r \r" );
  49. }
  50. /**
  51. * Indicate more data to follow and pause until a key is pressed
  52. *
  53. */
  54. void dbg_more ( void ) {
  55. dbg_printf ( "---more---" );
  56. getchar();
  57. dbg_printf ( "\r \r" );
  58. }
  59. /**
  60. * Print row of a 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. * @v offset Starting offset within data
  66. */
  67. static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
  68. unsigned long len, unsigned int offset ) {
  69. const uint8_t *bytes = data;
  70. unsigned int i;
  71. uint8_t byte;
  72. dbg_printf ( "%08lx :", ( dispaddr + offset ) );
  73. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  74. if ( i >= len ) {
  75. dbg_printf ( " " );
  76. continue;
  77. }
  78. dbg_printf ( "%c%02x",
  79. ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
  80. }
  81. dbg_printf ( " : " );
  82. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  83. if ( i >= len ) {
  84. dbg_printf ( " " );
  85. continue;
  86. }
  87. byte = bytes[i];
  88. if ( ( byte < 0x20 ) || ( byte >= 0x7f ) )
  89. byte = '.';
  90. dbg_printf ( "%c", byte );
  91. }
  92. dbg_printf ( "\n" );
  93. }
  94. /**
  95. * Print hex dump with specified display address
  96. *
  97. * @v dispaddr Display address
  98. * @v data Data to print
  99. * @v len Length of data
  100. */
  101. void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
  102. unsigned long len ) {
  103. unsigned int offset;
  104. for ( offset = 0 ; offset < len ; offset += 16 ) {
  105. dbg_hex_dump_da_row ( dispaddr, data, len, offset );
  106. }
  107. }
  108. /**
  109. * Maximum number of separately coloured message streams
  110. *
  111. * Six is the realistic maximum; there are 8 basic ANSI colours, one
  112. * of which will be the terminal default and one of which will be
  113. * invisible on the terminal because it matches the background colour.
  114. */
  115. #define NUM_AUTO_COLOURS 6
  116. /** A colour assigned to an autocolourised debug message stream */
  117. struct autocolour {
  118. /** Message stream ID */
  119. unsigned long stream;
  120. /** Last recorded usage */
  121. unsigned long last_used;
  122. };
  123. /**
  124. * Choose colour index for debug autocolourisation
  125. *
  126. * @v stream Message stream ID
  127. * @ret colour Colour ID
  128. */
  129. static int dbg_autocolour ( unsigned long stream ) {
  130. static struct autocolour acs[NUM_AUTO_COLOURS];
  131. static unsigned long use;
  132. unsigned int i;
  133. unsigned int oldest;
  134. unsigned int oldest_last_used;
  135. /* Increment usage iteration counter */
  136. use++;
  137. /* Scan through list for a currently assigned colour */
  138. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  139. if ( acs[i].stream == stream ) {
  140. acs[i].last_used = use;
  141. return i;
  142. }
  143. }
  144. /* No colour found; evict the oldest from the list */
  145. oldest = 0;
  146. oldest_last_used = use;
  147. for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
  148. if ( acs[i].last_used < oldest_last_used ) {
  149. oldest_last_used = acs[i].last_used;
  150. oldest = i;
  151. }
  152. }
  153. acs[oldest].stream = stream;
  154. acs[oldest].last_used = use;
  155. return oldest;
  156. }
  157. /**
  158. * Select automatic colour for debug messages
  159. *
  160. * @v stream Message stream ID
  161. */
  162. void dbg_autocolourise ( unsigned long stream ) {
  163. dbg_printf ( "\033[%dm",
  164. ( stream ? ( 31 + dbg_autocolour ( stream ) ) : 0 ) );
  165. }
  166. /**
  167. * Revert to normal colour
  168. *
  169. */
  170. void dbg_decolourise ( void ) {
  171. dbg_printf ( "\033[0m" );
  172. }