crashdlg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include "debugger.h"
  21. #include "wingdi.h"
  22. #include "winuser.h"
  23. #include "commctrl.h"
  24. #include "commdlg.h"
  25. #include "shellapi.h"
  26. #include "psapi.h"
  27. #include "resource.h"
  28. #define MAX_PROGRAM_NAME_LENGTH 80
  29. static char *crash_log;
  30. int msgbox_res_id(HWND hwnd, UINT textid, UINT captionid, UINT type)
  31. {
  32. if (DBG_IVAR(ShowCrashDialog))
  33. {
  34. WCHAR caption[256];
  35. WCHAR text[256];
  36. LoadStringW(GetModuleHandleW(NULL), captionid, caption, ARRAY_SIZE(caption));
  37. LoadStringW(GetModuleHandleW(NULL), textid, text, ARRAY_SIZE(text));
  38. return MessageBoxW(hwnd, text, caption, type);
  39. }
  40. return IDCANCEL;
  41. }
  42. static BOOL is_visible(void)
  43. {
  44. USEROBJECTFLAGS flags;
  45. HWINSTA winstation;
  46. if (!(winstation = GetProcessWindowStation()))
  47. return FALSE;
  48. if (!(GetUserObjectInformationA(winstation, UOI_FLAGS, &flags, sizeof(flags), NULL)))
  49. return FALSE;
  50. return flags.dwFlags & WSF_VISIBLE;
  51. }
  52. static WCHAR *get_program_name(HANDLE hProcess)
  53. {
  54. WCHAR image_name[MAX_PATH];
  55. WCHAR *programname;
  56. WCHAR *output;
  57. /* GetProcessImageFileNameW gives no way to query the correct buffer size,
  58. * but programs with a path longer than MAX_PATH can't be started by the
  59. * shell, so we expect they don't happen often */
  60. if (!GetProcessImageFileNameW(hProcess, image_name, MAX_PATH))
  61. {
  62. static WCHAR unidentified[MAX_PROGRAM_NAME_LENGTH];
  63. LoadStringW(GetModuleHandleW(NULL), IDS_UNIDENTIFIED,
  64. unidentified, MAX_PROGRAM_NAME_LENGTH);
  65. return unidentified;
  66. }
  67. programname = wcsrchr(image_name, '\\');
  68. if (programname != NULL)
  69. programname++;
  70. else
  71. programname = image_name;
  72. /* TODO: if the image has a VERSIONINFO, we could try to find there a more
  73. * user-friendly program name */
  74. /* don't display a too long string to the user */
  75. if (lstrlenW(programname) >= MAX_PROGRAM_NAME_LENGTH)
  76. {
  77. programname[MAX_PROGRAM_NAME_LENGTH - 4] = '.';
  78. programname[MAX_PROGRAM_NAME_LENGTH - 3] = '.';
  79. programname[MAX_PROGRAM_NAME_LENGTH - 2] = '.';
  80. programname[MAX_PROGRAM_NAME_LENGTH - 1] = 0;
  81. }
  82. output = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*(lstrlenW(programname) + 1));
  83. lstrcpyW(output, programname);
  84. return output;
  85. }
  86. static LPWSTR g_ProgramName;
  87. static HFONT g_hBoldFont;
  88. static HMENU g_hDebugMenu = NULL;
  89. static void set_bold_font(HWND hDlg)
  90. {
  91. HFONT hNormalFont = (HFONT)SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1,
  92. WM_GETFONT, 0, 0);
  93. LOGFONTW font;
  94. GetObjectW(hNormalFont, sizeof(LOGFONTW), &font);
  95. font.lfWeight = FW_BOLD;
  96. g_hBoldFont = CreateFontIndirectW(&font);
  97. SendDlgItemMessageW(hDlg, IDC_STATIC_TXT1, WM_SETFONT, (WPARAM)g_hBoldFont, TRUE);
  98. }
  99. static void set_fixed_font( HWND dlg, UINT id )
  100. {
  101. HFONT hfont = (HFONT)SendDlgItemMessageW( dlg, id, WM_GETFONT, 0, 0);
  102. LOGFONTW font;
  103. GetObjectW(hfont, sizeof(LOGFONTW), &font);
  104. font.lfPitchAndFamily = FIXED_PITCH;
  105. font.lfFaceName[0] = 0;
  106. hfont = CreateFontIndirectW(&font);
  107. SendDlgItemMessageW( dlg, id, WM_SETFONT, (WPARAM)hfont, TRUE );
  108. }
  109. static void set_message_with_filename(HWND hDlg)
  110. {
  111. WCHAR originalText[1000];
  112. WCHAR newText[1000 + MAX_PROGRAM_NAME_LENGTH];
  113. GetDlgItemTextW(hDlg, IDC_STATIC_TXT1, originalText, ARRAY_SIZE(originalText));
  114. wsprintfW(newText, originalText, g_ProgramName);
  115. SetDlgItemTextW(hDlg, IDC_STATIC_TXT1, newText);
  116. }
  117. static void load_crash_log( HANDLE file )
  118. {
  119. DWORD len, pos = 0, size = 65536;
  120. crash_log = HeapAlloc( GetProcessHeap(), 0, size );
  121. SetFilePointer( file, 0, NULL, FILE_BEGIN );
  122. while (ReadFile( file, crash_log + pos, size - pos - 1, &len, NULL ) && len)
  123. {
  124. pos += len;
  125. if (pos == size - 1) crash_log = HeapReAlloc( GetProcessHeap(), 0, crash_log, size *= 2 );
  126. }
  127. crash_log[pos] = 0;
  128. }
  129. static void save_crash_log( HWND hwnd )
  130. {
  131. OPENFILENAMEW save;
  132. HANDLE handle;
  133. DWORD err, written;
  134. WCHAR *p, path[MAX_PATH], buffer[1024];
  135. memset( &save, 0, sizeof(save) );
  136. lstrcpyW( path, L"backtrace.txt" );
  137. LoadStringW( GetModuleHandleW(0), IDS_TEXT_FILES, buffer, ARRAY_SIZE(buffer));
  138. p = buffer + lstrlenW(buffer) + 1;
  139. lstrcpyW(p, L"*.txt");
  140. p += lstrlenW(p) + 1;
  141. LoadStringW( GetModuleHandleW(0), IDS_ALL_FILES, p, ARRAY_SIZE(buffer) - (p - buffer) );
  142. p += lstrlenW(p) + 1;
  143. lstrcpyW(p, L"*.*");
  144. p += lstrlenW(p) + 1;
  145. *p = '\0';
  146. save.lStructSize = sizeof(OPENFILENAMEW);
  147. save.hwndOwner = hwnd;
  148. save.hInstance = GetModuleHandleW(0);
  149. save.lpstrFilter = buffer;
  150. save.lpstrFile = path;
  151. save.nMaxFile = MAX_PATH;
  152. save.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
  153. OFN_HIDEREADONLY | OFN_ENABLESIZING;
  154. save.lpstrDefExt = L"txt";
  155. if (!GetSaveFileNameW( &save )) return;
  156. handle = CreateFileW( save.lpstrFile, GENERIC_WRITE, FILE_SHARE_READ,
  157. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
  158. if (handle != INVALID_HANDLE_VALUE)
  159. {
  160. if (!WriteFile( handle, crash_log, strlen(crash_log), &written, NULL ))
  161. err = GetLastError();
  162. else if (written != strlen(crash_log))
  163. err = GetLastError();
  164. else
  165. {
  166. CloseHandle( handle );
  167. return;
  168. }
  169. CloseHandle( handle );
  170. DeleteFileW( save.lpstrFile );
  171. }
  172. else err = GetLastError();
  173. LoadStringW( GetModuleHandleW(0), IDS_SAVE_ERROR, buffer, ARRAY_SIZE(buffer));
  174. FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  175. NULL, err, 0, (LPWSTR)&p, 0, NULL);
  176. MessageBoxW( 0, p, buffer, MB_OK | MB_ICONERROR);
  177. LocalFree( p );
  178. }
  179. static INT_PTR WINAPI crash_dlg_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  180. {
  181. switch (msg)
  182. {
  183. case WM_INITDIALOG:
  184. {
  185. set_bold_font(hwnd);
  186. set_message_with_filename(hwnd);
  187. return TRUE;
  188. }
  189. case WM_CTLCOLORSTATIC:
  190. {
  191. /* WM_CTLCOLOR* don't use DWLP_MSGRESULT */
  192. INT_PTR id = GetDlgCtrlID((HWND)lParam);
  193. if (id == IDC_STATIC_BG || id == IDC_STATIC_TXT1)
  194. return (LONG_PTR)GetSysColorBrush(COLOR_WINDOW);
  195. return FALSE;
  196. }
  197. case WM_RBUTTONDOWN:
  198. {
  199. POINT mousePos;
  200. if (!(wParam & MK_SHIFT))
  201. return FALSE;
  202. if (g_hDebugMenu == NULL)
  203. g_hDebugMenu = LoadMenuW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDM_DEBUG_POPUP));
  204. GetCursorPos(&mousePos);
  205. TrackPopupMenu(GetSubMenu(g_hDebugMenu, 0), TPM_RIGHTBUTTON, mousePos.x, mousePos.y,
  206. 0, hwnd, NULL);
  207. return TRUE;
  208. }
  209. case WM_NOTIFY:
  210. switch (((NMHDR *)lParam)->code)
  211. {
  212. case NM_CLICK:
  213. case NM_RETURN:
  214. if (wParam == IDC_STATIC_TXT2)
  215. ShellExecuteW( NULL, L"open", ((NMLINK *)lParam)->item.szUrl, NULL, NULL, SW_SHOW );
  216. break;
  217. }
  218. break;
  219. case WM_COMMAND:
  220. switch (LOWORD(wParam))
  221. {
  222. case IDOK:
  223. case IDCANCEL:
  224. case ID_DEBUG:
  225. case ID_DETAILS:
  226. EndDialog(hwnd, LOWORD(wParam));
  227. return TRUE;
  228. }
  229. return TRUE;
  230. }
  231. return FALSE;
  232. }
  233. static INT_PTR WINAPI details_dlg_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
  234. {
  235. static POINT orig_size, min_size, edit_size, text_pos, save_pos, close_pos;
  236. switch (msg)
  237. {
  238. case WM_INITDIALOG:
  239. {
  240. RECT rect;
  241. WCHAR buffer[256];
  242. set_fixed_font( hwnd, IDC_CRASH_TXT );
  243. LoadStringW( GetModuleHandleW(0), IDS_LOADING, buffer, 256 );
  244. SetDlgItemTextW( hwnd, IDC_CRASH_TXT, buffer );
  245. EnableWindow( GetDlgItem( hwnd, IDC_CRASH_TXT ), FALSE );
  246. EnableWindow( GetDlgItem( hwnd, ID_SAVEAS ), FALSE );
  247. GetClientRect( hwnd, &rect );
  248. orig_size.x = rect.right;
  249. orig_size.y = rect.bottom;
  250. GetWindowRect( hwnd, &rect );
  251. min_size.x = rect.right - rect.left;
  252. min_size.y = rect.bottom - rect.top;
  253. GetWindowRect( GetDlgItem( hwnd, IDOK ), &rect );
  254. MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
  255. close_pos.x = rect.left;
  256. close_pos.y = rect.top;
  257. GetWindowRect( GetDlgItem( hwnd, ID_SAVEAS ), &rect );
  258. MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
  259. save_pos.x = rect.left;
  260. save_pos.y = rect.top;
  261. GetWindowRect( GetDlgItem( hwnd, IDC_STATIC_TXT2 ), &rect );
  262. MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
  263. text_pos.x = rect.left;
  264. text_pos.y = rect.top;
  265. GetWindowRect( GetDlgItem( hwnd, IDC_CRASH_TXT ), &rect );
  266. MapWindowPoints( 0, hwnd, (POINT *)&rect, 2 );
  267. edit_size.x = rect.right - rect.left;
  268. edit_size.y = rect.bottom - rect.top;
  269. return TRUE;
  270. }
  271. case WM_GETMINMAXINFO:
  272. ((MINMAXINFO *)lparam)->ptMinTrackSize = min_size;
  273. return TRUE;
  274. case WM_SIZE:
  275. if (wparam == SIZE_RESTORED || wparam == SIZE_MAXIMIZED)
  276. {
  277. int off_x = (short)LOWORD( lparam ) - orig_size.x;
  278. int off_y = (short)HIWORD( lparam ) - orig_size.y;
  279. SetWindowPos( GetDlgItem( hwnd, IDOK ), 0, close_pos.x + off_x,
  280. close_pos.y + off_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  281. SetWindowPos( GetDlgItem( hwnd, ID_SAVEAS ), 0, save_pos.x + off_x,
  282. save_pos.y + off_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  283. SetWindowPos( GetDlgItem( hwnd, IDC_STATIC_TXT2 ), 0, text_pos.x,
  284. text_pos.y + off_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  285. SetWindowPos( GetDlgItem( hwnd, IDC_CRASH_TXT ), 0, 0, 0, edit_size.x + off_x,
  286. edit_size.y + off_y, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
  287. }
  288. return TRUE;
  289. case WM_NOTIFY:
  290. switch (((NMHDR *)lparam)->code)
  291. {
  292. case NM_CLICK:
  293. case NM_RETURN:
  294. if (wparam == IDC_STATIC_TXT2)
  295. ShellExecuteW( NULL, L"open", ((NMLINK *)lparam)->item.szUrl, NULL, NULL, SW_SHOW );
  296. break;
  297. }
  298. break;
  299. case WM_COMMAND:
  300. switch (LOWORD(wparam))
  301. {
  302. case ID_SAVEAS:
  303. save_crash_log( hwnd );
  304. break;
  305. case IDOK:
  306. case IDCANCEL:
  307. PostQuitMessage( 0 );
  308. break;
  309. }
  310. return TRUE;
  311. }
  312. return FALSE;
  313. }
  314. int display_crash_dialog(void)
  315. {
  316. static const INITCOMMONCONTROLSEX init = { sizeof(init), ICC_LINK_CLASS };
  317. /* dbg_curr_process->handle is not set */
  318. HANDLE hProcess;
  319. if (!DBG_IVAR(ShowCrashDialog) || !is_visible())
  320. return TRUE;
  321. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dbg_curr_pid);
  322. g_ProgramName = get_program_name(hProcess);
  323. CloseHandle(hProcess);
  324. if (!wcscmp( g_ProgramName, L"winedevice.exe" )) return TRUE;
  325. InitCommonControlsEx( &init );
  326. return DialogBoxW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDD_CRASH_DLG), NULL, crash_dlg_proc);
  327. }
  328. static DWORD WINAPI crash_details_thread( void *event )
  329. {
  330. MSG msg;
  331. HWND dialog;
  332. dialog = CreateDialogW( GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DETAILS_DLG), 0, details_dlg_proc );
  333. if (!dialog) return 1;
  334. for (;;)
  335. {
  336. if (MsgWaitForMultipleObjects( 1, &event, FALSE, INFINITE, QS_ALLINPUT ) == 0)
  337. {
  338. load_crash_log( dbg_houtput );
  339. SetDlgItemTextA( dialog, IDC_CRASH_TXT, crash_log );
  340. EnableWindow( GetDlgItem( dialog, IDC_CRASH_TXT ), TRUE );
  341. EnableWindow( GetDlgItem( dialog, ID_SAVEAS ), TRUE );
  342. break;
  343. }
  344. while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ))
  345. {
  346. if (msg.message == WM_QUIT) return 0;
  347. TranslateMessage( &msg );
  348. DispatchMessageW( &msg );
  349. }
  350. }
  351. while (GetMessageW( &msg, 0, 0, 0 ))
  352. {
  353. TranslateMessage( &msg );
  354. DispatchMessageW( &msg );
  355. }
  356. return 0;
  357. }
  358. HANDLE display_crash_details( HANDLE event )
  359. {
  360. return CreateThread( NULL, 0, crash_details_thread, event, 0, NULL );
  361. }