ac/debug: use util_strchrnul() to fix android build error
[mesa.git] / src / amd / common / ac_debug.c
1 /*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Marek Olšák <maraeo@gmail.com>
25 */
26
27 #include "ac_debug.h"
28
29 #ifdef HAVE_VALGRIND
30 #include <valgrind.h>
31 #include <memcheck.h>
32 #define VG(x) x
33 #else
34 #define VG(x)
35 #endif
36
37 #include "sid.h"
38 #include "gfx9d.h"
39 #include "sid_tables.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "util/u_string.h"
43
44 /* Parsed IBs are difficult to read without colors. Use "less -R file" to
45 * read them, or use "aha -b -f file" to convert them to html.
46 */
47 #define COLOR_RESET "\033[0m"
48 #define COLOR_RED "\033[31m"
49 #define COLOR_GREEN "\033[1;32m"
50 #define COLOR_YELLOW "\033[1;33m"
51 #define COLOR_CYAN "\033[1;36m"
52
53 #define INDENT_PKT 8
54
55 struct ac_ib_parser {
56 FILE *f;
57 uint32_t *ib;
58 unsigned num_dw;
59 int trace_id;
60 enum chip_class chip_class;
61 ac_debug_addr_callback addr_callback;
62 void *addr_callback_data;
63
64 unsigned cur_dw;
65 };
66
67 static void ac_do_parse_ib(FILE *f, struct ac_ib_parser *ib);
68
69 static void print_spaces(FILE *f, unsigned num)
70 {
71 fprintf(f, "%*s", num, "");
72 }
73
74 static void print_value(FILE *file, uint32_t value, int bits)
75 {
76 /* Guess if it's int or float */
77 if (value <= (1 << 15)) {
78 if (value <= 9)
79 fprintf(file, "%u\n", value);
80 else
81 fprintf(file, "%u (0x%0*x)\n", value, bits / 4, value);
82 } else {
83 float f = uif(value);
84
85 if (fabs(f) < 100000 && f*10 == floor(f*10))
86 fprintf(file, "%.1ff (0x%0*x)\n", f, bits / 4, value);
87 else
88 /* Don't print more leading zeros than there are bits. */
89 fprintf(file, "0x%0*x\n", bits / 4, value);
90 }
91 }
92
93 static void print_named_value(FILE *file, const char *name, uint32_t value,
94 int bits)
95 {
96 print_spaces(file, INDENT_PKT);
97 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ", name);
98 print_value(file, value, bits);
99 }
100
101 void ac_dump_reg(FILE *file, unsigned offset, uint32_t value,
102 uint32_t field_mask)
103 {
104 int r, f;
105
106 for (r = 0; r < ARRAY_SIZE(sid_reg_table); r++) {
107 const struct si_reg *reg = &sid_reg_table[r];
108 const char *reg_name = sid_strings + reg->name_offset;
109
110 if (reg->offset == offset) {
111 bool first_field = true;
112
113 print_spaces(file, INDENT_PKT);
114 fprintf(file, COLOR_YELLOW "%s" COLOR_RESET " <- ",
115 reg_name);
116
117 if (!reg->num_fields) {
118 print_value(file, value, 32);
119 return;
120 }
121
122 for (f = 0; f < reg->num_fields; f++) {
123 const struct si_field *field = sid_fields_table + reg->fields_offset + f;
124 const int *values_offsets = sid_strings_offsets + field->values_offset;
125 uint32_t val = (value & field->mask) >>
126 (ffs(field->mask) - 1);
127
128 if (!(field->mask & field_mask))
129 continue;
130
131 /* Indent the field. */
132 if (!first_field)
133 print_spaces(file,
134 INDENT_PKT + strlen(reg_name) + 4);
135
136 /* Print the field. */
137 fprintf(file, "%s = ", sid_strings + field->name_offset);
138
139 if (val < field->num_values && values_offsets[val] >= 0)
140 fprintf(file, "%s\n", sid_strings + values_offsets[val]);
141 else
142 print_value(file, val,
143 util_bitcount(field->mask));
144
145 first_field = false;
146 }
147 return;
148 }
149 }
150
151 print_spaces(file, INDENT_PKT);
152 fprintf(file, COLOR_YELLOW "0x%05x" COLOR_RESET " <- 0x%08x\n", offset, value);
153 }
154
155 static uint32_t ac_ib_get(struct ac_ib_parser *ib)
156 {
157 uint32_t v = 0;
158
159 if (ib->cur_dw < ib->num_dw) {
160 v = ib->ib[ib->cur_dw];
161 #ifdef HAVE_VALGRIND
162 /* Help figure out where garbage data is written to IBs.
163 *
164 * Arguably we should do this already when the IBs are written,
165 * see RADEON_VALGRIND. The problem is that client-requests to
166 * Valgrind have an overhead even when Valgrind isn't running,
167 * and radeon_emit is performance sensitive...
168 */
169 if (VALGRIND_CHECK_VALUE_IS_DEFINED(v))
170 fprintf(ib->f, COLOR_RED "Valgrind: The next DWORD is garbage"
171 COLOR_RESET "\n");
172 #endif
173 fprintf(ib->f, "\n\035#%08x ", v);
174 } else {
175 fprintf(ib->f, "\n\035#???????? ");
176 }
177
178 ib->cur_dw++;
179 return v;
180 }
181
182 static void ac_parse_set_reg_packet(FILE *f, unsigned count, unsigned reg_offset,
183 struct ac_ib_parser *ib)
184 {
185 unsigned reg_dw = ac_ib_get(ib);
186 unsigned reg = ((reg_dw & 0xFFFF) << 2) + reg_offset;
187 unsigned index = reg_dw >> 28;
188 int i;
189
190 if (index != 0) {
191 print_spaces(f, INDENT_PKT);
192 fprintf(f, "INDEX = %u\n", index);
193 }
194
195 for (i = 0; i < count; i++)
196 ac_dump_reg(f, reg + i*4, ac_ib_get(ib), ~0);
197 }
198
199 static void ac_parse_packet3(FILE *f, uint32_t header, struct ac_ib_parser *ib)
200 {
201 unsigned first_dw = ib->cur_dw;
202 int count = PKT_COUNT_G(header);
203 unsigned op = PKT3_IT_OPCODE_G(header);
204 const char *predicate = PKT3_PREDICATE(header) ? "(predicate)" : "";
205 int i;
206
207 /* Print the name first. */
208 for (i = 0; i < ARRAY_SIZE(packet3_table); i++)
209 if (packet3_table[i].op == op)
210 break;
211
212 if (i < ARRAY_SIZE(packet3_table)) {
213 const char *name = sid_strings + packet3_table[i].name_offset;
214
215 if (op == PKT3_SET_CONTEXT_REG ||
216 op == PKT3_SET_CONFIG_REG ||
217 op == PKT3_SET_UCONFIG_REG ||
218 op == PKT3_SET_SH_REG)
219 fprintf(f, COLOR_CYAN "%s%s" COLOR_CYAN ":\n",
220 name, predicate);
221 else
222 fprintf(f, COLOR_GREEN "%s%s" COLOR_RESET ":\n",
223 name, predicate);
224 } else
225 fprintf(f, COLOR_RED "PKT3_UNKNOWN 0x%x%s" COLOR_RESET ":\n",
226 op, predicate);
227
228 /* Print the contents. */
229 switch (op) {
230 case PKT3_SET_CONTEXT_REG:
231 ac_parse_set_reg_packet(f, count, SI_CONTEXT_REG_OFFSET, ib);
232 break;
233 case PKT3_SET_CONFIG_REG:
234 ac_parse_set_reg_packet(f, count, SI_CONFIG_REG_OFFSET, ib);
235 break;
236 case PKT3_SET_UCONFIG_REG:
237 ac_parse_set_reg_packet(f, count, CIK_UCONFIG_REG_OFFSET, ib);
238 break;
239 case PKT3_SET_SH_REG:
240 ac_parse_set_reg_packet(f, count, SI_SH_REG_OFFSET, ib);
241 break;
242 case PKT3_ACQUIRE_MEM:
243 ac_dump_reg(f, R_0301F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
244 ac_dump_reg(f, R_0301F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
245 ac_dump_reg(f, R_030230_CP_COHER_SIZE_HI, ac_ib_get(ib), ~0);
246 ac_dump_reg(f, R_0301F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
247 ac_dump_reg(f, R_0301E4_CP_COHER_BASE_HI, ac_ib_get(ib), ~0);
248 print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
249 break;
250 case PKT3_SURFACE_SYNC:
251 if (ib->chip_class >= CIK) {
252 ac_dump_reg(f, R_0301F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
253 ac_dump_reg(f, R_0301F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
254 ac_dump_reg(f, R_0301F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
255 } else {
256 ac_dump_reg(f, R_0085F0_CP_COHER_CNTL, ac_ib_get(ib), ~0);
257 ac_dump_reg(f, R_0085F4_CP_COHER_SIZE, ac_ib_get(ib), ~0);
258 ac_dump_reg(f, R_0085F8_CP_COHER_BASE, ac_ib_get(ib), ~0);
259 }
260 print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
261 break;
262 case PKT3_EVENT_WRITE: {
263 uint32_t event_dw = ac_ib_get(ib);
264 ac_dump_reg(f, R_028A90_VGT_EVENT_INITIATOR, event_dw,
265 S_028A90_EVENT_TYPE(~0));
266 print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
267 print_named_value(f, "INV_L2", (event_dw >> 20) & 0x1, 1);
268 if (count > 0) {
269 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
270 print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 16);
271 }
272 break;
273 }
274 case PKT3_EVENT_WRITE_EOP: {
275 uint32_t event_dw = ac_ib_get(ib);
276 ac_dump_reg(f, R_028A90_VGT_EVENT_INITIATOR, event_dw,
277 S_028A90_EVENT_TYPE(~0));
278 print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
279 print_named_value(f, "TCL1_VOL_ACTION_ENA", (event_dw >> 12) & 0x1, 1);
280 print_named_value(f, "TC_VOL_ACTION_ENA", (event_dw >> 13) & 0x1, 1);
281 print_named_value(f, "TC_WB_ACTION_ENA", (event_dw >> 15) & 0x1, 1);
282 print_named_value(f, "TCL1_ACTION_ENA", (event_dw >> 16) & 0x1, 1);
283 print_named_value(f, "TC_ACTION_ENA", (event_dw >> 17) & 0x1, 1);
284 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
285 uint32_t addr_hi_dw = ac_ib_get(ib);
286 print_named_value(f, "ADDRESS_HI", addr_hi_dw, 16);
287 print_named_value(f, "DST_SEL", (addr_hi_dw >> 16) & 0x3, 2);
288 print_named_value(f, "INT_SEL", (addr_hi_dw >> 24) & 0x7, 3);
289 print_named_value(f, "DATA_SEL", addr_hi_dw >> 29, 3);
290 print_named_value(f, "DATA_LO", ac_ib_get(ib), 32);
291 print_named_value(f, "DATA_HI", ac_ib_get(ib), 32);
292 break;
293 }
294 case PKT3_RELEASE_MEM: {
295 uint32_t event_dw = ac_ib_get(ib);
296 ac_dump_reg(f, R_028A90_VGT_EVENT_INITIATOR, event_dw,
297 S_028A90_EVENT_TYPE(~0));
298 print_named_value(f, "EVENT_INDEX", (event_dw >> 8) & 0xf, 4);
299 print_named_value(f, "TCL1_VOL_ACTION_ENA", (event_dw >> 12) & 0x1, 1);
300 print_named_value(f, "TC_VOL_ACTION_ENA", (event_dw >> 13) & 0x1, 1);
301 print_named_value(f, "TC_WB_ACTION_ENA", (event_dw >> 15) & 0x1, 1);
302 print_named_value(f, "TCL1_ACTION_ENA", (event_dw >> 16) & 0x1, 1);
303 print_named_value(f, "TC_ACTION_ENA", (event_dw >> 17) & 0x1, 1);
304 print_named_value(f, "TC_NC_ACTION_ENA", (event_dw >> 19) & 0x1, 1);
305 print_named_value(f, "TC_WC_ACTION_ENA", (event_dw >> 20) & 0x1, 1);
306 print_named_value(f, "TC_MD_ACTION_ENA", (event_dw >> 21) & 0x1, 1);
307 uint32_t sel_dw = ac_ib_get(ib);
308 print_named_value(f, "DST_SEL", (sel_dw >> 16) & 0x3, 2);
309 print_named_value(f, "INT_SEL", (sel_dw >> 24) & 0x7, 3);
310 print_named_value(f, "DATA_SEL", sel_dw >> 29, 3);
311 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
312 print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 32);
313 print_named_value(f, "DATA_LO", ac_ib_get(ib), 32);
314 print_named_value(f, "DATA_HI", ac_ib_get(ib), 32);
315 print_named_value(f, "CTXID", ac_ib_get(ib), 32);
316 break;
317 }
318 case PKT3_WAIT_REG_MEM:
319 print_named_value(f, "OP", ac_ib_get(ib), 32);
320 print_named_value(f, "ADDRESS_LO", ac_ib_get(ib), 32);
321 print_named_value(f, "ADDRESS_HI", ac_ib_get(ib), 32);
322 print_named_value(f, "REF", ac_ib_get(ib), 32);
323 print_named_value(f, "MASK", ac_ib_get(ib), 32);
324 print_named_value(f, "POLL_INTERVAL", ac_ib_get(ib), 16);
325 break;
326 case PKT3_DRAW_INDEX_AUTO:
327 ac_dump_reg(f, R_030930_VGT_NUM_INDICES, ac_ib_get(ib), ~0);
328 ac_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ac_ib_get(ib), ~0);
329 break;
330 case PKT3_DRAW_INDEX_2:
331 ac_dump_reg(f, R_028A78_VGT_DMA_MAX_SIZE, ac_ib_get(ib), ~0);
332 ac_dump_reg(f, R_0287E8_VGT_DMA_BASE, ac_ib_get(ib), ~0);
333 ac_dump_reg(f, R_0287E4_VGT_DMA_BASE_HI, ac_ib_get(ib), ~0);
334 ac_dump_reg(f, R_030930_VGT_NUM_INDICES, ac_ib_get(ib), ~0);
335 ac_dump_reg(f, R_0287F0_VGT_DRAW_INITIATOR, ac_ib_get(ib), ~0);
336 break;
337 case PKT3_INDEX_TYPE:
338 ac_dump_reg(f, R_028A7C_VGT_DMA_INDEX_TYPE, ac_ib_get(ib), ~0);
339 break;
340 case PKT3_NUM_INSTANCES:
341 ac_dump_reg(f, R_030934_VGT_NUM_INSTANCES, ac_ib_get(ib), ~0);
342 break;
343 case PKT3_WRITE_DATA:
344 ac_dump_reg(f, R_370_CONTROL, ac_ib_get(ib), ~0);
345 ac_dump_reg(f, R_371_DST_ADDR_LO, ac_ib_get(ib), ~0);
346 ac_dump_reg(f, R_372_DST_ADDR_HI, ac_ib_get(ib), ~0);
347 /* The payload is written automatically */
348 break;
349 case PKT3_CP_DMA:
350 ac_dump_reg(f, R_410_CP_DMA_WORD0, ac_ib_get(ib), ~0);
351 ac_dump_reg(f, R_411_CP_DMA_WORD1, ac_ib_get(ib), ~0);
352 ac_dump_reg(f, R_412_CP_DMA_WORD2, ac_ib_get(ib), ~0);
353 ac_dump_reg(f, R_413_CP_DMA_WORD3, ac_ib_get(ib), ~0);
354 ac_dump_reg(f, R_414_COMMAND, ac_ib_get(ib), ~0);
355 break;
356 case PKT3_DMA_DATA:
357 ac_dump_reg(f, R_500_DMA_DATA_WORD0, ac_ib_get(ib), ~0);
358 ac_dump_reg(f, R_501_SRC_ADDR_LO, ac_ib_get(ib), ~0);
359 ac_dump_reg(f, R_502_SRC_ADDR_HI, ac_ib_get(ib), ~0);
360 ac_dump_reg(f, R_503_DST_ADDR_LO, ac_ib_get(ib), ~0);
361 ac_dump_reg(f, R_504_DST_ADDR_HI, ac_ib_get(ib), ~0);
362 ac_dump_reg(f, R_414_COMMAND, ac_ib_get(ib), ~0);
363 break;
364 case PKT3_INDIRECT_BUFFER_SI:
365 case PKT3_INDIRECT_BUFFER_CONST:
366 case PKT3_INDIRECT_BUFFER_CIK: {
367 uint32_t base_lo_dw = ac_ib_get(ib);
368 ac_dump_reg(f, R_3F0_IB_BASE_LO, base_lo_dw, ~0);
369 uint32_t base_hi_dw = ac_ib_get(ib);
370 ac_dump_reg(f, R_3F1_IB_BASE_HI, base_hi_dw, ~0);
371 uint32_t control_dw = ac_ib_get(ib);
372 ac_dump_reg(f, R_3F2_CONTROL, control_dw, ~0);
373
374 if (!ib->addr_callback)
375 break;
376
377 uint64_t addr = ((uint64_t)base_hi_dw << 32) | base_lo_dw;
378 void *data = ib->addr_callback(ib->addr_callback_data, addr);
379 if (!data)
380 break;
381
382 if (G_3F2_CHAIN(control_dw)) {
383 ib->ib = data;
384 ib->num_dw = G_3F2_IB_SIZE(control_dw);
385 ib->cur_dw = 0;
386 return;
387 }
388
389 struct ac_ib_parser ib_recurse;
390 memcpy(&ib_recurse, ib, sizeof(ib_recurse));
391 ib_recurse.ib = data;
392 ib_recurse.num_dw = G_3F2_IB_SIZE(control_dw);
393 ib_recurse.cur_dw = 0;
394
395 fprintf(f, "\n\035>------------------ nested begin ------------------\n");
396 ac_do_parse_ib(f, &ib_recurse);
397 fprintf(f, "\n\035<------------------- nested end -------------------\n");
398 break;
399 }
400 case PKT3_CLEAR_STATE:
401 case PKT3_INCREMENT_DE_COUNTER:
402 case PKT3_PFP_SYNC_ME:
403 break;
404 case PKT3_NOP:
405 if (header == 0xffff1000) {
406 count = -1; /* One dword NOP. */
407 } else if (count == 0 && ib->cur_dw < ib->num_dw &&
408 AC_IS_TRACE_POINT(ib->ib[ib->cur_dw])) {
409 unsigned packet_id = AC_GET_TRACE_POINT_ID(ib->ib[ib->cur_dw]);
410
411 print_spaces(f, INDENT_PKT);
412 fprintf(f, COLOR_RED "Trace point ID: %u\n", packet_id);
413
414 if (ib->trace_id == -1)
415 break; /* tracing was disabled */
416
417 print_spaces(f, INDENT_PKT);
418 if (packet_id < ib->trace_id)
419 fprintf(f, COLOR_RED
420 "This trace point was reached by the CP."
421 COLOR_RESET "\n");
422 else if (packet_id == ib->trace_id)
423 fprintf(f, COLOR_RED
424 "!!!!! This is the last trace point that "
425 "was reached by the CP !!!!!"
426 COLOR_RESET "\n");
427 else if (packet_id+1 == ib->trace_id)
428 fprintf(f, COLOR_RED
429 "!!!!! This is the first trace point that "
430 "was NOT been reached by the CP !!!!!"
431 COLOR_RESET "\n");
432 else
433 fprintf(f, COLOR_RED
434 "!!!!! This trace point was NOT reached "
435 "by the CP !!!!!"
436 COLOR_RESET "\n");
437 break;
438 }
439 break;
440 }
441
442 /* print additional dwords */
443 while (ib->cur_dw <= first_dw + count)
444 ac_ib_get(ib);
445
446 if (ib->cur_dw > first_dw + count + 1)
447 fprintf(f, COLOR_RED "\n!!!!! count in header too low !!!!!"
448 COLOR_RESET "\n");
449 }
450
451 /**
452 * Parse and print an IB into a file.
453 */
454 static void ac_do_parse_ib(FILE *f, struct ac_ib_parser *ib)
455 {
456 while (ib->cur_dw < ib->num_dw) {
457 uint32_t header = ac_ib_get(ib);
458 unsigned type = PKT_TYPE_G(header);
459
460 switch (type) {
461 case 3:
462 ac_parse_packet3(f, header, ib);
463 break;
464 case 2:
465 /* type-2 nop */
466 if (header == 0x80000000) {
467 fprintf(f, COLOR_GREEN "NOP (type 2)" COLOR_RESET "\n");
468 break;
469 }
470 /* fall through */
471 default:
472 fprintf(f, "Unknown packet type %i\n", type);
473 break;
474 }
475 }
476 }
477
478 static void format_ib_output(FILE *f, char *out)
479 {
480 unsigned depth = 0;
481
482 for (;;) {
483 char op = 0;
484
485 if (out[0] == '\n' && out[1] == '\035')
486 out++;
487 if (out[0] == '\035') {
488 op = out[1];
489 out += 2;
490 }
491
492 if (op == '<')
493 depth--;
494
495 unsigned indent = 4 * depth;
496 if (op != '#')
497 indent += 9;
498
499 if (indent)
500 print_spaces(f, indent);
501
502 char *end = util_strchrnul(out, '\n');
503 fwrite(out, end - out, 1, f);
504 fputc('\n', f); /* always end with a new line */
505 if (!*end)
506 break;
507
508 out = end + 1;
509
510 if (op == '>')
511 depth++;
512 }
513 }
514
515 /**
516 * Parse and print an IB into a file.
517 *
518 * \param f file
519 * \param ib_ptr IB
520 * \param num_dw size of the IB
521 * \param chip_class chip class
522 * \param trace_id the last trace ID that is known to have been reached
523 * and executed by the CP, typically read from a buffer
524 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
525 * be NULL.
526 * \param addr_callback_data user data for addr_callback
527 */
528 void ac_parse_ib_chunk(FILE *f, uint32_t *ib_ptr, int num_dw, int trace_id,
529 enum chip_class chip_class,
530 ac_debug_addr_callback addr_callback, void *addr_callback_data)
531 {
532 struct ac_ib_parser ib = {};
533 ib.ib = ib_ptr;
534 ib.num_dw = num_dw;
535 ib.trace_id = trace_id;
536 ib.chip_class = chip_class;
537 ib.addr_callback = addr_callback;
538 ib.addr_callback_data = addr_callback_data;
539
540 char *out;
541 size_t outsize;
542 FILE *memf = open_memstream(&out, &outsize);
543 ib.f = memf;
544 ac_do_parse_ib(memf, &ib);
545 fclose(memf);
546
547 if (out) {
548 format_ib_output(f, out);
549 free(out);
550 }
551
552 if (ib.cur_dw > ib.num_dw) {
553 printf("\nPacket ends after the end of IB.\n");
554 exit(1);
555 }
556 }
557
558 /**
559 * Parse and print an IB into a file.
560 *
561 * \param f file
562 * \param ib IB
563 * \param num_dw size of the IB
564 * \param chip_class chip class
565 * \param trace_id the last trace ID that is known to have been reached
566 * and executed by the CP, typically read from a buffer
567 * \param addr_callback Get a mapped pointer of the IB at a given address. Can
568 * be NULL.
569 * \param addr_callback_data user data for addr_callback
570 */
571 void ac_parse_ib(FILE *f, uint32_t *ib, int num_dw, int trace_id,
572 const char *name, enum chip_class chip_class,
573 ac_debug_addr_callback addr_callback, void *addr_callback_data)
574 {
575 fprintf(f, "------------------ %s begin ------------------\n", name);
576
577 ac_parse_ib_chunk(f, ib, num_dw, trace_id, chip_class, addr_callback,
578 addr_callback_data);
579
580 fprintf(f, "------------------- %s end -------------------\n\n", name);
581 }