您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

debug.c 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stdint.h>
  2. #include <io.h>
  3. #include <console.h>
  4. void pause ( void ) {
  5. printf ( "\nPress a key" );
  6. getchar();
  7. printf ( "\r \r" );
  8. }
  9. void more ( void ) {
  10. printf ( "---more---" );
  11. getchar();
  12. printf ( "\r \r" );
  13. }
  14. /* Produce a paged hex dump of the specified data and length */
  15. void hex_dump ( const unsigned char *data, const unsigned int len ) {
  16. unsigned int index;
  17. for ( index = 0; index < len; index++ ) {
  18. if ( ( index % 16 ) == 0 ) {
  19. printf ( "\n" );
  20. }
  21. if ( ( index % 368 ) == 352 ) {
  22. more();
  23. }
  24. if ( ( index % 16 ) == 0 ) {
  25. printf ( "%p [%lx] : %04x :", data + index,
  26. virt_to_phys ( data + index ), index );
  27. }
  28. printf ( " %02x", data[index] );
  29. }
  30. printf ( "\n" );
  31. }
  32. #define GUARD_SYMBOL ( ( 'M' << 24 ) | ( 'I' << 16 ) | ( 'N' << 8 ) | 'E' )
  33. /* Fill a region with guard markers. We use a 4-byte pattern to make
  34. * it less likely that check_region will find spurious 1-byte regions
  35. * of non-corruption.
  36. */
  37. void guard_region ( void *region, size_t len ) {
  38. uint32_t offset = 0;
  39. len &= ~0x03;
  40. for ( offset = 0; offset < len ; offset += 4 ) {
  41. *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  42. }
  43. }
  44. /* Check a region that has been guarded with guard_region() for
  45. * corruption.
  46. */
  47. int check_region ( void *region, size_t len ) {
  48. uint8_t corrupted = 0;
  49. uint8_t in_corruption = 0;
  50. uint32_t offset = 0;
  51. uint32_t test = 0;
  52. len &= ~0x03;
  53. for ( offset = 0; offset < len ; offset += 4 ) {
  54. test = *((uint32_t *)(region + offset)) = GUARD_SYMBOL;
  55. if ( ( in_corruption == 0 ) &&
  56. ( test != GUARD_SYMBOL ) ) {
  57. /* Start of corruption */
  58. if ( corrupted == 0 ) {
  59. corrupted = 1;
  60. printf ( "Region %p-%p (physical %#lx-%#lx) "
  61. "corrupted\n",
  62. region, region + len,
  63. virt_to_phys ( region ),
  64. virt_to_phys ( region + len ) );
  65. }
  66. in_corruption = 1;
  67. printf ( "--- offset %#lx ", offset );
  68. } else if ( ( in_corruption != 0 ) &&
  69. ( test == GUARD_SYMBOL ) ) {
  70. /* End of corruption */
  71. in_corruption = 0;
  72. printf ( "to offset %#lx", offset );
  73. }
  74. }
  75. if ( in_corruption != 0 ) {
  76. printf ( "to offset %#x (end of region)\n", len-1 );
  77. }
  78. return corrupted;
  79. }