summaryrefslogtreecommitdiff
path: root/dbus/dbus-backtrace-win.c
blob: d9bc2dc4f7eebccc942620c93043abea12c18212 (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-backtrace-win.c Backtrace Generator
 *
 * Copyright 2004 Eric Poech
 * Copyright 2004 Robert Shearman
 * Copyright 2010 Patrick von Reth <patrick.vonreth@gmail.com>
 * Copyright 2015 Ralf Habacker <ralf.habacker@freenet.de>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include "dbus-internals.h"
#include "dbus-sysdeps.h"

#if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_ENABLE_EMBEDDED_TESTS)

#if defined(_MSC_VER) || defined(DBUS_WINCE)
# ifdef BACKTRACES
#  undef BACKTRACES
# endif
#else
# define BACKTRACES
#endif

#ifdef BACKTRACES
#include "dbus-sysdeps-win.h"

#include <stdio.h>
#include <windows.h>
#include <imagehlp.h>

#define DPRINTF(fmt, ...) fprintf (stderr, fmt, ##__VA_ARGS__)

#ifdef _MSC_VER
#define BOOL int

#define __i386__
#endif

static void dump_backtrace_for_thread (HANDLE hThread)
{
  ADDRESS old_address;
  STACKFRAME sf;
  CONTEXT context;
  DWORD dwImageType;
  int i = 0;

  SymSetOptions (SYMOPT_UNDNAME | SYMOPT_LOAD_LINES);
  SymInitialize (GetCurrentProcess (), NULL, TRUE);


  /* can't use this function for current thread as GetThreadContext
   * doesn't support getting context from current thread */
  if (hThread == GetCurrentThread())
    return;

  DPRINTF ("Backtrace:\n");

  _DBUS_ZERO (old_address);
  _DBUS_ZERO (context);
  context.ContextFlags = CONTEXT_FULL;

  SuspendThread (hThread);

  if (!GetThreadContext (hThread, &context))
    {
      DPRINTF ("Couldn't get thread context (error %ld)\n", GetLastError ());
      ResumeThread (hThread);
      return;
    }

  _DBUS_ZERO (sf);

#ifdef __i386__
  dwImageType         = IMAGE_FILE_MACHINE_I386;
  sf.AddrFrame.Offset = context.Ebp;
  sf.AddrFrame.Mode   = AddrModeFlat;
  sf.AddrPC.Offset    = context.Eip;
  sf.AddrPC.Mode      = AddrModeFlat;
#elif defined(_M_X64)
  dwImageType         = IMAGE_FILE_MACHINE_AMD64;
  sf.AddrPC.Offset    = context.Rip;
  sf.AddrPC.Mode      = AddrModeFlat;
  sf.AddrFrame.Offset = context.Rsp;
  sf.AddrFrame.Mode   = AddrModeFlat;
  sf.AddrStack.Offset = context.Rsp;
  sf.AddrStack.Mode   = AddrModeFlat;
#elif defined(_M_IA64)
  dwImageType         = IMAGE_FILE_MACHINE_IA64;
  sf.AddrPC.Offset    = context.StIIP;
  sf.AddrPC.Mode      = AddrModeFlat;
  sf.AddrFrame.Offset = context.IntSp;
  sf.AddrFrame.Mode   = AddrModeFlat;
  sf.AddrBStore.Offset= context.RsBSP;
  sf.AddrBStore.Mode  = AddrModeFlat;
  sf.AddrStack.Offset = context.IntSp;
  sf.AddrStack.Mode   = AddrModeFlat;
#else
# error You need to fill in the STACKFRAME structure for your architecture
#endif

  /*
    backtrace format
    <level> <address> <symbol>[+offset] [ '[' <file> ':' <line> ']' ] [ 'in' <module> ]
    example:
      6 0xf75ade6b wine_switch_to_stack+0x2a [/usr/src/debug/wine-snapshot/libs/wine/port.c:59] in libwine.so.1
  */
  while (StackWalk (dwImageType, GetCurrentProcess (),
                    hThread, &sf, &context, NULL, SymFunctionTableAccess,
                    SymGetModuleBase, NULL))
    {
      char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(char)];
      PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
      DWORD64 displacement;
      IMAGEHLP_LINE line;
      DWORD dwDisplacement;
      IMAGEHLP_MODULE moduleInfo;

      /*
         on Wine64 version 1.7.54, we get an infinite number of stack entries
         pointing to the same stack frame  (_start+0x29 in <wine-loader>)
         see bug https://bugs.winehq.org/show_bug.cgi?id=39606
      */
#ifndef __i386__
      if (old_address.Offset == sf.AddrPC.Offset)
        {
          break;
        }
#endif

      pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
      pSymbol->MaxNameLen = MAX_SYM_NAME;

      if (SymFromAddr (GetCurrentProcess (), sf.AddrPC.Offset, &displacement, pSymbol))
        {
          if (displacement)
            DPRINTF ("%3d %s+0x%I64x", i++, pSymbol->Name, displacement);
          else
            DPRINTF ("%3d %s", i++, pSymbol->Name);
        }
      else
        DPRINTF ("%3d 0x%Ix", i++, sf.AddrPC.Offset);

      line.SizeOfStruct = sizeof(IMAGEHLP_LINE);
      if (SymGetLineFromAddr (GetCurrentProcess (), sf.AddrPC.Offset, &dwDisplacement, &line))
        {
          DPRINTF (" [%s:%ld]", line.FileName, line.LineNumber);
        }

      moduleInfo.SizeOfStruct = sizeof(moduleInfo);
      if (SymGetModuleInfo (GetCurrentProcess (), sf.AddrPC.Offset, &moduleInfo))
        {
          DPRINTF (" in %s", moduleInfo.ModuleName);
        }
      DPRINTF ("\n");
      old_address = sf.AddrPC;
    }
  ResumeThread (hThread);
}

static DWORD WINAPI dump_thread_proc (LPVOID lpParameter)
{
  dump_backtrace_for_thread ((HANDLE) lpParameter);
  return 0;
}

/* cannot get valid context from current thread, so we have to execute
 * backtrace from another thread */
static void
dump_backtrace (void)
{
  HANDLE hCurrentThread;
  HANDLE hThread;
  DWORD dwThreadId;
  DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
                   GetCurrentProcess (), &hCurrentThread,
                   0, FALSE, DUPLICATE_SAME_ACCESS);
  hThread = CreateThread (NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
                          0, &dwThreadId);
  WaitForSingleObject (hThread, INFINITE);
  CloseHandle (hThread);
  CloseHandle (hCurrentThread);
}
#endif
#endif /* asserts or tests enabled */

#ifdef BACKTRACES
void _dbus_print_backtrace (void)
{
  dump_backtrace ();
}
#else
void _dbus_print_backtrace (void)
{
  _dbus_verbose ("  D-Bus not compiled with backtrace support\n");
}
#endif