Comments (6)
It seems to work for me if I do it that way,
For x86: var offset = (someVal2 & 0xFF) * 4;
For x64: var offset = (someVal2 & 0xFF) * 8;
(or just use runtime.PointerSize
)
It's works for instance (methodPtr) and for static method (methodPtrAux). Still need to check for all types @mattwarren posted above
Update
I tried it in several cases including with invocation list and it works, If somebody think of special case that I need to test, please let me know.
from clrmd.
I agree there should be a method in ClrMD to do the dirty work for you. I'll add that to the todo list.
Also, your last line of code you should be able to replace with this:
ClrMethod method = clrRuntime.GetMethodByHandle(methodDescriptorPtr);
return method.Name; // or return the method itself
As for it not working on all instances...sometimes _methodPtrAux is set and it means something slightly different (I think it's the MethodDesc itself but that's based on my fuzzy memory). If you handle that case then you should be able to handle all delegate types.
In any case I agree this should be part of ClrMD itself so I'll try to get to it next time I have a chance to work on ClrMD (probably a big effort the last week of December).
from clrmd.
As for it not working on all instances...sometimes _methodPtrAux is set and it means something slightly different (I think it's the MethodDesc itself but that's based on my fuzzy memory). If you handle that case then you should be able to handle all delegate types.
FYI this table from the CoreCLR source explains the difference usages of _methodPtr
and _methodPtrAux
:
// DELEGATE KINDS TABLE
//
// _target _methodPtr _methodPtrAux _invocationList _invocationCount
//
// 1- Instance closed 'this' ptr target method null null 0
// 2- Instance open non-virt delegate shuffle thunk target method null 0
// 3- Instance open virtual delegate Virtual-stub dispatch method id null 0
// 4- Static closed first arg target method null null 0
// 5- Static closed (special sig) delegate specialSig thunk target method first arg 0
// 6- Static opened delegate shuffle thunk target method null 0
// 7- Secure delegate call thunk MethodDesc (frame) target delegate creator assembly
//
// Delegate invoke arg count == target method arg count - 2, 3, 6
// Delegate invoke arg count == 1 + target method arg count - 1, 4, 5
//
// 1, 4 - MulticastDelegate.ctor1 (simply assign _target and _methodPtr)
// 5 - MulticastDelegate.ctor2 (see table, takes 3 args)
// 2, 6 - MulticastDelegate.ctor3 (take shuffle thunk)
// 3 - MulticastDelegate.ctor4 (take shuffle thunk, retrieve MethodDesc) ???
//
// 7 - Needs special handling
from clrmd.
Is this still supposed to work? Because at the moment I can not make it work.
GetMethodByHandle
return always null.
In the dump (x86) I have this: Func<string, bool> func = M;
(M is an instance method) I take the methodPtr of func
and do that:
var methodPtrWithOffset = methodPtr + 5;
heap.ReadPointer(methodPtrWithOffset + 2, out ulong someVal1);
heap.ReadPointer(methodPtrWithOffset + 1, out ulong someVal2);
heap.ReadPointer(methodPtrWithOffset + (someVal1 & 0xFF) * 8 + 3, out ulong baseMethodDesc);
var offset = (someVal2 & 0xFF) * 8;
var handle = baseMethodDesc + offset;
var method = runtime.GetMethodByHandle(handle);
from clrmd.
Looking back at this issue, it's a borderline case of whether I want to add something to ClrMD to answer this question directly. I try to keep ClrMD as the raw building blocks to implement what you need. In this case I think it's possible to correctly get the function from a delegate, but it looks like it's really complicated due to the implementation. When it's that fiddly to get right, that's a case where we should consider adding something to ClrMD to support it.
I'm leaving the issue open for now.
from clrmd.
I'm going to close this issue. I'm in the process of streamlining ClrMD and I don't think this will make the cut since there's example code to do this.
from clrmd.
Related Issues (20)
- ClrMD 3.0 will be the next release
- Get ClrValueType similar to !DumpVC HOT 3
- This repo is missing important files
- Could not find matching DAC for process on macOS after switching to ClrMD 2.1
- ILOffsetMap points to QuickJitted assembly code even after method has been Tier-1 compiled (.NET 7+) HOT 1
- Where are the release notes for ClrMD 3.0 ? HOT 3
- Threads are not update after flush HOT 3
- ClrMd floods syslog with stackwalk messages HOT 1
- CreateStackWalk failed error messages in syslog HOT 5
- Using CreateSnapshotAndAttach leaves a process running on Linux using ClrMD 3.1.456101 and .NET 8 HOT 2
- Cross-Platform Analysis? HOT 1
- Unable to attach to macOS process HOT 1
- JIT Compilation type returns none for MinOptJitted method in .NET 6.0
- Execute ClrHeap.EnumerateRoots stops process with exit code 139 HOT 1
- How to get stack variables and their values? HOT 1
- Cannot calculate memory size correctly, from CLR Objects .Size property HOT 2
- GetMethodByHandle is not able to retrieve constructed generic methods HOT 1
- Access to static object yields Invalid HOT 2
- Missing methods in ClrMd 2.0 HOT 6
- Enumerate over a ClrType that implements IEnumerable HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clrmd.