From cdb7624451f5f641c4b6f8045869802e2ab12dca Mon Sep 17 00:00:00 2001 From: Charles Giessen Date: Mon, 14 Oct 2019 12:18:06 -0600 Subject: vulkaninfo: Added asserts to check output Several places in the output code of vulkaninfo had preconditions, like the indent level being zero or positive, but had no asserts checking the value. This commit adds such defensive measures, as well as a reordering IndentIncrease to be correct in regards to keeping indents zero or above. files modified - vulkaninfo/outputprinter.h - vulkaninfo/vulkaninfo.cpp Change-Id: Iafa03193407229d345f550c4c4c3a9dd1e235919 --- vulkaninfo/outputprinter.h | 14 ++++++--- vulkaninfo/vulkaninfo.cpp | 76 +++++++++++++++++++++++++--------------------- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/vulkaninfo/outputprinter.h b/vulkaninfo/outputprinter.h index 272b38fe..0707350b 100644 --- a/vulkaninfo/outputprinter.h +++ b/vulkaninfo/outputprinter.h @@ -196,15 +196,16 @@ class Printer { out << "\t\t\n"; out << "\t\n"; out << "\n"; + indents -= 3; break; case (OutputType::json): out << "\n}\n"; indents--; is_first_item.pop(); - assert(indents == 0); - assert(is_first_item.empty()); + assert(is_first_item.empty() && "mismatched number of ObjectStart/ObjectEnd or ArrayStart/ArrayEnd's"); break; } + assert(indents == 0 && "indents must be zero at program end"); }; OutputType Type() { return output_type; } @@ -238,6 +239,7 @@ class Printer { } Printer &SetElementIndex(int index) { + assert(index >= 0 && "cannot set element index to a negative value"); element_index = index; return *this; } @@ -248,7 +250,6 @@ class Printer { out << std::string(indents, '\t') << object_name; if (element_index != -1) { out << "[" << element_index << "]"; - element_index = -1; } out << ":\n"; int headersize = object_name.size() + 1; @@ -304,6 +305,7 @@ class Printer { } void ObjectEnd() { indents--; + assert(indents >= 0 && "indents cannot go below zero"); switch (output_type) { case (OutputType::text): @@ -353,6 +355,7 @@ class Printer { } void ArrayEnd() { indents--; + assert(indents >= 0 && "indents cannot go below zero"); switch (output_type) { case (OutputType::text): @@ -521,6 +524,7 @@ class Printer { switch (output_type) { case (OutputType::text): indents--; + assert(indents >= 0 && "indents cannot go below zero"); break; default: break; @@ -546,13 +550,15 @@ class Printer { bool set_object_name_as_type = false; // objects which are in an array - int element_index = -1; + int element_index = -1; // negative one is the sentinel value // json std::stack is_first_item; // utility void PrintHeaderUnderlines(int length) { + assert(indents >= 0 && "indents must not be negative"); + assert(length >= 0 && "length must not be negative"); if (set_next_header) { out << std::string(indents, '\t') << std::string(length, '=') << "\n"; set_next_header = false; diff --git a/vulkaninfo/vulkaninfo.cpp b/vulkaninfo/vulkaninfo.cpp index de1bd973..fbfda81a 100644 --- a/vulkaninfo/vulkaninfo.cpp +++ b/vulkaninfo/vulkaninfo.cpp @@ -174,8 +174,8 @@ void DumpPresentableSurfaces(Printer &p, AppInstance &inst, std::vector" + std::to_string(id++) + ")"; + device_out += " (ID: " + std::to_string(id++) + ")"; } p.PrintElement(device_out); } @@ -236,9 +236,9 @@ void DumpGroups(Printer &p, AppInstance &inst) { p.AddNewline(); group_id++; } + p.ObjectEnd(); + p.AddNewline(); } - p.ObjectEnd(); - p.AddNewline(); } void GpuDumpProps(Printer &p, AppGpu &gpu) { @@ -328,7 +328,7 @@ void GpuDumpQueueProps(Printer &p, std::vector &surfaces, AppQ // (kibi-, mebi-, gibi- etc.). #define kBufferSize 32 -static char *NumToNiceStr(const size_t sz) { +std::string NumToNiceStr(const size_t sz) { const char prefixes[] = "KMGTPEZY"; char buf[kBufferSize]; int which = -1; @@ -347,7 +347,7 @@ static char *NumToNiceStr(const size_t sz) { #else snprintf(buf, kBufferSize, "%.2f %sB", result, unit); #endif - return strndup(buf, kBufferSize); + return std::string(buf); } void GpuDumpMemoryProps(Printer &p, AppGpu &gpu) { @@ -356,7 +356,7 @@ void GpuDumpMemoryProps(Printer &p, AppGpu &gpu) { p.ArrayStart("memoryHeaps", gpu.memory_props.memoryHeapCount); for (uint32_t i = 0; i < gpu.memory_props.memoryHeapCount; ++i) { const VkDeviceSize memSize = gpu.memory_props.memoryHeaps[i].size; - std::string mem_size_human_readable = std::string(NumToNiceStr(static_cast(memSize))); + std::string mem_size_human_readable = NumToNiceStr(static_cast(memSize)); std::string mem_size_str = std::to_string(memSize) + " (" + to_hex_str(memSize) + ") (" + mem_size_human_readable + ")"; @@ -460,13 +460,28 @@ void GpuDumpFeatures(Printer &p, AppGpu &gpu) { } } -void GpuDumpFormatProperty(Printer &p, VkFormatProperties prop) { - p.SetOpenDetails(); - DumpVkFormatFeatureFlags(p, "linearTiling", prop.linearTilingFeatures); - p.SetOpenDetails(); - DumpVkFormatFeatureFlags(p, "optimalTiling", prop.optimalTilingFeatures); - p.SetOpenDetails(); - DumpVkFormatFeatureFlags(p, "bufferFeatures", prop.bufferFeatures); +void GpuDumpFormatProperty(Printer &p, VkFormat fmt, VkFormatProperties prop) { + if (p.Type() == OutputType::text) { + p.ObjectStart("Properies"); + } else if (p.Type() == OutputType::html) { + p.SetTitleAsType().ObjectStart(VkFormatString(fmt)); + } else if (p.Type() == OutputType::json) { + p.ObjectStart(""); + } + if (p.Type() == OutputType::html || p.Type() == OutputType::text) { + p.SetOpenDetails(); + DumpVkFormatFeatureFlags(p, "linearTiling", prop.linearTilingFeatures); + p.SetOpenDetails(); + DumpVkFormatFeatureFlags(p, "optimalTiling", prop.optimalTilingFeatures); + p.SetOpenDetails(); + DumpVkFormatFeatureFlags(p, "bufferFeatures", prop.bufferFeatures); + } else if (p.Type() == OutputType::json) { + p.PrintKeyValue("formatID", fmt); + p.PrintKeyValue("linearTilingFeatures", prop.linearTilingFeatures); + p.PrintKeyValue("optimalTilingFeatures", prop.optimalTilingFeatures); + p.PrintKeyValue("bufferFeatures", prop.bufferFeatures); + } + p.ObjectEnd(); } void GpuDevDump(Printer &p, AppGpu &gpu, pNextChainInfos &chainInfos) { @@ -500,9 +515,7 @@ void GpuDevDump(Printer &p, AppGpu &gpu, pNextChainInfos &chainInfos) { } p.ObjectEnd(); - p.ObjectStart("Properies"); - GpuDumpFormatProperty(p, props); - p.ObjectEnd(); + GpuDumpFormatProperty(p, VK_FORMAT_UNDEFINED, props); p.IndentIncrease(); p.ObjectEnd(); @@ -524,19 +537,12 @@ void GpuDevDump(Printer &p, AppGpu &gpu, pNextChainInfos &chainInfos) { VkFormatProperties props; vkGetPhysicalDeviceFormatProperties(gpu.phys_device, fmt, &props); - if (p.Type() == OutputType::html) { - p.SetTitleAsType().ObjectStart(VkFormatString(fmt)); - GpuDumpFormatProperty(p, props); - p.ObjectEnd(); - } else if (p.Type() == OutputType::json && - (props.linearTilingFeatures || props.optimalTilingFeatures || props.bufferFeatures)) { - p.SetTitleAsType().ObjectStart(""); - p.PrintKeyValue("formatID", fmt); - p.PrintKeyValue("linearTilingFeatures", props.linearTilingFeatures); - p.PrintKeyValue("optimalTilingFeatures", props.optimalTilingFeatures); - p.PrintKeyValue("bufferFeatures", props.bufferFeatures); - p.ObjectEnd(); - } + // if json, don't print format properties that are unsupported + if (p.Type() == OutputType::json && + (props.linearTilingFeatures || props.optimalTilingFeatures || props.bufferFeatures) == 0) + continue; + + GpuDumpFormatProperty(p, fmt, props); } } } @@ -545,8 +551,8 @@ void GpuDevDump(Printer &p, AppGpu &gpu, pNextChainInfos &chainInfos) { if (p.Type() == OutputType::json) { p.ArrayEnd(); } else { - p.ObjectEnd(); p.IndentIncrease(); + p.ObjectEnd(); } p.AddNewline(); @@ -578,8 +584,8 @@ void DumpGpu(Printer &p, AppGpu &gpu, bool show_formats, pNextChainInfos &chainI } if (p.Type() != OutputType::json) { - p.ObjectEnd(); p.IndentIncrease(); + p.ObjectEnd(); } p.AddNewline(); } @@ -736,8 +742,8 @@ int main(int argc, char **argv) { } } if (p->Type() != OutputType::json) { - p->ObjectEnd(); p->IndentIncrease(); + p->ObjectEnd(); } } -- cgit v1.2.3