Skip to content

Commit

Permalink
Add bootstrapper to auto-detect runtime arch.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfilippov committed Jun 8, 2018
1 parent 18744cb commit 789b5a2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/ClrStack.x64/ClrStack.x64.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Mikhail Filippov</Authors>
<PlatformTarget>x64</PlatformTarget>
<OutputPath>..\build</OutputPath>
<OutputPath>..\..\build</OutputPath>
<RootNamespace>ClrStack</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/ClrStack.x86/ClrStack.x86.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Authors>Mikhail Filippov</Authors>
<PlatformTarget>x86</PlatformTarget>
<OutputPath>..\build</OutputPath>
<OutputPath>..\..\build</OutputPath>
<RootNamespace>ClrStack</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
22 changes: 13 additions & 9 deletions src/ClrStack/ClrStack.csproj
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Title>ClrStack</Title>
<Authors>Mikhail Filippov</Authors>
<Description>Tool for capturing managed stack traces from .NET applications.</Description>
<Description>Tool for capturing managed stack traces from .NET applications.</Description>
<PackageLicenseUrl>https://github.com/mfilippov/clrstack/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/mfilippov/clrstack</PackageProjectUrl>
<RepositoryUrl>https://github.com/mfilippov/clrstack</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Copyright>Mikhail Filippov</Copyright>
<OutputPath>..\build</OutputPath>
<IncludeBuildOutput>False</IncludeBuildOutput>
<OutputPath>..\..\build</OutputPath>
</PropertyGroup>
<ItemGroup>
<None Include="..\build\net461\ClrStack.x64.exe" Pack="True" PackagePath="."/>
<None Include="..\build\net461\ClrStack.x86.exe" Pack="True" PackagePath="."/>
<None Include="..\build\net461\Microsoft.Diagnostics.Runtime.dll" Pack="True" PackagePath="."/>
<ItemGroup>
<None Include="..\build\net461\ClrStack.x64.exe" Pack="True" PackagePath=".\lib\net461"/>
<None Include="..\build\net461\ClrStack.x86.exe" Pack="True" PackagePath=".\lib\net461"/>
<None Include="..\build\net461\Microsoft.Diagnostics.Runtime.dll" Pack="True" PackagePath=".\lib\net461"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClrStack.x64\ClrStack.x64.csproj">
<ReferenceOutputAssembly>False</ReferenceOutputAssembly>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</ProjectReference>
<ProjectReference Include="..\ClrStack.x86\ClrStack.x86.csproj">
<ReferenceOutputAssembly>False</ReferenceOutputAssembly>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="0.9.180305.1" />
</ItemGroup>
</Project>
58 changes: 58 additions & 0 deletions src/ClrStack/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace ClrStack
{
public static class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool IsWow64Process(IntPtr hProcess, out bool isWow64Process);

public static void Main(string[] args)
{
if (args.Length != 1 ||
!int.TryParse(args[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out var pid))
{
Console.Error.WriteLine("Usage: ClrStack.exe [PID]");
return;
}
var targetProcess = Process.GetProcessById(pid);
var archSuffix = Is64BitProcess(targetProcess) ? "x64" : "x86";
var platformSpecificExecutable = $"ClrStack.{archSuffix}.exe";
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var fullPath = assemblyDirectory != null
? Path.Combine(assemblyDirectory, platformSpecificExecutable)
: platformSpecificExecutable;
var process = new Process
{
StartInfo = new ProcessStartInfo(fullPath, pid.ToString())
{
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
}
};
process.OutputDataReceived += (sender, eventArgs) => Console.WriteLine(eventArgs.Data);
process.ErrorDataReceived += (sender, eventArgs) => Console.WriteLine(eventArgs.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.CancelOutputRead();
process.CancelErrorRead();
}

private static bool Is64BitProcess(Process process)
{
if (!Environment.Is64BitOperatingSystem)
return false;
IsWow64Process(process.Handle, out var isWow64Process);
return !isWow64Process;
}
}
}

0 comments on commit 789b5a2

Please sign in to comment.