Git Product home page Git Product logo

clr-samples's Introduction

CoreCLR Samples Repository

This repository houses native code samples that explore the Profiling APIs. In the future we may add samples for other types of tools.

clr-samples's People

Contributors

kevingosse avatar kspawa avatar mariohewardt avatar microsoft-github-policy-service[bot] avatar mjsabby avatar noahfalk avatar richlander avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clr-samples's Issues

Dynamic Assembly loading

Is it possible to dynamically load an assembly from the profiler? I tried to use libcoreclr.so and call some c# code that will perform assembly resolve but it fails to create another domain.

Add a profiler sample that does EnterLeave instrumentation using ELT APIs (not IL modification)

The sample we have today doesn't use SetEnterLeaveFunctionHooks3 APIs. The reason why we wrote the initial sample is because it is "portable", i.e. requires only a C++-11 compiler.

This issue tracks adding assembler stubs for the following configurations:

  1. Windows X64
  2. Linux X64
  3. Windows X86

From what I can tell the above are the most popular configurations for perf analysis. Furthermore (ARM64/32 on both Windows and Linux have been getting JIT support only recently). Linux X86 recently had a slew of calling convention changes so there maybe some work there too.

I am not able to run ./build.sh for ReJITEnterLeaveHooks on my OSX

I get the following error when I try to run build.sh on my mac for ReJITEnterLeaveHooks.

 CORECLR_PATH : <path to the artifacts folder in the runtime repo>
  BuildOS      : OSX
  BuildArch    : x64
  BuildType    : Release
  Building CorProfiler.so ... In file included from ClassFactory.cpp:4:
./ClassFactory.h:6:10: fatal error: 'unknwn.h' file not found
#include "unknwn.h"
         ^~~~~~~~~~
1 error generated.
In file included from CorProfiler.cpp:4:
./CorProfiler.h:7:10: fatal error: 'cor.h' file not found
#include "cor.h"
         ^~~~~~~
1 error generated.
In file included from dllmain.cpp:4:
./ClassFactory.h:6:10: fatal error: 'unknwn.h' file not found
#include "unknwn.h"
         ^~~~~~~~~~
1 error generated.
ILRewriter.cpp:4:10: fatal error: 'cor.h' file not found
#include "cor.h"
         ^~~~~~~
1 error generated.

Before doing this, I followed the steps on https://github.com/dotnet/runtime/blob/master/docs/workflow/building/coreclr/README.md to build coreCLR

After that I set the requisite environment variables to point to this repo which contains the build

Trying to find what am I missing here,

No output when run profiler

I am trying to run profiler and see the output which it generates. Maybe I am wrong in understanding what it should do, but I expect to see "Enter" when entering the function execution and "Leave" when execution finished.

I created test project with dotnet new && dotnet restore and try to run the profiler with following command (on Linux):

CORECLR_PROFILER={cf0d821e-299b-5307-a3d8-b283c03916dd} CORECLR_ENABLE_PROFILING=1 CORECLR_PROFILER_PATH=/filePath/to/CorProfiler.so dotnet run

But in output I see only Hello World! and no one Enter/Leave What's wrong with this run? Documentation states that I should use ./corerun myassembly.dll but I have no idea what is ./corerun and suppose it's an old alias of dotnet run

undefined reference to 'FreeLibraryAndExitThread'

Hello.

I'm trying to use the FreeLibraryAndExitThread function for detaching a profiler from CLR.
so I've build a coreclr and link my profiler with 'libcoreclrpal.a' library.

But when I try to build my profiler, linker outputs error like this

/usr/bin/ld: /tmp/MODProfiler-7b0549.o: in function `MODProfiler::InitializeForAttach(IUnknown*, void*, unsigned int)':
MODProfiler.cpp:(.text+0xf48): undefined reference to `FreeLibraryAndExitThread'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Is there any manual about how to link with coreclr static library for profiler?
I don't know what cause of this error.

this is my build.sh file

#!/bin/sh

[ -z "${CORECLR_PATH:-}" ] && CORECLR_PATH=~/coreclr
[ -z "${BuildOS:-}"      ] && BuildOS=Linux
[ -z "${BuildArch:-}"    ] && BuildArch=x64
[ -z "${BuildType:-}"    ] && BuildType=Debug
[ -z "${Output:-}"       ] && Output=CorProfiler.so

