authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-10 01:00:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-10 10:26:54-04:00
logff051f8f5de47f4c5033c61fb70a5c5260f34dff
tree26c3f3b7a56f215b56c4e92cef5559dd5b1964b2
parent0489d06c249d16457d66523b5c407fc7ddeca45a
signaturelock-open Commit is signed but in an unrecognized format.

-fstack-report outputs JSON

See #3069

1 files changed, 52 insertions(+), 9 deletions(-)

src/stack_report.cpp+52-9
...@@ -36,6 +36,20 @@ static int compare_type_abi_sizes_desc(const void *a, const void *b) {...@@ -36,6 +36,20 @@ static int compare_type_abi_sizes_desc(const void *a, const void *b) {
36 return 0;36 return 0;
37}37}
3838
39static void start_child(FILE *f, size_t indent) {
40 fprintf(f, "\n");
41 for (size_t i = 0; i < indent; i += 1) {
42 fprintf(f, " ");
43 }
44}
45
46static void start_peer(FILE *f, size_t indent) {
47 fprintf(f, ",\n");
48 for (size_t i = 0; i < indent; i += 1) {
49 fprintf(f, " ");
50 }
51}
52
39static void tree_print_struct(FILE *f, ZigType *struct_type, size_t indent) {53static void tree_print_struct(FILE *f, ZigType *struct_type, size_t indent) {
40 ZigList<ZigType *> children = {};54 ZigList<ZigType *> children = {};
41 uint64_t sum_from_fields = 0;55 uint64_t sum_from_fields = 0;
...@@ -45,34 +59,63 @@ static void tree_print_struct(FILE *f, ZigType *struct_type, size_t indent) {...@@ -45,34 +59,63 @@ static void tree_print_struct(FILE *f, ZigType *struct_type, size_t indent) {
45 sum_from_fields += field->type_entry->abi_size;59 sum_from_fields += field->type_entry->abi_size;
46 }60 }
47 qsort(children.items, children.length, sizeof(ZigType *), compare_type_abi_sizes_desc);61 qsort(children.items, children.length, sizeof(ZigType *), compare_type_abi_sizes_desc);
48 fprintf(f, " (padding = %" ZIG_PRI_u64 ")\n", struct_type->abi_size - sum_from_fields);62
63 start_peer(f, indent);
64 fprintf(f, "\"padding\": \"%" ZIG_PRI_u64 "\"", struct_type->abi_size - sum_from_fields);
65
66 start_peer(f, indent);
67 fprintf(f, "\"fields\": [");
68
49 for (size_t i = 0; i < children.length; i += 1) {69 for (size_t i = 0; i < children.length; i += 1) {
70 if (i == 0) {
71 start_child(f, indent + 1);
72 } else {
73 start_peer(f, indent + 1);
74 }
75 fprintf(f, "{");
76
50 ZigType *child_type = children.at(i);77 ZigType *child_type = children.at(i);
51 tree_print(f, child_type, indent + 1);78 tree_print(f, child_type, indent + 2);
79
80 start_child(f, indent + 1);
81 fprintf(f, "}");
52 }82 }
83
84 start_child(f, indent);
85 fprintf(f, "]");
53}86}
5487
55static void tree_print(FILE *f, ZigType *ty, size_t indent) {88static void tree_print(FILE *f, ZigType *ty, size_t indent) {
56 for (size_t i = 0; i < indent; i += 1) {89 start_child(f, indent);
57 fprintf(f, " ");90 fprintf(f, "\"type\": \"%s\"", buf_ptr(&ty->name));
58 }91
59 fprintf(f, "%s: ", buf_ptr(&ty->name));92 start_peer(f, indent);
93 fprintf(f, "\"sizef\": \"");
60 pretty_print_bytes(f, ty->abi_size);94 pretty_print_bytes(f, ty->abi_size);
95 fprintf(f, "\"");
96
97 start_peer(f, indent);
98 fprintf(f, "\"size\": \"%" ZIG_PRI_u64 "\"", ty->abi_size);
99
61 switch (ty->id) {100 switch (ty->id) {
62 case ZigTypeIdFnFrame:101 case ZigTypeIdFnFrame:
63 return tree_print_struct(f, ty->data.frame.locals_struct, indent);102 return tree_print_struct(f, ty->data.frame.locals_struct, indent);
64 case ZigTypeIdStruct:103 case ZigTypeIdStruct:
65 return tree_print_struct(f, ty, indent);104 return tree_print_struct(f, ty, indent);
66 default:105 default:
67 fprintf(f, "\n");106 start_child(f, indent);
68 return;107 return;
69 }108 }
70}109}
71110
72void zig_print_stack_report(CodeGen *g, FILE *f) {111void zig_print_stack_report(CodeGen *g, FILE *f) {
73 if (g->largest_frame_fn == nullptr) {112 if (g->largest_frame_fn == nullptr) {
74 fprintf(f, "No async function frames in entire compilation.\n");113 fprintf(f, "{\"error\": \"No async function frames in entire compilation.\"}\n");
75 return;114 return;
76 }115 }
77 tree_print(f, g->largest_frame_fn->frame_type, 0);116 fprintf(f, "{");
117 tree_print(f, g->largest_frame_fn->frame_type, 1);
118
119 start_child(f, 0);
120 fprintf(f, "}\n");
78}121}