err.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* err.c --- handle errors for RX simulator.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of the GNU simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include "err.h"
  20. static unsigned char ee_actions[SIM_ERR_NUM_ERRORS];
  21. static enum execution_error last_error;
  22. static void
  23. ee_overrides (void)
  24. {
  25. /* GCC may initialize a bitfield by reading the uninitialized byte,
  26. masking in the bitfield, and writing the byte back out. */
  27. ee_actions[SIM_ERR_READ_UNWRITTEN_BYTES] = SIM_ERRACTION_IGNORE;
  28. /* This breaks stack unwinding for exceptions because it leaves
  29. MC_PUSHED_PC tags in the unwound stack frames. */
  30. ee_actions[SIM_ERR_CORRUPT_STACK] = SIM_ERRACTION_IGNORE;
  31. }
  32. void
  33. execution_error_init_standalone (void)
  34. {
  35. int i;
  36. for (i = 0; i < SIM_ERR_NUM_ERRORS; i++)
  37. ee_actions[i] = SIM_ERRACTION_EXIT;
  38. ee_overrides ();
  39. }
  40. void
  41. execution_error_init_debugger (void)
  42. {
  43. int i;
  44. for (i = 0; i < SIM_ERR_NUM_ERRORS; i++)
  45. ee_actions[i] = SIM_ERRACTION_DEBUG;
  46. ee_overrides ();
  47. }
  48. void
  49. execution_error_warn_all (void)
  50. {
  51. int i;
  52. for (i = 0; i < SIM_ERR_NUM_ERRORS; i++)
  53. ee_actions[i] = SIM_ERRACTION_WARN;
  54. }
  55. void
  56. execution_error_ignore_all (void)
  57. {
  58. int i;
  59. for (i = 0; i < SIM_ERR_NUM_ERRORS; i++)
  60. ee_actions[i] = SIM_ERRACTION_IGNORE;
  61. }
  62. void
  63. execution_error (enum execution_error num, unsigned long address)
  64. {
  65. if (ee_actions[num] != SIM_ERRACTION_IGNORE)
  66. last_error = num;
  67. if (ee_actions[num] == SIM_ERRACTION_EXIT
  68. || ee_actions[num] == SIM_ERRACTION_WARN)
  69. {
  70. switch (num)
  71. {
  72. case SIM_ERR_READ_UNWRITTEN_PAGES:
  73. case SIM_ERR_READ_UNWRITTEN_BYTES:
  74. printf("Read from unwritten memory at 0x%lx\n", address);
  75. break;
  76. case SIM_ERR_NULL_POINTER_DEREFERENCE:
  77. printf ("NULL pointer dereference\n");
  78. break;
  79. case SIM_ERR_CORRUPT_STACK:
  80. printf ("Stack corruption detected at 0x%lx\n", address);
  81. break;
  82. default:
  83. printf ("Unknown execution error %d\n", num);
  84. exit (1);
  85. }
  86. }
  87. if (ee_actions[num] == SIM_ERRACTION_EXIT)
  88. exit (1);
  89. }
  90. enum execution_error
  91. execution_error_get_last_error (void)
  92. {
  93. return last_error;
  94. }
  95. void
  96. execution_error_clear_last_error (void)
  97. {
  98. last_error = SIM_ERR_NONE;
  99. }
  100. void
  101. execution_error_set_action (enum execution_error num, enum execution_error_action act)
  102. {
  103. ee_actions[num] = act;
  104. }