Skip to content

Commit

Permalink
Add support dump to dir passed by THREAD_DUMP_DIR environment variabl…
Browse files Browse the repository at this point in the history
…e. Migrate to new csproj. Create NuGet package.
  • Loading branch information
mfilippov committed Jun 8, 2018
1 parent e6948bd commit 6fa79d3
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 136 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,5 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/

build/
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Usage: ClrStack.exe PID
**Tool for capturing managed stack traces from .NET applications.**

```Usage: ClrStack.exe PID```

You could pass THREAD_DUMP_DIR environment variable with path to existing directory to write thread dump in file.
22 changes: 12 additions & 10 deletions src/ClrStack.sln
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClrStack", "ClrStack\ClrStack.csproj", "{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClrStack.x64", "ClrStack.x64\ClrStack.x64.csproj", "{4C4DD06E-14DF-4B70-B28B-3D4A4EB0DAC0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClrStack.x86", "ClrStack.x86\ClrStack.x86.csproj", "{DEBBF0D6-F59A-45BC-AC4F-5FC7CE976C71}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClrStack", "ClrStack\ClrStack.csproj", "{A5A25E3F-D2AE-4130-91D4-56A8D0AA8201}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}.Release|x64.ActiveCfg = Release|x64
{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}.Release|x64.Build.0 = Release|x64
{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}.Release|x86.ActiveCfg = Release|x86
{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}.Release|x86.Build.0 = Release|x86
{4C4DD06E-14DF-4B70-B28B-3D4A4EB0DAC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C4DD06E-14DF-4B70-B28B-3D4A4EB0DAC0}.Release|Any CPU.Build.0 = Release|Any CPU
{DEBBF0D6-F59A-45BC-AC4F-5FC7CE976C71}.Release|Any CPU.ActiveCfg = Release|x86
{DEBBF0D6-F59A-45BC-AC4F-5FC7CE976C71}.Release|Any CPU.Build.0 = Release|x86
{A5A25E3F-D2AE-4130-91D4-56A8D0AA8201}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5A25E3F-D2AE-4130-91D4-56A8D0AA8201}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions src/ClrStack.x64/ClrStack.x64.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
<Version>1.1.0</Version>
<PlatformTarget>x64</PlatformTarget>
<OutputPath>..\build</OutputPath>
<RootNamespace>ClrStack</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="0.9.180305.1"/>
</ItemGroup>
</Project>
70 changes: 70 additions & 0 deletions src/ClrStack.x64/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Diagnostics.Runtime;

namespace ClrStack
{
internal static class Program
{
private const string ThreadDumpDirEnvVar = "THREAD_DUMP_DIR";

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 threadDumpDir = Environment.GetEnvironmentVariable(ThreadDumpDirEnvVar);
if (!string.IsNullOrEmpty(threadDumpDir) && !Directory.Exists(threadDumpDir))
{
Console.Error.WriteLine($"Path [{threadDumpDir}] in THREAD_DUMP_DIR environment vaiable not exists or not directory");
return;
}

var output = new StringBuilder();
try
{
using (var target = DataTarget.AttachToProcess(pid, 5000, AttachFlag.NonInvasive))
{
var clrVersion = target.ClrVersions.FirstOrDefault();
if (clrVersion == null)
{
output.AppendLine($"CLR not found in process: {pid}");
return;
}

var runtime = clrVersion.CreateRuntime();

foreach (var clrThread in runtime.Threads)
{
if (!clrThread.IsAlive)
continue;
output.AppendLine($"Thread #{clrThread.ManagedThreadId}:");

foreach (var frame in clrThread.StackTrace)
output.AppendLine($"\tat {frame}");
}
}
}
catch (Exception ex)
{
output.AppendLine($"Cannot capture stack trace from process[{pid}]. Error: {ex.Message}");
}

if (string.IsNullOrEmpty(threadDumpDir))
{
Console.Write(output.ToString());
}
else
{
var fileName = $"{DateTime.Now:yyyy-MM-dd_HH_mm_ss.fff}.tdump";
File.WriteAllText(Path.Combine(threadDumpDir, fileName), output.ToString(), Encoding.UTF8);
}
}
}
}
19 changes: 19 additions & 0 deletions src/ClrStack.x86/ClrStack.x86.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
<Version>1.1.0</Version>
<PlatformTarget>x86</PlatformTarget>
<OutputPath>..\build</OutputPath>
<RootNamespace>ClrStack</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="0.9.180305.1"/>
</ItemGroup>
<ItemGroup>
<Compile Include="..\ClrStack.x64\Program.cs">
<Link>Program.cs</Link>
</Compile>
</ItemGroup>
</Project>
91 changes: 28 additions & 63 deletions src/ClrStack/ClrStack.csproj
Original file line number Diff line number Diff line change
@@ -1,64 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2ECDA63E-31B6-4562-B88D-FDC51DCA1959}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ClrStack</RootNamespace>
<AssemblyName>ClrStack</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<OutputPath>bin\x64\Release\</OutputPath>
<PlatformTarget>x64</PlatformTarget>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<OutputPath>bin\x86\Release\</OutputPath>
<PlatformTarget>x86</PlatformTarget>
<Prefer32bit>true</Prefer32bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Diagnostics.Runtime, Version=0.9.0.0, Culture=neutral, PublicKeyToken=4c5463f04c407af6">
<HintPath>..\packages\Microsoft.Diagnostics.Runtime.0.9.180305.01\lib\net40\Microsoft.Diagnostics.Runtime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net461</TargetFramework>
<Version>1.1.0</Version>
<Authors>Mikhail Filippov</Authors>
<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>
</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>
<ItemGroup>
<ProjectReference Include="..\ClrStack.x64\ClrStack.x64.csproj">
<ReferenceOutputAssembly>False</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\ClrStack.x86\ClrStack.x86.csproj">
<ReferenceOutputAssembly>False</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
</Project>
39 changes: 0 additions & 39 deletions src/ClrStack/Program.cs

This file was deleted.

19 changes: 0 additions & 19 deletions src/ClrStack/Properties/AssemblyInfo.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/ClrStack/packages.config

This file was deleted.

0 comments on commit 6fa79d3

Please sign in to comment.