14e98e3e1Schristos /* err.c --- handle errors for RX simulator. 24e98e3e1Schristos 3*1f4e7eb9Schristos Copyright (C) 2008-2024 Free Software Foundation, Inc. 44e98e3e1Schristos Contributed by Red Hat, Inc. 54e98e3e1Schristos 64e98e3e1Schristos This file is part of the GNU simulators. 74e98e3e1Schristos 84e98e3e1Schristos This program is free software; you can redistribute it and/or modify 94e98e3e1Schristos it under the terms of the GNU General Public License as published by 104e98e3e1Schristos the Free Software Foundation; either version 3 of the License, or 114e98e3e1Schristos (at your option) any later version. 124e98e3e1Schristos 134e98e3e1Schristos This program is distributed in the hope that it will be useful, 144e98e3e1Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 154e98e3e1Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 164e98e3e1Schristos GNU General Public License for more details. 174e98e3e1Schristos 184e98e3e1Schristos You should have received a copy of the GNU General Public License 194e98e3e1Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 204e98e3e1Schristos 214b169a6bSchristos /* This must come before any other includes. */ 224b169a6bSchristos #include "defs.h" 234b169a6bSchristos 244e98e3e1Schristos #include <stdio.h> 254e98e3e1Schristos #include <stdlib.h> 264e98e3e1Schristos 274e98e3e1Schristos #include "err.h" 284e98e3e1Schristos 294e98e3e1Schristos static unsigned char ee_actions[SIM_ERR_NUM_ERRORS]; 304e98e3e1Schristos 314e98e3e1Schristos static enum execution_error last_error; 324e98e3e1Schristos 334e98e3e1Schristos static void 344b169a6bSchristos ee_overrides (void) 354e98e3e1Schristos { 364e98e3e1Schristos /* GCC may initialize a bitfield by reading the uninitialized byte, 374e98e3e1Schristos masking in the bitfield, and writing the byte back out. */ 384e98e3e1Schristos ee_actions[SIM_ERR_READ_UNWRITTEN_BYTES] = SIM_ERRACTION_IGNORE; 394e98e3e1Schristos /* This breaks stack unwinding for exceptions because it leaves 404e98e3e1Schristos MC_PUSHED_PC tags in the unwound stack frames. */ 414e98e3e1Schristos ee_actions[SIM_ERR_CORRUPT_STACK] = SIM_ERRACTION_IGNORE; 424e98e3e1Schristos } 434e98e3e1Schristos 444e98e3e1Schristos void 454e98e3e1Schristos execution_error_init_standalone (void) 464e98e3e1Schristos { 474e98e3e1Schristos int i; 484e98e3e1Schristos 494e98e3e1Schristos for (i = 0; i < SIM_ERR_NUM_ERRORS; i++) 504e98e3e1Schristos ee_actions[i] = SIM_ERRACTION_EXIT; 514e98e3e1Schristos 524e98e3e1Schristos ee_overrides (); 534e98e3e1Schristos } 544e98e3e1Schristos 554e98e3e1Schristos void 564e98e3e1Schristos execution_error_init_debugger (void) 574e98e3e1Schristos { 584e98e3e1Schristos int i; 594e98e3e1Schristos 604e98e3e1Schristos for (i = 0; i < SIM_ERR_NUM_ERRORS; i++) 614e98e3e1Schristos ee_actions[i] = SIM_ERRACTION_DEBUG; 624e98e3e1Schristos 634e98e3e1Schristos ee_overrides (); 644e98e3e1Schristos } 654e98e3e1Schristos 664e98e3e1Schristos void 674e98e3e1Schristos execution_error_warn_all (void) 684e98e3e1Schristos { 694e98e3e1Schristos int i; 704e98e3e1Schristos 714e98e3e1Schristos for (i = 0; i < SIM_ERR_NUM_ERRORS; i++) 724e98e3e1Schristos ee_actions[i] = SIM_ERRACTION_WARN; 734e98e3e1Schristos } 744e98e3e1Schristos 754e98e3e1Schristos void 764e98e3e1Schristos execution_error_ignore_all (void) 774e98e3e1Schristos { 784e98e3e1Schristos int i; 794e98e3e1Schristos 804e98e3e1Schristos for (i = 0; i < SIM_ERR_NUM_ERRORS; i++) 814e98e3e1Schristos ee_actions[i] = SIM_ERRACTION_IGNORE; 824e98e3e1Schristos } 834e98e3e1Schristos 844e98e3e1Schristos void 854e98e3e1Schristos execution_error (enum execution_error num, unsigned long address) 864e98e3e1Schristos { 874e98e3e1Schristos if (ee_actions[num] != SIM_ERRACTION_IGNORE) 884e98e3e1Schristos last_error = num; 894e98e3e1Schristos 904e98e3e1Schristos if (ee_actions[num] == SIM_ERRACTION_EXIT 914e98e3e1Schristos || ee_actions[num] == SIM_ERRACTION_WARN) 924e98e3e1Schristos { 934e98e3e1Schristos switch (num) 944e98e3e1Schristos { 954e98e3e1Schristos case SIM_ERR_READ_UNWRITTEN_PAGES: 964e98e3e1Schristos case SIM_ERR_READ_UNWRITTEN_BYTES: 974e98e3e1Schristos printf("Read from unwritten memory at 0x%lx\n", address); 984e98e3e1Schristos break; 994e98e3e1Schristos 1004e98e3e1Schristos case SIM_ERR_NULL_POINTER_DEREFERENCE: 1014e98e3e1Schristos printf ("NULL pointer dereference\n"); 1024e98e3e1Schristos break; 1034e98e3e1Schristos 1044e98e3e1Schristos case SIM_ERR_CORRUPT_STACK: 1054e98e3e1Schristos printf ("Stack corruption detected at 0x%lx\n", address); 1064e98e3e1Schristos break; 1074e98e3e1Schristos 1084e98e3e1Schristos default: 1094e98e3e1Schristos printf ("Unknown execution error %d\n", num); 1104e98e3e1Schristos exit (1); 1114e98e3e1Schristos } 1124e98e3e1Schristos } 1134e98e3e1Schristos 1144e98e3e1Schristos if (ee_actions[num] == SIM_ERRACTION_EXIT) 1154e98e3e1Schristos exit (1); 1164e98e3e1Schristos } 1174e98e3e1Schristos 1184e98e3e1Schristos enum execution_error 1194e98e3e1Schristos execution_error_get_last_error (void) 1204e98e3e1Schristos { 1214e98e3e1Schristos return last_error; 1224e98e3e1Schristos } 1234e98e3e1Schristos 1244e98e3e1Schristos void 1254e98e3e1Schristos execution_error_clear_last_error (void) 1264e98e3e1Schristos { 1274e98e3e1Schristos last_error = SIM_ERR_NONE; 1284e98e3e1Schristos } 1294e98e3e1Schristos 1304e98e3e1Schristos void 1314e98e3e1Schristos execution_error_set_action (enum execution_error num, enum execution_error_action act) 1324e98e3e1Schristos { 1334e98e3e1Schristos ee_actions[num] = act; 1344e98e3e1Schristos } 135