1 /* Dump registers.
2    Copyright (C) 2000-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <sgidefs.h>
20 #include <sys/uio.h>
21 #include <_itoa.h>
22 
23 #if _MIPS_SIM == _ABIO32
24 # define CTX_TYPE	struct sigcontext *
25 # define CTX_REG(ctx, i)	((ctx)->sc_regs[(i)])
26 # define CTX_PC(ctx)	((ctx)->sc_pc)
27 # define CTX_MDHI(ctx)	((ctx)->sc_mdhi)
28 # define CTX_MDLO(ctx)	((ctx)->sc_mdlo)
29 # define REG_HEX_SIZE	8
30 #else
31 # define CTX_TYPE	ucontext_t *
32 # define CTX_REG(ctx, i)	((ctx)->uc_mcontext.gregs[(i)])
33 # define CTX_PC(ctx)	((ctx)->uc_mcontext.pc)
34 # define CTX_MDHI(ctx)	((ctx)->uc_mcontext.mdhi)
35 # define CTX_MDLO(ctx)	((ctx)->uc_mcontext.mdhi)
36 # define REG_HEX_SIZE	16
37 #endif
38 
39 /* We will print the register dump in this format:
40 
41  R0   XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
42  R8   XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
43  R16  XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
44  R24  XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
45             pc       lo       hi
46       XXXXXXXX XXXXXXXX XXXXXXXX
47  The FPU registers will not be printed.
48 */
49 
50 static void
hexvalue(_ITOA_WORD_TYPE value,char * buf,size_t len)51 hexvalue (_ITOA_WORD_TYPE value, char *buf, size_t len)
52 {
53   char *cp = _itoa_word (value, buf + len, 16, 0);
54   while (cp > buf)
55     *--cp = '0';
56 }
57 
58 static void
register_dump(int fd,CTX_TYPE ctx)59 register_dump (int fd, CTX_TYPE ctx)
60 {
61   char regs[38][REG_HEX_SIZE];
62   struct iovec iov[38 * 2 + 10];
63   size_t nr = 0;
64   int i;
65 
66 #define ADD_STRING(str) \
67   iov[nr].iov_base = (char *) str;					      \
68   iov[nr].iov_len = strlen (str);					      \
69   ++nr
70 #define ADD_MEM(str, len) \
71   iov[nr].iov_base = str;						      \
72   iov[nr].iov_len = len;						      \
73   ++nr
74 
75   /* Generate strings of register contents.  */
76   for (i = 0; i < 32; i++)
77     hexvalue (CTX_REG (ctx, i), regs[i], REG_HEX_SIZE);
78   hexvalue (CTX_PC (ctx), regs[32], REG_HEX_SIZE);
79   hexvalue (CTX_MDHI (ctx), regs[33], REG_HEX_SIZE);
80   hexvalue (CTX_MDLO (ctx), regs[34], REG_HEX_SIZE);
81 
82   /* Generate the output.  */
83   ADD_STRING ("Register dump:\n\n R0   ");
84   for (i = 0; i < 8; i++)
85     {
86       ADD_MEM (regs[i], REG_HEX_SIZE);
87       ADD_STRING (" ");
88     }
89   ADD_STRING ("\n R8   ");
90   for (i = 8; i < 16; i++)
91     {
92       ADD_MEM (regs[i], REG_HEX_SIZE);
93       ADD_STRING (" ");
94     }
95   ADD_STRING ("\n R16  ");
96   for (i = 16; i < 24; i++)
97     {
98       ADD_MEM (regs[i], REG_HEX_SIZE);
99       ADD_STRING (" ");
100     }
101   ADD_STRING ("\n R24  ");
102   for (i = 24; i < 32; i++)
103     {
104       ADD_MEM (regs[i], REG_HEX_SIZE);
105       ADD_STRING (" ");
106     }
107   ADD_STRING ("\n            pc       lo       hi\n      ");
108   for (i = 32; i < 35; i++)
109     {
110       ADD_MEM (regs[i], REG_HEX_SIZE);
111       ADD_STRING (" ");
112     }
113   ADD_STRING ("\n");
114 
115   /* Write the stuff out.  */
116   writev (fd, iov, nr);
117 }
118 
119 
120 #define REGISTER_DUMP register_dump (fd, ctx)
121