printf '  CORECLR_PATH : %s\n' "$CORECLR_PATH"
printf '  BuildOS      : %s\n' "$BuildOS"
printf '  BuildArch    : %s\n' "$BuildArch"
printf '  BuildType    : %s\n' "$BuildType"

printf '  Building %s ... ' "$Output"

CXX_FLAGS="$CXX_FLAGS --no-undefined -Wno-invalid-noreturn -fPIC -fms-extensions -DBIT64 -DPAL_STDCPP_COMPAT -DPLATFORM_UNIX -std=c++11"
INCLUDES="-I $CORECLR_PATH/src/pal/inc/rt -I $CORECLR_PATH/src/pal/prebuilt/inc -I $CORECLR_PATH/src/pal/inc -I $CORECLR_PATH/src/inc -I $CORECLR_PATH/bin/Product/$BuildOS.$BuildArch.$BuildType/inc"
LIBS="-lpthread -L $CORECLR_PATH/bin/Product/Linux.x64.Release/lib -lcoreclrpal"
SOURCE="ClassFactory.cpp MODProfiler.cpp dllmain.cpp Common.cpp ProfileManager.cpp FunctionMapper.cpp FunctionInfo.cpp Logger.cpp MetaDataUtil.cpp StringUtil.cpp ThreadManager.cpp CallstackInfo.cpp TreeNode.cpp sigparse.cpp SignatureParser.cpp Setting.cpp Environment.cpp"

clang++ -shared -o $Output $CXX_FLAGS $INCLUDES $LIBS $SOURCE $CORECLR_PATH/src/pal/prebuilt/idl/corprof_i.cpp

printf 'Done.\n'

Can't compile solution on linux.

When I try to run build.sh command on Linux, I've got an error:

Building CorProfiler.so ... CorProfiler.cpp:527:108: error: cannot initialize a parameter of type 'WCHAR *'
      (aka 'char16_t *') with an rvalue of type 'value_type *' (aka 'wchar_t *')
  ...nullptr, nullptr, nullptr, cchName, nullptr, &name[0]);
                                                  ^~~~~~~~

Also build documentation is irrelevant, it says that it should build eltprofiler, but there is no such url in clr-samples

https://github.com/Microsoft/clr-samples/tree/master/ProfilingAPI/ReJITEnterLeaveHooks#build

git clone http://github.com/Microsoft/clr-samples
cd clr-samples/profiling/eltprofiler
./build.sh

Identify layout of the repository

Should we have:

profilers/
debuggers/
hosting/

And inside each of these subfolders we have profilers/SimpleJittingProfiler, hosting/SimpleHosting, etc.

Intermittent segmentation faults with ELTProfiler

I'm hooking the sample ELTProfiler (with printf calls removed for convenience) to a very simple test C# application:

using System;

namespace foo
{
    class Program
{ 
        static int foo(int a, int b, int c, string d)
        {
            return a + b + c + d.Length;
        }

        static void Main(string[] args)
        {
            Console.Write("foo: {0:d}\n", foo(0xaa, 0xbb, 0xcc, "bar"));
        }
    }
}

Sometimes, it runs fine, but I frequently get segmentation faults. Here's an excerpt from a gdb session:

> gdb --args ~/ext/coreclr/Tools/dotnetcli/dotnet run --no-build
GNU gdb (Debian 8.1-4) 8.1
...
Reading symbols from /home/bertrand/ext/coreclr/Tools/dotnetcli/dotnet...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/bertrand/ext/coreclr/Tools/dotnetcli/dotnet run --no-build
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff5fb5700 (LWP 686)]
[New Thread 0x7ffff57b4700 (LWP 687)]
[New Thread 0x7ffff4fb2700 (LWP 688)]
[New Thread 0x7fffeffff700 (LWP 689)]
[New Thread 0x7fffef7fe700 (LWP 690)]
[New Thread 0x7fffeeffd700 (LWP 691)]
[New Thread 0x7fffe7df0700 (LWP 692)]
[New Thread 0x7fffe75ef700 (LWP 693)]
[New Thread 0x7ffff408f700 (LWP 694)]
[New Thread 0x7fff7d107700 (LWP 695)]
[New Thread 0x7fff57ffd700 (LWP 696)]
[New Thread 0x7fffec0fa700 (LWP 697)]
[New Thread 0x7fff577fc700 (LWP 698)]
[New Thread 0x7fff5691d700 (LWP 701)]
[New Thread 0x7fff5611c700 (LWP 703)]
[Thread 0x7fffe7df0700 (LWP 692) exited]
[New Thread 0x7fffec0b9700 (LWP 705)]
foo: 564

