/// <summary> /// Processes a SizeType array and returns the first valid size value. /// /// Throws ExpressionEvaluationFailed in case of an evaluation error and /// InvalidOperationException if valid <Size> node is not found. /// </summary> /// <returns></returns> internal async Task <uint> ParseSizeAsync(SizeType[] sizes, IVariableInformation varInfo, NatvisScope natvisScope) { if (sizes == null) { throw new InvalidOperationException("Valid <Size> node not found."); } foreach (SizeType curSize in sizes) { string errorMsg = null; string sizeText = null; try { if (!NatvisViewsUtil.IsViewVisible(varInfo.FormatSpecifier, curSize.IncludeView, curSize.ExcludeView) || !await _evaluator.EvaluateConditionAsync(curSize.Condition, varInfo, natvisScope)) { continue; } if (string.IsNullOrEmpty(curSize.Value)) { errorMsg = "The expression cannot be empty."; } else { IVariableInformation sizeVarInfo = await _evaluator.EvaluateExpressionAsync( curSize.Value, varInfo, natvisScope, null); sizeVarInfo.FallbackValueFormat = ValueFormat.Default; sizeText = await sizeVarInfo.ValueAsync(); } } catch (ExpressionEvaluationFailed ex) when(curSize.Optional) { errorMsg = ex.Message; } uint size = 0; if (errorMsg == null) { if (!ParseUint(sizeText, out size)) { errorMsg = "The expression's value was not a number. " + $"Expression='{curSize.Value}' Value='{sizeText}'"; } } if (errorMsg != null) { if (!curSize.Optional) { throw new ExpressionEvaluationFailed("Failed to evaluate <Size> node. " + errorMsg); } _logger.Verbose(() => $"Failed to evaluate <Size> node for type" + $" '{varInfo.TypeName}'. Reason: {errorMsg}"); } else { return(size); } } throw new InvalidOperationException("Valid <Size> node not found."); }
/// <summary> /// Asynchronously returns a formatted string based on the format string context and /// variable provided. /// In case this method does not succeeded, it returns the fallback value specified. /// </summary> /// <param name="formatStringContext">The format string context that the formatted string /// should rely on</param> /// <param name="variable">The variable context used to evaluate expressions.</param> /// <param name="subexpressionFormatter">Delegate used to format subexpressions found /// within the string.</param> /// <param name="elementName">The Natvis element name that should be reported in logs. /// </param> /// <param name="fallbackValue">Fallback value used in case this method fails.</param> internal async Task <string> FormatStringAsync( FormatStringContext formatStringContext, IVariableInformation variable, Func <IVariableInformation, Task <string> > subexpressionFormatter, string elementName, Func <Task <string> > fallbackValue) { try { if (++_curFormatStringElementDepth > _maxFormatDepth) { return("..."); } foreach (var element in formatStringContext.StringElements) { try { // e.g. <DisplayString>{{ size={_Mypair._Myval2._Mylast - // _Mypair._Myval2._Myfirst} }}</DisplayString> if (!NatvisViewsUtil.IsViewVisible(variable.FormatSpecifier, element.IncludeView, element.ExcludeView) || !await _evaluator.EvaluateConditionAsync( element.Condition, variable, formatStringContext.NatvisScope)) { continue; } return(await FormatValueAsync(element.Value, variable, formatStringContext.NatvisScope, subexpressionFormatter)); } catch (ExpressionEvaluationFailed ex) { if (!element.Optional) { throw; } string expression = variable == null ? null : await variable.ValueAsync(); _logger.Verbose( () => $"Failed to evaluate natvis {elementName} expression" + $" '{expression}' for type " + $"'{variable?.TypeName}'. Reason: {ex.Message}"); } catch (Exception ex) when(ex is NotSupportedException || ex is InvalidOperationException) { _logger.Log(NatvisLoggingLevel.ERROR, $"Failed to format natvis {elementName}. " + $"Reason: {ex.Message}."); break; } catch (Exception ex) { _logger.Error(() => $"Failed to format natvis {elementName} for type" + $" '{variable?.TypeName}'. " + $"Reason: {ex.Message}.{Environment.NewLine}" + $"Stacktrace:{Environment.NewLine}{ex.StackTrace}"); throw; } } return(await fallbackValue.Invoke()); } finally { --_curFormatStringElementDepth; } }
/// <summary> /// Find Natvis visualizer by variable. /// </summary> public VisualizerInfo FindType(IVariableInformation variable) { // Check for custom visualizers first. if (variable.CustomVisualizer != CustomVisualizer.None) { // Custom visualizers are keyed by pseudo-types "$" + CustomVisualizer enum name. string pseudoTypeName = "$" + variable.CustomVisualizer; VisualizerInfo visualizer; if (_visualizerCache.TryGetValue(pseudoTypeName, out visualizer)) { _logger.Verbose(() => $"Selected cached custom Natvis Visualizer " + $"'{visualizer?.Visualizer.Name ?? "null"} '" + $"for custom visualizer '{variable.CustomVisualizer}'"); return(visualizer); } visualizer = Scan(pseudoTypeName, TypeName.Parse(pseudoTypeName)); if (visualizer != null) { _logger.Verbose(() => $"Selected Natvis Visualizer " + $"'{visualizer.Visualizer.Name}'" + $" for custom visualizer '{variable.CustomVisualizer}'"); return(visualizer); } } string initialTypeName = variable.TypeName; _logger.Verbose($"Finding Natvis Visualizer for type '{initialTypeName}'"); if (_visualizerCache.ContainsKey(initialTypeName)) { VisualizerInfo visualizer = _visualizerCache[variable.TypeName]; _logger.Verbose(() => $"Selected cached Natvis Visualizer " + $"'{visualizer?.Visualizer.Name ?? "null"}' for type " + $"'{initialTypeName}'"); return(visualizer); } uint count = 0; foreach (string typeName in variable.GetAllInheritedTypes()) { var parsedName = TypeName.Parse(typeName); if (parsedName == null) { break; } _logger.Verbose( () => $"Scanning for Natvis Visualizer for type '{parsedName.BaseName}' for " + $"variable of type '{initialTypeName}'"); VisualizerInfo visualizer = Scan(initialTypeName, parsedName); if (visualizer != null) { _logger.Verbose( () => $"Selected Natvis Visualizer '{visualizer.Visualizer.Name}'" + $" for type '{initialTypeName}'"); return(visualizer); } ++count; // Safety check to make sure we don't get in to an expensive loop. if (count > _maxInheritedTypes) { _logger.Warning($"The '{initialTypeName}' type exceeds the" + $" maximum number ({_maxInheritedTypes}) of searchable base " + $"classes."); break; } } _logger.Verbose($"No Natvis Visualizer found for type '{initialTypeName}'"); return(null); }