Skip to content

Commit 7062850

Browse files
committed
Use range-based datastructure to store modules
1 parent f61c42a commit 7062850

File tree

4 files changed

+238
-15
lines changed

4 files changed

+238
-15
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE-3rdparty.yml

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21080,6 +21080,213 @@ third_party_libraries:
2108021080
of your accepting any such warranty or additional liability.
2108121081

2108221082
END OF TERMS AND CONDITIONS
21083+
- package_name: rangemap
21084+
package_version: 1.6.0
21085+
repository: https://github.com/jeffparsons/rangemap
21086+
license: MIT/Apache-2.0
21087+
licenses:
21088+
- license: MIT
21089+
text: |
21090+
Copyright 2019 Jeffrey Parsons
21091+
21092+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
21093+
21094+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
21095+
21096+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21097+
- license: Apache-2.0
21098+
text: |2
21099+
21100+
Apache License
21101+
Version 2.0, January 2004
21102+
http://www.apache.org/licenses/
21103+
21104+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
21105+
21106+
1. Definitions.
21107+
21108+
"License" shall mean the terms and conditions for use, reproduction,
21109+
and distribution as defined by Sections 1 through 9 of this document.
21110+
21111+
"Licensor" shall mean the copyright owner or entity authorized by
21112+
the copyright owner that is granting the License.
21113+
21114+
"Legal Entity" shall mean the union of the acting entity and all
21115+
other entities that control, are controlled by, or are under common
21116+
control with that entity. For the purposes of this definition,
21117+
"control" means (i) the power, direct or indirect, to cause the
21118+
direction or management of such entity, whether by contract or
21119+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
21120+
outstanding shares, or (iii) beneficial ownership of such entity.
21121+
21122+
"You" (or "Your") shall mean an individual or Legal Entity
21123+
exercising permissions granted by this License.
21124+
21125+
"Source" form shall mean the preferred form for making modifications,
21126+
including but not limited to software source code, documentation
21127+
source, and configuration files.
21128+
21129+
"Object" form shall mean any form resulting from mechanical
21130+
transformation or translation of a Source form, including but
21131+
not limited to compiled object code, generated documentation,
21132+
and conversions to other media types.
21133+
21134+
"Work" shall mean the work of authorship, whether in Source or
21135+
Object form, made available under the License, as indicated by a
21136+
copyright notice that is included in or attached to the work
21137+
(an example is provided in the Appendix below).
21138+
21139+
"Derivative Works" shall mean any work, whether in Source or Object
21140+
form, that is based on (or derived from) the Work and for which the
21141+
editorial revisions, annotations, elaborations, or other modifications
21142+
represent, as a whole, an original work of authorship. For the purposes
21143+
of this License, Derivative Works shall not include works that remain
21144+
separable from, or merely link (or bind by name) to the interfaces of,
21145+
the Work and Derivative Works thereof.
21146+
21147+
"Contribution" shall mean any work of authorship, including
21148+
the original version of the Work and any modifications or additions
21149+
to that Work or Derivative Works thereof, that is intentionally
21150+
submitted to Licensor for inclusion in the Work by the copyright owner
21151+
or by an individual or Legal Entity authorized to submit on behalf of
21152+
the copyright owner. For the purposes of this definition, "submitted"
21153+
means any form of electronic, verbal, or written communication sent
21154+
to the Licensor or its representatives, including but not limited to
21155+
communication on electronic mailing lists, source code control systems,
21156+
and issue tracking systems that are managed by, or on behalf of, the
21157+
Licensor for the purpose of discussing and improving the Work, but
21158+
excluding communication that is conspicuously marked or otherwise
21159+
designated in writing by the copyright owner as "Not a Contribution."
21160+
21161+
"Contributor" shall mean Licensor and any individual or Legal Entity
21162+
on behalf of whom a Contribution has been received by Licensor and
21163+
subsequently incorporated within the Work.
21164+
21165+
2. Grant of Copyright License. Subject to the terms and conditions of
21166+
this License, each Contributor hereby grants to You a perpetual,
21167+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
21168+
copyright license to reproduce, prepare Derivative Works of,
21169+
publicly display, publicly perform, sublicense, and distribute the
21170+
Work and such Derivative Works in Source or Object form.
21171+
21172+
3. Grant of Patent License. Subject to the terms and conditions of
21173+
this License, each Contributor hereby grants to You a perpetual,
21174+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
21175+
(except as stated in this section) patent license to make, have made,
21176+
use, offer to sell, sell, import, and otherwise transfer the Work,
21177+
where such license applies only to those patent claims licensable
21178+
by such Contributor that are necessarily infringed by their
21179+
Contribution(s) alone or by combination of their Contribution(s)
21180+
with the Work to which such Contribution(s) was submitted. If You
21181+
institute patent litigation against any entity (including a
21182+
cross-claim or counterclaim in a lawsuit) alleging that the Work
21183+
or a Contribution incorporated within the Work constitutes direct
21184+
or contributory patent infringement, then any patent licenses
21185+
granted to You under this License for that Work shall terminate
21186+
as of the date such litigation is filed.
21187+
21188+
4. Redistribution. You may reproduce and distribute copies of the
21189+
Work or Derivative Works thereof in any medium, with or without
21190+
modifications, and in Source or Object form, provided that You
21191+
meet the following conditions:
21192+
21193+
(a) You must give any other recipients of the Work or
21194+
Derivative Works a copy of this License; and
21195+
21196+
(b) You must cause any modified files to carry prominent notices
21197+
stating that You changed the files; and
21198+
21199+
(c) You must retain, in the Source form of any Derivative Works
21200+
that You distribute, all copyright, patent, trademark, and
21201+
attribution notices from the Source form of the Work,
21202+
excluding those notices that do not pertain to any part of
21203+
the Derivative Works; and
21204+
21205+
(d) If the Work includes a "NOTICE" text file as part of its
21206+
distribution, then any Derivative Works that You distribute must
21207+
include a readable copy of the attribution notices contained
21208+
within such NOTICE file, excluding those notices that do not
21209+
pertain to any part of the Derivative Works, in at least one
21210+
of the following places: within a NOTICE text file distributed
21211+
as part of the Derivative Works; within the Source form or
21212+
documentation, if provided along with the Derivative Works; or,
21213+
within a display generated by the Derivative Works, if and
21214+
wherever such third-party notices normally appear. The contents
21215+
of the NOTICE file are for informational purposes only and
21216+
do not modify the License. You may add Your own attribution
21217+
notices within Derivative Works that You distribute, alongside
21218+
or as an addendum to the NOTICE text from the Work, provided
21219+
that such additional attribution notices cannot be construed
21220+
as modifying the License.
21221+
21222+
You may add Your own copyright statement to Your modifications and
21223+
may provide additional or different license terms and conditions
21224+
for use, reproduction, or distribution of Your modifications, or
21225+
for any such Derivative Works as a whole, provided Your use,
21226+
reproduction, and distribution of the Work otherwise complies with
21227+
the conditions stated in this License.
21228+
21229+
5. Submission of Contributions. Unless You explicitly state otherwise,
21230+
any Contribution intentionally submitted for inclusion in the Work
21231+
by You to the Licensor shall be under the terms and conditions of
21232+
this License, without any additional terms or conditions.
21233+
Notwithstanding the above, nothing herein shall supersede or modify
21234+
the terms of any separate license agreement you may have executed
21235+
with Licensor regarding such Contributions.
21236+
21237+
6. Trademarks. This License does not grant permission to use the trade
21238+
names, trademarks, service marks, or product names of the Licensor,
21239+
except as required for reasonable and customary use in describing the
21240+
origin of the Work and reproducing the content of the NOTICE file.
21241+
21242+
7. Disclaimer of Warranty. Unless required by applicable law or
21243+
agreed to in writing, Licensor provides the Work (and each
21244+
Contributor provides its Contributions) on an "AS IS" BASIS,
21245+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
21246+
implied, including, without limitation, any warranties or conditions
21247+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
21248+
PARTICULAR PURPOSE. You are solely responsible for determining the
21249+
appropriateness of using or redistributing the Work and assume any
21250+
risks associated with Your exercise of permissions under this License.
21251+
21252+
8. Limitation of Liability. In no event and under no legal theory,
21253+
whether in tort (including negligence), contract, or otherwise,
21254+
unless required by applicable law (such as deliberate and grossly
21255+
negligent acts) or agreed to in writing, shall any Contributor be
21256+
liable to You for damages, including any direct, indirect, special,
21257+
incidental, or consequential damages of any character arising as a
21258+
result of this License or out of the use or inability to use the
21259+
Work (including but not limited to damages for loss of goodwill,
21260+
work stoppage, computer failure or malfunction, or any and all
21261+
other commercial damages or losses), even if such Contributor
21262+
has been advised of the possibility of such damages.
21263+
21264+
9. Accepting Warranty or Additional Liability. While redistributing
21265+
the Work or Derivative Works thereof, You may choose to offer,
21266+
and charge a fee for, acceptance of support, warranty, indemnity,
21267+
or other liability obligations and/or rights consistent with this
21268+
License. However, in accepting such obligations, You may act only
21269+
on Your own behalf and on Your sole responsibility, not on behalf
21270+
of any other Contributor, and only if You agree to indemnify,
21271+
defend, and hold each Contributor harmless for any liability
21272+
incurred by, or claims asserted against, such Contributor by reason
21273+
of your accepting any such warranty or additional liability.
21274+
21275+
END OF TERMS AND CONDITIONS
21276+
21277+
Copyright 2019-2022 Jeff Parsons, and [contributors](https://github.com/jeffparsons/rangemap/contributors)
21278+
21279+
Licensed under the Apache License, Version 2.0 (the "License");
21280+
you may not use this file except in compliance with the License.
21281+
You may obtain a copy of the License at
21282+
21283+
http://www.apache.org/licenses/LICENSE-2.0
21284+
21285+
Unless required by applicable law or agreed to in writing, software
21286+
distributed under the License is distributed on an "AS IS" BASIS,
21287+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21288+
See the License for the specific language governing permissions and
21289+
limitations under the License.
2108321290
- package_name: redox_syscall
2108421291
package_version: 0.5.7
2108521292
repository: https://gitlab.redox-os.org/redox-os/syscall

datadog-crashtracker/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ thiserror = "1.0"
6464

6565
[target.'cfg(windows)'.dependencies]
6666
windows = { version = "0.59.0", features = ["Win32_System_Diagnostics_Debug", "Win32_System_Diagnostics_ToolHelp", "Win32_System_ErrorReporting", "Win32_System_Kernel", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_SystemServices", "Win32_System_Threading", "Win32_Security"] }
67+
rangemap = "1.6.0"
6768

6869
[dev-dependencies]
6970
criterion = { version = "0.5.1" }

datadog-crashtracker/src/collector_windows/api.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ use crate::{
1010
use anyhow::{anyhow, Context, Result};
1111
use core::mem::size_of;
1212
use ddcommon::Endpoint;
13+
use rangemap::RangeInclusiveMap;
1314
use serde::{Deserialize, Serialize};
1415
use std::ffi::{c_void, OsString};
1516
use std::fmt;
1617
use std::mem::MaybeUninit;
18+
use std::ops::RangeInclusive;
1719
use std::os::windows::ffi::OsStringExt;
1820
use std::ptr::{addr_of, read_unaligned};
1921
use std::sync::Mutex;
@@ -339,7 +341,7 @@ struct AlignedContext {
339341
fn walk_thread_stack(
340342
process_handle: HANDLE,
341343
thread_id: u32,
342-
modules: &[ModuleInfo],
344+
modules: &RangeInclusiveMap<usize, ModuleInfo>,
343345
) -> Result<StackTrace> {
344346
let mut stacktrace = StackTrace::new_incomplete();
345347
let thread_handle = unsafe { OpenThread(THREAD_ALL_ACCESS, false, thread_id)? };
@@ -403,10 +405,7 @@ fn walk_thread_stack(
403405
frame.sp = Some(format!("{:x}", native_frame.AddrStack.Offset));
404406

405407
// Find the module
406-
let module = modules.iter().find(|module| {
407-
module.base_address <= native_frame.AddrPC.Offset
408-
&& native_frame.AddrPC.Offset < module.base_address + module.size
409-
});
408+
let module = modules.get(&(native_frame.AddrPC.Offset as usize));
410409

411410
if let Some(module) = module {
412411
frame.module_base_address = Some(format!("{:x}", module.base_address));
@@ -417,7 +416,8 @@ fn walk_thread_stack(
417416
frame.path.clone_from(&module.path);
418417

419418
if let Some(pdb_info) = &module.pdb_info {
420-
frame.build_id = Some(format!("{:x}{:x}", pdb_info.signature, pdb_info.age));
419+
// in the backend we expect the AGE to be a uppercase hex number
420+
frame.build_id = Some(format!("{:x}{:X}", pdb_info.signature, pdb_info.age));
421421
frame.build_id_type = Some(BuildIdType::PDB);
422422
frame.file_type = Some(FileType::PE);
423423
}
@@ -432,21 +432,23 @@ fn walk_thread_stack(
432432
Ok(stacktrace)
433433
}
434434

435+
#[derive(Clone, Eq, PartialEq)]
435436
struct ModuleInfo {
436437
base_address: u64,
437438
size: u64,
438439
path: Option<String>,
439440
pdb_info: Option<PdbInfo>,
440441
}
441442

443+
#[derive(Clone, Eq, PartialEq)]
442444
struct PdbInfo {
443445
age: u32,
444446
signature: Guid,
445447
}
446448

447-
fn list_modules(process_handle: HANDLE) -> anyhow::Result<Vec<ModuleInfo>> {
449+
fn list_modules(process_handle: HANDLE) -> anyhow::Result<RangeInclusiveMap<usize, ModuleInfo>> {
448450
// Use EnumProcessModules to get a list of modules
449-
let mut module_infos = Vec::new();
451+
let mut module_infos: RangeInclusiveMap<usize, ModuleInfo> = RangeInclusiveMap::new();
450452

451453
// Get the number of bytes required to store the array of module handles
452454
let mut cb_needed = 0;
@@ -494,12 +496,18 @@ fn list_modules(process_handle: HANDLE) -> anyhow::Result<Vec<ModuleInfo>> {
494496

495497
let module_path = get_module_path(process_handle, hmodule);
496498

497-
module_infos.push(ModuleInfo {
498-
base_address: module_info.lpBaseOfDll as u64,
499-
size: module_info.SizeOfImage as u64,
500-
path: module_path.ok(),
501-
pdb_info: get_pdb_info(process_handle, module_info.lpBaseOfDll as u64).ok(),
502-
});
499+
module_infos.insert(
500+
RangeInclusive::new(
501+
module_info.lpBaseOfDll as usize,
502+
(module_info.lpBaseOfDll as usize).saturating_add(module_info.SizeOfImage as usize),
503+
),
504+
ModuleInfo {
505+
base_address: module_info.lpBaseOfDll as u64,
506+
size: module_info.SizeOfImage as u64,
507+
path: module_path.ok(),
508+
pdb_info: get_pdb_info(process_handle, module_info.lpBaseOfDll as u64).ok(),
509+
},
510+
);
503511
}
504512

505513
Ok(module_infos)
@@ -564,7 +572,7 @@ struct ImageNtHeadersGeneric {
564572
}
565573

566574
#[repr(C)]
567-
#[derive(Debug)]
575+
#[derive(Debug, Clone, Eq, PartialEq)]
568576
struct Guid {
569577
pub data1: u32,
570578
pub data2: u16,

0 commit comments

Comments
 (0)