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 23 : TraStatus ScdPtraceAttach(int32_t tid)
20 : {
21 23 : if (ptrace(PTRACE_ATTACH, tid, NULL, NULL) != 0) {
22 0 : return TRACE_FAILURE;
23 : }
24 23 : return TRACE_SUCCESS;
25 : }
26 :
27 23 : void ScdPtraceDetach(int32_t tid) { (void)ptrace(PTRACE_DETACH, tid, NULL, NULL); }
28 :
29 4 : void ScdPtraceContinue(int32_t tid) { (void)ptrace(PTRACE_CONT, tid, NULL, NULL); }
30 :
31 8 : TraStatus ScdPtraceGetRegs(int32_t tid, uintptr_t* regArray, size_t regNum)
32 : {
33 : uintptr_t regs[MAX_PTRACE_REG_NUM]; // big enough for all architectures
34 8 : size_t regLen = 0;
35 :
36 : #ifdef PTRACE_GETREGS
37 : if (ptrace(PTRACE_GETREGS, tid, NULL, ®s) != 0) {
38 : return TRACE_FAILURE;
39 : }
40 : regLen = regNum;
41 : #else
42 : struct iovec iovec;
43 8 : iovec.iov_base = ®s;
44 8 : iovec.iov_len = sizeof(regs);
45 8 : if (ptrace(PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec) != 0) {
46 4 : return TRACE_FAILURE;
47 : }
48 4 : regLen = iovec.iov_len / sizeof(uintptr_t);
49 4 : regLen = (regLen > regNum) ? regNum : regLen;
50 : #endif
51 4 : (void)memcpy_s(regArray, sizeof(uintptr_t) * regNum, regs, sizeof(uintptr_t) * regLen);
52 4 : return TRACE_SUCCESS;
53 : }
|