summaryrefslogtreecommitdiff
path: root/src/vm/profilingenumerators.cpp
blob: b5c1e57b608b73847c607719f12c1f9e5ce45afd (plain)
1
2
3
4
5
6
7
8
9
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// FILE: ProfilingEnumerators.cpp
//
// All enumerators returned by the profiling API to enumerate objects or to catch up on
// the current CLR state (usually for attaching profilers) are defined in
// ProfilingEnumerators.h,cpp.
// 
// This cpp file contains implementations specific to the derived enumerator classes, as
// well as helpers for iterating over AppDomains, assemblies, modules, etc., that have
// been loaded enough that they may be made visible to profilers.
//

// 

#include "common.h"

#ifdef PROFILING_SUPPORTED

#include "proftoeeinterfaceimpl.h"
#include "profilingenumerators.h"

// ---------------------------------------------------------------------------------------
//  ProfilerFunctionEnum/ICorProfilerFunctionEnum implementation
// ---------------------------------------------------------------------------------------

BOOL ProfilerFunctionEnum::Init(BOOL fWithReJITIDs)
{
    CONTRACTL 
    {
        // Yay!
        NOTHROW;

        // Yay!
        // If we needs to get rejit ID, which requires a lock (which, in turn may switch us to
        // preemptive mode).
        if (fWithReJITIDs) GC_TRIGGERS; else GC_NOTRIGGER;

        // Yay!
        MODE_ANY;

        // Depending on our GC mode, the jit manager may have to take a
        // reader lock to prevent things from changing while reading...
        CAN_TAKE_LOCK;

        SO_NOT_MAINLINE;
    } CONTRACTL_END;

    EEJitManager::CodeHeapIterator heapIterator;
    while(heapIterator.Next())
    {
        MethodDesc *pMD = heapIterator.GetMethod();

        // On AMD64 JumpStub is used to call functions that is 2GB away.  JumpStubs have a CodeHeader 
        // with NULL MethodDesc, are stored in code heap and are reported by EEJitManager::EnumCode.
        if (pMD == NULL)
            continue;

        // There are two possible reasons to skip this MD.
        //
        // 1) If it has no metadata (i.e., LCG / IL stubs), then skip it
        //
        // 2) If it has no code compiled yet for it, then skip it. 
        //
        if (pMD->IsNoMetadata() || !pMD->HasNativeCode())
        {
            continue;
        }

        COR_PRF_FUNCTION * element = m_elements.Append();
        if (element == NULL)
        {
            return FALSE;
        }
        element->functionId = (FunctionID) pMD;

        if (fWithReJITIDs)
        {
            // This guy causes triggering and locking, while the non-rejitid case does not.
            element->reJitId = ReJitManager::GetReJitId(pMD, heapIterator.GetMethodCode());
        }
        else
        {
            element->reJitId = 0;
        }
    }

    return TRUE;
}

