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.1KB

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