netmodule.cpp 10.6 KB
Newer Older
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
1 2
#include "stdafx.h"

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
3

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
4
#include "netmodule.h"
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
5
#include "net.h"
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
6
#include "nettype.h"
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
7 8

#include <cor.h>
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
9

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
//
//#include <boost/regex.hpp>
//
/////////////////////////////////////////////////////////////////////////////////
//
//namespace {
//
/////////////////////////////////////////////////////////////////////////////////
//
//const boost::wregex  r1(L"\\?");
//const boost::wregex  r2(L"\\*");
//const boost::wregex  r3(L"\\.");
//
//bool fnmatch( const std::wstring& pattern, const std::wstring& str)
//{
//    std::wstring mask = pattern;
//    mask = boost::regex_replace(mask, r1, L".");
//    mask = boost::regex_replace(mask, r2, L".*");
//    mask = boost::regex_replace(mask, r3, L"\\.");
//
//    boost::wsmatch  matchResult;
//    boost::wregex  regEx(mask);
//    return boost::regex_match(str, matchResult, regEx);
//}
//
/////////////////////////////////////////////////////////////////////////////////
//
//}
//
///////////////////////////////////////////////////////////////////////////////
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
40 41 42 43 44 45 46 47 48 49 50 51

namespace kdlib {

///////////////////////////////////////////////////////////////////////////////

ModulePtr loadNetModule( MEMOFFSET_64 offset )
{
    return ModulePtr( new NetModule(offset) );
}

///////////////////////////////////////////////////////////////////////////////

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
52
void NetModule::initCorDebugModule()
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
53
{
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
54 55
    if (m_corDebugModule)
        return;
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
56

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
57 58 59 60
    CComPtr<ICorDebugAppDomainEnum>  appDomainsEnum;
    HRESULT  hres = g_netMgr->targetProcess()->EnumerateAppDomains(&appDomainsEnum);
    if (FAILED(hres) )
        throw DbgException("Failed ICorDebugProcess::EnumerateAppDomains");
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
61

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
62 63 64 65 66
    while(true)
    {
        CComPtr<ICorDebugAppDomain>  appDomain;
        ULONG fetchedAppDomain;
        hres = appDomainsEnum->Next(1, &appDomain, &fetchedAppDomain);
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
67

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
68 69
        if (FAILED(hres))
            throw DbgException("Failed ICorDebugAppDomainEnum::Next");
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
70

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
71 72
        if (S_OK != hres)
            break;
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
73

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
74 75
        CComPtr<ICorDebugAssemblyEnum>  assemblyEnum;
        hres = appDomain->EnumerateAssemblies(&assemblyEnum);
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
76

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
77 78
        if (FAILED(hres))
            throw DbgException("Failed ICorDebugAppDomain::EnumerateAssemblies");
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
79

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
80 81 82 83 84
        while(true)
        {
            CComPtr<ICorDebugAssembly>  assembly;
            ULONG  fetchedAssembly;
            hres = assemblyEnum->Next(1, &assembly, &fetchedAssembly);
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
85

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
86 87
            if (FAILED(hres))
                throw DbgException("Failed ICorDebugAssemblyEnum::Next");
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
88

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
89 90
            if (S_OK != hres)
                break;
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
91

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
92 93 94
            std::vector<wchar_t>  assemblyNameBuf(0x100);
            ULONG32  assemblyNameLen;
            assembly->GetName(assemblyNameBuf.size(), &assemblyNameLen, &assemblyNameBuf.front());
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
95

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            CComPtr<ICorDebugModuleEnum>  moduleEnum;
            hres = assembly->EnumerateModules(&moduleEnum);

            if (FAILED(hres))
                throw DbgException("Failed ICorDebugAssembly::EnumerateModules");

            while(true)
            {
                CComPtr<ICorDebugModule>  module;
                ULONG  fetchedModule;
                hres = moduleEnum->Next(1, &module, &fetchedModule);

                if (FAILED(hres))
                    throw DbgException("Failed ICorDebugModuleEnum::Next");

                if (S_OK != hres)
                    break;

                CORDB_ADDRESS  baseAddr;
                hres = module->GetBaseAddress(&baseAddr);
                if (FAILED(hres))
                    throw DbgException("Failed ICorDebugModule::GetBaseAddress");
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
118

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
119 120 121 122 123 124 125 126 127
                if ( baseAddr == getBase() )
                {
                    m_corDebugModule = module;
                    m_metaDataProvider = getMetaDataProvider(module);
                    return;
                }
            }
        }
    }
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
128

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
129 130 131 132 133 134 135 136 137 138 139
    throw DbgException("Failed to find managed module");
}

///////////////////////////////////////////////////////////////////////////////

TypeNameList NetModule::enumTypes(const std::wstring& mask)
{
    initCorDebugModule();

    return m_metaDataProvider->getTypeNames(mask);
}
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
140

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
141 142 143 144 145 146 147 148 149 150
///////////////////////////////////////////////////////////////////////////////

MEMOFFSET_64 NetModule::getImageBase(ICorDebugModule* module)
{
    CORDB_ADDRESS  address;
    HRESULT  hres = module->GetBaseAddress(&address);
    if ( FAILED(hres) )
        throw DbgException("Failed ICorDebugModule::GetBaseAddress");

    return address;
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
151 152 153 154
}

///////////////////////////////////////////////////////////////////////////////

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
155 156
TypeInfoPtr NetModule::getTypeByName( const std::wstring &typeName )
{
157
    //initCorDebugModule();
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
158

159
    throw TypeException(typeName, L"not found");
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
160

161 162
    //auto typeToken = m_metaDataProvider->getTypeTokenByName(typeName);
    //return m_metaDataProvider->getTypeByName(type);
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
163
}
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
164

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
///////////////////////////////////////////////////////////////////////////////

}



















SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363


//
////IXCLRDataModule*  NetModule::get()
////{
////    if ( m_xclrDataModule )
////        return m_xclrDataModule;
////
////    HRESULT  hres = g_clrDataTarget.get()->GetModuleByAddress(m_base, &m_xclrDataModule);
////
////    if ( S_OK != hres )
////        throw DbgException("failed IXCLRDataProcess::GetModuleByAddress");
////
////    return m_xclrDataModule;
////}
//
/////////////////////////////////////////////////////////////////////////////////
//
//TypeNameList NetModule::enumTypes(const std::wstring& mask)
//{
//
// /*   TypeNameList  typeNameList;
//
//    CLRDATA_ENUM  enumAppDomainHandle;
//
//    HRESULT hres = g_clrDataTarget.get()->StartEnumAppDomains(&enumAppDomainHandle);
//    if ( FAILED(hres) )
//        throw DbgException("failed IXCLRDataModule::StartEnumAppDomains");
//
//    auto  enumAppDomainDeleter = [=](CLRDATA_ENUM*){g_clrDataTarget.get()->EndEnumAppDomains(enumAppDomainHandle); };
//    std::unique_ptr<CLRDATA_ENUM, decltype(enumAppDomainDeleter)>  scopedAppDomainEnum(&enumAppDomainHandle, enumAppDomainDeleter);
//
//    do {
//
//        CComPtr<IXCLRDataAppDomain>  appDomain;
//        hres = g_clrDataTarget.get()->EnumAppDomain(&enumAppDomainHandle, &appDomain);
//
//        if ( hres != S_OK )
//            break;
//
//        CLRDATA_ENUM  enumTypeHandle;
//
//        hres = get()->StartEnumTypeInstances(appDomain, &enumTypeHandle);
//        if ( FAILED(hres) )
//            throw DbgException("failed IXCLRDataModule::StartEnumTypeInstances");
//
//
//        auto  enumTypeDeleter = [=](CLRDATA_ENUM*){get()->EndEnumTypeInstances(enumTypeHandle);};
//        std::unique_ptr<CLRDATA_ENUM, decltype(enumTypeDeleter)>  scopedTypeEnum(&enumTypeHandle, enumTypeDeleter);
//
//        do {
//
//            CComPtr<IXCLRDataTypeInstance>  typeInstance;
//            hres = get()->EnumTypeInstance(&enumTypeHandle, &typeInstance);
//
//            if ( hres != S_OK )
//                break;
//
//            std::wstring  typeName;
//            hres = clrGetName(static_cast<IXCLRDataTypeInstance*>(typeInstance), &IXCLRDataTypeInstance::GetName, typeName);
//
//            if ( SUCCEEDED(hres) && fnmatch(mask, typeName) )
//            {
//                typeNameList.push_back(typeName);
//            }
//
//        } while(true);
//
//    } while(true);
//
//    return typeNameList;*/
//
//    /*CLRDATA_ENUM   typeEnum;
//
//    enumAssemblies();
//    enumAppDomain();
//
//    HRESULT hres = get()->StartEnumTypeDefinitions(&typeEnum);
//    if ( FAILED(hres) )
//        throw DbgException("failed IXCLRDataModule::StartEnumTypeDefinitions");
//
//    auto del = [=](CLRDATA_ENUM*){get()->EndEnumTypeDefinitions(typeEnum); };
//    std::unique_ptr<CLRDATA_ENUM, decltype(del)>  scopedTypeEnum(&typeEnum, del);
//
//    TypeNameList  typeNameList;
//
//    do {
//
//        CComPtr<IXCLRDataTypeDefinition>  typeDef;
//        hres = get()->EnumTypeDefinition(&typeEnum, &typeDef);
//        if (S_OK != hres )
//            break;
//
//        std::wstring  typeName;
//        hres = clrGetName(static_cast<IXCLRDataTypeDefinition*>(typeDef), &IXCLRDataTypeDefinition::GetName, typeName);
//        if ( SUCCEEDED(hres) && fnmatch(mask, typeName) )
//        {
//            typeNameList.push_back(typeName);
//        }
//
//    } while(true);
//
//    return typeNameList;
//    */
//}
//
/////////////////////////////////////////////////////////////////////////////////
//
///*
//void NetModule::enumAssemblies()
//{
//    CLRDATA_ENUM  enumHandle;
//
//    HRESULT hres = get()->StartEnumAssemblies(&enumHandle);
//
//    if ( FAILED(hres) )
//        throw DbgException("failed IXCLRDataModule::StartEnumAssemblies");
//
//    auto  enumDeleter = [=](CLRDATA_ENUM*){get()->EndEnumTypeDefinitions(enumHandle); };
//    std::unique_ptr<CLRDATA_ENUM, decltype(enumDeleter)>  scopedTypeEnum(&enumHandle, enumDeleter);
//
//    do {
//
//        CComPtr<IXCLRDataAssembly>  dataAssembly;
//        hres = get()->EnumAssembly(&enumHandle, &dataAssembly);
//
//        if (S_OK != hres )
//            break;
//
//        std::wstring  name;
//        hres = clrGetName(static_cast<IXCLRDataAssembly*>(dataAssembly), &IXCLRDataAssembly::GetName, name);
//
//        std::wstring  displayName;
//        hres = clrGetName(static_cast<IXCLRDataAssembly*>(dataAssembly), &IXCLRDataAssembly::GetDisplayName, displayName);
//
//        std::wstring  fileName;
//        hres = clrGetName(static_cast<IXCLRDataAssembly*>(dataAssembly), &IXCLRDataAssembly::GetFileName, fileName);
//
//
//        auto a = 0;
//
//    } while(true);
//}
//
/////////////////////////////////////////////////////////////////////////////////
//
//void NetModule::enumAppDomain()
//{
//    CLRDATA_ENUM  enumHandle;
//
//    HRESULT hres = g_clrDataTarget.get()->StartEnumAppDomains(&enumHandle);
//
//    if ( FAILED(hres) )
//        throw DbgException("failed IXCLRDataModule::StartEnumAppDomains");
//
//    auto  enumDeleter = [=](CLRDATA_ENUM*){get()->EndEnumAppDomains(enumHandle); };
//    std::unique_ptr<CLRDATA_ENUM, decltype(enumDeleter)>  scopedTypeEnum(&enumHandle, enumDeleter);
//
//    do {
//
//        CComPtr<IXCLRDataAppDomain>  appDomain;
//        hres = g_clrDataTarget.get()->EnumAppDomain(&enumHandle, &appDomain);
//
//        if ( hres != S_OK )
//            break;
//
//        std::wstring  name;
//        clrGetName(static_cast<IXCLRDataAppDomain*>(appDomain), &IXCLRDataAppDomain::GetName, name);
//
//        auto a = 0;
//
//    } while(true);
//}
//
/////////////////////////////////////////////////////////////////////////////////*/
//
//}