1// Copyright 2013 Google Inc. All rights reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7//     * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9//     * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following disclaimer
11// in the documentation and/or other materials provided with the
12// distribution.
13//     * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "common/windows/dia_util.h"
30
31#include <atlbase.h>
32
33namespace google_breakpad {
34
35bool FindDebugStream(const wchar_t* name,
36                     IDiaSession* session,
37                     IDiaEnumDebugStreamData** debug_stream) {
38  CComPtr<IDiaEnumDebugStreams> enum_debug_streams;
39  if (FAILED(session->getEnumDebugStreams(&enum_debug_streams))) {
40    fprintf(stderr, "IDiaSession::getEnumDebugStreams failed\n");
41    return false;
42  }
43
44  CComPtr<IDiaEnumDebugStreamData> temp_debug_stream;
45  ULONG fetched = 0;
46  while (SUCCEEDED(enum_debug_streams->Next(1, &temp_debug_stream, &fetched)) &&
47         fetched == 1) {
48    CComBSTR stream_name;
49    if (FAILED(temp_debug_stream->get_name(&stream_name))) {
50      fprintf(stderr, "IDiaEnumDebugStreamData::get_name failed\n");
51      return false;
52    }
53
54    // Found the stream?
55    if (wcsncmp((LPWSTR)stream_name, name, stream_name.Length()) == 0) {
56      *debug_stream = temp_debug_stream.Detach();
57      return true;
58    }
59
60    temp_debug_stream.Release();
61  }
62
63  // No table was found.
64  return false;
65}
66
67bool FindTable(REFIID iid, IDiaSession* session, void** table) {
68  // Get the table enumerator.
69  CComPtr<IDiaEnumTables> enum_tables;
70  if (FAILED(session->getEnumTables(&enum_tables))) {
71    fprintf(stderr, "IDiaSession::getEnumTables failed\n");
72    return false;
73  }
74
75  // Iterate through the tables.
76  CComPtr<IDiaTable> temp_table;
77  ULONG fetched = 0;
78  while (SUCCEEDED(enum_tables->Next(1, &temp_table, &fetched)) &&
79         fetched == 1) {
80    void* temp = NULL;
81    if (SUCCEEDED(temp_table->QueryInterface(iid, &temp))) {
82      *table = temp;
83      return true;
84    }
85    temp_table.Release();
86  }
87
88  // The table was not found.
89  return false;
90}
91
92}  // namespace google_breakpad