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_ptrace.h"
12 : #include <elf.h>
13 : #include <sys/uio.h>
14 : #include <sys/ptrace.h>
15 : #include "scd_log.h"
16 :
17 : #define MAX_PTRACE_REG_NUM 64U
18 :
19 1 : TraStatus ScdPtraceAttach(int32_t tid)
20 : {
21 1 : if (ptrace(PTRACE_ATTACH, tid, NULL, NULL) != 0) {
22 0 : return TRACE_FAILURE;
23 : }
24 1 : return TRACE_SUCCESS;
25 : }
26 :
27 1 : void ScdPtraceDetach(int32_t tid)
28 : {
29 1 : (void)ptrace(PTRACE_DETACH, tid, NULL, NULL);
30 1 : }
31 :
32 0 : void ScdPtraceContinue(int32_t tid)
33 : {
34 0 : (void)ptrace(PTRACE_CONT, tid, NULL, NULL);
35 0 : }
36 :
37 0 : TraStatus ScdPtraceGetRegs(int32_t tid, uintptr_t *regArray, size_t regNum)
38 : {
39 : uintptr_t regs[MAX_PTRACE_REG_NUM]; //big enough for all architectures
40 0 : size_t regLen = 0;
41 :
42 : #ifdef PTRACE_GETREGS
43 : if(ptrace(PTRACE_GETREGS, tid, NULL, ®s) != 0) {
44 : return TRACE_FAILURE;
45 : }
46 : regLen = regNum;
47 : #else
48 : struct iovec iovec;
49 0 : iovec.iov_base = ®s;
50 0 : iovec.iov_len = sizeof(regs);
51 0 : if(ptrace(PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec) != 0) {
52 0 : return TRACE_FAILURE;
53 : }
54 0 : regLen = iovec.iov_len / sizeof(uintptr_t);
55 0 : regLen = (regLen > regNum) ? regNum : regLen;
56 : #endif
57 0 : (void)memcpy_s(regArray, sizeof(uintptr_t) * regNum, regs, sizeof(uintptr_t) * regLen);
58 0 : return TRACE_SUCCESS;
59 : }
|