Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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., 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 <ctype.h>
  24. #include <ipxe/console.h>
  25. /**
  26. * Print debug message
  27. *
  28. * @v fmt Format string
  29. * @v ... Arguments
  30. */
  31. void dbg_printf ( const char *fmt, ... ) {
  32. int saved_usage;
  33. va_list args;
  34. /* Mark console as in use for debugging messages */
  35. saved_usage = console_set_usage ( CONSOLE_USAGE_DEBUG );
  36. /* Print message */
  37. va_start ( args, fmt );
  38. vprintf ( fmt, args );
  39. va_end ( args );
  40. /* Restore console usage */
  41. console_set_usage ( saved_usage );
  42. }
  43. /**
  44. * Pause until a key is pressed
  45. *
  46. */
  47. void dbg_pause ( void ) {
  48. dbg_printf ( "\nPress a key..." );
  49. getchar();
  50. dbg_printf ( "\r \r" );
  51. }
  52. /**
  53. * Indicate more data to follow and pause until a key is pressed
  54. *
  55. */
  56. void dbg_more ( void ) {
  57. dbg_printf ( "---more---" );
  58. getchar();
  59. dbg_printf ( "\r \r" );
  60. }
  61. /**
  62. * Print row of a hex dump with specified display address
  63. *
  64. * @v dispaddr Display address
  65. * @v data Data to print
  66. * @v len Length of data
  67. * @v offset Starting offset within data
  68. */
  69. static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
  70. unsigned long len, unsigned int offset ) {
  71. const uint8_t *bytes = data;
  72. unsigned int i;
  73. uint8_t byte;
  74. dbg_printf ( "%08lx :", ( dispaddr + offset ) );
  75. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  76. if ( i >= len ) {
  77. dbg_printf ( " " );
  78. continue;
  79. }
  80. dbg_printf ( "%c%02x",
  81. ( ( ( i % 16 ) == 8 ) ? '-' : ' ' ), bytes[i] );
  82. }
  83. dbg_printf ( " : " );
  84. for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
  85. if ( i >= len ) {
  86. dbg_printf ( " " );
  87. continue;
  88. }
  89. byte = bytes[i];
  90. dbg_printf ( "%c", ( isprint ( byte ) ? 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. }