Updated email mdc's email address
[gpxe.git] / src / core / debug.c
blob4754bfdeef2769615e4734e009d44f262e6ae0e4
1 #include <stdint.h>
2 #include <stdarg.h>
3 #include <io.h>
4 #include <console.h>
6 void pause ( void ) {
7 printf ( "\nPress a key" );
8 getchar();
9 printf ( "\r \r" );
12 void more ( void ) {
13 printf ( "---more---" );
14 getchar();
15 printf ( "\r \r" );
18 /**
19 * Print row of a hex dump with specified display address
21 * @v dispaddr Display address
22 * @v data Data to print
23 * @v len Length of data
24 * @v offset Starting offset within data
26 static void dbg_hex_dump_da_row ( unsigned long dispaddr, const void *data,
27 unsigned long len, unsigned int offset ) {
28 const uint8_t *bytes = data;
29 unsigned int i;
30 uint8_t byte;
32 printf ( "%08lx :", ( dispaddr + offset ) );
33 for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
34 if ( i >= len ) {
35 printf ( " " );
36 continue;
38 printf ( " %02x", bytes[i] );
40 printf ( " : " );
41 for ( i = offset ; i < ( offset + 16 ) ; i++ ) {
42 if ( i >= len ) {
43 printf ( " " );
44 continue;
46 byte = bytes[i];
47 if ( ( byte < 0x20 ) || ( byte >= 0x7f ) )
48 byte = '.';
49 printf ( "%c", byte );
51 printf ( "\n" );
54 /**
55 * Print hex dump with specified display address
57 * @v dispaddr Display address
58 * @v data Data to print
59 * @v len Length of data
61 void dbg_hex_dump_da ( unsigned long dispaddr, const void *data,
62 unsigned long len ) {
63 unsigned int offset;
65 for ( offset = 0 ; offset < len ; offset += 16 ) {
66 dbg_hex_dump_da_row ( dispaddr, data, len, offset );
70 #define GUARD_SYMBOL ( ( 'M' << 24 ) | ( 'I' << 16 ) | ( 'N' << 8 ) | 'E' )
71 /* Fill a region with guard markers. We use a 4-byte pattern to make
72 * it less likely that check_region will find spurious 1-byte regions
73 * of non-corruption.
75 void guard_region ( void *region, size_t len ) {
76 uint32_t offset = 0;
78 len &= ~0x03;
79 for ( offset = 0; offset < len ; offset += 4 ) {
80 *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
84 /* Check a region that has been guarded with guard_region() for
85 * corruption.
87 int check_region ( void *region, size_t len ) {
88 uint8_t corrupted = 0;
89 uint8_t in_corruption = 0;
90 uint32_t offset = 0;
91 uint32_t test = 0;
93 len &= ~0x03;
94 for ( offset = 0; offset < len ; offset += 4 ) {
95 test = *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
96 if ( ( in_corruption == 0 ) &&
97 ( test != GUARD_SYMBOL ) ) {
98 /* Start of corruption */
99 if ( corrupted == 0 ) {
100 corrupted = 1;
101 printf ( "Region %p-%p (physical %#lx-%#lx) "
102 "corrupted\n",
103 region, region + len,
104 virt_to_phys ( region ),
105 virt_to_phys ( region + len ) );
107 in_corruption = 1;
108 printf ( "--- offset %#lx ", offset );
109 } else if ( ( in_corruption != 0 ) &&
110 ( test == GUARD_SYMBOL ) ) {
111 /* End of corruption */
112 in_corruption = 0;
113 printf ( "to offset %#lx", offset );
117 if ( in_corruption != 0 ) {
118 printf ( "to offset %#x (end of region)\n", len-1 );
120 return corrupted;
124 * Maximum number of separately coloured message streams
126 * Six is the realistic maximum; there are 8 basic ANSI colours, one
127 * of which will be the terminal default and one of which will be
128 * invisible on the terminal because it matches the background colour.
130 #define NUM_AUTO_COLOURS 6
132 /** A colour assigned to an autocolourised debug message stream */
133 struct autocolour {
134 /** Message stream ID */
135 unsigned long stream;
136 /** Last recorded usage */
137 unsigned long last_used;
141 * Choose colour index for debug autocolourisation
143 * @v stream Message stream ID
144 * @ret colour Colour ID
146 static int dbg_autocolour ( unsigned long stream ) {
147 static struct autocolour acs[NUM_AUTO_COLOURS];
148 static unsigned long use;
149 unsigned int i;
150 unsigned int oldest;
151 unsigned int oldest_last_used;
153 /* Increment usage iteration counter */
154 use++;
156 /* Scan through list for a currently assigned colour */
157 for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
158 if ( acs[i].stream == stream ) {
159 acs[i].last_used = use;
160 return i;
164 /* No colour found; evict the oldest from the list */
165 oldest = 0;
166 oldest_last_used = use;
167 for ( i = 0 ; i < ( sizeof ( acs ) / sizeof ( acs[0] ) ) ; i++ ) {
168 if ( acs[i].last_used < oldest_last_used ) {
169 oldest_last_used = acs[i].last_used;
170 oldest = i;
173 acs[oldest].stream = stream;
174 acs[oldest].last_used = use;
175 return oldest;
179 * Select automatic colour for debug messages
181 * @v stream Message stream ID
183 void dbg_autocolourise ( unsigned long stream ) {
184 printf ( "\033[%dm",
185 ( stream ? ( 31 + dbg_autocolour ( stream ) ) : 0 ) );
189 * Revert to normal colour
192 void dbg_decolourise ( void ) {
193 printf ( "\033[0m" );