Print this page
12259 CTF shouldn't assume enum size
   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2019, Joyent, Inc.
  14  */
  15 
  16 /*
  17  * Dump information about CTF containers.
  18  */
  19 
  20 #include <stdio.h>
  21 #include <unistd.h>
  22 #include <libctf.h>
  23 #include <libgen.h>
  24 #include <stdarg.h>
  25 #include <stdlib.h>
  26 #include <stddef.h>
  27 #include <sys/sysmacros.h>
  28 #include <sys/types.h>
  29 #include <sys/stat.h>
  30 #include <sys/note.h>
  31 #include <fcntl.h>
  32 #include <errno.h>
  33 #include <string.h>


 596                         ctfdump_fatal("failed to get size of %s: %s\n", name,
 597                             ctf_errmsg(ctf_errno(g_fp)));
 598                 ctfdump_printf(CTFDUMP_TYPES, "%s (%zd bytes)\n", name, size);
 599                 count = 0;
 600                 if (ctf_member_iter(g_fp, id, ctfdump_member_cb, &count) != 0)
 601                         ctfdump_fatal("failed to iterate members of %s: %s\n",
 602                             name, ctf_errmsg(ctf_errno(g_fp)));
 603                 if (kind == CTF_K_STRUCT) {
 604                         g_stats.cs_nsmembs += count;
 605                         g_stats.cs_nsmax = MAX(count, g_stats.cs_nsmax);
 606                         g_stats.cs_structsz += size;
 607                         g_stats.cs_sszmax = MAX(size, g_stats.cs_sszmax);
 608                 } else {
 609                         g_stats.cs_numembs += count;
 610                         g_stats.cs_numax = MAX(count, g_stats.cs_numax);
 611                         g_stats.cs_unionsz += size;
 612                         g_stats.cs_uszmax = MAX(count, g_stats.cs_uszmax);
 613                 }
 614                 break;
 615         case CTF_K_ENUM:







 616                 ctfdump_printf(CTFDUMP_TYPES, "%s\n", name);


 617                 count = 0;
 618                 if (ctf_enum_iter(g_fp, id, ctfdump_enum_cb, &count) != 0)
 619                         ctfdump_fatal("failed to iterate enumerators of %s: "
 620                             "%s\n", name, ctf_errmsg(ctf_errno(g_fp)));
 621                 g_stats.cs_nemembs += count;
 622                 g_stats.cs_nemax = MAX(g_stats.cs_nemax, count);
 623                 break;
 624         case CTF_K_FORWARD:
 625                 ctfdump_printf(CTFDUMP_TYPES, "forward %s\n", name);
 626                 break;
 627         case CTF_K_TYPEDEF:
 628                 if ((ref = ctf_type_reference(g_fp, id)) == CTF_ERR)
 629                         ctfdump_fatal("failed to get reference type for %s: "
 630                             "%s\n", name, ctf_errmsg(ctf_errno(g_fp)));
 631                 ctfdump_printf(CTFDUMP_TYPES, "typedef %s refers to %ld", name,
 632                     ref);
 633                 break;
 634         case CTF_K_VOLATILE:
 635                 if ((ref = ctf_type_reference(g_fp, id)) == CTF_ERR)
 636                         ctfdump_fatal("failed to get reference type for %s: "


 788                             name, ctf_errmsg(ctf_errno(g_fp)));
 789                 }
 790 
 791                 printf("};\n\n");
 792                 break;
 793         case CTF_K_ENUM:
 794                 /*
 795                  * This will throw away any anon enum that isn't followed by a
 796                  * typedef...
 797                  */
 798                 if (is_anon_refname(name))
 799                         break;
 800 
 801                 printf("%s {\n", name);
 802 
 803                 if (ctf_enum_iter(g_fp, id, ctfsrc_enum_cb, NULL) != 0) {
 804                         ctfdump_fatal("failed to iterate enumerators of %s: "
 805                             "%s\n", name, ctf_errmsg(ctf_errno(g_fp)));
 806                 }
 807 






 808                 printf("};\n\n");

 809                 break;
 810         case CTF_K_TYPEDEF:
 811                 /*
 812                  * If this fails, it's probably because the referent type is in
 813                  * a parent container that was not supplied via -p.
 814                  */
 815                 if (ctfsrc_refname(id, refname, sizeof (refname)) == NULL) {
 816                         printf("typedef %s %s;\n\n", refname, name);
 817                         break;
 818                 }
 819 
 820                 if (!is_anon_refname(refname)) {
 821                         (void) ctf_type_cname(g_fp,
 822                             ctf_type_reference(g_fp, id), refname,
 823                             sizeof (refname), name);
 824 
 825                         printf("typedef %s;\n\n", refname);
 826                         break;
 827                 }
 828 


   1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2020 Joyent, Inc.
  14  */
  15 
  16 /*
  17  * Dump information about CTF containers.
  18  */
  19 
  20 #include <stdio.h>
  21 #include <unistd.h>
  22 #include <libctf.h>
  23 #include <libgen.h>
  24 #include <stdarg.h>
  25 #include <stdlib.h>
  26 #include <stddef.h>
  27 #include <sys/sysmacros.h>
  28 #include <sys/types.h>
  29 #include <sys/stat.h>
  30 #include <sys/note.h>
  31 #include <fcntl.h>
  32 #include <errno.h>
  33 #include <string.h>


 596                         ctfdump_fatal("failed to get size of %s: %s\n", name,
 597                             ctf_errmsg(ctf_errno(g_fp)));
 598                 ctfdump_printf(CTFDUMP_TYPES, "%s (%zd bytes)\n", name, size);
 599                 count = 0;
 600                 if (ctf_member_iter(g_fp, id, ctfdump_member_cb, &count) != 0)
 601                         ctfdump_fatal("failed to iterate members of %s: %s\n",
 602                             name, ctf_errmsg(ctf_errno(g_fp)));
 603                 if (kind == CTF_K_STRUCT) {
 604                         g_stats.cs_nsmembs += count;
 605                         g_stats.cs_nsmax = MAX(count, g_stats.cs_nsmax);
 606                         g_stats.cs_structsz += size;
 607                         g_stats.cs_sszmax = MAX(size, g_stats.cs_sszmax);
 608                 } else {
 609                         g_stats.cs_numembs += count;
 610                         g_stats.cs_numax = MAX(count, g_stats.cs_numax);
 611                         g_stats.cs_unionsz += size;
 612                         g_stats.cs_uszmax = MAX(count, g_stats.cs_uszmax);
 613                 }
 614                 break;
 615         case CTF_K_ENUM:
 616                 size = ctf_type_size(g_fp, id);
 617 
 618                 /* Only the oddest enums are worth reporting on size. */
 619                 if (size != CTF_ERR && size != sizeof (int)) {
 620                         ctfdump_printf(CTFDUMP_TYPES, "%s (%zd bytes)\n",
 621                             name, size);
 622                 } else {
 623                         ctfdump_printf(CTFDUMP_TYPES, "%s\n", name);
 624                 }
 625 
 626                 count = 0;
 627                 if (ctf_enum_iter(g_fp, id, ctfdump_enum_cb, &count) != 0)
 628                         ctfdump_fatal("failed to iterate enumerators of %s: "
 629                             "%s\n", name, ctf_errmsg(ctf_errno(g_fp)));
 630                 g_stats.cs_nemembs += count;
 631                 g_stats.cs_nemax = MAX(g_stats.cs_nemax, count);
 632                 break;
 633         case CTF_K_FORWARD:
 634                 ctfdump_printf(CTFDUMP_TYPES, "forward %s\n", name);
 635                 break;
 636         case CTF_K_TYPEDEF:
 637                 if ((ref = ctf_type_reference(g_fp, id)) == CTF_ERR)
 638                         ctfdump_fatal("failed to get reference type for %s: "
 639                             "%s\n", name, ctf_errmsg(ctf_errno(g_fp)));
 640                 ctfdump_printf(CTFDUMP_TYPES, "typedef %s refers to %ld", name,
 641                     ref);
 642                 break;
 643         case CTF_K_VOLATILE:
 644                 if ((ref = ctf_type_reference(g_fp, id)) == CTF_ERR)
 645                         ctfdump_fatal("failed to get reference type for %s: "


 797                             name, ctf_errmsg(ctf_errno(g_fp)));
 798                 }
 799 
 800                 printf("};\n\n");
 801                 break;
 802         case CTF_K_ENUM:
 803                 /*
 804                  * This will throw away any anon enum that isn't followed by a
 805                  * typedef...
 806                  */
 807                 if (is_anon_refname(name))
 808                         break;
 809 
 810                 printf("%s {\n", name);
 811 
 812                 if (ctf_enum_iter(g_fp, id, ctfsrc_enum_cb, NULL) != 0) {
 813                         ctfdump_fatal("failed to iterate enumerators of %s: "
 814                             "%s\n", name, ctf_errmsg(ctf_errno(g_fp)));
 815                 }
 816 
 817                 size = ctf_type_size(g_fp, id);
 818 
 819                 /* Only the oddest enums are worth reporting on size. */
 820                 if (size != CTF_ERR && size != sizeof (int)) {
 821                         printf("} /* 0x%x bytes */;\n\n", size);
 822                 } else {
 823                         printf("};\n\n");
 824                 }
 825                 break;
 826         case CTF_K_TYPEDEF:
 827                 /*
 828                  * If this fails, it's probably because the referent type is in
 829                  * a parent container that was not supplied via -p.
 830                  */
 831                 if (ctfsrc_refname(id, refname, sizeof (refname)) == NULL) {
 832                         printf("typedef %s %s;\n\n", refname, name);
 833                         break;
 834                 }
 835 
 836                 if (!is_anon_refname(refname)) {
 837                         (void) ctf_type_cname(g_fp,
 838                             ctf_type_reference(g_fp, id), refname,
 839                             sizeof (refname), name);
 840 
 841                         printf("typedef %s;\n\n", refname);
 842                         break;
 843                 }
 844