Imported Upstream version 5.10.0.47

Former-commit-id: d0813289fa2d35e1f8ed77530acb4fb1df441bc0
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-01-24 17:04:36 +00:00
parent 88ff76fe28
commit e46a49ecf1
5927 changed files with 226314 additions and 129848 deletions

View File

@@ -0,0 +1,176 @@
//
// ConsoleDependencyGraph.cs: text output related code for dependency graph
//
// Author:
// Radek Doulik (rodo@xamarin.com)
//
// Copyright 2015 Xamarin Inc (http://www.xamarin.com).
//
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using LinkerAnalyzer.Core;
namespace LinkerAnalyzer
{
public class ConsoleDependencyGraph : DependencyGraph
{
public bool Tree = false;
public bool FlatDeps;
public void ShowDependencies (string raw, List<VertexData> verticesList, string searchString)
{
VertexData vertex = Vertex (raw);
if (vertex == null) {
Regex regex = new Regex (searchString);
int count = 0;
foreach (var v in verticesList) {
if (regex.Match (v.value) != Match.Empty) {
ShowDependencies (v);
count++;
}
}
if (count == 0)
Console.WriteLine ("\nUnable to find vertex: {0}", raw);
else
Console.WriteLine ("\nFound {0} matches", count);
} else
ShowDependencies (vertex);
}
void ShowFlatDependencies (VertexData vertex)
{
bool first = true;
var flatDeps = GetAllDependencies (vertex);
Console.WriteLine ();
foreach (var d in flatDeps) {
if (first) {
Console.WriteLine ($"Distance | {d.Item1.value} [total deps: {flatDeps.Count}]");
Line ();
first = false;
continue;
}
Console.WriteLine ($"{string.Format ("{0,8}", d.Item2)} | {d.Item1.value}");
}
}
public void ShowDependencies (VertexData vertex)
{
if (FlatDeps) {
ShowFlatDependencies (vertex);
return;
}
Header ("{0} dependencies", vertex.value);
if (vertex.parentIndexes == null) {
Console.WriteLine ("Root dependency");
} else {
int i = 0;
foreach (int index in vertex.parentIndexes) {
Console.WriteLine ("Dependency #{0}", ++i);
Console.WriteLine ("\t{0}", vertex.value);
var childVertex = Vertex (index);
Console.WriteLine ("\t| {0}{1}", childVertex.value, childVertex.DepsCount);
while (childVertex.parentIndexes != null) {
childVertex = Vertex (childVertex.parentIndexes [0]);
Console.WriteLine ("\t| {0}{1}", childVertex.value, childVertex.DepsCount);
}
if (Tree)
break;
}
}
}
public void ShowAllDependencies ()
{
Header ("All dependencies");
Console.WriteLine ("Types count: {0}", vertices.Count);
foreach (var vertex in vertices)
ShowDependencies (vertex);
}
public void ShowTypesDependencies ()
{
Header ("All types dependencies");
Console.WriteLine ("Deps count: {0}", Types.Count);
foreach (var type in Types)
ShowDependencies (type);
}
string Tabs (string key)
{
int count = Math.Max (1, 2 - key.Length / 8);
if (count == 1)
return "\t";
else
return "\t\t";
}
public void ShowStat (bool verbose = false)
{
Header ("Statistics");
if (verbose) {
foreach (var key in counts.Keys)
Console.WriteLine ("Vertex type:\t{0}{1}count:{2}", key, Tabs (key), counts [key]);
} else {
Console.WriteLine ("Assemblies:\t{0}", counts ["Assembly"]);
Console.WriteLine ("Modules:\t{0}", counts ["Module"]);
Console.WriteLine ("Types:\t\t{0}", counts ["TypeDef"]);
Console.WriteLine ("Fields:\t\t{0}", counts ["Field"]);
Console.WriteLine ("Methods:\t{0}", counts ["Method"]);
}
Console.WriteLine ();
Console.WriteLine ("Total vertices: {0}", vertices.Count);
}
public void ShowRoots ()
{
Header ("Root vertices");
int count = 0;
foreach (var vertex in vertices) {
if (vertex.parentIndexes == null) {
Console.WriteLine ("{0}", vertex.value);
count++;
}
}
Console.WriteLine ();
Console.WriteLine ("Total root vertices: {0}", count);
}
public void ShowRawDependencies (string raw)
{
Header ("Raw dependencies: '{0}'", raw);
ShowDependencies (raw, vertices, raw);
}
public void ShowTypeDependencies (string raw)
{
Header ("Type dependencies: '{0}'", raw);
ShowDependencies ("TypeDef:" + raw, Types, raw);
}
static readonly string line = new string ('-', 72);
void Line ()
{
Console.Write (line);
Console.WriteLine ();
}
void Header (string header, params object[] values)
{
string formatted = string.Format (header, values);
Console.WriteLine ();
Console.WriteLine ($"--- {formatted} {new string ('-', Math.Max (3, 67 - formatted.Length))}");
}
}
}

