winedbg: Add a dialog to display crash details and save them to a file.
[wine] / programs / winedbg / crashdlg.c
1 /*
2  * The dialog that displays after a crash
3  *
4  * Copyright 2008 Mikolaj Zalewski
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "debugger.h"
22 #include "wingdi.h"
23 #include "winuser.h"
24 #include "commctrl.h"
25 #include "commdlg.h"
26 #include "shellapi.h"
27 #include "psapi.h"
28
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31
32 #include "resource.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
35
36 #define MAX_PROGRAM_NAME_LENGTH 80
37
38 static char *crash_log;
39
40 int msgbox_res_id(HWND hwnd, UINT textId, UINT captionId, UINT uType)
41 {
42     WCHAR caption[256];
43     WCHAR text[256];
44     LoadStringW(GetModuleHandleW(NULL), captionId, caption, sizeof(caption)/sizeof(caption[0]));
45     LoadStringW(GetModuleHandleW(NULL), textId, text, sizeof(text)/sizeof(text[0]));
46     return MessageBoxW(hwnd, text, caption, uType);
47 }
48
49 static WCHAR *get_program_name(HANDLE hProcess)
50 {
51     WCHAR image_name[MAX_PATH];
52     WCHAR *programname;
53     WCHAR *output;
54
55     /* GetProcessImageFileNameW gives no way to query the correct buffer size,
56      * but programs with a path longer than MAX_PATH can't be started by the
57      * shell, so we expect they don't happen often */
58     if (!GetProcessImageFileNameW(hProcess, image_name, MAX_PATH))
59     {
60         static WCHAR unidentified[MAX_PROGRAM_NAME_LENGTH];
61         LoadStringW(GetModuleHandleW(NULL), IDS_UNIDENTIFIED,
62                 unidentified, MAX_PROGRAM_NAME_LENGTH);
63         return unidentified;
64     }
65
66     programname = strrchrW(image_name, '\\');
67     if (programname != NULL)
68         programname++;
69     else
70         programname = image_name;
71
72     /* TODO: if the image has a VERSIONINFO, we could try to find there a more
73      * user-friendly program name */
74
75     /* don't display a too long string to the user */
76     if (strlenW(programname) >= MAX_PROGRAM_NAME_LENGTH)
77     {
78         programname[MAX_PROGRAM_NAME_LENGTH - 4] = '.';
79         programname[MAX_PROGRAM_NAME_LENGTH - 3] = '.';
80         programname[MAX_PROGRAM_NAME_LENGTH - 2] = '.';
81         programname[MAX_PROGRAM_NAME_LENGTH - 1] = 0;
82     }
83
84     output = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(lstrlenW(programname) + 1));
85     lstrcpyW(output, programname);
86
87     return output;
88 }
89
90 static LPWSTR g_ProgramName;
91 static HFONT g_hBoldFont;
92 static HMENU g_hDebugMenu = NULL;
93
94 static void set_bold_font(HWND hDlg)
95 {
96     HFONT hNormalFont = (HFONT)SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1,
97             WM_GETFONT, 0, 0);
98     LOGFONTW font;
99     GetObjectW(hNormalFont, sizeof(LOGFONTW), &font);
100     font.lfWeight = FW_BOLD;
101     g_hBoldFont = CreateFontIndirectW(&font);
102     SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1, WM_SETFONT, (WPARAM)g_hBoldFont, TRUE);
103 }
104
105 static void set_fixed_font( HWND dlg, UINT id )
106 {
107     HFONT hfont = (HFONT)SendDlgItemMessageW( dlg, id, WM_GETFONT, 0, 0);
108     LOGFONTW font;
109
110     GetObjectW(hfont, sizeof(LOGFONTW), &font);
111     font.lfPitchAndFamily = FIXED_PITCH;
112     font.lfFaceName[0] = 0;
113     hfont = CreateFontIndirectW(&font);
114     SendDlgItemMessageW( dlg, id, WM_SETFONT, (WPARAM)hfont, TRUE );
115 }
116
117 static void set_message_with_filename(HWND hDlg)
118 {
119     WCHAR originalText[1000];
120     WCHAR newText[1000 + MAX_PROGRAM_NAME_LENGTH];
121
122     GetDlgItemTextW(hDlg, IDC_STATIC_TXT1, originalText,
123             sizeof(originalText)/sizeof(originalText[0]));
124     wsprintfW(newText, originalText, g_ProgramName);
125     SetDlgItemTextW(hDlg, IDC_STATIC_TXT1, newText);
126 }
127
128 static void load_crash_log( HANDLE file )
129 {
130     DWORD len, pos = 0, size = 65536;
131
132     crash_log = HeapAlloc( GetProcessHeap(), 0, size );
133     SetFilePointer( file, 0, NULL, FILE_BEGIN );
134     while (ReadFile( file, crash_log + pos, size - pos - 1, &len, NULL ) && len)
135     {
136         pos += len;
137         break;
138         if (pos == size - 1) crash_log = HeapReAlloc( GetProcessHeap(), 0, crash_log, size *= 2 );
139     }
140     crash_log[pos] = 0;
141 }
142
143 static void save_crash_log( HWND hwnd )
144 {
145     OPENFILENAMEW save;
146     HANDLE handle;
147     DWORD err, written;
148     WCHAR *p, path[MAX_PATH], buffer[1024];
149     static const WCHAR default_name[] = { 'b','a','c','k','t','r','a','c','e','.','t','x','t',0 };
150     static const WCHAR default_ext[] = { 't','x','t',0 };
151     static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
152     static const WCHAR all_files[] = { '*','.','*',0 };
153
154     memset( &save, 0, sizeof(save) );
155     lstrcpyW( path, default_name );
156
157     LoadStringW( GetModuleHandleW(0), IDS_TEXT_FILES, buffer, sizeof(buffer) );
158     p = buffer + lstrlenW(buffer) + 1;
159     lstrcpyW(p, txt_files);
160     p += lstrlenW(p) + 1;
161     LoadStringW( GetModuleHandleW(0), IDS_ALL_FILES, p, sizeof(buffer) - (p - buffer) );
162     p += lstrlenW(p) + 1;
163     lstrcpyW(p, all_files);
164     p += lstrlenW(p) + 1;
165     *p = '\0';
166
167     save.lStructSize = sizeof(OPENFILENAMEW);
168     save.hwndOwner   = hwnd;
169     save.hInstance   = GetModuleHandleW(0);
170     save.lpstrFilter = buffer;
171     save.lpstrFile   = path;
172     save.nMaxFile    = MAX_PATH;
173     save.Flags       = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
174                        OFN_HIDEREADONLY | OFN_ENABLESIZING;
175     save.lpstrDefExt = default_ext;
176
177     if (!GetSaveFileNameW( &save )) return;
178     handle = CreateFileW( save.lpstrFile, GENERIC_WRITE, FILE_SHARE_READ,
179                         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
180     if (handle != INVALID_HANDLE_VALUE)
181     {
182         if (!WriteFile( handle, crash_log, strlen(crash_log), &written, NULL ))
183             err = GetLastError();
184         else if (written != strlen(crash_log))
185             err = GetLastError();
186         else
187         {
188             CloseHandle( handle );
189             return;
190         }
191         CloseHandle( handle );
192         DeleteFileW( save.lpstrFile );
193     }
194     else err = GetLastError();
195
196     LoadStringW( GetModuleHandleW(0), IDS_SAVE_ERROR, buffer, sizeof(buffer)/sizeof(WCHAR) );
197     FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
198                     NULL, err, 0, (LPWSTR)&p, 0, NULL);
199     MessageBoxW( 0, p, buffer, MB_OK | MB_ICONERROR);
200     LocalFree( p );
201 }
202
203 static INT_PTR WINAPI crash_dlg_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
204 {
205     static const WCHAR openW[] = {'o','p','e','n',0};
206     switch (msg)
207     {
208     case WM_INITDIALOG:
209     {
210         set_bold_font(hwnd);
211         set_message_with_filename(hwnd);
212         return TRUE;
213     }
214     case WM_CTLCOLORSTATIC:
215     {
216         /* WM_CTLCOLOR* don't use DWLP_MSGRESULT */
217         INT_PTR id = GetDlgCtrlID((HWND)lParam);
218         if (id == IDC_STATIC_BG || id == IDC_STATIC_TXT1)
219             return (LONG_PTR)GetSysColorBrush(COLOR_WINDOW);
220
221         return FALSE;
222     }
223     case WM_RBUTTONDOWN:
224     {
225         POINT mousePos;
226         if (!(wParam & MK_SHIFT))
227             return FALSE;
228         if (g_hDebugMenu == NULL)
229             g_hDebugMenu = LoadMenuW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDM_DEBUG_POPUP));
230         GetCursorPos(&mousePos);
231         TrackPopupMenu(GetSubMenu(g_hDebugMenu, 0), TPM_RIGHTBUTTON, mousePos.x, mousePos.y,
232                 0, hwnd, NULL);
233         return TRUE;
234     }
235     case WM_NOTIFY:
236         switch (((NMHDR *)lParam)->code)
237         {
238         case NM_CLICK:
239         case NM_RETURN:
240             if (wParam == IDC_STATIC_TXT2)
241                 ShellExecuteW( NULL, openW, ((NMLINK *)lParam)->item.szUrl, NULL, NULL, SW_SHOW );
242             break;
243         }
244         break;
245
246     case WM_COMMAND:
247         switch (LOWORD(wParam))
248         {
249             case IDOK:
250             case IDCANCEL:
251             case ID_DEBUG:
252             case ID_DETAILS:
253                 EndDialog(hwnd, LOWORD(wParam));
254                 return TRUE;
255         }
256         return TRUE;
257     }
258     return FALSE;
259 }
260
261 static INT_PTR WINAPI details_dlg_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
262 {
263     static const WCHAR openW[] = {'o','p','e','n',0};
264
265     switch (msg)
266     {
267     case WM_INITDIALOG:
268         set_fixed_font( hwnd, IDC_CRASH_TXT );
269         SetDlgItemTextA( hwnd, IDC_CRASH_TXT, crash_log );
270         return TRUE;
271
272     case WM_NOTIFY:
273         switch (((NMHDR *)lparam)->code)
274         {
275         case NM_CLICK:
276         case NM_RETURN:
277             if (wparam == IDC_STATIC_TXT2)
278                 ShellExecuteW( NULL, openW, ((NMLINK *)lparam)->item.szUrl, NULL, NULL, SW_SHOW );
279             break;
280         }
281         break;
282
283     case WM_COMMAND:
284         switch (LOWORD(wparam))
285         {
286         case ID_SAVEAS:
287             save_crash_log( hwnd );
288             break;
289         case IDOK:
290         case IDCANCEL:
291             EndDialog(hwnd, LOWORD(wparam));
292             break;
293         }
294         return TRUE;
295     }
296     return FALSE;
297 }
298
299 int display_crash_dialog(void)
300 {
301     static const WCHAR winedeviceW[] = {'w','i','n','e','d','e','v','i','c','e','.','e','x','e',0};
302     static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_LINK_CLASS };
303
304     /* dbg_curr_process->handle is not set */
305     HANDLE hProcess;
306
307     if (!DBG_IVAR(ShowCrashDialog))
308         return TRUE;
309
310     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dbg_curr_pid);
311     g_ProgramName = get_program_name(hProcess);
312     CloseHandle(hProcess);
313     if (!strcmpW( g_ProgramName, winedeviceW )) return TRUE;
314     InitCommonControlsEx( &init );
315     return DialogBoxW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_CRASH_DLG), NULL, crash_dlg_proc);
316 }
317
318 int display_crash_details( HANDLE logfile )
319 {
320     load_crash_log( logfile );
321     return DialogBoxW( GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DETAILS_DLG), 0, details_dlg_proc );
322 }