Thread 11 "dotnet" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fff7d107700 (LWP 695)]
0x00007ffff6f16ee3 in ProfileEnter ()
   from /home/bertrand/ext/coreclr/Tools/dotnetcli/shared/Microsoft.NETCore.App/2.1.1/libcoreclr.so
(gdb) bt
#0  0x00007ffff6f16ee3 in ProfileEnter ()
   from /home/bertrand/ext/coreclr/Tools/dotnetcli/shared/Microsoft.NETCore.App/2.1.1/libcoreclr.so
#1  0x00007ffff6f7fc2d in ProfileEnterNaked ()
   from /home/bertrand/ext/coreclr/Tools/dotnetcli/shared/Microsoft.NETCore.App/2.1.1/libcoreclr.so
#2  0x00007fff814ea327 in ?? ()
#3  0x00007fff814ea300 in ?? ()
#4  0x0000000000000000 in ?? ()
(gdb) info registers
rax            0x0	0
rbx            0x0	0
rcx            0x7fff7d106b90	140735291616144
rdx            0x7ffff7501c78	140737342610552
rsi            0x7fff7d106890	140735291615376
rdi            0x7ffff74914e0	140737342149856
rbp            0x7fff7d106880	0x7fff7d106880
rsp            0x7fff7d106660	0x7fff7d106660
r8             0x5	5
r9             0x0	0
r10            0x1	1
r11            0x0	0
r12            0x0	0
r13            0x7fff48001740	140734401353536
r14            0x7fff80c8f5a0	140735354041760
r15            0x0	0
rip            0x7ffff6f16ee3	0x7ffff6f16ee3 <ProfileEnter+243>
eflags         0x10206	[ PF IF RF ]
cs             0x33	51
ss             0x2b	43
ds             0x0	0
es             0x0	0
fs             0x0	0
gs             0x0	0

As you can see, it happens in ProfileEnterNaked/ProfileEnter.

I can't tell whether this is an issue with the profiler or CoreCLR. Maybe it should be reported there? Let me know if you need more information to reproduce this issue.

> ~/ext/coreclr/Tools/dotnetcli/dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   2.1.301
 Commit:    59524873d6

Runtime Environment:
 OS Name:     debian
 OS Version:  
 OS Platform: Linux
 RID:         debian-x64
 Base Path:   /home/bertrand/ext/coreclr/Tools/dotnetcli/sdk/2.1.301/

Host (useful for support):
  Version: 2.1.1
  Commit:  6985b9f684

.NET Core SDKs installed:
  2.1.301 [/home/bertrand/ext/coreclr/Tools/dotnetcli/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [/home/bertrand/ext/coreclr/Tools/dotnetcli/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [/home/bertrand/ext/coreclr/Tools/dotnetcli/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.1 [/home/bertrand/ext/coreclr/Tools/dotnetcli/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

This sample does not work on Windows CoreCLR, failed at LoadLibrayExW call

I built CoreCLR and CoreFX, as well as this sample on 7/20/2016. It builds OK but failed on Windows 10 during LoadProfiler() process.

Per instruction, I set following environment variables:
set CORECLR_ENABLE_PROFILING=1
set CORECLR_PROFILER={cf0d821e-299b-5307-a3d8-b283c03916dd}
set CORECLR_PROFILER_PATH=D:\CoreCLR\GitHub\coreclr-demo\runtime\ClrProfiler.dll

and tried to run: CoreRun.exe hello.exe

It is failed in at line 65 at: https://github.com/dotnet/coreclr/blob/master/src/utilcode/longfilepathwrappers.cpp:

    if (LongFile::IsPathNotFullyQualified(path) || SUCCEEDED(LongFile::NormalizePath(path)))
    {

ifndef FEATURE_PAL

        //Adding the assert to ensure relative paths which are not just filenames are not used for LoadLibrary Calls
        _ASSERTE(!LongFile::IsPathNotFullyQualified(path) || !LongFile::ContainsDirectorySeparator(path));

endif //FEATURE_PAL

        ret = LoadLibraryExW(path.GetUnicode(), hFile, dwFlags);
    }

    lastError = GetLastError();

It failed on LoadLibraryExW() call. right after the call, the last error number is 0xC1.

image

Error 0xC1 means:

ERROR_BAD_EXE_FORMAT
193 (0xC1)
%1 is not a valid Win32 application.

according to: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

Following is the callstack:

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.