// ---------------------------------------------------------------------------------------
// Catch-up helpers
// 
// #ProfilerEnumGeneral
// 
// The following functions factor out the iteration code to ensure we only consider
// AppDomains, assemblies, modules, etc., that the profiler can safely query about. The
// parameters to these functions are of types that may have confusing syntax, but all
// that's going on is that the caller may supply an object instance and a member function
// on that object (non-static) to be called for each iterated item. This is just a
// statically-typed way of doing the usual pattern of providing a function pointer for
// the callback plus a void * context object to pass to the function. If the
// caller-supplied callback returns anything other than S_OK, the iteration code will
// stop iterating, and immediately propagate the callback's return value to the original
// caller. Start looking at code:ProfilerModuleEnum::Init for an example of how these
// helpers get used.
// 
// The reason we have helpers to begin with is so we can centralize the logic that
// enforces the following rather subtle invariants:
// 
//     * Provide enough entities that the profiler gets a complete set of entities from
//         the union of catch-up enumeration and "callbacks" (e.g., ModuleLoadFinished).
//     * Exclude entities that have unloaded to the point where it's no longer safe to
//         query information about them.
// 
// The catch-up spec summarizes this via the following timeline for any given entity:
// 
// Entity available in catch-up enumeration
//     < Entity's LoadFinished (or equivalent) callback is issued
//     < Entity NOT available from catch-up enumeration
//     < Entity's UnloadStarted (or equivalent) callback is issued
// 
// These helpers avoid duplicate code in the ProfilerModuleEnum implementation, and will
// also help avoid future duplicate code should we decide to provide more catch-up
// enumerations for attaching profilers to find currently loaded AppDomains, Classes,
// etc.
// 
// Note: The debugging API has similar requirements around which entities at which stage
// of loading are permitted to be enumerated over. See code:IDacDbiInterface#Enumeration
// for debugger details. Note that profapi's needs are not exactly the same. For example,
// Assemblies appear in the debugging API enumerations as soon as they begin to load,
// whereas Assemblies (like all other entities) appear in the profiling API enumerations
// once their load is complete (i.e., just before AssemblyLoadFinished).  Also,
// debuggers enumerate DomainModules and DomainAssemblies, whereas profilers enumerate
// Modules and Assemblies.
// 
// For information about other synchronization issues with profiler catch-up, see
// code:ProfilingAPIUtility::LoadProfiler#ProfCatchUpSynchronization
// 
// ---------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------
//
// Iterates through exactly those AppDomains that should be visible to the profiler, and
// calls a caller-supplied function to operate on each iterated AppDomain
//
// Arguments:
//    * callbackObj - Caller-supplied object containing the callback method to call for
//        each AppDomain
//    * callbackMethod - Caller-supplied method to call for each AppDomain. If this
//        method returns anything other than S_OK, then the iteration is aborted, and
//        callbackMethod's return value is returned to our caller.
//

template<typename CallbackObject>
HRESULT IterateAppDomains(CallbackObject * callbackObj, 
                          HRESULT (CallbackObject:: * callbackMethod)(AppDomain *))
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
        CAN_TAKE_LOCK;
        // (See comments in code:ProfToEEInterfaceImpl::EnumModules for info about contracts.)

        SO_NOT_MAINLINE;
    }
    CONTRACTL_END;

    // #ProfilerEnumAppDomains (See also code:#ProfilerEnumGeneral)
    // 
    // When enumerating AppDomains, ensure this timeline:
    // AD available in catch-up enumeration
    //     < AppDomainCreationFinished issued
    //     < AD NOT available from catch-up enumeration
    //     
    // The AppDomainIterator constructor parameter m_bActive is set to be TRUE below,
    // meaning only AppDomains in stage STAGE_ACTIVE or higher will be included
    // in the iteration.
    //     * AppDomainCreationFinished (with S_OK hrStatus) is issued once the AppDomain
    //         reaches STAGE_ACTIVE.
    AppDomainIterator appDomainIterator(TRUE);
    while (appDomainIterator.Next())
    {
        AppDomain * pAppDomain = appDomainIterator.GetDomain();

        // Of course, the AD could start unloading here, but if it does we're guaranteed
        // the profiler has had a chance to see the Unload callback for the AD, and thus
        // the profiler can block in that callback until it's done with the enumerator
        // we provide.

        // Call user-supplied callback, and cancel iteration if requested
        HRESULT hr = (callbackObj->*callbackMethod)(pAppDomain);
        if (hr != S_OK)
        {
            return hr;
        }
    }

    return S_OK;
}


//---------------------------------------------------------------------------------------
//
// Iterates through exactly those Modules that should be visible to the profiler, and
// calls a caller-supplied function to operate on each iterated Module.  Any module that
// is loaded domain-neutral is skipped.
//
// Arguments:
//    * pAppDomain - Only unshared modules loaded into this AppDomain will be iterated 
//    * callbackObj - Caller-supplied object containing the callback method to call for
//        each Module
//    * callbackMethod - Caller-supplied method to call for each Module. If this
//        method returns anything other than S_OK, then the iteration is aborted, and
//        callbackMethod's return value is returned to our caller.
//
// Notes:
//     * In theory, this could be broken down into an unshared assembly iterator that
//         takes a callback, and an unshared module iterator (based on an input
//         assembly) that takes a callback.  But that kind of granularity is unnecessary
//         now, and probably not useful in the future.  If that turns out to be wrong,
//         this can still be broken down that way later on.
//

template<typename CallbackObject>
HRESULT IterateUnsharedModules(AppDomain * pAppDomain, 
                               CallbackObject * callbackObj, 
                               HRESULT (CallbackObject:: * callbackMethod)(Module *))
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
        CAN_TAKE_LOCK;
    }
    CONTRACTL_END;

    // #ProfilerEnumAssemblies (See also code:#ProfilerEnumGeneral)
    // 
    // When enumerating assemblies, ensure this timeline:
    // Assembly available in catch-up enumeration
    //     < AssemblyLoadFinished issued
    //     < Assembly NOT available from catch-up enumeration
    //     < AssemblyUnloadStarted issued
    //     
    // The IterateAssembliesEx parameter below ensures we will only include assemblies at
    // load level >= FILE_LOAD_LOADLIBRARY.
    //     * AssemblyLoadFinished is issued once the Assembly reaches
    //         code:FILE_LOAD_LOADLIBRARY
    //     * AssemblyUnloadStarted is issued as a result of either:
    //         * Collectible assemblies unloading. Such assemblies will no longer be
    //             enumerable.
    //
    // Note: To determine what happens in a given load stage of a module or assembly,
    // look at the switch statement in code:DomainFile::DoIncrementalLoad, and keep in
    // mind that it takes cases on the *next* load stage; in other words, the actions
    // that appear in a case for a given load stage are actually executed as we attempt
    // to transition TO that load stage, and thus they actually execute while the module
    // / assembly is still in the previous load stage.
    //         
    // Note that the CLR may issue ModuleLoadFinished / AssemblyLoadFinished later, at
    // FILE_LOAD_EAGER_FIXUPS stage, if for some reason MLF/ALF hadn't been issued
    // earlier during FILE_LOAD_LOADLIBRARY. This does not affect the timeline, as either
    // way the profiler receives the notification AFTER the assembly would appear in the
    // enumeration.
    // 
    // Although it's called an "AssemblyIterator", it actually iterates over
    // DomainAssembly instances.
    AppDomain::AssemblyIterator domainAssemblyIterator = 
        pAppDomain->IterateAssembliesEx(
            (AssemblyIterationFlags) (kIncludeAvailableToProfilers | kIncludeExecution));
    CollectibleAssemblyHolder<DomainAssembly *> pDomainAssembly;
    
    while (domainAssemblyIterator.Next(pDomainAssembly.This()))
    {
        _ASSERTE(pDomainAssembly != NULL);
        _ASSERTE(pDomainAssembly->GetAssembly() != NULL);

        // We're only adding unshared assemblies / modules
        if (pDomainAssembly->GetAssembly()->IsDomainNeutral())
        {
            continue;
        }

        // #ProfilerEnumModules (See also code:#ProfilerEnumGeneral)
        // 
        // When enumerating modules, ensure this timeline:
        // Module available in catch-up enumeration
        //     < ModuleLoadFinished issued
        //     < Module NOT available from catch-up enumeration
        //     < ModuleUnloadStarted issued
        //     
        // The IterateModules parameter below ensures only modules at level >=
        // code:FILE_LOAD_LOADLIBRARY will be included in the iteration.
        // 
        // Details for module callbacks are the same as those for assemblies, so see
        // code:#ProfilerEnumAssemblies for info on how the timing works.
        DomainModuleIterator domainModuleIterator = 
            pDomainAssembly->IterateModules(kModIterIncludeAvailableToProfilers);
        while (domainModuleIterator.Next())
        {
            // Call user-supplied callback, and cancel iteration if requested
            HRESULT hr = (callbackObj->*callbackMethod)(domainModuleIterator.GetModule());
            if (hr != S_OK)
            {
                return hr;
            }
        }
    }

    return S_OK;
}

//---------------------------------------------------------------------------------------
// ProfilerModuleEnum implementation
//---------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------
// This is a helper class used by ProfilerModuleEnum when determining which shared
// modules should be added to the enumerator. See code:ProfilerModuleEnum::Init for how
// this gets used

class IterateAppDomainsForSharedModule
{
public:
    IterateAppDomainsForSharedModule(CDynArray< ModuleID > * pElements, Module * pModule)
        : m_pElements(pElements), m_pModule(pModule)
    {
        LIMITED_METHOD_CONTRACT;
    }

    //---------------------------------------------------------------------------------------
    // Callback passed to IterateAppDomains, that takes the currently iterated AppDomain,
    // and adds m_pModule to the enumerator if it's loaded into the AppDomain. See
    // code:ProfilerModuleEnum::Init for how this gets used.
    // 
    // Arguments:
    //      * pAppDomain - Current AppDomain being iterated.
    //
    // Return Value:
    //      * S_OK = the iterator should continue after we return.
    //      * S_FALSE = we verified m_pModule is loaded into this AppDomain, so no need
    //          for the iterator to continue with the next AppDomain
    //      * error indicating a failure
    //
    HRESULT AddSharedModuleForAppDomain(AppDomain * pAppDomain)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
            CANNOT_TAKE_LOCK;
        }
        CONTRACTL_END;

        DomainFile * pDomainFile = m_pModule->FindDomainFile(pAppDomain);
        if ((pDomainFile == NULL) || !pDomainFile->IsAvailableToProfilers())
        {
            // This AD doesn't contain a fully loaded DomainFile for m_pModule.  So continue
            // iterating with the next AD
            return S_OK;
        }

        ModuleID * pElement = m_pElements->Append();
        if (pElement == NULL)
        {
            // Stop iteration with error
            return E_OUTOFMEMORY;
        }

        // If we're here, we found a fully loaded DomainFile for m_pModule. So add
        // m_pModule to our array, and no need to look at other other ADs for this
        // m_pModule.
        *pElement = (ModuleID) m_pModule;
        return S_FALSE;
    }

private:
    // List of ModuleIDs in the enumerator we're building
    CDynArray< ModuleID > * m_pElements;    

    // Shared Module we're testing for load status in the iterated ADs.
    Module * m_pModule;                     
};


//---------------------------------------------------------------------------------------
//
// Callback passed to IterateAppDomains, that takes the currently iterated AppDomain,
// and then iterates through the unshared modules loaded into that AD.  See
// code:ProfilerModuleEnum::Init for how this gets used.
//
// Arguments:
//      * pAppDomain - Current AppDomain being iterated.
//
// Return Value:
//      * S_OK = the iterator should continue after we return.
//      * S_FALSE = we verified m_pModule is loaded into this AppDomain, so no need
//          for the iterator to continue with the next AppDomain
//      * error indicating a failure
//

HRESULT ProfilerModuleEnum::AddUnsharedModulesFromAppDomain(AppDomain * pAppDomain)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
        CAN_TAKE_LOCK;
    }
    CONTRACTL_END;

    return IterateUnsharedModules<ProfilerModuleEnum>(
        pAppDomain, 
        this, 
        &ProfilerModuleEnum::AddUnsharedModule);
}


//---------------------------------------------------------------------------------------
//
// Callback passed to IterateUnsharedModules, that takes the currently iterated unshared
// Module, and adds it to the enumerator. See code:ProfilerModuleEnum::Init for how this
// gets used.
//
// Arguments:
//      * pModule - Current Module being iterated.
//
// Return Value:
//      * S_OK = the iterator should continue after we return.
//      * error indicating a failure
//
HRESULT ProfilerModuleEnum::AddUnsharedModule(Module * pModule)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        CANNOT_TAKE_LOCK;
    }
    CONTRACTL_END;

    ModuleID * pElement = m_elements.Append();
    if (pElement == NULL)
    {
        return E_OUTOFMEMORY;
    }
    *pElement = (ModuleID) pModule;
    return S_OK;
}


//---------------------------------------------------------------------------------------
//
// Populate the module enumerator that's about to be given to the profiler. This is
// called from the ICorProfilerInfo3::EnumModules implementation.
// 
// This code controls how the above iterator helpers and callbacks are used, so you might
// want to look here first to understand how how the helpers and callbacks are used.
//
// Return Value:
//     HRESULT indicating success or failure.
//
HRESULT ProfilerModuleEnum::Init()
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
        CAN_TAKE_LOCK;
        // (See comments in code:ProfToEEInterfaceImpl::EnumModules for info about contracts.)

        SO_NOT_MAINLINE;
    }
    CONTRACTL_END;

    HRESULT hr = S_OK;

    // When an assembly or module is loaded into an AppDomain, a separate DomainFile is
    // created (one per pairing of the AppDomain with the module or assembly). This means
    // that we'll create multiple DomainFiles for the same module if it is loaded
    // domain-neutral (i.e., "shared"). The profiling API callbacks shield the profiler
    // from this, and only report a given module the first time it's loaded. So a
    // profiler sees only one ModuleLoadFinished for a module loaded domain-neutral, even
    // though the module may be used by multiple AppDomains. The module enumerator must
    // mirror the behavior of the profiling API callbacks, by avoiding duplicate Modules
    // in the module list we return to the profiler. So first add unshared modules (non
    // domain-neutral) to the enumerator, and then separately add any shared modules that
    // were loaded into at least one AD.

    // First, iterate through all ADs. For each one, call
    // AddUnsharedModulesFromAppDomain, which iterates through all UNSHARED modules and
    // adds them to the enumerator.
    hr = IterateAppDomains<ProfilerModuleEnum>(
        this, 
        &ProfilerModuleEnum::AddUnsharedModulesFromAppDomain);
    if (FAILED(hr))
    {
        return hr;
    }

    // Next, find all SHARED modules that have a corresponding DomainModule loaded into
    // at least one AppDomain with a load level high enough that it should be visible to
    // profilers. For each such shared module, add it once to the enumerator. Note that
    // enumerating assemblies/modules from the SharedDomain uses different internal CLR
    // interators than enumerating DomainAssemblies/DomainModules from AppDomains. So we
    // need to special case the iteration here. We could probably factor the following
    // into yet more iterator helpers the same way we've already done for the
    // DomainAssembly/DomainModule iterators above, but it's unclear how useful that
    // would be.
    SharedDomain::SharedAssemblyIterator sharedAssemblyIterator;
    while (sharedAssemblyIterator.Next())
    {
        Assembly * pAssembly = sharedAssemblyIterator.GetAssembly();
        Assembly::ModuleIterator moduleIterator = pAssembly->IterateModules();
        while (moduleIterator.Next())
        {
            Module * pModule = moduleIterator.GetModule();

            // Create an instance of this helper class (IterateAppDomainsForSharedModule)
            // to remember which Module we're testing. This will be used as our callback
            // for when we iterate AppDomains trying to find at least one AD that has loaded
            // pModule enough that pModule would be made visible to profilers.
            IterateAppDomainsForSharedModule iterateAppDomainsForSharedModule(&m_elements, pModule);
            hr = IterateAppDomains<IterateAppDomainsForSharedModule>(
                &iterateAppDomainsForSharedModule,
                &IterateAppDomainsForSharedModule::AddSharedModuleForAppDomain);
            if (FAILED(hr))
            {
                return hr;
            }
        }
    }
    return S_OK;
}


