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: 77.2 % 57 44
Test Date: 2026-07-28 10:53:44 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           48 : STATIC TraStatus ScdDlMmap(ScdDl *dl, const char *dlName)
      17              : {
      18              :     // open file
      19           48 :     int32_t fd = ScdUtilOpen(dlName);
      20           48 :     SCD_CHK_EXPR_ACTION(fd < 0, return TRACE_FAILURE, "open %s failed.", dlName);
      21              : 
      22              :     // get file size
      23           32 :     struct stat st = {0};
      24           32 :     int32_t ret = fstat(fd, &st);
      25           32 :     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           32 :     ScdMemoryInitLocal(&dl->memory);
      31              :     // mmap the file
      32           32 :     void *mapAddr = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
      33           32 :     if (mapAddr == MAP_FAILED) {
      34            0 :         SCD_DLOG_ERR("mmap file[%s] failed, errno = %s.", dlName, strerror(AdiagGetErrorCode()));
      35            0 :         (void)close(fd);
      36            0 :         return TRACE_FAILURE;
      37              :     }
      38           32 :     SCD_DLOG_INF("map so %s to %p, size %zu", dlName, dl->memory.data, st.st_size);
      39           32 :     dl->fd = fd;
      40           32 :     dl->memory.data = (uintptr_t)mapAddr;
      41           32 :     dl->memory.size = (size_t)st.st_size; 
      42           32 :     return TRACE_SUCCESS;
      43              : }
      44              : 
      45         1023 : STATIC void ScdDlMunmap(ScdDl *dl)
      46              : {
      47         1023 :     if (dl->memory.data != 0) {
      48           32 :         (void)munmap((void *)dl->memory.data, dl->memory.size);
      49           32 :         dl->memory.data = 0;
      50           32 :         dl->memory.size = 0;
      51              :     }
      52              :     
      53         1023 :     if (dl->fd >= 0) {
      54           32 :         (void)close(dl->fd);
      55           32 :         dl->fd = -1;
      56              :     }
      57         1023 : }
      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           48 : TraStatus ScdDlLoad(ScdDl *dl, int32_t pid, const char *dlName)
      67              : {
      68           48 :     SCD_CHK_PTR_ACTION(dl, return TRACE_FAILURE);
      69           48 :     if (dl->elfLoaded) {
      70            0 :         return TRACE_SUCCESS;
      71              :     }
      72              : 
      73           48 :     TraStatus ret = ScdDlMmap(dl, dlName);
      74           48 :     if (ret != TRACE_SUCCESS) {
      75           16 :         return TRACE_FAILURE;
      76              :     }
      77              : 
      78           32 :     ret = ScdElfLoad(&dl->elf, pid, &dl->memory);
      79           32 :     if (ret != TRACE_SUCCESS) {
      80           32 :         ScdDlMunmap(dl);
      81           32 :         return TRACE_FAILURE;
      82              :     }
      83            0 :     dl->elfLoaded = true;
      84            0 :     dl->pid = pid;
      85            0 :     SCD_RLOG_INF("load %s successfully.", dlName);
      86            0 :     return TRACE_SUCCESS;
      87              : }
      88              : 
      89              : /**
      90              :  * @brief       init dynamic library info
      91              :  * @param [in]  dl:        dynamic library info
      92              :  * @return      TraStatus
      93              :  */
      94         1007 : TraStatus ScdDlInit(ScdDl *dl)
      95              : {
      96         1007 :     dl->pid = 0;
      97         1007 :     dl->fd = -1;
      98         1007 :     dl->memory.data = 0;
      99         1007 :     dl->memory.size = 0;
     100         1007 :     dl->elfLoaded = false;
     101         1007 :     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          991 : void ScdDlUninit(ScdDl *dl)
     110              : {
     111          991 :     if (dl->elfLoaded) {
     112            0 :         dl->elfLoaded = false;
     113            0 :         ScdElfUninit(&dl->elf);
     114              :     }
     115          991 :     ScdDlMunmap(dl);
     116          991 :     return;
     117              : }
        

Generated by: LCOV version 2.0-1