image_watch.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //! Diagnostics (`VICTORY_IMAGE_WATCH`; off by default): words of the title's
  2. //! image, logged once a second, for what a launch's own counters say (how
  3. //! much of its streaming memory it uses, whether an allocation failed) where
  4. //! two launches end up with different pictures.
  5. //!
  6. //! `VICTORY_IMAGE_WATCH=used=98d26d8:8,failed=98d26d0:1`: a name, the offset
  7. //! in the image (hexadecimal) and the bytes (1, 2, 4 or 8).
  8. use std::time::Duration;
  9. struct Word {
  10. name: String,
  11. offset: usize,
  12. bytes: usize,
  13. }
  14. /// The words `text` names; what cannot be read of it is left out.
  15. fn words(text: &str) -> Vec<Word> {
  16. text.split(',')
  17. .filter_map(|part| {
  18. let (name, rest) = part.trim().split_once('=')?;
  19. let (offset, bytes) = rest.split_once(':').unwrap_or((rest, "8"));
  20. let offset = usize::from_str_radix(offset.trim().trim_start_matches("0x"), 16).ok()?;
  21. let bytes = bytes.trim().parse().ok().filter(|n| matches!(n, 1 | 2 | 4 | 8))?;
  22. Some(Word { name: name.trim().to_owned(), offset, bytes })
  23. })
  24. .collect()
  25. }
  26. /// `word` of the image at `base`; `None` if it cannot be read.
  27. fn read(base: usize, word: &Word) -> Option<u64> {
  28. use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
  29. use windows_sys::Win32::System::Threading::GetCurrentProcess;
  30. let mut value = [0u8; 8];
  31. // SAFETY: the kernel copies at most eight bytes into `value`, and fails
  32. // where there is nothing to read.
  33. let ok = unsafe {
  34. ReadProcessMemory(
  35. GetCurrentProcess(),
  36. (base + word.offset) as *const _,
  37. value.as_mut_ptr().cast(),
  38. word.bytes,
  39. std::ptr::null_mut(),
  40. )
  41. };
  42. (ok != 0).then(|| u64::from_le_bytes(value))
  43. }
  44. /// Starts watching the image at `base`, if asked for.
  45. pub(crate) fn start(base: usize) {
  46. let Ok(text) = std::env::var("VICTORY_IMAGE_WATCH") else {
  47. return;
  48. };
  49. let words = words(&text);
  50. if base == 0 || words.is_empty() {
  51. return;
  52. }
  53. static STARTED: std::sync::Once = std::sync::Once::new();
  54. STARTED.call_once(|| {
  55. let _ = std::thread::Builder::new().name("victory-image-watch".into()).spawn(move || {
  56. let mut last = Vec::new();
  57. loop {
  58. std::thread::sleep(Duration::from_secs(1));
  59. let now: Vec<Option<u64>> = words.iter().map(|word| read(base, word)).collect();
  60. if now == last {
  61. continue;
  62. }
  63. let values: Vec<String> = words
  64. .iter()
  65. .zip(&now)
  66. .map(|(word, value)| match value {
  67. Some(value) => format!("{}={value:#x}", word.name),
  68. None => format!("{}=?", word.name),
  69. })
  70. .collect();
  71. tracing::info!("the title's image: {}", values.join(" "));
  72. last = now;
  73. }
  74. });
  75. });
  76. }
  77. #[cfg(test)]
  78. mod tests {
  79. use super::*;
  80. #[test]
  81. fn words_are_read_where_they_are_named() {
  82. let all = words("used=98d26d8:8, failed = 0x10:1,odd=3:3,none");
  83. assert_eq!(all.len(), 2);
  84. assert_eq!((all[0].name.as_str(), all[0].offset, all[0].bytes), ("used", 0x98d_26d8, 8));
  85. assert_eq!((all[1].name.as_str(), all[1].offset, all[1].bytes), ("failed", 0x10, 1));
  86. let memory = [0x11u8, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  87. let word = |offset, bytes| Word { name: String::new(), offset, bytes };
  88. let base = memory.as_ptr() as usize;
  89. assert_eq!(read(base, &word(0, 4)), Some(0x4433_2211));
  90. assert_eq!(read(base, &word(2, 1)), Some(0x33));
  91. assert_eq!(read(8, &word(0, 8)), None);
  92. }
  93. }