testsuite: skip confirmation in 'gdb_reinitialize_dir'
[binutils-gdb.git] / libsframe / sframe-dump.c
blob69633d53a33a859e607afe65873b6b7ceb4dd9cb
1 /* sframe-dump.c - Textual dump of .sframe.
3 Copyright (C) 2022-2024 Free Software Foundation, Inc.
5 This file is part of libsframe.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24 #include "sframe-impl.h"
26 #define SFRAME_HEADER_FLAGS_STR_MAX_LEN 50
28 /* Return TRUE if the SFrame section is associated with the aarch64 ABIs. */
30 static bool
31 is_sframe_abi_arch_aarch64 (sframe_decoder_ctx *sfd_ctx)
33 bool aarch64_p = false;
35 uint8_t abi_arch = sframe_decoder_get_abi_arch (sfd_ctx);
36 if (abi_arch == SFRAME_ABI_AARCH64_ENDIAN_BIG
37 || abi_arch == SFRAME_ABI_AARCH64_ENDIAN_LITTLE)
38 aarch64_p = true;
40 return aarch64_p;
43 static void
44 dump_sframe_header (sframe_decoder_ctx *sfd_ctx)
46 uint8_t ver;
47 uint8_t flags;
48 char *flags_str;
49 const char *ver_str = NULL;
50 int8_t cfa_fixed_fp_offset;
51 int8_t cfa_fixed_ra_offset;
52 const sframe_header *header = &(sfd_ctx->sfd_header);
54 /* Prepare SFrame section version string. */
55 const char *version_names[]
56 = { "NULL",
57 "SFRAME_VERSION_1",
58 "SFRAME_VERSION_2" };
60 /* PS: Keep SFRAME_HEADER_FLAGS_STR_MAX_LEN in sync if adding more members to
61 this array. */
62 const char *flag_names[]
63 = { "SFRAME_F_FDE_SORTED",
64 "SFRAME_F_FRAME_POINTER" };
66 ver = sframe_decoder_get_version (sfd_ctx);
67 if (ver <= SFRAME_VERSION)
68 ver_str = version_names[ver];
70 /* Prepare SFrame section flags string. */
71 flags = header->sfh_preamble.sfp_flags;
72 flags_str = (char*) calloc (SFRAME_HEADER_FLAGS_STR_MAX_LEN, sizeof (char));
73 if (flags)
75 if (flags & SFRAME_F_FDE_SORTED)
76 strcpy (flags_str, flag_names[0]);
77 if (flags & SFRAME_F_FRAME_POINTER)
79 if (strlen (flags_str) > 0)
80 strcpy (flags_str, ",");
81 strcpy (flags_str, flag_names[1]);
84 else
85 strcpy (flags_str, "NONE");
87 /* CFA fixed FP and RA offsets. */
88 cfa_fixed_fp_offset = header->sfh_cfa_fixed_fp_offset;
89 cfa_fixed_ra_offset = header->sfh_cfa_fixed_ra_offset;
91 const char* subsec_name = "Header";
92 printf ("\n");
93 printf (" %s :\n", subsec_name);
94 printf ("\n");
95 printf (" Version: %s\n", ver_str);
96 printf (" Flags: %s\n", flags_str);
97 if (cfa_fixed_fp_offset != SFRAME_CFA_FIXED_FP_INVALID)
98 printf (" CFA fixed FP offset: %d\n", cfa_fixed_fp_offset);
99 if (cfa_fixed_ra_offset != SFRAME_CFA_FIXED_RA_INVALID)
100 printf (" CFA fixed RA offset: %d\n", cfa_fixed_ra_offset);
101 printf (" Num FDEs: %d\n", sframe_decoder_get_num_fidx (sfd_ctx));
102 printf (" Num FREs: %d\n", header->sfh_num_fres);
104 free (flags_str);
107 static void
108 dump_sframe_func_with_fres (sframe_decoder_ctx *sfd_ctx,
109 unsigned int funcidx,
110 uint64_t sec_addr)
112 uint32_t j = 0;
113 uint32_t num_fres = 0;
114 uint32_t func_size = 0;
115 int32_t func_start_address = 0;
116 unsigned char func_info = 0;
118 uint64_t func_start_pc_vma = 0;
119 uint64_t fre_start_pc_vma = 0;
120 const char *base_reg_str[] = {"fp", "sp"};
121 int32_t cfa_offset = 0;
122 int32_t fp_offset = 0;
123 int32_t ra_offset = 0;
124 uint8_t base_reg_id = 0;
125 int err[3] = {0, 0, 0};
127 sframe_frame_row_entry fre;
129 /* Get the SFrame function descriptor. */
130 sframe_decoder_get_funcdesc (sfd_ctx, funcidx, &num_fres,
131 &func_size, &func_start_address, &func_info);
132 /* Calculate the virtual memory address for function start pc. */
133 func_start_pc_vma = func_start_address + sec_addr;
135 /* Mark FDEs with [m] where the FRE start address is interpreted as a
136 mask. */
137 int fde_type_addrmask_p = (SFRAME_V1_FUNC_FDE_TYPE (func_info)
138 == SFRAME_FDE_TYPE_PCMASK);
139 const char *fde_type_marker
140 = (fde_type_addrmask_p ? "[m]" : " ");
142 printf ("\n func idx [%d]: pc = 0x%"PRIx64 ", size = %d bytes",
143 funcidx,
144 func_start_pc_vma,
145 func_size);
147 if (is_sframe_abi_arch_aarch64 (sfd_ctx)
148 && (SFRAME_V1_FUNC_PAUTH_KEY (func_info) == SFRAME_AARCH64_PAUTH_KEY_B))
149 printf (", pauth = B key");
151 char temp[100];
153 printf ("\n %-7s%-8s %-10s%-10s%-13s",
154 "STARTPC", fde_type_marker, "CFA", "FP", "RA");
155 for (j = 0; j < num_fres; j++)
157 sframe_decoder_get_fre (sfd_ctx, funcidx, j, &fre);
159 fre_start_pc_vma = (fde_type_addrmask_p
160 ? fre.fre_start_addr
161 : func_start_pc_vma + fre.fre_start_addr);
163 /* FIXME - fixup the err caching in array.
164 assert no error for base reg id. */
165 base_reg_id = sframe_fre_get_base_reg_id (&fre, &err[0]);
166 cfa_offset = sframe_fre_get_cfa_offset (sfd_ctx, &fre, &err[0]);
167 fp_offset = sframe_fre_get_fp_offset (sfd_ctx, &fre, &err[1]);
168 ra_offset = sframe_fre_get_ra_offset (sfd_ctx, &fre, &err[2]);
170 /* Dump CFA info. */
171 printf ("\n");
172 printf (" %016"PRIx64, fre_start_pc_vma);
173 sprintf (temp, "%s+%d", base_reg_str[base_reg_id], cfa_offset);
174 printf (" %-10s", temp);
176 /* Dump SP/FP info. */
177 if (err[1] == 0)
178 sprintf (temp, "c%+d", fp_offset);
179 else
180 strcpy (temp, "u");
181 printf ("%-10s", temp);
183 /* Dump RA info.
184 If an ABI does not track RA offset, e.g., AMD64, display 'f',
185 else display the offset d as 'c+-d'. */
186 if (sframe_decoder_get_fixed_ra_offset (sfd_ctx)
187 != SFRAME_CFA_FIXED_RA_INVALID)
188 strcpy (temp, "f");
189 else if (err[2] == 0)
190 sprintf (temp, "c%+d", ra_offset);
191 else
192 strcpy (temp, "u");
194 /* Mark SFrame FRE's RA information with "[s]" if the RA is mangled
195 with signature bits. */
196 const char *ra_mangled_p_str
197 = ((sframe_fre_get_ra_mangled_p (sfd_ctx, &fre, &err[2]))
198 ? "[s]" : " ");
199 strcat (temp, ra_mangled_p_str);
200 printf ("%-13s", temp);
204 static void
205 dump_sframe_functions (sframe_decoder_ctx *sfd_ctx, uint64_t sec_addr)
207 uint32_t i;
208 uint32_t num_fdes;
210 const char* subsec_name = "Function Index";
211 printf ("\n %s :\n", subsec_name);
213 num_fdes = sframe_decoder_get_num_fidx (sfd_ctx);
214 for (i = 0; i < num_fdes; i++)
216 dump_sframe_func_with_fres (sfd_ctx, i, sec_addr);
217 printf ("\n");
221 void
222 dump_sframe (sframe_decoder_ctx *sfd_ctx, uint64_t sec_addr)
224 uint8_t ver;
226 dump_sframe_header (sfd_ctx);
228 ver = sframe_decoder_get_version (sfd_ctx);
229 if (ver == SFRAME_VERSION)
230 dump_sframe_functions (sfd_ctx, sec_addr);
231 else
232 printf ("\n No further information can be displayed. %s",
233 "SFrame version not supported\n");