a22_mingw_debug
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
* MinGW上でデバックするときのためのメモ
-(by [[K]], 2022.12.19)
** (8) まとまってない情報
*** (8-1) win32-apiのSetUnhandledExceptionFilter
--以下のようにすれば例外が拾えるようになる。
--[参考] https://learn.microsoft.com/ja-jp/windows/win32/...
#include <windows.h>
#include <stdio.h>
#include <string.h>
LONG WINAPI aExceptionHandler(struct _EXCEPTION_POINTERS...
{
char *s = "unknown";
DWORD c = ExceptionInfo->ExceptionRecord->ExceptionC...
if (c == EXCEPTION_ACCESS_VIOLATION) s = "EXCE...
if (c == EXCEPTION_GUARD_PAGE) s = "EXCE...
if (c == EXCEPTION_ILLEGAL_INSTRUCTION) s = "EXCE...
if (c == EXCEPTION_IN_PAGE_ERROR) s = "EXCE...
if (c == EXCEPTION_INT_DIVIDE_BY_ZERO) s = "EXCE...
if (c == EXCEPTION_PRIV_INSTRUCTION) s = "EXCE...
fprintf(stderr, "aExceptionHandler: addr=%08x, code=...
(int) ExceptionInfo->ExceptionRecord->ExceptionA...
s,
(int) ExceptionInfo->ExceptionRecord->ExceptionC...
(int) ExceptionInfo->ExceptionRecord->ExceptionF...
);
int i, n = ExceptionInfo->ExceptionRecord->NumberPar...
fprintf(stderr, " ExceptionInformation: n=%d", n);
for (i = 0; i < n; i++)
fprintf(stderr, ", %08x", (int) ExceptionInfo->E...
fprintf(stderr, "\n");
return EXCEPTION_EXECUTE_HANDLER; // これを返せばす...
}
int main()
{
SetUnhandledExceptionFilter(aExceptionHandler);
int *p = (int *) 32; printf("%d", *p); // code=EXCEP...
// int *p = (int *) 0x00401000; p[0] = 123; // code=EXC...
return 0;
}
***(8-2) マップファイルの取得と簡単な解析
--以下のようなバッチファイルを作れば、ビルド時にmapファイ...
c:\mingw\bin\gcc -m32 -Wall -Wextra -Wl,-s,-Map,%1_map.t...
--そのマップファイルがあれば、以下のような簡単なプログラ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char **argv)
{
if (argc < 3) {
printf("usage>srchadr0 map-file addr\n");
return 1;
}
FILE *fp = fopen(argv[1], "rb");
if (fp == 0) {
printf("fopen error: %s\n", argv[1]);
return 1;
}
char *b = malloc(65536); // 65536は1行の最大の長さ.
char *bb = malloc(65536);
bb[0] = 0;
int adr, i;
sscanf(argv[2], "%i", &adr);
printf("addr=0x%08x\n", adr);
for (;;) {
if (fgets(b, 65536, fp) == 0) {
printf("not found: .text\n");
return 1;
}
if (strncmp(b, ".text ", 6) == 0) break;
}
for (;;) {
if (fgets(b, 65536, fp) == 0) break;
if (strncmp(b, ".data ", 6) == 0) break;
if (strncmp(b, ".rdata ", 7) == 0) break;
if (strncmp(b, ".pdata ", 7) == 0) break;
if (strncmp(b, ".bss ", 5) == 0) break;
if (strncmp(b, ".idata ", 7) == 0) break;
if (strncmp(b, " ", 4) != 0) continue;
sscanf(b, "%i", &i);
if (i > adr) {
printf("%s%s", bb, b);
return 0;
}
strcpy(bb, b);
}
printf("addr not found\n");
return 0;
}
***(8-3) 使えそうなマクロ
__LINE__ : 行番号
__FILE__ : ソースファイル名
--[参考] https://www.hiroom2.com/2015/09/07/c%E8%A8%80%E8...
***(8-4) スレッドID関係
-GetCurrentThreadId : https://learn.microsoft.com/ja-jp/w...
--[Q] マルチスレッドでプログラムが動いているとき、もし一...
---・・・うん、たぶんきっとそうだろう。わざわざ他のスレッ...
* コメント欄
#comment
終了行:
* MinGW上でデバックするときのためのメモ
-(by [[K]], 2022.12.19)
** (8) まとまってない情報
*** (8-1) win32-apiのSetUnhandledExceptionFilter
--以下のようにすれば例外が拾えるようになる。
--[参考] https://learn.microsoft.com/ja-jp/windows/win32/...
#include <windows.h>
#include <stdio.h>
#include <string.h>
LONG WINAPI aExceptionHandler(struct _EXCEPTION_POINTERS...
{
char *s = "unknown";
DWORD c = ExceptionInfo->ExceptionRecord->ExceptionC...
if (c == EXCEPTION_ACCESS_VIOLATION) s = "EXCE...
if (c == EXCEPTION_GUARD_PAGE) s = "EXCE...
if (c == EXCEPTION_ILLEGAL_INSTRUCTION) s = "EXCE...
if (c == EXCEPTION_IN_PAGE_ERROR) s = "EXCE...
if (c == EXCEPTION_INT_DIVIDE_BY_ZERO) s = "EXCE...
if (c == EXCEPTION_PRIV_INSTRUCTION) s = "EXCE...
fprintf(stderr, "aExceptionHandler: addr=%08x, code=...
(int) ExceptionInfo->ExceptionRecord->ExceptionA...
s,
(int) ExceptionInfo->ExceptionRecord->ExceptionC...
(int) ExceptionInfo->ExceptionRecord->ExceptionF...
);
int i, n = ExceptionInfo->ExceptionRecord->NumberPar...
fprintf(stderr, " ExceptionInformation: n=%d", n);
for (i = 0; i < n; i++)
fprintf(stderr, ", %08x", (int) ExceptionInfo->E...
fprintf(stderr, "\n");
return EXCEPTION_EXECUTE_HANDLER; // これを返せばす...
}
int main()
{
SetUnhandledExceptionFilter(aExceptionHandler);
int *p = (int *) 32; printf("%d", *p); // code=EXCEP...
// int *p = (int *) 0x00401000; p[0] = 123; // code=EXC...
return 0;
}
***(8-2) マップファイルの取得と簡単な解析
--以下のようなバッチファイルを作れば、ビルド時にmapファイ...
c:\mingw\bin\gcc -m32 -Wall -Wextra -Wl,-s,-Map,%1_map.t...
--そのマップファイルがあれば、以下のような簡単なプログラ...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char **argv)
{
if (argc < 3) {
printf("usage>srchadr0 map-file addr\n");
return 1;
}
FILE *fp = fopen(argv[1], "rb");
if (fp == 0) {
printf("fopen error: %s\n", argv[1]);
return 1;
}
char *b = malloc(65536); // 65536は1行の最大の長さ.
char *bb = malloc(65536);
bb[0] = 0;
int adr, i;
sscanf(argv[2], "%i", &adr);
printf("addr=0x%08x\n", adr);
for (;;) {
if (fgets(b, 65536, fp) == 0) {
printf("not found: .text\n");
return 1;
}
if (strncmp(b, ".text ", 6) == 0) break;
}
for (;;) {
if (fgets(b, 65536, fp) == 0) break;
if (strncmp(b, ".data ", 6) == 0) break;
if (strncmp(b, ".rdata ", 7) == 0) break;
if (strncmp(b, ".pdata ", 7) == 0) break;
if (strncmp(b, ".bss ", 5) == 0) break;
if (strncmp(b, ".idata ", 7) == 0) break;
if (strncmp(b, " ", 4) != 0) continue;
sscanf(b, "%i", &i);
if (i > adr) {
printf("%s%s", bb, b);
return 0;
}
strcpy(bb, b);
}
printf("addr not found\n");
return 0;
}
***(8-3) 使えそうなマクロ
__LINE__ : 行番号
__FILE__ : ソースファイル名
--[参考] https://www.hiroom2.com/2015/09/07/c%E8%A8%80%E8...
***(8-4) スレッドID関係
-GetCurrentThreadId : https://learn.microsoft.com/ja-jp/w...
--[Q] マルチスレッドでプログラムが動いているとき、もし一...
---・・・うん、たぶんきっとそうだろう。わざわざ他のスレッ...
* コメント欄
#comment
ページ名: