Debugging a Stack Overflow

别再只会重启了!用Keil的Fault Reports和Call Stack窗口,给你的HardFault做个‘CT扫描’ 本文深入解析Keil MDK中的Fault Reports和Call Stack窗口,帮助嵌入式开发者高效诊断HardFault问题。通过系统化的调试方法,快速定位内存越界、堆栈溢出等常见故障,提升单片机bug调试效率,告别盲目重启的无效操作。 阅读详情

A stack overflow is an error that user-mode threads can encounter. There are three possible causes for this error:

  • A thread uses the entire stack reserved for it. This is often caused by infinite recursion.
  • A thread cannot extend the stack because the page file is maxed out, and therefore no additional pages can be committed to extend the stack.
  • A thread cannot extend the stack because the system is within the brief period used to extend the page file.

 

Here is an example of how to debug this error. In this example, NTSD is running on the same computer as the target application and is redirecting its output to KD on the host computer. See Controlling the User-Mode Debugger from the Kernel Debugger for details.

The first step is see what event caused the debugger to break in:

0:002> .lastevent
Last event: Exception C00000FD, second chance

You can look up exception code 0xC00000FD in ntstatus.h, which can be found in the Microsoft Windows SDK and the Windows Driver Kit (WDK). This exception code is STATUS_STACK_OVERFLOW.

To double-check that the stack overflowed, you can use the k (Display Stack Backtrace) command:

0:002> k 
ChildEBP RetAddr
009fdd0c 71a32520 COMCTL32!_chkstk+0x25
009fde78 77cf8290 COMCTL32!ListView_WndProc+0x4c4
009fde98 77cfd634 USER32!_InternalCallWinProc+0x18
009fdf00 77cd55e9 USER32!UserCallWinProcCheckWow+0x17f
009fdf3c 77cd63b2 USER32!SendMessageWorker+0x4a3
009fdf5c 71a45b30 USER32!SendMessageW+0x44
009fdfec 71a45bb0 COMCTL32!CCSendNotify+0xc0e
009fdffc 71a1d688 COMCTL32!CICustomDrawNotify+0x2a
009fe074 71a1db30 COMCTL32!Header_Draw+0x63
009fe0d0 71a1f196 COMCTL32!Header_OnPaint+0x3f
009fe128 77cf8290 COMCTL32!Header_WndProc+0x4e2
009fe148 77cfd634 USER32!_InternalCallWinProc+0x18
009fe1b0 77cd4490 USER32!UserCallWinProcCheckWow+0x17f
009fe1d8 77cd46c8 USER32!DispatchClientMessage+0x31
009fe200 77f7bb3f USER32!__fnDWORD+0x22
009fe220 77cd445e ntdll!_KiUserCallbackDispatcher+0x13
009fe27c 77cfd634 USER32!DispatchMessageWorker+0x3bc
009fe2e4 009fe4a8 USER32!UserCallWinProcCheckWow+0x17f
00000000 00000000 0x9fe4a8

The target thread has broken into COMCTL32!_chkstk, which indicates a stack problem.

Now you should investigate the stack usage of the target process. The process has multiple threads, but the important one is the one that caused the overflow, so identify this thread first:

0:002> ~*k

   0  id: 570.574   Suspend: 1 Teb 7ffde000 Unfrozen
   .....

   1  id: 570.590   Suspend: 1 Teb 7ffdd000 Unfrozen
   .....

. 2  id: 570.598   Suspend: 1 Teb 7ffdc000 Unfrozen
ChildEBP RetAddr
009fdd0c 71a32520 COMCTL32!_chkstk+0x25  
.....

   3  id: 570.760   Suspend: 1 Teb 7ffdb000 Unfrozen

Now you need to investigate thread 2. The period at the left of this line indicates that this is the current thread.

The stack information is contained in the TEB (Thread Environment Block) at 0x7FFDC000. The easiest way to list it is using !teb. However, this requires you to have the proper symbols. For maximum versatility, assume you have no symbols:

0:002> dd 7ffdc000 L4
7ffdc000   009fdef0 00a00000 009fc000 00000000 

To interpret this, you need to look up the definition of the TEB data structure. If you had complete symbols, you could use dt TEB to do this. But in this case, you will need to look at the ntpsapi.h file in the Microsoft Windows SDK. For Windows XP and later versions of Windows, this file contains the following information:

typedef struct _TEB {
    NT_TIB NtTib;
    PVOID  EnvironmentPointer;
    CLIENT_ID ClientId;
    PVOID ActiveRpcHandle;
    PVOID ThreadLocalStoragePointer;
    PPEB ProcessEnvironmentBlock;
    ULONG LastErrorValue;
    .....
    PVOID DeallocationStack;
    .....
} TEB;

typedef struct _NT_TIB {
    struct _EXCEPTION_REGISTRATION_RECORD *ExceptionList;
    PVOID StackBase;
    PVOID StackLimit;
    .....
} NT_TIB;

This indicates that the second and third DWORDs in the TEB structure point to the bottom and top of the stack, respectively. In this case, these addresses are 0x00A00000 and 0x009FC000. (The stack grows downward in memory.) You can calculate the stack size using the ? (Evaluate Expression) command:

0:002> ? a00000-9fc000
Evaluate expression: 16384 = 00004000

This shows that the stack size is 16 K. The maximum stack size is stored in the field DeallocationStack. After some calculation, you can determine that this field's offset is 0xE0C.

0:002> dd 7ffdc000+e0c L1 
7ffdce0c   009c0000 

0:002> ? a00000-9c0000 
Evaluate expression: 262144 = 00040000

This shows that the maximum stack size is 256 K, which means more than adequate stack space is left.

Furthermore, this process looks clean — it is not in an infinite recursion or exceeding its stack space by using excessively large stack-based data structures.

Now break into KD and look at the overall system memory usage with the !vm extension command:

0:002> .breakin 
Break instruction exception - code 80000003 (first chance)
ntoskrnl!_DbgBreakPointWithStatus+4:
80148f9c cc               int     3

kd> !vm 

*** Virtual Memory Usage ***
        Physical Memory:     16268   (   65072 Kb)
        Page File: /??/C:/pagefile.sys
           Current:    147456Kb Free Space:     65988Kb
           Minimum:     98304Kb Maximum:       196608Kb
        Available Pages:      2299   (    9196 Kb)
        ResAvail Pages:       4579   (   18316 Kb)
        Locked IO Pages:        93   (     372 Kb)
        Free System PTEs:    42754   (  171016 Kb)
        Free NP PTEs:         5402   (   21608 Kb)
        Free Special NP:       348   (    1392 Kb)
        Modified Pages:        757   (    3028 Kb)
        NonPagedPool Usage:    811   (    3244 Kb)
        NonPagedPool Max:     6252   (   25008 Kb)
        PagedPool 0 Usage:    1337   (    5348 Kb)
        PagedPool 1 Usage:     893   (    3572 Kb)
        PagedPool 2 Usage:     362   (    1448 Kb)
        PagedPool Usage:      2592   (   10368 Kb)
        PagedPool Maximum:   13312   (   53248 Kb)
        Shared Commit:        3928   (   15712 Kb)
        Special Pool:         1040   (    4160 Kb)
        Shared Process:       3641   (   14564 Kb)
        PagedPool Commit:     2592   (   10368 Kb)
        Driver Commit:         887   (    3548 Kb)
        Committed pages:     45882   (  183528 Kb)
        Commit limit:        50570   (  202280 Kb)

        Total Private:       33309   (  133236 Kb)
         .....

First, look at nonpaged and paged pool usage. Both are well within limits, so these are not the cause of the problem.

Next, look at the number of committed pages: 183528 out of 202280. This is very close to the limit. Although this display does not show this number to be at the limit, you should keep in mind that while you are performing user-mode debugging, other processes are running on the system. Each time an NTSD command is executed, these other processes are also allocating and freeing memory. That means you do not know exactly what the memory state was like at the time the stack overflow occurred. Given how close the committed page number is to the limit, it is reasonable to conclude that the page file was used up at some point and this caused the stack overflow.

This is not an uncommon occurrence, and the target application cannot really be faulted for this. If it happens frequently, you may want to consider raising the initial stack commitment for the failing application.

Analyzing a Single Function Call

It can also be useful to find out exactly how much stack space a certain function call is allocating.

To do this, disassemble the first few instructions and look for the instruction sub esp,number. This moves the stack pointer, effectively reserving number bytes for local data.

Here is an example:

0:002> k 
ChildEBP RetAddr
009fdd0c 71a32520 COMCTL32!_chkstk+0x25
009fde78 77cf8290 COMCTL32!ListView_WndProc+0x4c4
009fde98 77cfd634 USER32!_InternalCallWinProc+0x18
009fdf00 77cd55e9 USER32!UserCallWinProcCheckWow+0x17f
009fdf3c 77cd63b2 USER32!SendMessageWorker+0x4a3
009fdf5c 71a45b30 USER32!SendMessageW+0x44
009fdfec 71a45bb0 COMCTL32!CCSendNotify+0xc0e
009fdffc 71a1d688 COMCTL32!CICustomDrawNotify+0x2a
009fe074 71a1db30 COMCTL32!Header_Draw+0x63
009fe0d0 71a1f196 COMCTL32!Header_OnPaint+0x3f
009fe128 77cf8290 COMCTL32!Header_WndProc+0x4e2
009fe148 77cfd634 USER32!_InternalCallWinProc+0x18

0:002> u COMCTL32!Header_Draw
COMCTL32!Header_Draw:
71a1d625 55               push    ebp
71a1d626 8bec             mov     ebp,esp
71a1d628 83ec58           sub     esp,0x58
71a1d62b 53               push    ebx
71a1d62c 8b5d08           mov     ebx,[ebp+0x8]
71a1d62f 56               push    esi
71a1d630 57               push    edi
71a1d631 33f6             xor     esi,esi

This shows that Header_Draw allocated 0x58 bytes of stack space.

 
【pyinstaller】modulenotfounderror no module named ‘rasterio._shim‘、‘rasterio.control‘等 对于出现no module not found error的情况一般的解决办法就是使用hidden imports参数 当前对于这种问题的解决方法就是将所有缺少的模块用hidden imports给加进去,当然手动加有些麻烦, 故参考博文https://stackoverflow.com/questions/53149750/something-wrong-with-how-im-bundling-rasterio-into-an-executable给出的解决方法: # -*- mode: pyt 阅读详情

相关推荐

AttributeError: ‘Tensor‘ object has no attribute ‘lower‘

Debug进行时,错误代码如题所示。、 问题出使用GlobaleAveragePooling2D,我写成了GlobaleAveragePooling2D(net) net就是定义的、要进行最大均值池化的模型。 但这样是不行的,有两个解决办法: 1. 先定义GlobalAP=GlobaleAveragePooling2D() 然后再x=GlobalAP(net) 2.直接GlobaleAveragePooling2D()(net) ...

weixin_48253350的博客 3213

stack overflow常见解决方法

stack overflow一般都是堆栈内存不够 情况一:递归次数过多 解决办法:用循环来代替递归一般百分之九十能解决堆栈溢出的问题 情况二:创建的局部变量过多 解决办法:这种情况一般就是因为局部变量没有及时清除导致的,一般也是递归,要么把局部变量改成全局变量,要么传入引用(就是再数据类型的前面加以个&,这样不用去重新再子函数里面去开辟一个空间给变量了)要么用指针 情况三:数组开的过大 解...

萌新的博客 1万+

WinDbg Preview实战:手把手教你分析Windows 10崩溃dmp文件(附符号文件配置避坑指南)

