Mono集成中使用api獲取當前mono 調用堆棧的方法


// 在mono源代碼層級中加如下兩個api 可以獲取堆棧字符串 這兩個api我新加的,原來沒有。基於原來的代碼改的。

// add by bodong
#if PLATFORM_WIN32


__declspec(dllexport) GString* __stdcall mono_debugger_get_stacktrace(int depth) { GString* text = g_string_new(0); MonoDomain* domain = mono_domain_get(); MonoJitTlsData* jit_tls = TlsGetValue(mono_jit_tls_id); MonoLMF* lmf = mono_get_lmf(); MonoJitInfo* ji, rji; gint native_offset, il_offset; gboolean managed; MonoContext ctx, new_ctx; int count = 0; char* fname; MONO_INIT_CONTEXT_FROM_FUNC(&ctx, mono_debugger_get_stacktrace); MONO_ARCH_CONTEXT_DEF mono_arch_flush_register_windows(); while (MONO_CONTEXT_GET_SP(&ctx) < jit_tls->end_of_stack) { ji = mono_find_jit_info(domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed); g_assert(ji); if (ji == (gpointer)-1) { break; } fname = mono_method_full_name(ji->method, TRUE); g_string_append_printf(text, "%s\n", fname); g_free(fname); if (++count >= depth) { break; } ctx = new_ctx; } return text; } __declspec(dllexport) void __stdcall mono_debugger_free_stacktrace(GString* ptr) { if (ptr != NULL) { g_string_free(ptr, TRUE); } }
#endif

    GString的定義如下:

typedef struct {
                char* str;
                size_t len;
                size_t allocated_len;
            } GString;

  使用時只需要獲取->str即可得到堆棧。用完后需要主動調用mono_debugger_free_stacktrace釋放內存。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM