1 : // Copyright 2012 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/grinder/grinder_util.h"
16 :
17 : #include "base/file_util.h"
18 : #include "mnemonics.h" // NOLINT
19 : #include "syzygy/common/defs.h"
20 : #include "syzygy/common/indexed_frequency_data.h"
21 : #include "syzygy/core/disassembler_util.h"
22 : #include "syzygy/pdb/omap.h"
23 : #include "syzygy/pdb/pdb_file.h"
24 : #include "syzygy/pdb/pdb_reader.h"
25 : #include "syzygy/pdb/pdb_util.h"
26 :
27 : namespace grinder {
28 :
29 : bool GetBasicBlockAddresses(const base::FilePath& pdb_path,
30 E : RelativeAddressVector* bb_addresses) {
31 E : DCHECK(bb_addresses != NULL);
32 :
33 E : pdb::PdbFile pdb_file;
34 E : pdb::PdbReader pdb_reader;
35 E : if (!pdb_reader.Read(pdb_path, &pdb_file)) {
36 i : LOG(ERROR) << "Failed to read PDB file: " << pdb_path.value();
37 i : return false;
38 : }
39 :
40 E : pdb::PdbInfoHeader70 pdb_header = {};
41 E : pdb::NameStreamMap name_stream_map;
42 E : if (!pdb::ReadHeaderInfoStream(pdb_file, &pdb_header, &name_stream_map)) {
43 i : LOG(ERROR) << "Failed to read PDB header info stream for PDB file: "
44 : << pdb_path.value();
45 i : return false;
46 : }
47 :
48 : pdb::NameStreamMap::const_iterator stream_id_it =
49 E : name_stream_map.find(common::kBasicBlockRangesStreamName);
50 E : if (stream_id_it == name_stream_map.end()) {
51 i : LOG(ERROR) << "Failed to find stream \""
52 : << common::kBasicBlockRangesStreamName << "\" in PDB file: "
53 : << pdb_path.value();
54 i : return false;
55 : }
56 :
57 : scoped_refptr<pdb::PdbStream> stream =
58 E : pdb_file.GetStream(stream_id_it->second);
59 E : if (stream.get() == NULL) {
60 i : LOG(ERROR) << "No stream with id " << stream_id_it->second
61 : << " in PDB file: " << pdb_path.value();
62 i : return false;
63 : }
64 :
65 E : stream->Seek(0);
66 E : bb_addresses->clear();
67 E : if (!stream->Read(bb_addresses)) {
68 i : LOG(ERROR) << "Failed to parse basic block range stream from PDB file: "
69 : << pdb_path.value();
70 i : return false;
71 : }
72 :
73 E : return true;
74 E : }
75 :
76 : } // namespace grinder
|