xref: /linux/tools/perf/util/disasm.h (revision 487e428bb30eb1311887f05ece5b2c6beceb42ea)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef __PERF_UTIL_DISASM_H
3 #define __PERF_UTIL_DISASM_H
4 
5 #include "map_symbol.h"
6 
7 #ifdef HAVE_LIBDW_SUPPORT
8 #include "dwarf-aux.h"
9 #endif
10 
11 struct annotation_options;
12 struct disasm_line;
13 struct ins;
14 struct evsel;
15 struct symbol;
16 struct data_loc_info;
17 struct type_state;
18 struct disasm_line;
19 struct annotated_op_loc;
20 
21 struct e_machine_and_e_flags {
22 	uint32_t e_flags;
23 	uint16_t e_machine;
24 };
25 
26 struct arch {
27 	/** @name: name such as "x86" or "powerpc". */
28 	const char		*name;
29 	const struct ins	*instructions;
30 	size_t			nr_instructions;
31 	size_t			nr_instructions_allocated;
32 	const char		*insn_suffix;
33 	unsigned int		model;
34 	unsigned int		family;
35 	/** @id: ELF machine and flags associated with arch. */
36 	struct e_machine_and_e_flags id;
37 	bool			sorted_instructions;
38 	struct		{
39 		char comment_char;
40 		char skip_functions_char;
41 		char register_char;
42 		char memory_ref_char;
43 		char imm_char;
44 	} objdump;
45 	bool		(*ins_is_fused)(const struct arch *arch, const char *ins1,
46 					const char *ins2);
47 	const struct ins_ops  *(*associate_instruction_ops)(struct arch *arch, const char *name);
48 #ifdef HAVE_LIBDW_SUPPORT
49 	void		(*update_insn_state)(struct type_state *state,
50 				struct data_loc_info *dloc, Dwarf_Die *cu_die,
51 				struct disasm_line *dl);
52 #endif
53 	void		(*extract_op_location)(const struct arch *arch, struct disasm_line *dl,
54 					       const char *op_str, int op_idx,
55 					       struct annotated_op_loc *op_loc);
56 };
57 
58 struct ins {
59 	const char     *name;
60 	const struct ins_ops *ops;
61 };
62 
63 struct ins_operands {
64 	char	*raw;
65 	struct {
66 		char	*raw;
67 		char	*name;
68 		struct symbol *sym;
69 		u64	addr;
70 		s64	offset;
71 		bool	offset_avail;
72 		bool	outside;
73 		bool	multi_regs;
74 		bool	mem_ref;
75 	} target;
76 	union {
77 		struct {
78 			char	*raw;
79 			char	*name;
80 			u64	addr;
81 			bool	multi_regs;
82 			bool	mem_ref;
83 		} source;
84 		struct {
85 			struct ins	    ins;
86 			struct ins_operands *ops;
87 		} locked;
88 		struct {
89 			char	*raw_comment;
90 			char	*raw_func_start;
91 		} jump;
92 	};
93 };
94 
95 struct ins_ops {
96 	void (*free)(struct ins_operands *ops);
97 	int (*parse)(const struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
98 			struct disasm_line *dl);
99 	int (*scnprintf)(const struct ins *ins, char *bf, size_t size,
100 			 struct ins_operands *ops, int max_ins_name);
101 	bool is_jump;
102 	bool is_call;
103 };
104 
105 struct annotate_args {
106 	const struct arch	  *arch;
107 	struct map_symbol	  *ms;
108 	struct annotation_options *options;
109 	s64			  offset;
110 	char			  *line;
111 	int			  line_nr;
112 	char			  *fileloc;
113 };
114 
115 const struct arch *arch__find(uint16_t e_machine, uint32_t e_flags, const char *cpuid);
116 bool arch__is_x86(const struct arch *arch);
117 bool arch__is_powerpc(const struct arch *arch);
118 bool arch__is_arm64(const struct arch *arch);
119 
120 extern const struct ins_ops call_ops;
121 extern const struct ins_ops dec_ops;
122 extern const struct ins_ops jump_ops;
123 extern const struct ins_ops mov_ops;
124 extern const struct ins_ops nop_ops;
125 extern const struct ins_ops lock_ops;
126 extern const struct ins_ops ret_ops;
127 
128 int arch__associate_ins_ops(struct arch *arch, const char *name, const struct ins_ops *ops);
129 
130 const struct arch *arch__new_alpha(const struct e_machine_and_e_flags *id, const char *cpuid);
131 const struct arch *arch__new_arc(const struct e_machine_and_e_flags *id, const char *cpuid);
132 const struct arch *arch__new_arm(const struct e_machine_and_e_flags *id, const char *cpuid);
133 const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id, const char *cpuid);
134 const struct arch *arch__new_csky(const struct e_machine_and_e_flags *id, const char *cpuid);
135 const struct arch *arch__new_loongarch(const struct e_machine_and_e_flags *id, const char *cpuid);
136 const struct arch *arch__new_mips(const struct e_machine_and_e_flags *id, const char *cpuid);
137 const struct arch *arch__new_powerpc(const struct e_machine_and_e_flags *id, const char *cpuid);
138 const struct arch *arch__new_riscv64(const struct e_machine_and_e_flags *id, const char *cpuid);
139 const struct arch *arch__new_s390(const struct e_machine_and_e_flags *id, const char *cpuid);
140 const struct arch *arch__new_sparc(const struct e_machine_and_e_flags *id, const char *cpuid);
141 const struct arch *arch__new_x86(const struct e_machine_and_e_flags *id, const char *cpuid);
142 
143 const struct ins_ops *ins__find(const struct arch *arch, const char *name, struct disasm_line *dl);
144 
145 bool ins__is_call(const struct ins *ins);
146 bool ins__is_jump(const struct ins *ins);
147 bool ins__is_fused(const struct arch *arch, const char *ins1, const char *ins2);
148 bool ins__is_ret(const struct ins *ins);
149 bool ins__is_lock(const struct ins *ins);
150 
151 const struct ins_ops *check_ppc_insn(struct disasm_line *dl);
152 
153 struct disasm_line *disasm_line__new(struct annotate_args *args);
154 void disasm_line__free(struct disasm_line *dl);
155 
156 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size,
157 			   bool raw, int max_ins_name);
158 
159 int ins__raw_scnprintf(const struct ins *ins, char *bf, size_t size,
160 			   struct ins_operands *ops, int max_ins_name);
161 int ins__scnprintf(const struct ins *ins, char *bf, size_t size,
162 		   struct ins_operands *ops, int max_ins_name);
163 int call__scnprintf(const struct ins *ins, char *bf, size_t size,
164 		    struct ins_operands *ops, int max_ins_name);
165 int jump__scnprintf(const struct ins *ins, char *bf, size_t size,
166 		    struct ins_operands *ops, int max_ins_name);
167 int mov__scnprintf(const struct ins *ins, char *bf, size_t size,
168 		   struct ins_operands *ops, int max_ins_name);
169 
170 void jump__delete(struct ins_operands *ops);
171 
172 int symbol__disassemble(struct symbol *sym, struct annotate_args *args);
173 
174 char *expand_tabs(char *line, char **storage, size_t *storage_len);
175 
176 #endif /* __PERF_UTIL_DISASM_H */
177