/ src / common / windows / dia_util.cc
dia_util.cc
 1  // Copyright 2013 Google LLC
 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 LLC 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  #ifdef HAVE_CONFIG_H
30  #include <config.h>  // Must come first
31  #endif
32  
33  #include "common/windows/dia_util.h"
34  
35  #include <atlbase.h>
36  
37  namespace google_breakpad {
38  
39  bool FindDebugStream(const wchar_t* name,
40                       IDiaSession* session,
41                       IDiaEnumDebugStreamData** debug_stream) {
42    CComPtr<IDiaEnumDebugStreams> enum_debug_streams;
43    if (FAILED(session->getEnumDebugStreams(&enum_debug_streams))) {
44      fprintf(stderr, "IDiaSession::getEnumDebugStreams failed\n");
45      return false;
46    }
47  
48    CComPtr<IDiaEnumDebugStreamData> temp_debug_stream;
49    ULONG fetched = 0;
50    while (SUCCEEDED(enum_debug_streams->Next(1, &temp_debug_stream, &fetched)) &&
51           fetched == 1) {
52      CComBSTR stream_name;
53      if (FAILED(temp_debug_stream->get_name(&stream_name))) {
54        fprintf(stderr, "IDiaEnumDebugStreamData::get_name failed\n");
55        return false;
56      }
57  
58      // Found the stream?
59      if (wcsncmp((LPWSTR)stream_name, name, stream_name.Length()) == 0) {
60        *debug_stream = temp_debug_stream.Detach();
61        return true;
62      }
63  
64      temp_debug_stream.Release();
65    }
66  
67    // No table was found.
68    return false;
69  }
70  
71  bool FindTable(REFIID iid, IDiaSession* session, void** table) {
72    // Get the table enumerator.
73    CComPtr<IDiaEnumTables> enum_tables;
74    if (FAILED(session->getEnumTables(&enum_tables))) {
75      fprintf(stderr, "IDiaSession::getEnumTables failed\n");
76      return false;
77    }
78  
79    // Iterate through the tables.
80    CComPtr<IDiaTable> temp_table;
81    ULONG fetched = 0;
82    while (SUCCEEDED(enum_tables->Next(1, &temp_table, &fetched)) &&
83           fetched == 1) {
84      void* temp = NULL;
85      if (SUCCEEDED(temp_table->QueryInterface(iid, &temp))) {
86        *table = temp;
87        return true;
88      }
89      temp_table.Release();
90    }
91  
92    // The table was not found.
93    return false;
94  }
95  
96  }  // namespace google_breakpad