如何修复“无效访问内存位置”错误? - 风袋

问题描述

我是使用 windbg 的新手,我试图在我试图调试的 .net 程序集的主要函数内设置断点,但我得到了:

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.webdriver.support.ui import webdriverwait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.firefox_profile import FirefoxProfile from selenium.webdriver import DesiredCapabilities from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoAlertPresentException from selenium.common.exceptions import TimeoutException from selenium.common.exceptions import UnexpecteDalertPresentException from fake_useragent import UserAgent from random import randint from time import sleep from random_word import RandomWords import unittest,time,re import winsound import sched import os import random torexe = os.popen(r'C:\Users\Casa\Desktop\Tor browser\browser\Torbrowser\Tor\tor.exe') profile = FirefoxProfile(r'C:\Users\Casa\Desktop\Tor browser\browser\Torbrowser\Data\browser\profile.default') profile.set_preference('network.proxy.type',1) profile.set_preference('network.proxy.socks','127.0.0.1') profile.set_preference('network.proxy.socks_port',9050) profile.set_preference("network.proxy.socks_remote_dns",False) profile.set_preference("browser.cache.disk.enable",False) profile.set_preference("browser.cache.memory.enable",False) profile.set_preference("browser.cache.offline.enable",False) profile.set_preference("dom.webdriver.enabled",False) profile.set_preference('useAutomationExtension',False) profile.update_preferences() desired = DesiredCapabilities.FIREFOX driver = webdriver.Firefox(firefox_profile= profile,desired_capabilities=desired,executable_path=r'C:\WebDrivers\geckodriver.exe') option = webdriver.FirefoxOptions() option.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe' options = Options() ua = UserAgent() userAgent = ua.random print(userAgent) options.add_argument(f'user-agent={userAgent}') options.headless = False driver.delete_all_cookies() driver.get("https://check.torproject.org/") # # # #do something... # # driver.quit()

我曾尝试使用 bp 和 bu $exentry 为程序入口设置断点,但即使这样也给了我同样的错误。我已经尝试在这个问题上和通过谷歌搜索其他旧的 stackoverflow 主题,但仍然没有找到解决方案。

任何帮助将不胜感激。

解决方法

给定一个为 .NET 框架 4.7 编译的简单 .NET 控制台应用程序

using System;

namespace DebugNetMainMethod
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("If you can read this,it's too late. You wanted to set a breakpoint earlier.");
            Console.ReadLine();
        }
    }
}

您可以使用 WinDbg Preview 对其进行调试。

  1. 运行 WinDbg 预览

  2. 选择“Launch Executable”并选择EXE

  3. WinDbg 将在初始断点处停止

     ntdll!LdrpDoDebuggerBreak+0x2b:
     7743ecc2 cc              int     3
    
  4. 此时,你得到了你描述的问题:

     0:000> bp $exentry
     0:000> bl
          0 e Disable Clear  007a27c6     0001 (0001)  0:**** DebugNetMainMethod!COM+_Entry_Point <PERF> (DebugNetMainMethod+0x27c6)
     0:000> g
     Unable to insert breakpoint 0 at 007a27c6,Win32 error 0n998
         "Invalid access to memory location."
     0:000> bc 0
     0:000> bl
    

注意:以后您希望准确提供上述信息,以便每个人都能重现您的问题。


WinDbg 不是为 .NET 制作的,而是用于调试“本机代码”,即为特定处理器(如 x86 或 AMD64)编译的代码。 WinDbg 不适用于 Java、Python 或 .NET。但是,对于 .NET,Microsoft 提供了一个名为 SOS 的扩展。您通常会像这样加载它:

0:000> .loadby sos clr
Unable to find module 'clr'

但在调试的早期阶段,加载的 DLL 并不多,clr 仍然缺失。所以让我们推迟这个:

0:000> sxe ld clrjit
0:000> g
[...]
ModLoad: 72950000 729da000   C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll
[...]
0:000> .loadby sos clr

没有输出意味着它有效。

0:000> !bpmd DebugNetMainMethod Program.Main
Found 1 methods in module 00914044...
MethodDesc = 00914d5c
Adding pending breakpoints...

0:000> g
[...]
(2658.2e08): CLR notification exception - code e0444143 (first chance)
JITTED DebugNetMainMethod!DebugNetMainMethod.Program.Main()
Setting breakpoint: bp 00BA085F [DebugNetMainMethod.Program.Main()]
Breakpoint 2 hit

0:000> !clrstack
OS Thread Id: 0x2e08 (0)
Child SP       IP Call Site
0075eff4 00ba085f DebugNetMainMethod.Program.Main() [C:\...\Program.cs @ 8]
0075f170 63dff036 [GCFrame: 0075f170] 

0:000> !u eip
Normal JIT generated code
DebugNetMainMethod.Program.Main()
Begin 00ba0848,size 32
[...]