Giter Club home page Giter Club logo

Comments (8)

idealvin avatar idealvin commented on August 16, 2024

@CNchence
猜测 mono_native_thread_set_name 内部做了类似下面的事情,请参考这里

#pragma pack(pop)
void SetThreadName(DWORD dwThreadID, const char* threadName) {
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = threadName;
    info.dwThreadID = dwThreadID;
    info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
    __try{
        RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
    }
    __except (EXCEPTION_EXECUTE_HANDLER){
    }
#pragma warning(pop)

内部调用 RaiseException 抛出了异常,被 log 库捕捉到了。

用下面这段代码,复现了你的问题:

//
// Usage: SetThreadName ((DWORD)-1, "MainThread");
//
#include <windows.h>
const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
    DWORD dwType; // Must be 0x1000.
    LPCSTR szName; // Pointer to name (in user addr space).
    DWORD dwThreadID; // Thread ID (-1=caller thread).
    DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
#pragma pack(pop)
void SetThreadName(DWORD dwThreadID, const char* threadName) {
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = threadName;
    info.dwThreadID = dwThreadID;
    info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
    __try{
        RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
    }
    __except (EXCEPTION_EXECUTE_HANDLER){
    }
#pragma warning(pop)
}

from coost.

CNchence avatar CNchence commented on August 16, 2024

@idealvin
感谢回答和分析,我在dll工程中Release运行目标exe,打印log正常,但是直接运行目标exe就会出现.fatal文件。看fatal文件的错误,怀疑是直接运行exe时log无法拿到目标线程名导致的错误。

from coost.

idealvin avatar idealvin commented on August 16, 2024

co/base/stack_trace/stack_trace_win.cpp
可以在这个地方 加一行

return 0;

忽略掉异常

from coost.

CNchence avatar CNchence commented on August 16, 2024

仍然会有问题,相应线程调用log打印的数据仍然无法写入文件

from coost.

idealvin avatar idealvin commented on August 16, 2024

@CNchence 无法写日志到文件是另外一个问题,log 库捕获到异常后,调用 log::close() 关闭了写日志的线程。请更新代码,再验证一下

from coost.

idealvin avatar idealvin commented on August 16, 2024

from coost.

CNchence avatar CNchence commented on August 16, 2024

新代码验证了,没有问题

from coost.

idealvin avatar idealvin commented on August 16, 2024

👌

from coost.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.