1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/refinery/analyzers/thread_analyzer.h"
16 :
17 : #include <dbghelp.h>
18 :
19 : #include "base/macros.h"
20 : #include "syzygy/refinery/analyzers/analyzer_util.h"
21 : #include "syzygy/refinery/process_state/refinery.pb.h"
22 :
23 m : namespace refinery {
24 :
25 : // static
26 m : const char ThreadAnalyzer::kThreadAnalyzerName[] = "ThreadAnalyzer";
27 :
28 m : Analyzer::AnalysisResult ThreadAnalyzer::Analyze(
29 m : const minidump::Minidump& minidump,
30 m : const ProcessAnalysis& process_analysis) {
31 m : DCHECK(process_analysis.process_state() != nullptr);
32 :
33 m : StackLayerPtr stack_layer;
34 m : process_analysis.process_state()->FindOrCreateLayer(&stack_layer);
35 :
36 m : minidump::Minidump::TypedThreadList threads = minidump.GetThreadList();
37 m : if (!threads.IsValid())
38 m : return ANALYSIS_ERROR;
39 :
40 m : for (const auto& thread : threads) {
41 : // Create the stack record.
42 m : StackRecordPtr stack_record;
43 m : AddressRange range(thread.Stack.StartOfMemoryRange,
44 m : thread.Stack.Memory.DataSize);
45 m : if (!range.IsValid())
46 m : return ANALYSIS_ERROR;
47 m : stack_layer->CreateRecord(range, &stack_record);
48 m : ThreadInformation* thread_info =
49 m : stack_record->mutable_data()->mutable_thread_info();
50 m : if (thread_info == nullptr)
51 m : return ANALYSIS_ERROR;
52 :
53 m : thread_info->set_thread_id(thread.ThreadId);
54 m : thread_info->set_suspend_count(thread.SuspendCount);
55 m : thread_info->set_priority_class(thread.PriorityClass);
56 m : thread_info->set_priority(thread.Priority);
57 m : thread_info->set_teb_address(thread.Teb);
58 :
59 : // TODO(siggi): Add to bytes layer?
60 m : minidump::Minidump::Stream thread_memory =
61 m : minidump.GetStreamFor(thread.Stack.Memory);
62 m : if (!thread_memory.IsValid())
63 m : return ANALYSIS_ERROR;
64 :
65 m : minidump::Minidump::Stream thread_context =
66 m : minidump.GetStreamFor(thread.ThreadContext);
67 m : if (!thread_context.IsValid())
68 m : return ANALYSIS_ERROR;
69 :
70 : // TODO(siggi): This ought to probe for the architecture somehow.
71 m : CONTEXT ctx = {};
72 m : if (!thread_context.ReadAndAdvanceElement(&ctx))
73 m : return ANALYSIS_ERROR;
74 m : ParseContext(ctx, thread_info->mutable_register_info());
75 m : }
76 :
77 m : return ANALYSIS_COMPLETE;
78 m : }
79 :
80 m : } // namespace refinery
|