View File

@@ -0,0 +1,141 @@
//
// DependencyGraph.cs: linker dependencies graph
//
// Author:
// Radek Doulik (rodo@xamarin.com)
//
// Copyright 2015 Xamarin Inc (http://www.xamarin.com).
//
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Xml;
namespace LinkerAnalyzer.Core
{
public class VertexData {
public string value;
public List<int> parentIndexes;
public int index;
public string DepsCount {
get {
if (parentIndexes == null || parentIndexes.Count < 1)
return "";
return string.Format (" [{0} deps]", parentIndexes.Count);
}
}
};
public class DependencyGraph
{
protected List<VertexData> vertices = new List<VertexData> ();
public List<VertexData> Types = new List<VertexData> ();
Dictionary<string, int> indexes = new Dictionary<string, int> ();
protected Dictionary<string, int> counts = new Dictionary<string, int> ();
public void Load (string filename)
{
Console.WriteLine ("Loading dependency tree from: {0}", filename);
try {
using (var fileStream = File.OpenRead (filename))
using (var zipStream = new GZipStream (fileStream, CompressionMode.Decompress)) {
Load (zipStream);
}
} catch (Exception) {
Console.WriteLine ("Unable to open and read the dependecies.");
Environment.Exit (1);
}
}
void Load (GZipStream zipStream) {
using (XmlReader reader = XmlReader.Create (zipStream)) {
while (reader.Read ()) {
switch (reader.NodeType) {
case XmlNodeType.Element:
//Console.WriteLine (reader.Name);
if (reader.Name == "edge" && reader.IsStartElement ()) {
string b = reader.GetAttribute ("b");
string e = reader.GetAttribute ("e");
//Console.WriteLine ("edge value " + b + " --> " + e);
if (e != b) {
VertexData begin = Vertex (b, true);
VertexData end = Vertex (e, true);
if (end.parentIndexes == null)
end.parentIndexes = new List<int> ();
if (!end.parentIndexes.Contains (begin.index)) {
end.parentIndexes.Add (begin.index);
//Console.WriteLine (" end parent index: {0}", end.parentIndexes);
}
}
}
break;
default:
//Console.WriteLine ("node: " + reader.NodeType);
break;
}
}
}
}
public VertexData Vertex (string vertexName, bool create = false)
{
VertexData vertex;
try {
vertex = vertices [indexes [vertexName]];
} catch (KeyNotFoundException) {
if (create) {
int index = vertices.Count;
vertex = new VertexData () { value = vertexName, index = index };
vertices.Add (vertex);
indexes.Add (vertexName, index);
string prefix = vertexName.Substring (0, vertexName.IndexOf (':'));
if (counts.ContainsKey (prefix))
counts [prefix]++;
else
counts [prefix] = 1;
//Console.WriteLine ("prefix " + prefix + " count " + counts[prefix]);
if (prefix == "TypeDef") {
Types.Add (vertex);
}
} else
return null;
}
return vertex;
}
public VertexData Vertex (int index)
{
return vertices [index];
}
IEnumerable<Tuple<VertexData, int>> AddDependencies (VertexData vertex, HashSet<int> reachedVertices, int depth)
{
reachedVertices.Add (vertex.index);
yield return new Tuple<VertexData, int> (vertex, depth);
if (vertex.parentIndexes == null)
yield break;
foreach (var pi in vertex.parentIndexes) {
var parent = Vertex (pi);
if (reachedVertices.Contains (parent.index))
continue;
foreach (var d in AddDependencies (parent, reachedVertices, depth + 1))
yield return d;
}
}
public List<Tuple<VertexData, int>> GetAllDependencies (VertexData vertex)
{
return new List<Tuple<VertexData, int>> (AddDependencies (vertex, new HashSet<int> (), 0));
}
}
}

88
external/linker/analyzer/Main.cs vendored Normal file
View File

@@ -0,0 +1,88 @@
//
// Main.cs: Main program file of command line utility.
//
// Author:
// Radek Doulik (rodo@xamarin.com)
//
// Copyright 2015 Xamarin Inc (http://www.xamarin.com).
//
using System;
using Mono.Options;
using LinkerAnalyzer.Core;
namespace LinkerAnalyzer
{
static class MainClass
{
static void Main (string[] args)
{
bool showUsage = true;
bool showAllDeps = false;
bool showTypeDeps = false;
string typeName = null;
bool showRawDeps = false;
string rawName = null;
bool showRoots = false;
bool showSpaceUsage = false;
bool showStat = false;
bool showTypes = false;
bool reduceToTree = false;
bool verbose = false;
bool flatDeps = false;
var optionsParser = new OptionSet () {
{ "a|alldeps", "show all dependencies", v => { showAllDeps = v != null; } },
{ "h|help", "show this message and exit.", v => showUsage = v != null },
{ "r|rawdeps=", "show raw vertex dependencies. Raw vertex VALUE is in the raw format written by linker to the dependency XML file. VALUE can be regular expression", v => { showRawDeps = v != null; rawName = v; } },
{ "roots", "show root dependencies.", v => showRoots = v != null },
{ "stat", "show statistic of loaded dependencies.", v => showStat = v != null },
{ "tree", "reduce the dependency graph to the tree.", v => reduceToTree = v != null },
{ "types", "show all types dependencies.", v => showTypes = v != null },
{ "t|typedeps=", "show type dependencies. The VALUE can be regular expression", v => { showTypeDeps = v != null; typeName = v; } },
//{ "u|spaceusage", "show space analysis.", v => showSpaceUsage = v != null },
{ "f|flat", "show all dependencies per vertex and their distance", v => flatDeps = v != null },
{ "v|verbose", "be more verbose. Enables stat and roots options.", v => verbose = v != null },
};
if (args.Length > 0) {
showUsage = false;
optionsParser.Parse (args);
}
if (showUsage) {
Console.WriteLine ("Usage:\n\n\tillinkanalyzer [Options] <linker-dependency-file.xml.gz>\n\nOptions:\n");
optionsParser.WriteOptionDescriptions (Console.Out);
Console.WriteLine ();
return;
}
string dependencyFile = args [args.Length - 1];
ConsoleDependencyGraph deps = new ConsoleDependencyGraph () { Tree = reduceToTree, FlatDeps = flatDeps };
deps.Load (dependencyFile);
if (showSpaceUsage) {
// SpaceAnalyzer sa = new SpaceAnalyzer (System.IO.Path.GetDirectoryName (dependencyFile));
// sa.LoadAssemblies (verbose);
}
if (verbose) {
showStat = true;
showRoots = true;
}
if (showStat)
deps.ShowStat (verbose);
if (showRoots)
deps.ShowRoots ();
if (showRawDeps)
deps.ShowRawDependencies (rawName);
if (showTypeDeps)
deps.ShowTypeDependencies (typeName);
if (showAllDeps)
deps.ShowAllDependencies ();
else if (showTypes)
deps.ShowTypesDependencies ();
}
}
}

126
external/linker/analyzer/README.md vendored Normal file
View File

@@ -0,0 +1,126 @@
Linker analyzer is a command line tool to analyze dependencies, which
were recorded during linker processing, and led linker to mark an item
to keep it in the resulting linked assembly.
It works on an oriented graph of dependencies, which are collected and
dumped during the linker run. The vertices of this graph are the items
of interest like assemblies, types, methods, fields, linker steps,
etc. The edges represent the dependencies.
How to dump dependencies
------------------------
The linker analyzer needs a linker dependencies file as an input. It
can be retrieved by enabling dependencies dumping during linking of a
Xamarin.Android or a Xamarin.iOS project.
That can be done on the command line by setting
`LinkerDumpDependencies` property to `true` and building the
project. (make sure the LinkAssemblies task is called, it might
require cleaning the project sometimes) Usually it is enough to build
the project like this:
```msbuild /p:LinkerDumpDependencies=true /p:Configuration=Release YourAppProject.csproj```
After a successful build, there will be a linker-dependencies.xml.gz
file created, containing the information for the analyzer.
How to use the analyzer
-----------------------
Let say you would like to know, why a type, Android.App.Activity for
example, was marked by the linker. So run the analyzer like this:
```illinkanalyzer -t Android.App.Activity linker-dependencies.xml.gz```
Output:
```
Loading dependency tree from: linker-dependencies.xml.gz
--- Type dependencies: 'Android.App.Activity' -----------------------
--- TypeDef:Android.App.Activity dependencies -----------------------
Dependency #1
TypeDef:Android.App.Activity
| TypeDef:XA.App.MainActivity [2 deps]
| Assembly:XA.App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null [3 deps]
| Other:Mono.Linker.Steps.ResolveFromAssemblyStep
```
The output contains dependencies string(s), starting with the type and
continuing with the item of interest, which depends on the type. The
dependency could be result of multiple reasons. For example the type
was referenced from a method, or the type was listed in the linker xml
file to be protected.
In our example there is only one dependency string called `Dependency
#1`. It shows us that the type `Android.App.Activity` was marked
during processing of type `XA.App.MainActivity` by the linker. In this
case because the `MainActivity` type is based on the `Activity` type
and thus the linker marked it and kept it in the linked assembly. We
can also see that there are 2 dependencies for the `MainActivity`
class. Note that in the string (above) we see only 1st dependency of
the 2, the dependency on the assembly `XA.App`. And finally the
assembly vertex depends on the `ResolveFromAssemblyStep` vertex. So we
see that the assembly was processed in the `ResolveFromAssembly`
linker step.
Now we might want to see the `MainActivity` dependencies. That could
be done by the following analyzer run:
```illinkanalyzer -r TypeDef:XA.App.MainActivity linker-dependencies.xml.gz```
Output:
```
Loading dependency tree from: linker-dependencies.xml.gz
--- Raw dependencies: 'TypeDef:XA.App.MainActivity' -----------------
--- TypeDef:XA.App.MainActivity dependencies ------------------------
Dependency #1
TypeDef:XA.App.MainActivity
| Assembly:XA.App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null [3 deps]
| Other:Mono.Linker.Steps.ResolveFromAssemblyStep
Dependency #2
TypeDef:XA.App.MainActivity
| TypeDef:XA.App.MainActivity/<>c__DisplayClass1_0 [2 deps]
| TypeDef:XA.App.MainActivity [2 deps]
| Assembly:XA.App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null [3 deps]
| Other:Mono.Linker.Steps.ResolveFromAssemblyStep
```
Known issues
------------
Sometimes the linker processing is not straight forward and the
marking is postponed, like processing of some of the methods. They are
queued to be processed later. In such case the dependencies are
"interrupted" and the dependecy string for the method usually shows
just dependency on the Mark step.
Command line help
-----------------
```
Usage:
illinkanalyzer [Options] <linker-dependency-file.xml.gz>
Options:
-a, --alldeps show all dependencies
-h, --help show this message and exit.
-r, --rawdeps=VALUE show raw vertex dependencies. Raw vertex VALUE is
in the raw format written by linker to the
dependency XML file. VALUE can be regular
expression
--roots show root dependencies.
--stat show statistic of loaded dependencies.
--tree reduce the dependency graph to the tree.
--types show all types dependencies.
-t, --typedeps=VALUE show type dependencies. The VALUE can be regular
expression
-f, --flat show all dependencies per vertex and their distance
-v, --verbose be more verbose. Enables stat and roots options.
```

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{4F328B3E-39C1-4E48-8093-F24390C58A5E}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>LinkerAnalyzer</RootNamespace>
<AssemblyName>illinkanalyzer</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConsoleDependencyGraph.cs" />
<Compile Include="Main.cs" />
<Compile Include="LinkerAnalyzerCore\DependencyGraph.cs" />
<Compile Include="..\common\Mono.Options\Options.cs">
<Link>Options.cs</Link>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>