Repository: ossrs/srs
File: trunk/src/main/srs_main_server.cpp (C++)

Code before:
// @global version of srs, which can grep keyword "XCORE"
extern const char* _srs_version;

// @global main SRS server, for debugging
SrsServer* _srs_server = NULL;

// Whether setup config by environment variables, see https://github.com/ossrs/srs/issues/2277
bool _srs_config_by_env = false;

// The binary name of SRS.
const char* _srs_binary = NULL;

// Free global data, for address sanitizer.
extern void srs_free_global_system_ips();

#ifdef SRS_SANITIZER_LOG
void asan_report_callback(const char* str)
{
    char buffer[256];

    std::vector<std::string> asan_logs = srs_string_split(string(str), "\n");
    size_t log_count = asan_logs.size();
    for (size_t i = 0; i < log_count; i++) {
        std::string l = srs_string_trim_start(asan_logs[i], " ");

        if (srs_string_starts_with(l, "#")) {
            if (srs_parse_asan_backtrace_symbols((char*)l.c_str(), buffer)) {
                asan_logs[i] += "," + std::string(buffer);
            }
        }
        srs_trace("%s", asan_logs[i].c_str());
    }
}
#endif

/**
 * main entrance.
 */
srs_error_t do_main(int argc, char** argv, char** envp)
{
    srs_error_t err = srs_success;

    // TODO: Might fail if change working directory.
    _srs_binary = argv[0];

    // Initialize global and thread-local variables.
    if ((err = srs_global_initialize()) != srs_success) {
        return srs_error_wrap(err, "global init");
    }

    if ((err = SrsThreadPool::setup_thread_locals()) != srs_success) {

Review comment (bug):
If asan already has the symbol table, there is no need for parsing, for example [3300](https://github.com/ossrs/srs/issues/3300#issuecomment-1347574311):

```
==6821==ERROR: AddressSanitizer: memcpy-param-overlap: memory ranges [0x631000190800,0x631000190814) and [0x63100019080c, 0x631000190820) overlap
    #0 0x7f4cb027dffe  (/lib64/libasan.so.5+0x99ffe)
    #1 0x65e714 in SrsBuffer::read_bytes(char*, int) src/kernel/srs_kernel_buffer.cpp:238
    #2 0xc03c14 in SrsLazyGbMediaTcpConn::do_cycle() src/app/srs_app_gb28181.cpp:1448
    #3 0xc020d0 in SrsLazyGbMediaTcpConn::cycle() src/app/srs_app_gb28181.cpp:1316
    #4 0x8ef256 in SrsFastCoroutine::cycle() src/app/srs_app_st.cpp:285
    #5 0x8ef343 in SrsFastCoroutine::pfn(void*) src/app/srs_app_st.cpp:300
    #6 0xc21b69 in _st_thread_main /home/tmp/srs/trunk/objs/Platform-Linux-3.10.0-GCC9.3.1-SRS5-x86_64/st-srs/sched.c:380
    #7 0xc22463 in st_thread_create /home/tmp/srs/trunk/objs/Platform-Linux-3.10.0-GCC9.3.1-SRS5-x86_64/st-srs/sched.c:666
    #8 0x7f4caa919647  (<unknown module>)
```

Only cases like [3297](https://github.com/ossrs/srs/issues/3297#issue-1485670484) require parsing.

```
==14647== ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60040001a919 at pc 0x6795e2 bp 0x7fdacfc34cf0 sp 0x7fdacfc34ce0
READ of size 1 at 0x60040001a919 thread T1 (srs-hybrid-2)
    #0 0x6795e1 (/usr/local/srs/trunk/objs/srs+0x6795e1)
    #1 0x67e275 (/usr/local/srs/trunk/objs/srs+0x67e275)
    #2 0x67de8a (/usr/local/srs/trunk/objs/srs+0x67de8a)
    #3 0x67d49e (/usr/local/srs/trunk/objs/srs+0x67d49e)
```

`TRANS_BY_GPT3`

Code after:
// @global version of srs, which can grep keyword "XCORE"
extern const char* _srs_version;

// @global main SRS server, for debugging
SrsServer* _srs_server = NULL;

// Whether setup config by environment variables, see https://github.com/ossrs/srs/issues/2277
bool _srs_config_by_env = false;

// The binary name of SRS.
const char* _srs_binary = NULL;

// Free global data, for address sanitizer.
extern void srs_free_global_system_ips();

#ifdef SRS_SANITIZER_LOG
extern void asan_report_callback(const char* str);
#endif

/**
 * main entrance.
 */
srs_error_t do_main(int argc, char** argv, char** envp)
{
    srs_error_t err = srs_success;

    // TODO: Might fail if change working directory.
    _srs_binary = argv[0];

    // Initialize global and thread-local variables.
    if ((err = srs_global_initialize()) != srs_success) {
        return srs_error_wrap(err, "global init");
    }

    if ((err = SrsThreadPool::setup_thread_locals()) != srs_success) {
        return srs_error_wrap(err, "thread init");
    }

    // For background context id.
    _srs_context->set_id(_srs_context->generate_id());

    // TODO: support both little and big endian.
    srs_assert(srs_is_little_endian());
    
    // for gperf gmp or gcp,
    // should never enable it when not enabled for performance issue.
#ifdef SRS_GPERF_MP
    HeapProfilerStart("gperf.srs.gmp");
#endif
#ifdef SRS_GPERF_CP
    ProfilerStart("gperf.srs.gcp");