Print this page
rm code review
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/test/util-tests/tests/ctf/check-common.c
+++ new/usr/src/test/util-tests/tests/ctf/check-common.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 4 * You may only use this file in accordance with the terms of version
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 13 * Copyright 2020 Joyent, Inc.
14 14 */
15 15
16 16 /*
17 17 * Collection of common utilities for CTF testing.
18 18 */
19 19
20 20 #include <strings.h>
21 21 #include <libctf.h>
22 22 #include "check-common.h"
23 23
24 24 typedef struct ctftests_lookup_cb {
25 25 ctf_file_t *clc_fp;
26 26 ctf_id_t clc_id;
27 27 const char *clc_name;
28 28 } ctftests_lookup_cb_t;
29 29
30 30 typedef struct ctftest_member_cb {
31 31 ctf_file_t *cmc_fp;
32 32 const check_member_t *cmc_members;
33 33 const char *cmc_name;
34 34 } ctftest_member_cb_t;
35 35
36 36 static int
37 37 ctftest_lookup_type_cb(ctf_id_t id, boolean_t root, void *arg)
38 38 {
39 39 char buf[2048];
40 40 ctftests_lookup_cb_t *clc = arg;
41 41
42 42 if (ctf_type_name(clc->clc_fp, id, buf, sizeof (buf)) == NULL)
43 43 return (0);
44 44
45 45 if (strcmp(buf, clc->clc_name) != 0)
46 46 return (0);
47 47
48 48 clc->clc_id = id;
49 49 return (1);
50 50 }
51 51
52 52 /*
53 53 * This is a variant on the classic ctf_lookup_by_name(). ctf_lookup_by_name()
54 54 * skips qualifiers, which makes sense given what the consumers of it are trying
55 55 * to do. However, that's not what we want here. So instead we basically have to
56 56 * walk the type table.
57 57 */
58 58 static ctf_id_t
59 59 ctftest_lookup_type(ctf_file_t *fp, const char *name)
60 60 {
61 61 ctftests_lookup_cb_t clc;
62 62
63 63 clc.clc_fp = fp;
64 64 clc.clc_id = CTF_ERR;
65 65 clc.clc_name = name;
66 66
67 67 (void) ctf_type_iter(fp, B_TRUE, ctftest_lookup_type_cb, &clc);
68 68 return (clc.clc_id);
69 69 }
70 70
71 71 static int
72 72 ctftest_lookup_object_cb(const char *obj, ctf_id_t type, ulong_t idx, void *arg)
73 73 {
74 74 ctftests_lookup_cb_t *clc = arg;
75 75
76 76 if (strcmp(obj, clc->clc_name) == 0) {
77 77 clc->clc_id = type;
78 78 return (1);
79 79 }
80 80
81 81 return (0);
82 82 }
83 83
84 84 static ctf_id_t
85 85 ctftest_lookup_symbol(ctf_file_t *fp, const char *name)
86 86 {
87 87 ctftests_lookup_cb_t clc;
88 88
89 89 clc.clc_fp = fp;
90 90 clc.clc_id = CTF_ERR;
91 91 clc.clc_name = name;
92 92
93 93 (void) ctf_object_iter(fp, ctftest_lookup_object_cb, &clc);
94 94 return (clc.clc_id);
95 95 }
96 96
97 97 typedef struct ctf_function_cb {
98 98 const char *cfc_name;
99 99 ulong_t *cfc_symp;
100 100 ctf_funcinfo_t *cfc_fip;
101 101 } ctf_function_cb_t;
102 102
103 103 static int
104 104 ctftest_lookup_function_cb(const char *name, ulong_t symidx,
105 105 ctf_funcinfo_t *fip, void *arg)
106 106 {
107 107 ctf_function_cb_t *cfc = arg;
108 108 if (strcmp(name, cfc->cfc_name) != 0)
109 109 return (0);
110 110
111 111 *cfc->cfc_symp = symidx;
112 112 *cfc->cfc_fip = *fip;
113 113
114 114 return (1);
115 115 }
116 116
117 117 /*
118 118 * Note, this function finds the first one with a matching name. This must not
119 119 * be used when performing searches where a given name may occur more than once.
120 120 */
121 121 static boolean_t
122 122 ctftest_lookup_function(ctf_file_t *fp, const char *name, ulong_t *symp,
123 123 ctf_funcinfo_t *fip)
124 124 {
125 125 ctf_function_cb_t cfc;
126 126
127 127 *symp = 0;
128 128 cfc.cfc_name = name;
129 129 cfc.cfc_symp = symp;
130 130 cfc.cfc_fip = fip;
131 131 (void) ctf_function_iter(fp, ctftest_lookup_function_cb, &cfc);
132 132 return (*symp == 0 ? B_FALSE : B_TRUE);
133 133 }
134 134
135 135 boolean_t
136 136 ctftest_check_numbers(ctf_file_t *fp, const check_number_t *tests)
137 137 {
138 138 uint_t i;
139 139 boolean_t ret = B_TRUE;
140 140
141 141 for (i = 0; tests[i].cn_tname != NULL; i++) {
142 142 ctf_id_t id;
143 143 ctf_encoding_t enc;
144 144
145 145 id = ctftest_lookup_type(fp, tests[i].cn_tname);
146 146 if (id == CTF_ERR) {
147 147 warnx("failed to look up %s", tests[i].cn_tname);
148 148 ret = B_FALSE;
149 149 continue;
150 150 }
151 151
152 152 if (ctf_type_kind(fp, id) != tests[i].cn_kind) {
153 153 warnx("type kind mismatch for %s: got %u, expected %u",
154 154 tests[i].cn_tname, ctf_type_kind(fp, id),
155 155 tests[i].cn_kind);
156 156 ret = B_FALSE;
157 157 continue;
158 158 }
159 159
160 160 if (ctf_type_encoding(fp, id, &enc) == CTF_ERR) {
161 161 warnx("failed to get type encoding for %s: %s",
162 162 tests[i].cn_tname, ctf_errmsg(ctf_errno(fp)));
163 163 ret = B_FALSE;
164 164 continue;
165 165 }
166 166
167 167 if (enc.cte_format != tests[i].cn_flags) {
168 168 warnx("encoding flags mismatch for %s: got 0x%x, "
169 169 "expected 0x%x", tests[i].cn_tname, enc.cte_format,
170 170 tests[i].cn_flags);
171 171 ret = B_FALSE;
172 172 continue;
173 173 }
174 174
175 175 if (enc.cte_offset != tests[i].cn_offset) {
176 176 warnx("encoding offset mismatch for %s: got 0x%x, "
177 177 "expected 0x%x", tests[i].cn_tname, enc.cte_offset,
178 178 tests[i].cn_offset);
179 179 ret = B_FALSE;
180 180 continue;
181 181 }
182 182
183 183 if (enc.cte_bits != tests[i].cn_size) {
184 184 warnx("encoding size mismatch for %s: got 0x%x, "
185 185 "expected 0x%x", tests[i].cn_tname, enc.cte_bits,
186 186 tests[i].cn_size);
187 187 ret = B_FALSE;
188 188 continue;
189 189 }
190 190 }
191 191
192 192 return (ret);
193 193 }
194 194
195 195 typedef struct ctftests_symbol_cb {
196 196 ctf_file_t *csc_fp;
197 197 boolean_t csc_ret;
198 198 const check_symbol_t *csc_tests;
199 199 } ctftest_symbol_cb_t;
200 200
201 201 static int
202 202 ctftest_check_symbol_cb(const char *obj, ctf_id_t type, ulong_t idx, void *arg)
203 203 {
204 204 ctftest_symbol_cb_t *cb = arg;
205 205 const check_symbol_t *tests = cb->csc_tests;
206 206 ctf_file_t *fp = cb->csc_fp;
207 207 uint_t i;
208 208
209 209 for (i = 0; tests[i].cs_symbol != NULL; i++) {
210 210 ctf_id_t id;
211 211
212 212 if (strcmp(obj, tests[i].cs_symbol) != 0)
213 213 continue;
214 214
215 215 id = ctftest_lookup_type(fp, tests[i].cs_type);
216 216 if (id == CTF_ERR) {
217 217 warnx("failed to lookup type %s for symbol %s",
218 218 tests[i].cs_type, tests[i].cs_symbol);
219 219 cb->csc_ret = B_FALSE;
220 220 return (0);
221 221 }
222 222
223 223 if (id != type) {
224 224 warnx("type mismatch for symbol %s, has type id %u, "
225 225 "but specified type %s has id %u",
226 226 tests[i].cs_symbol, type, tests[i].cs_type, id);
227 227 cb->csc_ret = B_FALSE;
228 228 return (0);
229 229 }
230 230 }
231 231
232 232 return (0);
233 233 }
234 234
235 235 boolean_t
236 236 ctftest_check_symbols(ctf_file_t *fp, const check_symbol_t *tests)
237 237 {
238 238 ctftest_symbol_cb_t cb;
239 239
240 240 cb.csc_fp = fp;
241 241 cb.csc_ret = B_TRUE;
242 242 cb.csc_tests = tests;
243 243 if (ctf_object_iter(fp, ctftest_check_symbol_cb, &cb) != 0)
244 244 return (B_FALSE);
245 245 return (cb.csc_ret);
246 246 }
247 247
248 248
249 249 boolean_t
250 250 ctftest_check_descent(const char *symbol, ctf_file_t *fp,
251 251 const check_descent_t *tests, boolean_t quiet)
252 252 {
253 253 ctf_id_t base;
254 254 uint_t layer = 0;
255 255
256 256 /*
257 257 * First, find the initial type of the symbol.
258 258 */
259 259 base = ctftest_lookup_symbol(fp, symbol);
260 260 if (base == CTF_ERR) {
261 261 warnx("failed to lookup type for symbol %s", symbol);
262 262 return (B_FALSE);
263 263 }
264 264
265 265 while (tests->cd_tname != NULL) {
266 266 ctf_id_t tid;
267 267 int kind;
268 268 ctf_arinfo_t ari;
269 269
270 270 if (base == CTF_ERR) {
271 271 if (!quiet) {
272 272 warnx("encountered non-reference type at layer "
273 273 "%u while still expecting type %s for "
274 274 "symbol %s", layer,
275 275 tests->cd_tname, symbol);
276 276 }
277 277 return (B_FALSE);
278 278 }
279 279
280 280 tid = ctftest_lookup_type(fp, tests->cd_tname);
281 281 if (tid == CTF_ERR) {
282 282 if (!quiet) {
283 283 warnx("failed to lookup type %s",
284 284 tests->cd_tname);
285 285 }
286 286 return (B_FALSE);
287 287 }
288 288
289 289 if (tid != base) {
290 290 if (!quiet) {
291 291 warnx("type mismatch at layer %u: found id %u, "
292 292 "but expecting type id %u for type %s, "
293 293 "symbol %s", layer, base, tid,
294 294 tests->cd_tname, symbol);
295 295 }
296 296 return (B_FALSE);
297 297 }
298 298
299 299 kind = ctf_type_kind(fp, base);
300 300 if (kind != tests->cd_kind) {
301 301 if (!quiet) {
302 302 warnx("type kind mismatch at layer %u: found "
303 303 "kind %u, but expected kind %u for %s, "
304 304 "symbol %s", layer, kind, tests->cd_kind,
305 305 tests->cd_tname, symbol);
306 306 }
307 307 return (B_FALSE);
308 308 }
309 309
310 310 switch (kind) {
311 311 case CTF_K_ARRAY:
312 312 if (ctf_array_info(fp, base, &ari) == CTF_ERR) {
313 313 if (!quiet) {
314 314 warnx("failed to lookup array info at "
315 315 "layer %u for type %s, symbol "
316 316 "%s: %s", base, tests->cd_tname,
317 317 symbol, ctf_errmsg(ctf_errno(fp)));
318 318 }
319 319 return (B_FALSE);
320 320 }
321 321
322 322 if (tests->cd_nents != ari.ctr_nelems) {
323 323 if (!quiet) {
324 324 warnx("array element mismatch at layer "
325 325 "%u for type %s, symbol %s: found "
326 326 "%u, expected %u", layer,
327 327 tests->cd_tname, symbol,
328 328 ari.ctr_nelems, tests->cd_nents);
329 329 }
330 330 return (B_FALSE);
331 331 }
332 332
333 333 tid = ctftest_lookup_type(fp, tests->cd_contents);
334 334 if (tid == CTF_ERR) {
335 335 if (!quiet) {
336 336 warnx("failed to look up type %s",
337 337 tests->cd_contents);
338 338 }
339 339 return (B_FALSE);
340 340 }
341 341
342 342 if (ari.ctr_contents != tid) {
343 343 if (!quiet) {
344 344 warnx("array contents mismatch at "
345 345 "layer %u for type %s, symbol %s: "
346 346 "found %u, expected %s/%u", layer,
347 347 tests->cd_tname, symbol,
348 348 ari.ctr_contents,
349 349 tests->cd_contents, tid);
350 350 }
351 351 return (B_FALSE);
352 352 }
353 353 base = ari.ctr_contents;
354 354 break;
355 355 default:
356 356 base = ctf_type_reference(fp, base);
357 357 break;
358 358 }
359 359
360 360 tests++;
361 361 layer++;
362 362 }
363 363
364 364 if (base != CTF_ERR) {
365 365 if (!quiet) {
366 366 warnx("found additional type %u in chain, "
367 367 "but expected no more", base);
368 368 }
369 369 return (B_FALSE);
370 370 }
371 371
372 372 return (B_TRUE);
373 373 }
374 374
375 375 int
376 376 ctftest_check_enum_count(const char *name, int value, void *arg)
377 377 {
378 378 uint_t *u = arg;
379 379 *u = *u + 1;
380 380 return (0);
381 381 }
382 382
383 383 int
384 384 ctftest_check_enum_value(const char *name, int value, void *arg)
385 385 {
386 386 uint_t i;
387 387 const check_enum_t *enums = arg;
388 388
389 389 for (i = 0; enums[i].ce_name != NULL; i++) {
390 390 if (strcmp(enums[i].ce_name, name) != 0)
391 391 continue;
392 392 if (enums[i].ce_value == (int64_t)value)
393 393 return (0);
394 394 warnx("enum %s value mismatch: found %d, expected %" PRId64,
395 395 name, value, enums[i].ce_value);
396 396 return (1);
397 397 }
398 398
399 399 warnx("found no matching entry for enum member %s", name);
400 400 return (1);
401 401 }
402 402
403 403 boolean_t
404 404 ctftest_check_enum(const char *type, ctf_file_t *fp, const check_enum_t *enums)
405 405 {
406 406 int ret;
407 407 uint_t tcount, ecount;
408 408 ctf_id_t base;
409 409
410 410 if ((base = ctftest_lookup_type(fp, type)) == CTF_ERR) {
411 411 warnx("Failed to look up type %s", type);
412 412 return (B_FALSE);
413 413 }
414 414
415 415 if (ctf_type_kind(fp, base) != CTF_K_ENUM) {
416 416 warnx("%s is not an enum", type);
417 417 return (B_FALSE);
418 418 }
419 419
420 420 /*
421 421 * First count how many entries we have.
422 422 */
423 423 tcount = 0;
424 424 while (enums[tcount].ce_name != NULL) {
425 425 tcount++;
426 426 }
427 427
428 428 ecount = 0;
429 429 if (ctf_enum_iter(fp, base, ctftest_check_enum_count, &ecount) != 0) {
430 430 warnx("failed to walk enum %s: %s", type,
431 431 ctf_errmsg(ctf_errno(fp)));
432 432 return (B_FALSE);
433 433 }
434 434
435 435 if (tcount != ecount) {
436 436 warnx("enum value mismatch: expected %u values, but found %u",
437 437 tcount, ecount);
438 438 return (B_FALSE);
439 439 }
440 440
441 441 if ((ret = ctf_enum_iter(fp, base, ctftest_check_enum_value,
442 442 (void *)enums)) != 0) {
443 443 if (ret == -1) {
444 444 warnx("failed to walk enum %s: %s", type,
445 445 ctf_errmsg(ctf_errno(fp)));
446 446 }
447 447 return (B_FALSE);
448 448 }
449 449
450 450 return (B_TRUE);
451 451 }
452 452
453 453 int
454 454 ctftest_check_member_count(const char *mname, ctf_id_t mtype, ulong_t bitoff,
455 455 void *arg)
456 456 {
457 457 uint_t *countp = arg;
458 458 *countp = *countp + 1;
459 459 return (0);
460 460 }
461 461
462 462 int
463 463 ctftest_check_members_cb(const char *mname, ctf_id_t mtype, ulong_t bitoff,
464 464 void *arg)
465 465 {
466 466 uint_t i;
467 467 const ctftest_member_cb_t *cmc = arg;
468 468 const check_member_t *members = cmc->cmc_members;
469 469 ctf_file_t *fp = cmc->cmc_fp;
470 470
471 471 for (i = 0; members[i].cm_name != NULL; i++) {
472 472 boolean_t bad = B_FALSE;
473 473 char buf[2048];
474 474
475 475 if (strcmp(mname, members[i].cm_name) != 0)
476 476 continue;
477 477
478 478 if (bitoff != members[i].cm_offset) {
479 479 warnx("member %s of type %s has mismatched bit offset: "
480 480 "found %lu, expected %lu", mname, cmc->cmc_name,
481 481 bitoff, members[i].cm_offset);
482 482 bad = B_TRUE;
483 483 }
484 484
485 485 if (ctf_type_name(fp, mtype, buf, sizeof (buf)) == NULL) {
486 486 warnx("failed to obtain type name for member %s",
487 487 mname, ctf_errmsg(ctf_errno(fp)));
488 488 bad = B_TRUE;
489 489 } else if (strcmp(buf, members[i].cm_type) != 0) {
490 490 warnx("member %s has bad type, found %s, expected %s",
491 491 mname, buf, members[i].cm_type);
492 492 bad = B_TRUE;
493 493 }
494 494
495 495 return (bad ? 1 : 0);
496 496 }
497 497
498 498 warnx("found no matching entry for member %s of type %s", mname,
499 499 cmc->cmc_name);
500 500 return (1);
501 501 }
502 502
503 503 boolean_t
504 504 ctftest_check_members(const char *type, ctf_file_t *fp, int kind,
505 505 size_t size, const check_member_t *members)
506 506 {
507 507 int ret;
508 508 uint_t tcount, mcount;
509 509 ctf_id_t base;
510 510 ctftest_member_cb_t cmc;
511 511
512 512 if ((base = ctftest_lookup_type(fp, type)) == CTF_ERR) {
513 513 warnx("failed to look up type %s", type);
514 514 return (B_FALSE);
515 515 }
516 516
517 517 if (ctf_type_kind(fp, base) != kind) {
518 518 warnx("%s has kind %s, expected %s", type,
519 519 ctf_kind_name(fp, ctf_type_kind(fp, base)),
520 520 ctf_kind_name(fp, kind));
521 521 return (B_FALSE);
522 522 }
523 523
524 524 if (size != ctf_type_size(fp, base)) {
525 525 warnx("%s has bad size, expected %lu, found %lu",
526 526 type, size, ctf_type_size(fp, base));
527 527 return (B_FALSE);
528 528 }
529 529
530 530 /*
531 531 * First count how many entries we have.
532 532 */
533 533 tcount = 0;
534 534 while (members[tcount].cm_name != NULL) {
535 535 tcount++;
536 536 }
537 537
538 538 mcount = 0;
539 539 if (ctf_member_iter(fp, base, ctftest_check_member_count, &mcount) !=
540 540 0) {
541 541 warnx("failed to walk members of %s: %s", type,
542 542 ctf_errmsg(ctf_errno(fp)));
543 543 return (B_FALSE);
544 544 }
545 545
546 546 if (tcount != mcount) {
547 547 warnx("type member mismatch: expected %u values, but found %u",
548 548 tcount, mcount);
549 549 return (B_FALSE);
550 550 }
551 551
552 552 cmc.cmc_fp = fp;
553 553 cmc.cmc_members = members;
554 554 cmc.cmc_name = type;
555 555 if ((ret = ctf_member_iter(fp, base, ctftest_check_members_cb,
556 556 &cmc)) != 0) {
557 557 if (ret == -1) {
558 558 warnx("failed to walk type %s: %s", type,
559 559 ctf_errmsg(ctf_errno(fp)));
560 560 }
561 561 return (B_FALSE);
562 562 }
563 563
564 564 return (B_TRUE);
565 565 }
566 566
567 567 boolean_t
568 568 ctftest_check_function(const char *symbol, ctf_file_t *fp, const char *rtype,
569 569 uint_t nargs, uint_t flags, const char **argv)
570 570 {
571 571 ulong_t sym;
572 572 ctf_funcinfo_t fi;
573 573 uint_t i;
574 574 boolean_t ret = B_TRUE;
575 575 ctf_id_t *args;
576 576 char buf[2048];
577 577
578 578
579 579 if (!ctftest_lookup_function(fp, symbol, &sym, &fi)) {
580 580 warnx("failed to look up function %s", symbol);
581 581 return (B_FALSE);
582 582 }
583 583
584 584 if (ctf_type_name(fp, fi.ctc_return, buf, sizeof (buf)) == NULL) {
585 585 warnx("failed to lookup return type name for function %s",
586 586 symbol);
587 587 ret = B_FALSE;
588 588 } else if (strcmp(rtype, buf) != 0) {
589 589 warnx("return type has wrong type: found %s, expected %s",
590 590 buf, rtype);
591 591 ret = B_FALSE;
592 592 }
593 593
594 594 if (nargs != fi.ctc_argc) {
595 595 warnx("function argument mismatch: found %u, expected %u",
596 596 fi.ctc_argc, nargs);
597 597 ret = B_FALSE;
598 598 }
599 599
600 600 if (flags != fi.ctc_flags) {
601 601 warnx("function flags mismatch, found 0x%x, expected 0x%x",
602 602 fi.ctc_flags, flags);
603 603 ret = B_FALSE;
604 604 }
605 605
606 606 if (!ret || fi.ctc_argc == 0) {
607 607 return (ret);
608 608 }
609 609
610 610 if ((args = calloc(fi.ctc_argc, sizeof (ctf_id_t))) == NULL) {
611 611 warnx("failed to allocate memory for function arguments");
612 612 return (B_FALSE);
613 613 }
614 614
615 615 if (ctf_func_args(fp, sym, fi.ctc_argc, args) != 0) {
616 616 warnx("failed to get function information: %s",
617 617 ctf_errmsg(ctf_errno(fp)));
618 618 free(args);
619 619 return (B_FALSE);
620 620 }
621 621
622 622 for (i = 0; i < fi.ctc_argc; i++) {
623 623 if (ctf_type_name(fp, args[i], buf, sizeof (buf)) == NULL) {
624 624 warnx("failed to obtain type name for argument %u",
625 625 i, ctf_errmsg(ctf_errno(fp)));
626 626 ret = B_FALSE;
627 627 break;
628 628 }
629 629
630 630 if (strcmp(buf, argv[i]) != 0) {
631 631 warnx("argument %u has wrong type: found %s, "
632 632 "expected %s", i, buf, argv[i]);
633 633 ret = B_FALSE;
634 634 break;
635 635 }
636 636 }
637 637
638 638 free(args);
639 639 return (ret);
640 640 }
641 641
642 642 boolean_t
643 643 ctftest_check_fptr(const char *type, ctf_file_t *fp, const char *rtype,
644 644 uint_t nargs, uint_t flags, const char **argv)
645 645 {
646 646 ctf_id_t tid;
647 647 ctf_funcinfo_t fi;
648 648 uint_t i;
649 649 boolean_t ret = B_TRUE;
650 650 ctf_id_t *args;
651 651 char buf[2048];
652 652
653 653
654 654 if ((tid = ctf_lookup_by_name(fp, type)) == CTF_ERR) {
655 655 warnx("failed to look up type %s: %s", type,
656 656 ctf_errmsg(ctf_errno(fp)));
657 657 return (B_FALSE);
658 658 }
659 659
660 660 /*
661 661 * Perform two CTF type resolves, one for the function pointer and one
662 662 * for the typedef that gets passed in.
663 663 */
664 664 if ((tid = ctf_type_resolve(fp, tid)) == CTF_ERR) {
665 665 warnx("failed to convert type %s to base type: %s", type,
666 666 ctf_errmsg(ctf_errno(fp)));
667 667 return (B_FALSE);
668 668 }
669 669
670 670 if (ctf_type_kind(fp, tid) == CTF_K_POINTER &&
671 671 (tid = ctf_type_reference(fp, tid)) == CTF_ERR) {
672 672 warnx("failed to convert type %s to base type: %s", type,
673 673 ctf_errmsg(ctf_errno(fp)));
674 674 return (B_FALSE);
675 675 }
676 676
677 677 if (ctf_func_info_by_id(fp, tid, &fi) != 0) {
678 678 warnx("failed to get function information for type %s: %s",
679 679 type, ctf_errmsg(ctf_errno(fp)));
680 680 return (B_FALSE);
681 681 }
682 682
683 683 if (ctf_type_name(fp, fi.ctc_return, buf, sizeof (buf)) == NULL) {
684 684 warnx("failed to lookup return type name for function %s",
685 685 type);
686 686 ret = B_FALSE;
687 687 } else if (strcmp(rtype, buf) != 0) {
688 688 warnx("return type has wrong type: found %s, expected %s",
689 689 buf, rtype);
690 690 ret = B_FALSE;
691 691 }
692 692
693 693 if (nargs != fi.ctc_argc) {
694 694 warnx("function argument mismatch: found %u, expected %u",
695 695 fi.ctc_argc, nargs);
696 696 ret = B_FALSE;
697 697 }
698 698
699 699 if (flags != fi.ctc_flags) {
700 700 warnx("function flags mismatch, found 0x%x, expected 0x%x",
701 701 fi.ctc_flags, flags);
702 702 ret = B_FALSE;
703 703 }
704 704
705 705 if (!ret || fi.ctc_argc == 0) {
706 706 return (ret);
707 707 }
708 708
709 709 if ((args = calloc(fi.ctc_argc, sizeof (ctf_id_t))) == NULL) {
710 710 warnx("failed to allocate memory for function arguments");
711 711 return (B_FALSE);
712 712 }
713 713
714 714 if (ctf_func_args_by_id(fp, tid, fi.ctc_argc, args) != 0) {
715 715 warnx("failed to get function information: %s",
716 716 ctf_errmsg(ctf_errno(fp)));
717 717 free(args);
718 718 return (B_FALSE);
719 719 }
720 720
721 721 for (i = 0; i < fi.ctc_argc; i++) {
722 722 if (ctf_type_name(fp, args[i], buf, sizeof (buf)) == NULL) {
723 723 warnx("failed to obtain type name for argument %u",
724 724 i, ctf_errmsg(ctf_errno(fp)));
725 725 ret = B_FALSE;
726 726 break;
727 727 }
728 728
729 729 if (strcmp(buf, argv[i]) != 0) {
↓ open down ↓ |
729 lines elided |
↑ open up ↑ |
730 730 warnx("argument %u has wrong type: found %s, "
731 731 "expected %s", i, buf, argv[i]);
732 732 ret = B_FALSE;
733 733 break;
734 734 }
735 735 }
736 736
737 737 free(args);
738 738 return (ret);
739 739 }
740 +
740 741 boolean_t
741 742 ctftest_check_size(const char *type, ctf_file_t *fp, size_t size)
742 743 {
743 744 ctf_id_t base;
744 745
745 746 if ((base = ctftest_lookup_type(fp, type)) == CTF_ERR) {
746 747 warnx("Failed to look up type %s", type);
747 748 return (B_FALSE);
748 749 }
749 750
750 751 if (size != ctf_type_size(fp, base)) {
751 752 warnx("%s has bad size, expected %lu, found %lu",
752 753 type, size, ctf_type_size(fp, base));
753 754 return (B_FALSE);
754 755 }
755 756
756 757 return (B_TRUE);
757 758 }
758 759
759 760 typedef struct ctftest_duplicates {
760 761 ctf_file_t *ctd_fp;
761 762 char **ctd_names;
762 763 size_t ctd_len;
763 764 size_t ctd_curent;
764 765 boolean_t ctd_ret;
765 766 } ctftest_duplicates_t;
766 767
767 768 static int
768 769 ctftest_duplicates_cb(ctf_id_t id, boolean_t root, void *arg)
769 770 {
770 771 char buf[2048];
771 772 ctftest_duplicates_t *dup = arg;
772 773 size_t i;
773 774
774 775 if (ctf_type_name(dup->ctd_fp, id, buf, sizeof (buf)) == NULL) {
775 776 warnx("failed to lookup name for id %ld", id);
776 777 dup->ctd_ret = B_FALSE;
777 778 return (1);
778 779 }
779 780
780 781 for (i = 0; i < dup->ctd_curent; i++) {
781 782 if (strcmp(buf, dup->ctd_names[i]) == 0) {
782 783 warnx("encountered duplicate type '%s'", buf);
783 784 dup->ctd_ret = B_FALSE;
784 785 /*
785 786 * Don't break out of the loop and keep going in case we
786 787 * find another duplicate.
787 788 */
788 789 return (0);
789 790 }
790 791 }
791 792
792 793 if (dup->ctd_curent == dup->ctd_len) {
793 794 char **n;
794 795 size_t newlen = dup->ctd_len * 2;
795 796
796 797 n = recallocarray(dup->ctd_names, dup->ctd_len, newlen,
797 798 sizeof (char *));
798 799 if (n == NULL) {
799 800 warnx("failed to resize type name array");
800 801 dup->ctd_ret = B_FALSE;
801 802 return (1);
802 803 }
803 804
804 805 dup->ctd_names = n;
805 806 dup->ctd_len = newlen;
806 807 }
807 808
808 809 dup->ctd_names[dup->ctd_curent] = strdup(buf);
809 810 if (dup->ctd_names[dup->ctd_curent] == NULL) {
810 811 warn("failed to duplicate type name");
811 812 dup->ctd_ret = B_FALSE;
812 813 return (1);
813 814 }
814 815 dup->ctd_curent++;
815 816
816 817 return (0);
817 818 }
818 819
819 820 boolean_t
820 821 ctftest_duplicates(ctf_file_t *fp)
821 822 {
822 823 size_t i;
823 824 ctftest_duplicates_t d;
824 825
825 826 bzero(&d, sizeof (d));
826 827 d.ctd_fp = fp;
827 828 d.ctd_len = 4;
828 829 d.ctd_ret = B_TRUE;
829 830 d.ctd_names = recallocarray(NULL, 0, d.ctd_len, sizeof (char *));
830 831 if (d.ctd_names == NULL) {
831 832 warnx("failed to allocate duplicate name storage");
832 833 return (B_FALSE);
833 834 }
834 835
835 836 (void) ctf_type_iter(fp, B_TRUE, ctftest_duplicates_cb, &d);
836 837
837 838 for (i = 0; i < d.ctd_curent; i++) {
838 839 free(d.ctd_names[i]);
839 840 }
840 841 free(d.ctd_names);
841 842
842 843 return (d.ctd_ret);
843 844 }
↓ open down ↓ |
94 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX