# 5.1.3.載入符號, 檢視堆疊

## 1.堆疊 (Stack)

* 一支執行中的程式稱為**程序(process)**, 一個程序中可以有多個[**續程(thread)**](https://jenhsuan.gitbooks.io/c-programming/content/134xu-cheng-tong-bu-kong-zhi/1311xu-cheng.html), 每個續程中CPU都獨立執行程式, 不會互相干擾
* 每個續程有各自的**堆疊**, 堆疊可以存放**回返點**, **函式參數及區域變數**
  * 1.回返點:
    * 當一個執行緒由一個函式執行到另一個函式時, 需要將回返點記錄下來, 以便函式結束後返回到原函式

      ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-26%20%E4%B8%8B%E5%8D%882.40.40.png)
  * 2.函式參數及區域變數
    * 遞迴函式
      * 每呼叫一次函式, 便會將函式內的參數及區域變數新增加到堆疊上
      * 在函式返回時系統必須將堆疊上屬於那個函式的資料所有權釋放掉
    * 多執行緒執行相同函式
      * 一個函式內的參數及區域變數對於不一樣的執行緒來說都是**不一樣**的

## 2.範例程式碼

* 1.貼上範例程式碼

```
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void func(int a)
{
    printf("func call at %d!!\n", a);
}

int main()
{
    int i;
    for (i = 0; i<100; i++){ /* break point here */
        if (i % 10 == 0)
            func(i);
    }
    return 0;
}
```

* 2.設定[中斷點](https://jenhsuan.gitbooks.io/debugging-tools-experience/content/chapter4-fu-zhi-wen-ti-de-ji-qiao/42zhui-zong-cheng-shi-zhi-xing/422chu-cuo-gong-ju/4221shu-chudebug-xun-606f3a-visual-studio.html)

  ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-25%20%E4%B8%8B%E5%8D%881.29.47.png)

## 3.載入符號

* 1.用途
  * 通過載入系統DLL的符號資訊以獲得全部呼叫堆疊資訊
* 2.用法
  * 1.Tools -> Options -> Debugging -> Symbols -> 勾選Microsoft Symbols Server

    ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-19%20%E4%B8%8A%E5%8D%8810.27.55.png)

## 4.切換堆疊

* 1.在debug狀態下呼叫堆疊: DEBUG -> Windows -> Call Stack ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-25%20%E4%B8%8B%E5%8D%881.34.25.png)
* 2.從堆疊視窗中可以看到系統DLL的資訊 ![](https://github.com/jenhsuan/debugging-tools-experience/tree/91ad7a157f155ea77065c5600800bb4627bd1ae2/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202017-06-19%20%E4%B8%8A%E5%8D%8810.37.27.png)
