github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/debug/dwarf/type_test.go (about)

     1  // Copyright 2009 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dwarf_test
     6  
     7  import (
     8  	. "debug/dwarf"
     9  	"debug/elf"
    10  	"debug/macho"
    11  	"testing"
    12  )
    13  
    14  var typedefTests = map[string]string{
    15  	"t_ptr_volatile_int":                    "*volatile int",
    16  	"t_ptr_const_char":                      "*const char",
    17  	"t_long":                                "long int",
    18  	"t_ushort":                              "short unsigned int",
    19  	"t_func_int_of_float_double":            "func(float, double) int",
    20  	"t_ptr_func_int_of_float_double":        "*func(float, double) int",
    21  	"t_ptr_func_int_of_float_complex":       "*func(complex float) int",
    22  	"t_ptr_func_int_of_double_complex":      "*func(complex double) int",
    23  	"t_ptr_func_int_of_long_double_complex": "*func(complex long double) int",
    24  	"t_func_ptr_int_of_char_schar_uchar":    "func(char, signed char, unsigned char) *int",
    25  	"t_func_void_of_char":                   "func(char) void",
    26  	"t_func_void_of_void":                   "func() void",
    27  	"t_func_void_of_ptr_char_dots":          "func(*char, ...) void",
    28  	"t_my_struct":                           "struct my_struct {vi volatile int@0; x char@4 : 1@7; y int@4 : 4@27; z [0]int@8; array [40]long long int@8; zz [0]int@328}",
    29  	"t_my_struct1":                          "struct my_struct1 {zz [1]int@0}",
    30  	"t_my_union":                            "union my_union {vi volatile int@0; x char@0 : 1@7; y int@0 : 4@28; array [40]long long int@0}",
    31  	"t_my_enum":                             "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}",
    32  	"t_my_list":                             "struct list {val short int@0; next *t_my_list@8}",
    33  	"t_my_tree":                             "struct tree {left *struct tree@0; right *struct tree@8; val long long unsigned int@16}",
    34  }
    35  
    36  // As Apple converts gcc to a clang-based front end
    37  // they keep breaking the DWARF output.  This map lists the
    38  // conversion from real answer to Apple answer.
    39  var machoBug = map[string]string{
    40  	"func(*char, ...) void":                                 "func(*char) void",
    41  	"enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}": "enum my_enum {e1=1; e2=2; e3=-5; e4=-1530494976}",
    42  }
    43  
    44  func elfData(t *testing.T, name string) *Data {
    45  	f, err := elf.Open(name)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	d, err := f.DWARF()
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	return d
    55  }
    56  
    57  func machoData(t *testing.T, name string) *Data {
    58  	f, err := macho.Open(name)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	d, err := f.DWARF()
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	return d
    68  }
    69  
    70  func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf"), "elf") }
    71  
    72  func TestTypedefsMachO(t *testing.T) {
    73  	testTypedefs(t, machoData(t, "testdata/typedef.macho"), "macho")
    74  }
    75  
    76  func testTypedefs(t *testing.T, d *Data, kind string) {
    77  	r := d.Reader()
    78  	seen := make(map[string]bool)
    79  	for {
    80  		e, err := r.Next()
    81  		if err != nil {
    82  			t.Fatal("r.Next:", err)
    83  		}
    84  		if e == nil {
    85  			break
    86  		}
    87  		if e.Tag == TagTypedef {
    88  			typ, err := d.Type(e.Offset)
    89  			if err != nil {
    90  				t.Fatal("d.Type:", err)
    91  			}
    92  			t1 := typ.(*TypedefType)
    93  			var typstr string
    94  			if ts, ok := t1.Type.(*StructType); ok {
    95  				typstr = ts.Defn()
    96  			} else {
    97  				typstr = t1.Type.String()
    98  			}
    99  
   100  			if want, ok := typedefTests[t1.Name]; ok {
   101  				if seen[t1.Name] {
   102  					t.Errorf("multiple definitions for %s", t1.Name)
   103  				}
   104  				seen[t1.Name] = true
   105  				if typstr != want && (kind != "macho" || typstr != machoBug[want]) {
   106  					t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
   107  				}
   108  			}
   109  		}
   110  		if e.Tag != TagCompileUnit {
   111  			r.SkipChildren()
   112  		}
   113  	}
   114  
   115  	for k := range typedefTests {
   116  		if !seen[k] {
   117  			t.Errorf("missing %s", k)
   118  		}
   119  	}
   120  }