3
3
4
4
use crate :: Metadata ;
5
5
use anyhow:: { anyhow, Context , Result } ;
6
+ use core:: mem:: size_of;
6
7
use datadog_crashtracker:: { CrashInfoBuilder , StackFrame , StackTrace , ThreadData } ;
7
8
use ddcommon:: Endpoint ;
8
9
use ddcommon_ffi:: slice:: AsBytes ;
@@ -49,7 +50,6 @@ use windows::Win32::System::Threading::{GetProcessId, GetThreadId, OpenThread, T
49
50
50
51
#[ no_mangle]
51
52
#[ must_use]
52
- #[ named]
53
53
/// Initialize the crash-tracking infrastructure.
54
54
///
55
55
/// # Preconditions
@@ -66,9 +66,9 @@ pub unsafe extern "C" fn ddog_crasht_init_windows(
66
66
metadata : Metadata ,
67
67
) -> bool {
68
68
let result: Result < ( ) , _ > = ( || {
69
- let endpoint = endpoint. map ( |endpoint| endpoint . clone ( ) ) ;
69
+ let endpoint = endpoint. cloned ( ) ;
70
70
let error_context = ErrorContext {
71
- endpoint : endpoint ,
71
+ endpoint,
72
72
metadata : metadata. try_into ( ) ?,
73
73
} ;
74
74
let error_context_json = serde_json:: to_string ( & error_context) ?;
@@ -88,22 +88,18 @@ pub unsafe extern "C" fn ddog_crasht_init_windows(
88
88
89
89
if result. is_err ( ) {
90
90
output_debug_string (
91
- format ! (
92
- "ddog_crasht_init_windows failed: {}" ,
93
- result. err( ) . unwrap( ) . to_string( )
94
- )
95
- . as_str ( ) ,
91
+ format ! ( "ddog_crasht_init_windows failed: {}" , result. err( ) . unwrap( ) ) . as_str ( ) ,
96
92
) ;
97
93
return false ;
98
94
}
99
95
100
96
true
101
97
}
102
98
103
- unsafe fn create_registry_key ( path : & String ) -> Result < ( ) > {
104
- // First, check if there is already a key named "path" in SOFTWARE\Microsoft\Windows\Windows Error Reporting\RuntimeExceptionHelperModules,
105
- // in either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
106
- // If not, create it in HKEY_CURRENT_USER.
99
+ unsafe fn create_registry_key ( path : & str ) -> Result < ( ) > {
100
+ // First, check if there is already a key named "path" in SOFTWARE\Microsoft\Windows\Windows
101
+ // Error Reporting\RuntimeExceptionHelperModules, in either HKEY_LOCAL_MACHINE or
102
+ // HKEY_CURRENT_USER. If not, create it in HKEY_CURRENT_USER.
107
103
108
104
// Convert value name to null-terminated wide string
109
105
let mut name_wide: Vec < u16 > = path. encode_utf16 ( ) . collect ( ) ;
@@ -210,7 +206,6 @@ pub struct WerContext {
210
206
}
211
207
212
208
#[ no_mangle]
213
- #[ named]
214
209
#[ cfg( windows) ]
215
210
pub unsafe extern "C" fn ddog_crasht_event_signature_callback (
216
211
_context : * const c_void ,
@@ -226,7 +221,6 @@ pub unsafe extern "C" fn ddog_crasht_event_signature_callback(
226
221
}
227
222
228
223
#[ no_mangle]
229
- #[ named]
230
224
#[ cfg( windows) ]
231
225
pub unsafe extern "C" fn ddog_crasht_debugger_launch_callback (
232
226
_context : * const c_void ,
@@ -241,7 +235,6 @@ pub unsafe extern "C" fn ddog_crasht_debugger_launch_callback(
241
235
}
242
236
243
237
#[ no_mangle]
244
- #[ named]
245
238
#[ cfg( windows) ]
246
239
pub unsafe extern "C" fn ddog_crasht_exception_event_callback (
247
240
context : * const c_void ,
@@ -274,18 +267,18 @@ pub unsafe extern "C" fn ddog_crasht_exception_event_callback(
274
267
let stack: StackTrace ;
275
268
let stack_result = walk_thread_stack ( exception_information. hProcess , thread, & modules) ;
276
269
277
- if stack_result. is_err ( ) {
270
+ let stack : StackTrace = if stack_result. is_err ( ) {
278
271
output_debug_string (
279
272
format ! (
280
273
"Failed to walk thread stack: {}" ,
281
- stack_result. err( ) . unwrap( ) . to_string ( )
274
+ stack_result. err( ) . unwrap( )
282
275
)
283
276
. as_str ( ) ,
284
277
) ;
285
- stack = StackTrace :: new_incomplete ( ) ;
278
+ StackTrace :: new_incomplete ( )
286
279
} else {
287
- stack = stack_result. unwrap ( ) ;
288
- }
280
+ stack_result. unwrap ( )
281
+ } ;
289
282
290
283
if thread == crash_tid {
291
284
builder
@@ -296,7 +289,7 @@ pub unsafe extern "C" fn ddog_crasht_exception_event_callback(
296
289
let thread_data = ThreadData {
297
290
crashed : thread == crash_tid,
298
291
name : format ! ( "{}" , thread) ,
299
- stack : stack ,
292
+ stack,
300
293
state : None ,
301
294
} ;
302
295
@@ -329,7 +322,7 @@ pub unsafe extern "C" fn ddog_crasht_exception_event_callback(
329
322
output_debug_string (
330
323
format ! (
331
324
"ddog_crasht_exception_event_callback failed: {}" ,
332
- result. err( ) . unwrap( ) . to_string ( )
325
+ result. err( ) . unwrap( )
333
326
)
334
327
. as_str ( ) ,
335
328
) ;
@@ -386,7 +379,7 @@ pub unsafe fn read_wer_context(process_handle: HANDLE, base_address: usize) -> R
386
379
let wer_context = WerContext {
387
380
prefix : WER_CONTEXT_PREFIX ,
388
381
ptr : static_slice. as_ptr ( ) ,
389
- len : len ,
382
+ len,
390
383
suffix : WER_CONTEXT_SUFFIX ,
391
384
} ;
392
385
@@ -403,7 +396,7 @@ struct AlignedContext {
403
396
unsafe fn walk_thread_stack (
404
397
process_handle : HANDLE ,
405
398
thread_id : u32 ,
406
- modules : & Vec < ModuleInfo > ,
399
+ modules : & [ ModuleInfo ] ,
407
400
) -> Result < StackTrace > {
408
401
let mut stacktrace = StackTrace :: new_incomplete ( ) ;
409
402
let thread_handle = OpenThread ( THREAD_ALL_ACCESS , false , thread_id) ?;
@@ -426,11 +419,11 @@ unsafe fn walk_thread_stack(
426
419
#[ cfg( target_arch = "x86_64" ) ]
427
420
{
428
421
machine_type = IMAGE_FILE_MACHINE_AMD64 . 0 as u32 ;
429
- native_frame. AddrPC . Offset = context. ctx . Rip as u64 ;
422
+ native_frame. AddrPC . Offset = context. ctx . Rip ;
430
423
native_frame. AddrPC . Mode = AddrModeFlat ;
431
- native_frame. AddrStack . Offset = context. ctx . Rsp as u64 ;
424
+ native_frame. AddrStack . Offset = context. ctx . Rsp ;
432
425
native_frame. AddrStack . Mode = AddrModeFlat ;
433
- native_frame. AddrFrame . Offset = context. ctx . Rbp as u64 ;
426
+ native_frame. AddrFrame . Offset = context. ctx . Rbp ;
434
427
native_frame. AddrFrame . Mode = AddrModeFlat ;
435
428
}
436
429
@@ -508,7 +501,7 @@ struct ModuleInfo {
508
501
509
502
struct PdbInfo {
510
503
age : u32 ,
511
- signature : GUID ,
504
+ signature : Guid ,
512
505
}
513
506
514
507
unsafe fn list_modules ( process_handle : HANDLE ) -> anyhow:: Result < Vec < ModuleInfo > > {
@@ -517,25 +510,21 @@ unsafe fn list_modules(process_handle: HANDLE) -> anyhow::Result<Vec<ModuleInfo>
517
510
518
511
// Get the number of bytes required to store the array of module handles
519
512
let mut cb_needed = 0 ;
520
- if !EnumProcessModules ( process_handle, std:: ptr:: null_mut ( ) , 0 , & mut cb_needed) . is_ok ( ) {
521
- return Err ( anyhow ! ( "Failed to get module list size" ) ) ;
522
- }
513
+ EnumProcessModules ( process_handle, std:: ptr:: null_mut ( ) , 0 , & mut cb_needed)
514
+ . context ( "Failed to get module list size" ) ?;
523
515
524
516
// Allocate enough space for the module handles
525
517
let modules_count = cb_needed as usize / size_of :: < HMODULE > ( ) ;
526
518
let mut hmodules: Vec < HMODULE > = Vec :: with_capacity ( modules_count) ;
527
519
let mut cb_actual = 0 ;
528
520
529
- if ! EnumProcessModules (
521
+ EnumProcessModules (
530
522
process_handle,
531
523
hmodules. as_mut_ptr ( ) ,
532
524
cb_needed,
533
525
& mut cb_actual,
534
526
)
535
- . is_ok ( )
536
- {
537
- return Err ( anyhow ! ( "Failed to enumerate process modules" ) ) ;
538
- }
527
+ . context ( "Failed to enumerate process modules" ) ?;
539
528
540
529
hmodules. set_len ( cb_actual as usize / size_of :: < HMODULE > ( ) ) ;
541
530
@@ -614,14 +603,14 @@ struct IMAGE_NT_HEADERS_GENERIC {
614
603
615
604
#[ repr( C ) ]
616
605
#[ derive( Debug ) ]
617
- struct GUID {
606
+ struct Guid {
618
607
pub data1 : u32 ,
619
608
pub data2 : u16 ,
620
609
pub data3 : u16 ,
621
610
pub data4 : [ u8 ; 8 ] ,
622
611
}
623
612
624
- impl fmt:: LowerHex for GUID {
613
+ impl fmt:: LowerHex for Guid {
625
614
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
626
615
write ! (
627
616
f,
@@ -644,7 +633,7 @@ impl fmt::LowerHex for GUID {
644
633
#[ repr( C ) ]
645
634
struct CV_INFO_PDB70 {
646
635
pub signature : u32 ,
647
- pub guid : GUID ,
636
+ pub guid : Guid ,
648
637
pub age : u32 ,
649
638
}
650
639
@@ -669,17 +658,13 @@ unsafe fn get_pdb_info(process_handle: HANDLE, base_address: u64) -> Result<PdbI
669
658
return Err ( anyhow ! ( "Invalid machine type" ) ) ;
670
659
}
671
660
672
- let debug_data_dir: IMAGE_DATA_DIRECTORY ;
673
-
674
- if is_pe32 {
661
+ let debug_data_dir: IMAGE_DATA_DIRECTORY = if is_pe32 {
675
662
let nt_headers32: IMAGE_NT_HEADERS32 = read_memory ( process_handle, nt_headers_address) ?;
676
- debug_data_dir =
677
- nt_headers32. OptionalHeader . DataDirectory [ IMAGE_DIRECTORY_ENTRY_DEBUG . 0 as usize ] ;
663
+ nt_headers32. OptionalHeader . DataDirectory [ IMAGE_DIRECTORY_ENTRY_DEBUG . 0 as usize ]
678
664
} else {
679
665
let nt_headers64: IMAGE_NT_HEADERS64 = read_memory ( process_handle, nt_headers_address) ?;
680
- debug_data_dir =
681
- nt_headers64. OptionalHeader . DataDirectory [ IMAGE_DIRECTORY_ENTRY_DEBUG . 0 as usize ] ;
682
- }
666
+ nt_headers64. OptionalHeader . DataDirectory [ IMAGE_DIRECTORY_ENTRY_DEBUG . 0 as usize ]
667
+ } ;
683
668
684
669
let debug_dir_buffer = read_memory_raw (
685
670
process_handle,
@@ -722,9 +707,10 @@ unsafe fn get_module_path(
722
707
let len = GetModuleFileNameExW (
723
708
Some ( process_handle) ,
724
709
Some ( module_handle) ,
725
- & mut * module_name_buffer,
710
+ & mut module_name_buffer,
726
711
) ;
727
- if len <= 0 {
712
+
713
+ if len == 0 {
728
714
return Err ( anyhow ! ( "GetModuleFileNameExW failed: {}" , len) ) ;
729
715
}
730
716
@@ -740,16 +726,18 @@ unsafe fn list_threads(pid: u32) -> anyhow::Result<Vec<u32>> {
740
726
741
727
let snapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPTHREAD , pid) ?;
742
728
743
- let mut thread_entry = THREADENTRY32 :: default ( ) ;
744
- thread_entry. dwSize = size_of :: < THREADENTRY32 > ( ) as u32 ;
729
+ let mut thread_entry = THREADENTRY32 {
730
+ dwSize : size_of :: < THREADENTRY32 > ( ) as u32 ,
731
+ ..Default :: default ( )
732
+ } ;
745
733
746
734
if Thread32First ( snapshot, & mut thread_entry) . is_ok ( ) {
747
735
loop {
748
736
if thread_entry. th32OwnerProcessID == pid {
749
737
thread_ids. push ( thread_entry. th32ThreadID ) ;
750
738
}
751
739
752
- if ! Thread32Next ( snapshot, & mut thread_entry) . is_ok ( ) {
740
+ if Thread32Next ( snapshot, & mut thread_entry) . is_err ( ) {
753
741
break ;
754
742
}
755
743
}
0 commit comments