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) {
3636 return 0;
3737}
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
3953static void tree_print_struct(FILE *f, ZigType *struct_type, size_t indent) {
4054 ZigList<ZigType *> children = {};
4155 uint64_t sum_from_fields = 0;
......@@ -45,34 +59,63 @@ static void tree_print_struct(FILE *f, ZigType *struct_type, size_t indent) {
4559 sum_from_fields += field->type_entry->abi_size;
4660 }
4761 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
4969 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
5077 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, "}");
5282 }
83
84 start_child(f, indent);
85 fprintf(f, "]");
5386}
5487
5588static void tree_print(FILE *f, ZigType *ty, size_t indent) {
56 for (size_t i = 0; i < indent; i += 1) {
57 fprintf(f, " ");
58 }
59 fprintf(f, "%s: ", buf_ptr(&ty->name));
89 start_child(f, indent);
90 fprintf(f, "\"type\": \"%s\"", buf_ptr(&ty->name));
91
92 start_peer(f, indent);
93 fprintf(f, "\"sizef\": \"");
6094 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
61100 switch (ty->id) {
62101 case ZigTypeIdFnFrame:
63102 return tree_print_struct(f, ty->data.frame.locals_struct, indent);
64103 case ZigTypeIdStruct:
65104 return tree_print_struct(f, ty, indent);
66105 default:
67 fprintf(f, "\n");
106 start_child(f, indent);
68107 return;
69108 }
70109}
71110
72111void zig_print_stack_report(CodeGen *g, FILE *f) {
73112 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");
75114 return;
76115 }
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");
78121}