Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

debug.c 4.1KB

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