//---------------------------------------------------------------------------------------
//
// Callback passed to IterateAppDomains, that takes the currently iterated AppDomain, 
// and adds it to the enumerator if it has loaded the given module. See 
// code:IterateAppDomainContainingModule::PopulateArray for how this gets used.
//
// Arguments:
//      * pAppDomain - Current AppDomain being iterated.
//
// Return Value:
//      * S_OK = the iterator should continue after we return.
//      * error indicating a failure
//
HRESULT IterateAppDomainContainingModule::AddAppDomainContainingModule(AppDomain * pAppDomain)
{
    CONTRACTL
    {
        NOTHROW;
        // This method iterates over AppDomains, which adds, then releases, a reference on 
        // each AppDomain iterated.  This causes locking, and can cause triggering if the 
        // AppDomain gets destroyed as a result of the release. (See code:AppDomainIterator::Next 
        // and its call to code:AppDomain::Release.)
        GC_TRIGGERS;
        MODE_ANY;
        CAN_TAKE_LOCK;
        SO_NOT_MAINLINE;
    }
    CONTRACTL_END;

    DomainFile * pDomainFile = m_pModule->FindDomainFile(pAppDomain);
    if ((pDomainFile != NULL) && (pDomainFile->IsAvailableToProfilers()))
    {
        if (m_index < m_cAppDomainIds)
        {
            m_rgAppDomainIds[m_index] = reinterpret_cast<AppDomainID>(pAppDomain);
        }

        m_index++;
    }

    return S_OK;
}


//---------------------------------------------------------------------------------------
//
// Populate the array with AppDomains in which the given module has been loaded
//
// Return Value:
//     HRESULT indicating success or failure.
//
HRESULT IterateAppDomainContainingModule::PopulateArray()
{
    CONTRACTL
    {
        NOTHROW;
        // This method iterates over AppDomains, which adds, then releases, a reference on 
        // each AppDomain iterated.  This causes locking, and can cause triggering if the 
        // AppDomain gets destroyed as a result of the release. (See code:AppDomainIterator::Next 
        // and its call to code:AppDomain::Release.)
        GC_TRIGGERS;
        MODE_ANY;
        CAN_TAKE_LOCK;
        SO_NOT_MAINLINE;
    }
    CONTRACTL_END;

    HRESULT hr = IterateAppDomains<IterateAppDomainContainingModule>(
        this, 
        &IterateAppDomainContainingModule::AddAppDomainContainingModule);

    *m_pcAppDomainIds = m_index;

    return hr;
}

//---------------------------------------------------------------------------------------
//
// Populate the thread enumerator that's about to be given to the profiler. This is
// called from the ICorProfilerInfo4::EnumThread implementation.
// 
// Return Value:
//     HRESULT indicating success or failure.
//
HRESULT ProfilerThreadEnum::Init()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        CAN_TAKE_LOCK;
        SO_NOT_MAINLINE;
    }
    CONTRACTL_END;

    ThreadStoreLockHolder tsLock;

    Thread * pThread = NULL;

    //
    // Walk through all the threads with the lock taken
    // Because the thread enumeration status need to change before the ThreadCreated/ThreadDestroyed
    // callback, we need to:
    // 1. Include Thread::TS_FullyInitialized threads for ThreadCreated
    // 2. Exclude Thread::TS_Dead | Thread::TS_ReportDead for ThreadDestroyed
    //
    while((pThread = ThreadStore::GetAllThreadList(
        pThread, 
        Thread::TS_Dead | Thread::TS_ReportDead | Thread::TS_FullyInitialized,
        Thread::TS_FullyInitialized
        )))
    {
        if (pThread->IsGCSpecial())
            continue;

        *m_elements.Append() = (ThreadID) pThread;
    }
    
    return S_OK;
}


#endif // PROFILING_SUPPORTED