LCOV - code coverage report
Current view: top level - atrace/utrace/stacktrace/stacktrace_dumper - scd_dl.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.7 % 57 54
Test Date: 2026-08-31 10:07:06 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "scd_dl.h"
      12              : #include "scd_elf.h"
      13              : #include "scd_log.h"
      14              : #include "adiag_utils.h"
      15              : 
      16           64 : STATIC TraStatus ScdDlMmap(ScdDl* dl, const char* dlName)
      17              : {
      18              :     // open file
      19           64 :     int32_t fd = ScdUtilOpen(dlName);
      20           64 :     SCD_CHK_EXPR_ACTION(fd < 0, return TRACE_FAILURE, "open %s failed.", dlName);
      21              : 
      22              :     // get file size
      23           44 :     struct stat st = {0};
      24           44 :     int32_t ret = fstat(fd, &st);
      25           44 :     if ((ret != 0) || (st.st_size == 0)) {
      26            0 :         SCD_DLOG_ERR("get file[%s] size failed, ret = %d, errno = %s.", dlName, ret, strerror(AdiagGetErrorCode()));
      27            0 :         (void)close(fd);
      28            0 :         return TRACE_FAILURE;
      29              :     }
      30           44 :     ScdMemoryInitLocal(&dl->memory);
      31              :     // mmap the file
      32           44 :     void* mapAddr = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
      33           44 :     if (mapAddr == MAP_FAILED) {
      34            2 :         SCD_DLOG_ERR("mmap file[%s] failed, errno = %s.", dlName, strerror(AdiagGetErrorCode()));
      35            2 :         (void)close(fd);
      36            2 :         return TRACE_FAILURE;
      37              :     }
      38           42 :     SCD_DLOG_INF("map so %s to %p, size %zu", dlName, dl->memory.data, st.st_size);
      39           42 :     dl->fd = fd;
      40           42 :     dl->memory.data = (uintptr_t)mapAddr;
      41           42 :     dl->memory.size = (size_t)st.st_size;
      42           42 :     return TRACE_SUCCESS;
      43              : }
      44              : 
      45         1183 : STATIC void ScdDlMunmap(ScdDl* dl)
      46              : {
      47         1183 :     if (dl->memory.data != 0) {
      48           42 :         (void)munmap((void*)dl->memory.data, dl->memory.size);
      49           42 :         dl->memory.data = 0;
      50           42 :         dl->memory.size = 0;
      51              :     }
      52              : 
      53         1183 :     if (dl->fd >= 0) {
      54           42 :         (void)close(dl->fd);
      55           42 :         dl->fd = -1;
      56              :     }
      57         1183 : }
      58              : 
      59              : /**
      60              :  * @brief       load dynamic library info
      61              :  * @param [in]  dl:        dynamic library info
      62              :  * @param [in]  pid:       process id
      63              :  * @param [in]  dlName:    dynamic library name
      64              :  * @return      TraStatus
      65              :  */
      66           74 : TraStatus ScdDlLoad(ScdDl* dl, int32_t pid, const char* dlName)
      67              : {
      68           74 :     SCD_CHK_PTR_ACTION(dl, return TRACE_FAILURE);
      69           74 :     if (dl->elfLoaded) {
      70           10 :         return TRACE_SUCCESS;
      71              :     }
      72              : 
      73           64 :     TraStatus ret = ScdDlMmap(dl, dlName);
      74           64 :     if (ret != TRACE_SUCCESS) {
      75           22 :         return TRACE_FAILURE;
      76              :     }
      77              : 
      78           42 :     ret = ScdElfLoad(&dl->elf, pid, &dl->memory);
      79           42 :     if (ret != TRACE_SUCCESS) {
      80           36 :         ScdDlMunmap(dl);
      81           36 :         return TRACE_FAILURE;
      82              :     }
      83            6 :     dl->elfLoaded = true;
      84            6 :     dl->pid = pid;
      85            6 :     SCD_RLOG_INF("load %s successfully.", dlName);
      86            6 :     return TRACE_SUCCESS;
      87              : }
      88              : 
      89              : /**
      90              :  * @brief       init dynamic library info
      91              :  * @param [in]  dl:        dynamic library info
      92              :  * @return      TraStatus
      93              :  */
      94         1165 : TraStatus ScdDlInit(ScdDl* dl)
      95              : {
      96         1165 :     dl->pid = 0;
      97         1165 :     dl->fd = -1;
      98         1165 :     dl->memory.data = 0;
      99         1165 :     dl->memory.size = 0;
     100         1165 :     dl->elfLoaded = false;
     101         1165 :     return ScdElfInit(&dl->elf);
     102              : }
     103              : 
     104              : /**
     105              :  * @brief       uninit dynamic library info
     106              :  * @param [in]  dl:        dynamic library info
     107              :  * @return      NA
     108              :  */
     109         1147 : void ScdDlUninit(ScdDl* dl)
     110              : {
     111         1147 :     if (dl->elfLoaded) {
     112            6 :         dl->elfLoaded = false;
     113            6 :         ScdElfUninit(&dl->elf);
     114              :     }
     115         1147 :     ScdDlMunmap(dl);
     116         1147 :     return;
     117              : }
        

Generated by: LCOV version 2.0-1