📄 erl_mtrace.c
字号:
sys_get_pid(pid); write_trace_header(nodename ? nodename : "", pid, hostname); erts_mtrace_update_heap_size(); }}voiderts_mtrace_install_wrapper_functions(void){ if (erts_mtrace_enabled) { int i; /* Install trace functions */ ASSERT(sizeof(erts_allctrs) == sizeof(real_allctrs)); sys_memcpy((void *) real_allctrs, (void *) erts_allctrs, sizeof(erts_allctrs)); for (i = ERTS_ALC_A_MIN; i <= ERTS_ALC_A_MAX; i++) { erts_allctrs[i].alloc = mtrace_alloc; erts_allctrs[i].realloc = mtrace_realloc; erts_allctrs[i].free = mtrace_free; erts_allctrs[i].extra = (void *) &real_allctrs[i]; } }}voiderts_mtrace_stop(void){ erts_mtx_lock(&mtrace_op_mutex); erts_mtx_lock(&mtrace_buf_mutex); if (erts_mtrace_enabled) { Uint32 ti = get_time_inc(); if (ti != INVALID_TIME_INC && MAKE_TBUF_SZ(UI8_SZ + UI16_SZ + UI32_SZ)) { byte *hdrp; Uint16 hdr; int ti_n; *(tracep++) = ERTS_MT_STOP_BDY_TAG; hdrp = tracep; tracep += 2; PUT_VSZ_UI32(tracep, ti_n, ti); hdr = ti_n; WRITE_UI16(hdrp, hdr); if(send_trace_buffer()) { erts_mtrace_enabled = 0; erts_sock_close(socket_desc); socket_desc = ERTS_SOCK_INVALID_SOCKET; } } } erts_mtx_unlock(&mtrace_buf_mutex); erts_mtx_unlock(&mtrace_op_mutex);}voiderts_mtrace_exit(Uint32 exit_value){ erts_mtx_lock(&mtrace_op_mutex); erts_mtx_lock(&mtrace_buf_mutex); if (erts_mtrace_enabled) { Uint32 ti = get_time_inc(); if (ti != INVALID_TIME_INC && MAKE_TBUF_SZ(UI8_SZ + UI16_SZ + 2*UI32_SZ)) { byte *hdrp; Uint16 hdr; int ti_n, exit_value_n; *(tracep++) = ERTS_MT_EXIT_BDY_TAG; hdrp = tracep; tracep += 2; PUT_VSZ_UI32(tracep, exit_value_n, exit_value); PUT_VSZ_UI32(tracep, ti_n, ti); hdr = ti_n; hdr <<= UI32_MSB_EHF_SZ; hdr |= exit_value_n; WRITE_UI16(hdrp, hdr); if(send_trace_buffer()) { erts_mtrace_enabled = 0; erts_sock_close(socket_desc); socket_desc = ERTS_SOCK_INVALID_SOCKET; } } } erts_mtx_unlock(&mtrace_buf_mutex); erts_mtx_unlock(&mtrace_op_mutex);}static ERTS_INLINE voidwrite_alloc_entry(byte tag, void *res, ErtsAlcType_t x, ErtsAlcType_t y, Uint size){ erts_mtx_lock(&mtrace_buf_mutex); if (erts_mtrace_enabled) { Uint32 ti = get_time_inc(); if (ti != INVALID_TIME_INC && MAKE_TBUF_SZ(UI8_SZ + 2*UI16_SZ + 2*UI_SZ + UI32_SZ)) { Uint16 hdr, t_no = (Uint16) x, ct_no = (Uint16) y; byte *hdrp; int t_no_n, ct_no_n = 0, res_n, size_n, ti_n; *(tracep++) = tag; hdrp = tracep; tracep += 2; if (tag == ERTS_MT_CRR_ALLOC_BDY_TAG) { PUT_VSZ_UI16(tracep, ct_no_n, ct_no); } PUT_VSZ_UI16(tracep, t_no_n, t_no); PUT_VSZ_UI( tracep, res_n, res); PUT_VSZ_UI( tracep, size_n, size); PUT_VSZ_UI32(tracep, ti_n, ti); hdr = ti_n; hdr <<= UI_MSB_EHF_SZ; hdr |= size_n; hdr <<= UI_MSB_EHF_SZ; hdr |= res_n; hdr <<= UI16_MSB_EHF_SZ; hdr |= t_no_n; if (tag == ERTS_MT_CRR_ALLOC_BDY_TAG) { hdr <<= UI16_MSB_EHF_SZ; hdr |= ct_no_n; } WRITE_UI16(hdrp, hdr);#if TRACE_PRINTOUTS print_trace_entry(tag, ct_no, ct_no_n, t_no, t_no_n, (Uint) res, res_n, 0, 0, size, size_n, ti, ti_n);#endif#ifdef DEBUG check_alloc_entry(hdrp-1, tracep, tag, ct_no, ct_no_n, t_no, t_no_n, (Uint) res, res_n, size, size_n, ti, ti_n);#endif } } erts_mtx_unlock(&mtrace_buf_mutex);}static ERTS_INLINE voidwrite_realloc_entry(byte tag, void *res, ErtsAlcType_t x, ErtsAlcType_t y, void *ptr, Uint size){ erts_mtx_lock(&mtrace_buf_mutex); if (erts_mtrace_enabled) { Uint32 ti = get_time_inc(); if (ti != INVALID_TIME_INC && MAKE_TBUF_SZ(UI8_SZ + 2*UI16_SZ + 3*UI_SZ + UI32_SZ)) { Uint16 hdr, t_no = (Uint16) x, ct_no = (Uint16) y; byte *hdrp; int t_no_n, ct_no_n = 0, res_n, ptr_n, size_n, ti_n; *(tracep++) = tag; hdrp = tracep; tracep += 2; if (tag == ERTS_MT_CRR_REALLOC_BDY_TAG) { PUT_VSZ_UI16(tracep, ct_no_n, ct_no); } PUT_VSZ_UI16(tracep, t_no_n, t_no); PUT_VSZ_UI( tracep, res_n, res); PUT_VSZ_UI( tracep, ptr_n, ptr); PUT_VSZ_UI( tracep, size_n, size); PUT_VSZ_UI32(tracep, ti_n, ti); hdr = ti_n; hdr <<= UI_MSB_EHF_SZ; hdr |= size_n; hdr <<= UI_MSB_EHF_SZ; hdr |= ptr_n; hdr <<= UI_MSB_EHF_SZ; hdr |= res_n; hdr <<= UI16_MSB_EHF_SZ; hdr |= t_no_n; if (tag == ERTS_MT_CRR_REALLOC_BDY_TAG) { hdr <<= UI16_MSB_EHF_SZ; hdr |= ct_no_n; } WRITE_UI16(hdrp, hdr);#if TRACE_PRINTOUTS print_trace_entry(tag, ct_no, ct_no_n, t_no, t_no_n, (Uint) res, res_n, (Uint) ptr, ptr_n, size, size_n, ti, ti_n);#endif#ifdef DEBUG check_realloc_entry(hdrp-1, tracep, tag, ct_no, ct_no_n, t_no, t_no_n, (Uint) res, res_n, (Uint) ptr, ptr_n, size, size_n, ti, ti_n);#endif } } erts_mtx_unlock(&mtrace_buf_mutex);}static ERTS_INLINE voidwrite_free_entry(byte tag, ErtsAlcType_t x, ErtsAlcType_t y, void *ptr){ erts_mtx_lock(&mtrace_buf_mutex); if (erts_mtrace_enabled) { Uint32 ti = get_time_inc(); if (ti != INVALID_TIME_INC && MAKE_TBUF_SZ(UI8_SZ + 2*UI16_SZ + UI_SZ + UI32_SZ)) { Uint16 hdr, t_no = (Uint16) x, ct_no = (Uint16) y; byte *hdrp; int t_no_n, ct_no_n = 0, ptr_n, ti_n; *(tracep++) = tag; hdrp = tracep; tracep += 2; if (tag == ERTS_MT_CRR_FREE_BDY_TAG) { PUT_VSZ_UI16(tracep, ct_no_n, ct_no); } PUT_VSZ_UI16(tracep, t_no_n, t_no); PUT_VSZ_UI( tracep, ptr_n, ptr); PUT_VSZ_UI32(tracep, ti_n, ti); hdr = ti_n; hdr <<= UI_MSB_EHF_SZ; hdr |= ptr_n; hdr <<= UI16_MSB_EHF_SZ; hdr |= t_no_n; if (tag == ERTS_MT_CRR_FREE_BDY_TAG) { hdr <<= UI16_MSB_EHF_SZ; hdr |= ct_no_n; } WRITE_UI16(hdrp, hdr);#if TRACE_PRINTOUTS print_trace_entry(tag, ct_no, ct_no_n, t_no, t_no_n, (Uint) 0, 0, (Uint) ptr, ptr_n, 0, 0, ti, ti_n);#endif#ifdef DEBUG check_free_entry(hdrp-1, tracep, tag, ct_no, ct_no_n, t_no, t_no_n, (Uint) ptr, ptr_n, ti, ti_n);#endif } } erts_mtx_unlock(&mtrace_buf_mutex);}static void *mtrace_alloc(ErtsAlcType_t n, void *extra, Uint size){ ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra; void *res; erts_mtx_lock(&mtrace_op_mutex); res = (*real_af->alloc)(n, real_af->extra, size); write_alloc_entry(ERTS_MT_ALLOC_BDY_TAG, res, n, 0, size); erts_mtx_unlock(&mtrace_op_mutex); return res;}static void *mtrace_realloc(ErtsAlcType_t n, void *extra, void *ptr, Uint size){ ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra; void *res; erts_mtx_lock(&mtrace_op_mutex); res = (*real_af->realloc)(n, real_af->extra, ptr, size); write_realloc_entry(ERTS_MT_REALLOC_BDY_TAG, res, n, 0, ptr, size); erts_mtx_unlock(&mtrace_op_mutex); return res;}static voidmtrace_free(ErtsAlcType_t n, void *extra, void *ptr){ ErtsAllocatorFunctions_t *real_af = (ErtsAllocatorFunctions_t *) extra; erts_mtx_lock(&mtrace_op_mutex); (*real_af->free)(n, real_af->extra, ptr); write_free_entry(ERTS_MT_FREE_BDY_TAG, n, 0, ptr); erts_mtx_unlock(&mtrace_op_mutex);}voiderts_mtrace_crr_alloc(void *res, ErtsAlcType_t n, ErtsAlcType_t m, Uint size){ write_alloc_entry(ERTS_MT_CRR_ALLOC_BDY_TAG, res, n, m, size);}voiderts_mtrace_crr_realloc(void *res, ErtsAlcType_t n, ErtsAlcType_t m, void *ptr, Uint size){ write_realloc_entry(ERTS_MT_CRR_REALLOC_BDY_TAG, res, n, m, ptr, size);}voiderts_mtrace_crr_free(ErtsAlcType_t n, ErtsAlcType_t m, void *ptr){ write_free_entry(ERTS_MT_CRR_FREE_BDY_TAG, n, m, ptr);}#if TRACE_PRINTOUTSstatic voidprint_trace_entry(byte tag, Uint16 t_no, int t_no_n, Uint16 ct_no, int ct_no_n, Uint res, int res_n, Uint ptr, int ptr_n, Uint size, int size_n, Uint32 ti,int ti_n){ switch (tag) { case ERTS_MT_ALLOC_BDY_TAG: fprintf(stderr, "{alloc, {%lu, %lu, %lu}, {%u, %u, %u, %u}}\n\r", (unsigned long) t_no, (unsigned long) res, (unsigned long) size, MSB2BITS(t_no_n), MSB2BITS(res_n), MSB2BITS(size_n), MSB2BITS(ti_n)); break; case ERTS_MT_REALLOC_BDY_TAG: fprintf(stderr, "{realloc, {%lu, %lu, %lu, %lu}, {%u, %u, %u, %u, %u}}\n\r", (unsigned long) t_no, (unsigned long) res, (unsigned long) ptr, (unsigned long) size, MSB2BITS(t_no_n), MSB2BITS(res_n), MSB2BITS(ptr_n), MSB2BITS(size_n), MSB2BITS(ti_n)); break; case ERTS_MT_FREE_BDY_TAG: fprintf(stderr, "{free, {%lu, %lu}, {%u, %u, %u, %u, %u}}\n\r", (unsigned long) t_no, (unsigned long) ptr, MSB2BITS(t_no_n), MSB2BITS(ptr_n), MSB2BITS(ti_n)); break; case ERTS_MT_CRR_ALLOC_BDY_TAG: fprintf(stderr, "{crr_alloc, {%lu, %lu, %lu, %lu}, {%u, %u, %u, %u, %u}}\n\r", (unsigned long) ct_no, (unsigned long) t_no, (unsigned long) res, (unsigned long) size, MSB2BITS(ct_no_n), MSB2BITS(t_no_n), MSB2BITS(res_n), MSB2BITS(size_n), MSB2BITS(ti_n)); break; case ERTS_MT_CRR_REALLOC_BDY_TAG: fprintf(stderr, "{crr_realloc, {%lu, %lu, %lu, %lu, %lu}, " "{%u, %u, %u, %u, %u, %u}}\n\r", (unsigned long) ct_no, (unsigned long) t_no, (unsigned long) res, (unsigned long) ptr, (unsigned long) size, MSB2BITS(ct_no_n), MSB2BITS(t_no_n), MSB2BITS(res_n), MSB2BITS(ptr_n), MSB2BITS(size_n), MSB2BITS(ti_n)); break; case ERTS_MT_CRR_FREE_BDY_TAG: fprintf(stderr, "{crr_free, {%lu, %lu, %lu}, {%u, %u, %u, %u}}\n\r", (unsigned long) ct_no, (unsigned long) t_no, (unsigned long) ptr, MSB2BITS(ct_no_n), MSB2BITS(t_no_n), MSB2BITS(ptr_n), MSB2BITS(ti_n)); break; default: fprintf(stderr, "{'\?\?\?'}\n\r"); break; }}#endif /* #if TRACE_PRINTOUTS */#ifdef DEBUG#define GET_UI16(P) ((P) += UI16_SZ, \ (((Uint16) (*((P) - 2) << 8)) | ((Uint16) (*((P) - 1)))))static voidcheck_ui(Uint16 *hdrp, byte **pp, Uint ui, int msb, Uint16 f_mask, Uint16 f_size){ Uint x; int n; ASSERT((msb & ~f_mask) == 0); n = (int) (*hdrp & f_mask); ASSERT(n == msb); *hdrp >>= f_size; x = 0; switch (n) {#ifdef ARCH_64 case 7: x |= *((*pp)++); x <<= 8; case 6: x |= *((*pp)++); x <<= 8; case 5: x |= *((*pp)++); x <<= 8; case 4: x |= *((*pp)++); x <<= 8;#endif case 3: x |= *((*pp)++); x <<= 8; case 2: x |= *((*pp)++); x <<= 8; case 1: x |= *((*pp)++); x <<= 8; case 0: x |= *((*pp)++); break; default: ASSERT(0); } ASSERT(x == ui);}voidcheck_alloc_entry(byte *sp, byte *ep, byte tag, Uint16 ct_no, int ct_no_n, Uint16 t_no, int t_no_n, Uint res, int res_n, Uint size, int size_n, Uint32 ti,int ti_n){ byte *p = sp; Uint16 hdr; ASSERT(*p == tag); p++; hdr = GET_UI16(p); if (tag == ERTS_MT_CRR_ALLOC_BDY_TAG) check_ui(&hdr, &p, ct_no, ct_no_n, UI16_MSB_EHF_MSK, UI16_MSB_EHF_SZ); check_ui(&hdr, &p, t_no, t_no_n, UI16_MSB_EHF_MSK, UI16_MSB_EHF_SZ); check_ui(&hdr, &p, res, res_n, UI_MSB_EHF_MSK, UI_MSB_EHF_SZ); check_ui(&hdr, &p, size, size_n, UI_MSB_EHF_MSK, UI_MSB_EHF_SZ); check_ui(&hdr, &p, ti, ti_n, UI32_MSB_EHF_MSK, UI32_MSB_EHF_SZ); ASSERT(hdr == 0); ASSERT(p == ep);}voidcheck_realloc_entry(byte *sp, byte *ep, byte tag, Uint16 ct_no, int ct_no_n, Uint16 t_no, int t_no_n, Uint res, int res_n, Uint ptr, int ptr_n, Uint size, int size_n, Uint32 ti,int ti_n){ byte *p = sp; Uint16 hdr; ASSERT(*p == tag); p++; hdr = GET_UI16(p); if (tag == ERTS_MT_CRR_REALLOC_BDY_TAG) check_ui(&hdr, &p, ct_no, ct_no_n, UI16_MSB_EHF_MSK, UI16_MSB_EHF_SZ); check_ui(&hdr, &p, t_no, t_no_n, UI16_MSB_EHF_MSK, UI16_MSB_EHF_SZ); check_ui(&hdr, &p, res, res_n, UI_MSB_EHF_MSK, UI_MSB_EHF_SZ); check_ui(&hdr, &p, ptr, ptr_n, UI_MSB_EHF_MSK, UI_MSB_EHF_SZ); check_ui(&hdr, &p, size, size_n, UI_MSB_EHF_MSK, UI_MSB_EHF_SZ); check_ui(&hdr, &p, ti, ti_n, UI32_MSB_EHF_MSK, UI32_MSB_EHF_SZ); ASSERT(hdr == 0); ASSERT(p == ep);}voidcheck_free_entry(byte *sp, byte *ep, byte tag, Uint16 ct_no, int ct_no_n, Uint16 t_no, int t_no_n, Uint ptr, int ptr_n, Uint32 ti,int ti_n){ byte *p = sp; Uint16 hdr; ASSERT(*p == tag); p++; hdr = GET_UI16(p); if (tag == ERTS_MT_CRR_FREE_BDY_TAG) check_ui(&hdr, &p, ct_no, ct_no_n, UI16_MSB_EHF_MSK, UI16_MSB_EHF_SZ); check_ui(&hdr, &p, t_no, t_no_n, UI16_MSB_EHF_MSK, UI16_MSB_EHF_SZ); check_ui(&hdr, &p, ptr, ptr_n, UI_MSB_EHF_MSK, UI_MSB_EHF_SZ); check_ui(&hdr, &p, ti, ti_n, UI32_MSB_EHF_MSK, UI32_MSB_EHF_SZ); ASSERT(hdr == 0); ASSERT(p == ep);}voidcheck_time_inc_entry(byte *sp, byte *ep, Uint32 secs, int secs_n, Uint32 usecs, int usecs_n){ byte *p = sp; Uint16 hdr; ASSERT(*p == ERTS_MT_TIME_INC_BDY_TAG); p++; hdr = GET_UI16(p); check_ui(&hdr, &p, secs, secs_n, UI32_MSB_EHF_MSK, UI32_MSB_EHF_SZ); check_ui(&hdr, &p, usecs, usecs_n, UI32_MSB_EHF_MSK, UI32_MSB_EHF_SZ); ASSERT(hdr == 0); ASSERT(p == ep);}#endif /* #ifdef DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -