poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/2] pickles: ctf: add new pickle ctf-print.pk for printing CTF s


From: Indu Bhagat
Subject: [PATCH 2/2] pickles: ctf: add new pickle ctf-print.pk for printing CTF section
Date: Wed, 24 Feb 2021 16:47:15 -0800

2021-02-24  Indu Bhagat  <indu.bhagat@oracle.com>

        * pickles/Makefile.am: add ctf-print.pk to dist_pickles_DATA.
        * pickles/ctf-print.pk: New file.
---
 pickles/Makefile.am  |   3 +-
 pickles/ctf-print.pk | 194 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 196 insertions(+), 1 deletion(-)
 create mode 100644 pickles/ctf-print.pk

diff --git a/pickles/Makefile.am b/pickles/Makefile.am
index 1372c40d..4586ea7a 100644
--- a/pickles/Makefile.am
+++ b/pickles/Makefile.am
@@ -1,5 +1,6 @@
 picklesdir = $(pkgdatadir)/pickles
-dist_pickles_DATA = elf.pk ctf.pk leb128.pk bpf.pk btf.pk btf-dump.pk bmp.pk \
+dist_pickles_DATA = elf.pk ctf.pk ctf-print.pk leb128.pk \
+                    bpf.pk btf.pk btf-dump.pk bmp.pk \
                     color.pk rgb24.pk id3v1.pk \
                     dwarf.pk dwarf-common.pk dwarf-frame.pk dwarf-pubnames.pk \
                     dwarf-types.pk time.pk argp.pk pktest.pk mbr.pk ustar.pk
diff --git a/pickles/ctf-print.pk b/pickles/ctf-print.pk
new file mode 100644
index 00000000..67adb9d8
--- /dev/null
+++ b/pickles/ctf-print.pk
@@ -0,0 +1,194 @@
+/* ctf-print.pk - Utilities for pretty-printing CTF information.  */
+
+/* Copyright (C) 2021 Oracle Inc.  */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* This file contains routines to print out CTF information.  Currently, it
+   only supports pretty-printing contents of a CTF dictionary.  */
+
+load ctf;
+
+/* Print all strings in the CTF string section.  */
+
+fun ctf_print_all_strings = (CTF_Dictionary ctf) void:
+{
+  print "#<\n";
+  for (s in ctf.strings)
+    printf("   %s \n", s);
+  print ">\n";
+}
+
+/* Print CTF integer type.  */
+
+fun ctf_print_int = (CTF_Integer_Type t) void:
+{
+  var signed_p = t.encoding & CTF_INT_SIGNED;
+  var char_p = t.encoding & CTF_INT_CHAR;
+  var bool_p = t.encoding & CTF_INT_BOOL;
+
+  printf (" %s%s%s",
+          signed_p ? "(signed)" : "",
+          char_p ? "(char)" : "",
+          bool_p ? "(bool)" : "");
+}
+
+/* Print CTF array type.  */
+
+fun ctf_print_array = (CTF_Array t) void:
+{
+  printf (" [%u32d], (ID %u32d)",
+          t.cta_nelems, t.cta_contents);
+}
+
+/* Print CTF slice type.  */
+
+fun ctf_print_slice = (CTF_Dictionary ctf, CTF_Slice t) void:
+{
+  printf (" [offset=%v,bits=%u16d] --> ID %u32d",
+          t.cts_offset, t.cts_bits/#b, t.cts_type);
+  /* XXX - forward declaration of ctf_print_type is not allowed.  So, this call
+     to ctf_print_type cannot be made.  */
+  /* ctf_print_type (ctf, ctf.types[t.cts_type-1], 0); */
+}
+
+/* Print CTF struct or union members.  */
+
+fun ctf_print_sou_members = (CTF_Dictionary ctf, CTF_Member[] t) void:
+{
+  for (m in t)
+    {
+      printf ("\n        [%v] %s (ID %u32d)",
+              m.ctm_offset,
+              ctf.get_string (m.ctm_name.offset),
+              m.ctm_type);
+    }
+}
+
+/* Print CTF enum type.  */
+
+fun ctf_print_enum = (CTF_Dictionary ctf, CTF_Enum[] enums) void:
+{
+  printf (" {");
+  for (e in enums)
+    printf ("%s = %u32d ",
+            ctf.get_string (e.cte_name.offset), e.cte_value);
+  printf ("}");
+}
+
+/* Print CTF information in the variable length number of bytes following the
+   CTF Type.  */
+
+fun ctf_print_vlen_data = (CTF_Dictionary ctf, CTF_Type t) void:
+{
+  try
+    {
+      ctf_print_int (t.data.integer);
+      return;
+    }
+  catch if E_elem {};
+
+  try
+    {
+      ctf_print_array (t.data.array);
+      return;
+    }
+  catch if E_elem {};
+  
+  try 
+    {
+      ctf_print_slice (ctf, t.data.slice);
+      return;
+    }
+  catch if E_elem {};
+
+  try
+    {
+      ctf_print_sou_members (ctf,t.data.members);
+      return;
+    }
+  catch if E_elem {};
+
+  try
+    {
+      ctf_print_enum (ctf, t.data.enum);
+      return;
+    }
+  catch if E_elem {};
+
+  /* XXX CTF_Func_Args declaration scoped within CTF_Type is not available.  */
+  /*  try { ctf_print_func (t.data.func); return; } catch if E_elem {} */
+}
+
+/* Print the given CTF type.  */
+
+fun ctf_print_type = (CTF_Dictionary ctf, CTF_Type t, int print_vlen) void:
+{
+  var name = ctf.get_string (t.name.offset);
+  printf (" (kind %u6d) %s %s",
+          t.info.kind, ctf_kind_str[t.info.kind], name);
+
+  if (t.info.kind == CTF_KIND_FORWARD)
+    printf (" %s ", ctf_kind_str[t.common.ttype]);
+
+  try printf ("(size %u32d)", t.common.size.normal);
+  catch if E_elem
+    {
+      if (t.info.kind != CTF_KIND_FORWARD)
+        {
+          printf (" --> ID %u32d:", t.common.ttype);
+          /* Offset by 1 as the ctf.types[] array is mapped starting at offset
+             0.  In the types section of the CTF dictionary, however, the first
+             valid CTF type is at offset 1.  */
+          ctf_print_type (ctf, ctf.types[t.common.ttype-1], 0);
+        }
+    }
+  if (print_vlen == 1)
+    ctf_print_vlen_data (ctf, t);
+}
+
+/* Print all CTF types in the given CTF dictionary.  */
+
+fun ctf_print_all_types = (CTF_Dictionary ctf) void:
+{
+  var type_id = 1;
+  /* To avoid recursion, print vlen bytes when a type is encountered the first
+     time only.  */
+  var print_vlen = 1;
+
+  print "#<\n";
+  for (t in ctf.types)
+    {
+      printf ("   %v: ", type_id);
+      ctf_print_type (ctf, t, print_vlen);
+      printf ("\n");
+      type_id++;
+    }
+
+  print ">\n";
+}
+
+/* Print all strings in the CTF string section.  */
+
+fun ctf_print_all_strings = (CTF_Dictionary ctf) void:
+{
+  var str_id = 1;
+
+  print "#<\n";
+  for (s in ctf.strings)
+    printf("   %v: %s \n", str_id++, s);
+  print ">\n";
+}
+
-- 
2.27.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]