本文详细介绍了如何使用WinDbg Preview分析Windows 10崩溃dmp文件,包括符号文件配置的优化技巧。从环境准备到实战演练,提供了一套完整的调试流程,帮助开发者快速定位和解决系统崩溃问题,特别强调了符号文件配置中的常见陷阱和解决方案。

weixin_30896511的博客 337

ESP32 出现A stack overflow in task httpd has been detected.错误

ESP32 stack overflow task httpd

liuguomark的专栏 6158

OpenGL 调试Debugging

OpenGL调试Debugging调试Debugging简介glGetError()调试输出GLFW中的调试输出过滤调试输出回溯调试错误源调试着色器输出OpenGL GLSL参考编译器帧缓冲输出外部调试软件gDebuggerRenderDocCodeXLNVIDIA Nsight 调试Debugging简介 图形编程可以带来很多的乐趣,然而如果什么东西渲染错误,或者甚至根本就没有渲染,它同样可以给你带来大量的沮丧感!由于我们大部分时间都在与像素打交道,当出现错误的时候寻找错误的源头可能会非常困难。调试(De

希望我的博客,能帮上你解决学习中工作中所遇到的问题 767

告别龟速下载!保姆级教程:用离线包5分钟搞定Arduino IDE的STM32开发环境(附最新库文件)

本文提供了一套完整的Arduino IDE离线配置方案,帮助开发者5分钟快速搭建STM32开发环境。通过详细的板支持包安装指南、文件校验方法和常见问题解决方案,彻底解决GitHub下载慢的问题,提升开发效率。

weixin_26802933的博客 375

偷换windows窗口过程

Window user32子动态库控件封装和消息分发浅析  这篇文章提到窗口程序在分发消息时会调用:UserCallWinProcCheckWow--->_InternalCallWinProc-->各个窗口程序的WndProc。各类控件的窗口程序不同,比如Button控件用的是Button_WndProc,_InternalCallWinProc { } 会通过。受到这篇文章的启示,

lixiangminghate的专栏 3252

HFSS15 win10_64位破解

HFSS15 win10_64位破解 找到的别人的软件,但在首次安装时失败了,并不能patched。寻找了几种方法,已成功安装。在此写下安装经验,供大家参考。 软件安装包以及破解软件: 链接:https://pan.baidu.com/s/1MMjV_31maU5QD6fmI4zfIA 提取码:yj4q 在安装之前,建议关闭杀毒软件。 Step1:进入HFSS150Win64安装包,点击au...

玉笙寒 4万+

dll反复加载卸载过程中遇见的诡异问题

这两天遇见了个诡异的问题,扩展进程反复的加载和卸载一个插件的dll的时候,扩展会在启动的时候崩溃。   通过windbg查看崩溃栈: 0:024> kbChildEBP RetAddr Args to Child WARNING: Frame IP not in any known module. Following frames may be wrong.02c6f7d4 ...

weixin_30725315的博客 902

深度解析中国支付清算体系

说起国之重器,不仅仅有 东风快递、渔16、渔17、渔18,还有另外一个国之重器-CNAPS2(中国支付清算体系)。 中国国内支付行业的发达水平、繁荣程度,不客气的讲,在全世界都是数一数二的,世界上最大的银行卡组织银联在中国,世界上交易量最大的网络零售支付清算体网联在中国。可以说现在我国经济的高速发展,离不开CNAPS2。

银行信息系统应用架构详解 2543

告别环境配置烦恼:用Conda一键搞定GATK4、samtools、fastp等生信分析全家桶

本文介绍如何利用Conda快速搭建包含GATK4、samtools、fastp等工具的生物信息学分析环境,解决软件安装和版本冲突问题。通过详细的配置步骤和实战案例,帮助研究者十分钟内完成重测序分析全家桶的环境配置,显著提升上游分析效率。

weixin_30753873的博客 339

Executor(五):ForkJoinPool详解 jdk1.8

ForkJoinPool在1.7引入,它只被用来运行ForkJoinTask的子类任务。这个线程池和其他的线程池的不同之处在于它使用分而治之和工作窃取算法去执行任务。有效的去处理大多数任务能衍生出小任务的问题。 工作窃取算法 看看维基百科对工作窃取算法的描述:在并行计算中,工作窃取是多线程计算机程序的调度策略。它解决了在具有固定数量的处理器(或内核)的静态多线程计算机上执行动态多线程计算的问题...

lc 3744

使用Python爬虫获取Facebook帖子和评论数据

Web爬虫是用于自动抓取网页数据的程序。它通过发送HTTP请求,获取网页的HTML源代码,再对源代码进行解析,提取所需的内容。爬虫常用于数据收集、信息提取、数据分析等应用。

2201_76125261的博客 2003

Debugging a Stack Overflow with Windbg

zz from windbg help doc Debugging a Stack Overflow A stack overflow is an error that user-mode threads can encounter. There are three possible causes for this error: A thread uses the e

fxismonk的专栏 772

8.5华为OD机试真题 新系统 - 最长不连续子串 (Java/Py/C/C++/Js/Go)

8.5华为OD机试真题 新系统 - 最长不连续子串 (Java/Py/C/C++/Js/Go)

算法大师! 637

无法将“choco”项识别为 cmdlet、函数、脚本文件或可运行程序的名称的解决方案

本文主要介绍了无法将“choco”项识别为 cmdlet、函数、脚本文件或可运行程序的名称的解决方案,希望能对使用wIndows的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 12万+

xcode - iPhone Debugging: How to resolve 'failed to get the task for process'? - Stack Overflow

The program being debugged is not being run. Everyone sees this once in a while during Xcode development for iPhone apps. And there are a million reasons and hacks that make it go away. Here is...

aikun2355的博客 184

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

xv6 stack overflow

学习目的:kernel stack overflow.

weixin_41028621的博客 697

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

C++ no debugging symbol found

C++ no debugging symbol found

上善若水 2836

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Android realase debug 区别,xmpp - aSmack Android documentation and debugging - Stack Overflow

ASmack library refers to the documentation at http://www.igniterealtime.org/builds/smack/docs/latest/documentation/index.htmlwhich I think it is outdated and does not provide accurate info. Examples a...

weixin_34656273的博客 145

android生成coredump,debugging - Generate core dump in android - Stack Overflow

The ndk comes with ndk-gdb, which supposedly allows you to debug native applications. Also, if you download the whole andriod open source project, they also have some version of gdb used for debuggin...

weixin_32494599的博客 186

stack的wp

https://hackme.inndy.tw/scoreboard/ 题目很有趣,我做了stack这个题目感觉还不错,我把wp分享出来,方便大家学习 stack的题目要求是: nc hackme.inndy.tw 7716Here is my C programming homework But compiled with full protection (RELRO, PIE, NX, Sta

一个码农的笔记 2314

android studio break,debugging - Android Studio IDE: Break on Exception - Stack Overflow

To break on all exceptions in your code and other exceptions if uncaught:This methods filters out the exception types that the runtime throws during normal operation (not very exceptional, are they?)....

weixin_39951930的博客 164

An experience of fixing a memory-corruption bug (2)

(4) Asking for

一个手艺人的专栏 1087

AndroidStudio 疑难杂症 - app:dataBindingGenBaseClassesDebug Parameter ‘directory‘ is not a directory

在编译项目时,有时会有到一个奇葩错误。 Execution failed for task ':app:dataBindingGenBaseClassesDebug'. Parameter 'directory' is not a directory 尝试过 invalidata Cache/Restart 或者删除 .idea 文件夹,都不好使,一度有些想卸载 android studio。 最终 stackoverflow 上看到解决方法 通过命令方式执行gradle一次就好类,命令如下: ./gra

愛橙絮 6323
上一篇: 从内存中加载Pe文件
下一篇: 反向进程注入及隐藏--动手做一个最简单的PELoader
iiprogram
博客等级 码龄25年 1068粉丝 1515原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址