From patchwork Mon Jan 31 16:12:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12730850 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4963C433EF for ; Mon, 31 Jan 2022 16:13:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379648AbiAaQNN (ORCPT ); Mon, 31 Jan 2022 11:13:13 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:43924 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349651AbiAaQNI (ORCPT ); Mon, 31 Jan 2022 11:13:08 -0500 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20VFx21n031412; Mon, 31 Jan 2022 16:12:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=XT6/J332vzrfsQjZeYLEA0jnrN1qvH4FqmwmPAJHpBM=; b=IZYT0Z5HiDcrQgTjb9W3J9D/x3pV71xOIUKkQ86YOSBpLUtbzLhmJPpgPXzC7CQz6IF7 kNEavJ9gbVZ40rOEeTvKOpugYzgA/UHtwFRE7w+1lv4ioWr+9I6RoAb8fxi0d6OPCctj +JmHd+VU7MTwBfzkxeuIh+g7SPNtH5/nxzrMOkzX97X9q8rtRjwoNHm1yyUpr+RhZQQh 8F89KE5Q8lO758RkiDOLh/wpRa9sW9bb6pvpLUwC6j3wRkR5Oos9kGZCrSkFI8pAIFoF tKfGKkTGXUTCdZG4hJaoUsCY5PIJSAtTstL7moz6vTIR15nc0UAlW6T4YAsmJ4avaAVj 6w== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3dxj9v86dh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:46 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20VGB6DG195237; Mon, 31 Jan 2022 16:12:45 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2172.outbound.protection.outlook.com [104.47.56.172]) by aserp3020.oracle.com with ESMTP id 3dvwd4j528-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZdQ5vMOLUJNAszCgYzk+av+iGthYmMLLesmIcoRYo4ITenAvrG/LAR6sRXy2bNILh20yYN3hgJyhsa77BRSyIl+w+4qpS4Q8aNWQIwoWagG+y0uz3+raZtcj5+CpJKlT91Ci5SkKvqc7ntgrWUTGkeRxaVicdWGYVBRkPTbc382yYghCP10ZI3Dyrp2RDBgBnDUaYPnCHZ+n/Fg1QX1QCGjtgxwVcssPHFueLXeeYjiBvMbe5equacZ9sk3UeKp+HOCWT5pzL37VFUzVEAUUE/oRQGa1tMH462wdEMrYiIZl+1KqcPxuSNluqL9mpt3RexfRJrFjXZEa/aQn6TCrAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=XT6/J332vzrfsQjZeYLEA0jnrN1qvH4FqmwmPAJHpBM=; b=BSXgkZSr0nHIrxLxX/pDvN+OOs7vSgP3FJ6qwdApRip8pbAcrL0IRaqnF/x5GH2/p1gmcXxKIp8fxyr02DkNbRmLSscGzxF14txXXsqMIy/n3Z5gNFvHXGuvZfsi+SWNXFR/FR8Pc3jl9+HQTb1a9E1shBGk7QiJ/P15V4qIi077YvDmfTMuNqWM9Weh0VsH2pCau9VG4FyTvMjHoLExkWSYyqUqj+F3kQ3oNxg2yeyKCL3trtoPFFyUQ7LC0cIsS9Y+4V8AIirIrhKnuaIKseH/chPkhqRROKA0JFP5c24wSPTi5M3J6y2ranPZkincvKp+oxVhFWMkOl6u32tywA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XT6/J332vzrfsQjZeYLEA0jnrN1qvH4FqmwmPAJHpBM=; b=oXl7FfhKenXlOOOz8t4KdCDVGjUqZEcTjQZcVYRNLIUSI6pf4ENTSiKpnpmiRyncYZ0E7ZEJ+mtX0QQamkldukSlnyci3wfJw1aESyPOlEFym7E63OiwaOIhlbQYzKVxog7gFb5ARdKeTDYHdavyN4QtQA40ADXEB2KSPcFVAd4= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by BN7PR10MB2516.namprd10.prod.outlook.com (2603:10b6:406:c1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.17; Mon, 31 Jan 2022 16:12:42 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6%6]) with mapi id 15.20.4930.022; Mon, 31 Jan 2022 16:12:42 +0000 From: Alan Maguire To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, jolsa@kernel.org, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v3 bpf-next 1/4] libbpf: support function name-based attach uprobes Date: Mon, 31 Jan 2022 16:12:31 +0000 Message-Id: <1643645554-28723-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> References: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM0PR06CA0124.eurprd06.prod.outlook.com (2603:10a6:208:ab::29) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d9423111-9fa9-4331-aa51-08d9e4d48220 X-MS-TrafficTypeDiagnostic: BN7PR10MB2516:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3276; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 59ua2A8zHrUm2HGGU7PPq9dvckK6EsGlRKYKSTDQK21LHRZRa6x3u9SVJSMdDbEQV7OZn27PXoO31X+g8CKCwhWYTd3R/SdvL8zQdNZQ7jdRbIyRz3hZY4OEPJ8SvjhPUbIzUOUduKT85GdHt8lUiFWA48rXR3jCNeLWQnAy4WVeqhcU1CikpHDd6vrlcNI1As716LGoMkwmCGjsBmXyj0penrxkDm2LGVUWb+lHNAKDzhmWDy90ldTc86rHUJZac/h3Uc0dr41gZqne92BRvjN5pP+mIsyj7JUvV9l0ddLbhXmlHN18RD6v/2CB99Lz0rTLMk/rX/4GLlsl1mvGlMtME/SHeM0WLX59stfSzUm4HvYbHFjf6w8piFhHGdaKiaGo7v/nqmb0E+W/WRyHuRZgZBksNNS9JyyzW5asFm+ADyE9cdwgjAMDbpWOKU3v+ZndRLrdynRhOQlkCaBpDp/q63Ym1XPXk84TB0E5iYxM43SIiWwOfyj1oDg678gUnm5aaY4KVKpjxJiDSovjEszENb7hrMQe3hrDMFcHw2BewEnoiPsh/8oZNt4Y6dg6NDYJHKbhFBI5I8C/oYkahPYv6H4sdE6SWhDyMb6YLj6oAYp0dkgWA+PH0hCBMBvnV1ptGubg/OUg7sYfCts8bnPUTwqraY1KM3BpO7aY6JQDv6P2tiRA/8Wj1F9G1aVA3e3uAKKq5g7lVxwiNVG6G7MsE+VCZLtmfS/zgPe/fyorWOFkUYYerZqsA7jxvqKZa6dzSa0OKzVf6s6+tDcmdlMAZArCeBbwVZId7cy9DDk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(26005)(186003)(38350700002)(6486002)(38100700002)(966005)(5660300002)(6666004)(107886003)(2616005)(52116002)(86362001)(508600001)(6512007)(6506007)(316002)(83380400001)(44832011)(8936002)(4326008)(8676002)(66946007)(2906002)(66556008)(66476007)(7416002)(36756003)(30864003)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Z869li6at8jHNxQtedzwYdlkDVMmrqZWDUkXCP8AvGwLq0HTa2nOGJe+JwzqawjK0CFCash8YfSBnrfUl1CDIxGWbAMl6pJZTIfp86IxTHUt1zMLZfA4QxNzGMsxkv4ImHt8aeqAjKmHtetqciQXkd+Wp8AuPQiS7AhsuYbe1LtU3dLlemK1048FiYqmms+Ju5Q8XdqD+U5/yC5G4wruiHTBC3SkvPW1rH8WbHc3E0BQugLV9T6S7umUCHNQCoa9DZXiIppgOWyEXVd2jIuNm/6IXohdvCXcfVjUcKAjNNANbanzPW5wbKjYRXuf3AeoZuO3xRlxHnpWQChEB/418qyO0LcdhrEMy2hiSl0kgB0r7Dxun2QHGt6ylEGVNeuvHV49XAyqRrOVoPqOUUhtAmStxnzjiE1KsgipWcV3DorfelUn0Q18t77uTIxW8IC8kRaG9jaAuPUrrDkJTOazXFFhAHMCGaAchkO/jg5fXLtiOKx/RhYnytYmdTnzHd/Gqm5vbUJe0eUjpR9n/JFjblfh+UV5ffp+wLVJ92aVffe5/eu2qbyG9Mz8OgmqBQF352eeKdU89csjnH5U1IxGWeN2tCjt1ZLb77CgcBXhznIBXZh9R8CDuHNqtYGGucKJqy0fYYMd8BzpZCqKYu/lSZVPoeX+K92i3qcs0Okx6859O+ra99ei5J95+efi+j8VIc5sVVOSPK0cGYvzYsFolQyNk7C5Q7GWpsSnwuFNfPdzS8vuGNWmtw4JEFWC4liyybSXaJzVWB4ulXMC2pUI19eUqTdz82jSqgmmxRGUTCcKbnLik897ZV4PlqNYW9pJpcy5zu1icEsLKe87OjDnwDKdo8AD8eStsIppjucpPQEqWetfMb00vMqzqMR1CsnzyCgaLk1/PrEHAxJHOwLYXriiq6awZoAb6dPLFrlMpNrAI9JmhyiJ1jtSneFn3JZshSMJ4Zt25roLGHqQfwR5Tbb9NZNLKSmr3O4LMYDqUb7zudww0jmq7GxEIZhrTRS1ecmBZ6m1dTAVu5AYgxUBe3poDXkZ27i2g0/E+LUshbqH915JvlmXbL3QPG0P4cLWsnCEoejeszoeutdGmFXqTu5ma/5jflcaObpChVvDy/4zaJW8Pdzk7ScyOCC5Ttzsz6h3OFriw/yQNuAL3zIXi46WhK8sgdBRhttKyWjCSWrdhpZBGpzJ3LUbqmkUuyOGqF52tlS8l/oUdRqui5brNn/CvprU5MSGZqWz5fo5T9j25+XaqM9LEwJm14COuo7Yq1GDAeodI3dvItAalYOqQhnCAXDhbierFcfxaM92Pk2RfHg3n5+GkxpKOnfZFehlf17xscH+rg1mdhwYcgIgmT8s7sAr5NhZPSJYTABNnJwErilZzcpuhedy++mmVnhwdYIfTPIAZUYyhoOTLJFcVxqTFWvyOMFmUxS/1G19WYUC7mZrvB+31u6IGbKbExpBhM2xy2G5J2+rChZ2TFBwctI13/6qEZRbwAWFuZSHVTL4BnqGKdFFEwuriRMR1mHMopVotQIRIZkna/kH5aS4xjl0GKSyXoZxkFFsvHQCRxNe/d8AvHHSAF+Y/fsUx57r+YWIKL1U1b+dAIlZQoQKcqGJKoNlAmDUNJjhAjzySMc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d9423111-9fa9-4331-aa51-08d9e4d48220 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jan 2022 16:12:41.8107 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: SlWWvEAaZYMres5nBrNq8/UykOWqJrYlrFBEoAAvq3ZdM/cxUNExz6kXo7dIP53lLRTaiR843qLh1vG0QjrbqA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7PR10MB2516 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10244 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 suspectscore=0 malwarescore=0 bulkscore=0 mlxscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2201310106 X-Proofpoint-ORIG-GUID: dqlqxv_dh8EEl0G0VETxlYTuY_Fj5e7W X-Proofpoint-GUID: dqlqxv_dh8EEl0G0VETxlYTuY_Fj5e7W Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net kprobe attach is name-based, using lookups of kallsyms to translate a function name to an address. Currently uprobe attach is done via an offset value as described in [1]. Extend uprobe opts for attach to include a function name which can then be converted into a uprobe-friendly offset. The calcualation is done in several steps: 1. First, determine the symbol address using libelf; this gives us the offset as reported by objdump; then, in the case of local functions 2. If the function is a shared library function - and the binary provided is a shared library - no further work is required; the address found is the required address 3. If the function is a shared library function in a program (as opposed to a shared library), the Procedure Linking Table (PLT) table address is found (it is indexed via the dynamic symbol table index). This allows us to instrument a call to a shared library function locally in the calling binary, reducing overhead versus having a breakpoint in global lib. 4. Finally, if the function is local, subtract the base address associated with the object, retrieved from ELF program headers. The resultant value is then added to the func_offset value passed in to specify the uprobe attach address. So specifying a func_offset of 0 along with a function name "printf" will attach to printf entry. The modes of operation supported are then 1. to attach to a local function in a binary; function "foo1" in "/usr/bin/foo" 2. to attach to a shared library function in a binary; function "malloc" in "/usr/bin/foo" 3. to attach to a shared library function in a shared library - function "malloc" in libc. [1] https://www.kernel.org/doc/html/latest/trace/uprobetracer.html Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 250 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 10 +- 2 files changed, 259 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 4ce94f4..eb95629 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10203,6 +10203,241 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, return pfd; } +/* uprobes deal in relative offsets; subtract the base address associated with + * the mapped binary. See Documentation/trace/uprobetracer.rst for more + * details. + */ +static long elf_find_relative_offset(Elf *elf, long addr) +{ + size_t n; + int i; + + if (elf_getphdrnum(elf, &n)) { + pr_warn("elf: failed to find program headers: %s\n", + elf_errmsg(-1)); + return -ENOENT; + } + + for (i = 0; i < n; i++) { + int seg_start, seg_end, seg_offset; + GElf_Phdr phdr; + + if (!gelf_getphdr(elf, i, &phdr)) { + pr_warn("elf: failed to get program header %d: %s\n", + i, elf_errmsg(-1)); + return -ENOENT; + } + if (phdr.p_type != PT_LOAD || !(phdr.p_flags & PF_X)) + continue; + + seg_start = phdr.p_vaddr; + seg_end = seg_start + phdr.p_memsz; + seg_offset = phdr.p_offset; + if (addr >= seg_start && addr < seg_end) + return addr - seg_start + seg_offset; + } + pr_warn("elf: failed to find prog header containing 0x%lx\n", addr); + return -ENOENT; +} + +/* Return next ELF section of sh_type after scn, or first of that type + * if scn is NULL. + */ +static Elf_Scn *elf_find_next_scn_by_type(Elf *elf, int sh_type, Elf_Scn *scn) +{ + while ((scn = elf_nextscn(elf, scn)) != NULL) { + GElf_Shdr sh; + + if (!gelf_getshdr(scn, &sh)) + continue; + if (sh.sh_type == sh_type) + break; + } + return scn; +} + +/* For Position-Independent Code-based libraries, a table of trampolines + * (Procedure Linking Table) is used to support resolution of symbol + * names at linking time. The goal here is to find the offset associated + * with the jump to the actual library function. If we can instrument that + * locally in the specific binary (rather than instrumenting glibc say), + * overheads are greatly reduced. + * + * The method used is to find the .plt section and determine the offset + * of the relevant entry (given by the base address plus the index + * of the function multiplied by the size of a .plt entry). + */ +static ssize_t elf_find_plt_offset(Elf *elf, size_t ndx) +{ + Elf_Scn *scn = NULL; + size_t shstrndx; + + if (elf_getshdrstrndx(elf, &shstrndx)) { + pr_debug("elf: failed to get section names section index: %s\n", + elf_errmsg(-1)); + return -LIBBPF_ERRNO__FORMAT; + } + while ((scn = elf_find_next_scn_by_type(elf, SHT_PROGBITS, scn))) { + long plt_entry_sz, plt_base; + const char *name; + GElf_Shdr sh; + + if (!gelf_getshdr(scn, &sh)) + continue; + name = elf_strptr(elf, shstrndx, sh.sh_name); + if (!name || strcmp(name, ".plt") != 0) + continue; + plt_base = sh.sh_addr; + plt_entry_sz = sh.sh_entsize; + return plt_base + (ndx * plt_entry_sz); + } + pr_debug("elf: no .plt section found\n"); + return -LIBBPF_ERRNO__FORMAT; +} + +/* Find offset of function name in object specified by path. "name" matches + * symbol name or name@@LIB for library functions. + */ +static long elf_find_func_offset(const char *binary_path, const char *name) +{ + int fd, i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB }; + bool is_shared_lib, is_name_qualified; + size_t name_len, sym_ndx = -1; + char errmsg[STRERR_BUFSIZE]; + long ret = -ENOENT; + GElf_Ehdr ehdr; + Elf *elf; + + fd = open(binary_path, O_RDONLY | O_CLOEXEC); + if (fd < 0) { + ret = -errno; + pr_warn("failed to open %s: %s\n", binary_path, + libbpf_strerror_r(ret, errmsg, sizeof(errmsg))); + return ret; + } + elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (!elf) { + pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1)); + close(fd); + return -LIBBPF_ERRNO__FORMAT; + } + if (!gelf_getehdr(elf, &ehdr)) { + pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1)); + ret = -LIBBPF_ERRNO__FORMAT; + goto out; + } + /* for shared lib case, we do not need to calculate relative offset */ + is_shared_lib = ehdr.e_type == ET_DYN; + + name_len = strlen(name); + /* Does name specify "@@LIB"? */ + is_name_qualified = strstr(name, "@@") != NULL; + + /* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if + * the symbol is found in SHY_DYNSYM, the index in that table tells us which index + * to use in the Procedure Linking Table to instrument calls to the shared library + * function, but locally in the binary rather than in the shared library ifself. + * If a binary is stripped, it may also only have SHT_DYNSYM, and a fully-statically + * linked binary may not have SHT_DYMSYM, so absence of a section should not be + * reported as a warning/error. + */ + for (i = 0; i < ARRAY_SIZE(sh_types); i++) { + size_t strtabidx, ndx, nr_syms; + Elf_Data *symbols = NULL; + Elf_Scn *scn = NULL; + int last_bind = -1; + const char *sname; + GElf_Shdr sh; + + scn = elf_find_next_scn_by_type(elf, sh_types[i], NULL); + if (!scn) { + pr_debug("elf: failed to find symbol table ELF sections in %s\n", + binary_path); + continue; + } + if (!gelf_getshdr(scn, &sh)) + continue; + strtabidx = sh.sh_link; + symbols = elf_getdata(scn, 0); + if (!symbols) { + pr_warn("elf: failed to get symbols for symtab section in %s: %s\n", + binary_path, elf_errmsg(-1)); + ret = -LIBBPF_ERRNO__FORMAT; + goto out; + } + nr_syms = symbols->d_size / sh.sh_entsize; + + for (ndx = 0; ndx < nr_syms; ndx++) { + int curr_bind; + GElf_Sym sym; + + if (!gelf_getsym(symbols, ndx, &sym)) + continue; + if (GELF_ST_TYPE(sym.st_info) != STT_FUNC) + continue; + + sname = elf_strptr(elf, strtabidx, sym.st_name); + if (!sname) + continue; + curr_bind = GELF_ST_BIND(sym.st_info); + + /* User can specify func, func@@LIB or func@@LIB_VERSION. */ + if (strncmp(sname, name, name_len) != 0) + continue; + /* ...but we don't want a search for "foo" to match 'foo2" also, so any + * additional characters in sname should be of the form "@@LIB". + */ + if (!is_name_qualified && strlen(sname) > name_len && + sname[name_len] != '@') + continue; + + if (ret >= 0 && last_bind != -1) { + /* handle multiple matches */ + if (last_bind != STB_WEAK && curr_bind != STB_WEAK) { + /* Only accept one non-weak bind. */ + pr_warn("elf: ambiguous match for '%s': %s\n", + sname, name); + ret = -LIBBPF_ERRNO__FORMAT; + goto out; + } else if (curr_bind == STB_WEAK) { + /* already have a non-weak bind, and + * this is a weak bind, so ignore. + */ + continue; + } + } + ret = sym.st_value; + last_bind = curr_bind; + sym_ndx = ndx; + } + /* The index of the entry in SHT_DYNSYM gives us the index into the PLT */ + if (ret == 0 && sh_types[i] == SHT_DYNSYM) + ret = elf_find_plt_offset(elf, sym_ndx); + /* For binaries that are not shared libraries, we need relative offset */ + if (ret > 0 && !is_shared_lib) + ret = elf_find_relative_offset(elf, ret); + if (ret > 0) + break; + } + + if (ret > 0) { + pr_debug("elf: symbol address match for '%s': 0x%lx\n", name, ret); + } else { + if (ret == 0) { + pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path, + is_shared_lib ? "should not be 0 in a shared library" : + "try using shared library path instead"); + ret = -ENOENT; + } else { + pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path); + } + } +out: + elf_end(elf); + close(fd); + return ret; +} + LIBBPF_API struct bpf_link * bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, const char *binary_path, size_t func_offset, @@ -10214,6 +10449,7 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, size_t ref_ctr_off; int pfd, err; bool retprobe, legacy; + const char *func_name; if (!OPTS_VALID(opts, bpf_uprobe_opts)) return libbpf_err_ptr(-EINVAL); @@ -10222,6 +10458,20 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0); pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); + func_name = OPTS_GET(opts, func_name, NULL); + if (func_name) { + long sym_off; + + if (!binary_path) { + pr_warn("name-based attach requires binary_path\n"); + return libbpf_err_ptr(-EINVAL); + } + sym_off = elf_find_func_offset(binary_path, func_name); + if (sym_off < 0) + return libbpf_err_ptr(sym_off); + func_offset += (size_t)sym_off; + } + legacy = determine_uprobe_perf_type() < 0; if (!legacy) { pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path, diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 5762b57..1de3eeb 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -433,9 +433,17 @@ struct bpf_uprobe_opts { __u64 bpf_cookie; /* uprobe is return probe, invoked at function return time */ bool retprobe; + /* name of function name or function@@LIBRARY. Partial matches + * work for library functions, such as printf, printf@@GLIBC. + * To specify function entry, func_offset argument should be 0 and + * func_name should specify function to trace. To trace an offset + * within the function, specify func_name and use func_offset + * argument to specify argument _within_ the function. + */ + const char *func_name; size_t :0; }; -#define bpf_uprobe_opts__last_field retprobe +#define bpf_uprobe_opts__last_field func_name /** * @brief **bpf_program__attach_uprobe()** attaches a BPF program From patchwork Mon Jan 31 16:12:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12730849 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27903C43219 for ; Mon, 31 Jan 2022 16:13:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376476AbiAaQNL (ORCPT ); Mon, 31 Jan 2022 11:13:11 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:42804 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244114AbiAaQNH (ORCPT ); Mon, 31 Jan 2022 11:13:07 -0500 Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20VFwpWR006233; Mon, 31 Jan 2022 16:12:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=W3rPGqEUBOFPO/4b2CVaML0yj8othIL08IFUlfNKW78=; b=Qrl0BNK83qbB1KecFg5WUj0HaKpIlAVA8kp4Z96xC1BppXei7SxQMEl+HX4TwLSkwZ93 2KaingN6QtmJcB3NwYxZhDmRyrL0IUnQ9hNix4DJM7SMyNe2dQHi6SkRstk4i0/E+y97 w6g66WHeo3DkrbNiIhYqv/hjJnEAJuKpSQu6o1W24g8JK2p1k+G/v80pzBSYNUVaG2LQ MYH7e4lozqg6+NiTSbJeD+dgi6b1DOIpCokoutUGn+Ylu05G4mN45OM/UCgamU25Aul0 fHGNtdKe+iTCYSI5P2Lu09UAhKNOMo8lIkkWNTlCAQDDGBxYOU8Ukq0AN4f0VYDZf5DP 2g== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3dxj9w861c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:47 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20VGB6DH195237; Mon, 31 Jan 2022 16:12:46 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2172.outbound.protection.outlook.com [104.47.56.172]) by aserp3020.oracle.com with ESMTP id 3dvwd4j528-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H3TB+1hHALQ5vpFfwHeW7oQ+JDUmv0Dh4T63FNGOmvT+1vJGvAapC0Mwykf711W5gLwj1Gyynj6T5OjcIweTeL+FM2fwxK2OA8IDlO0GW8ZbtCI59bmGgvJOguOi0a3syDbXal5feUKSuSUXMJlKkAtVEBP+KAYIfIZu3hZB13UXzOnzxHJHrlKw9WLsUBEXMKNY0U8BfufYweD92eX1ouljDXRTY14GUZvNjviNYtlo60dleLrUaBj76jhFUVx91/1kXiuSUxaXVgzIevf4br6UDS4ABH/SMmWODrqACyrhNoqiiuOQM3Q2iUWI/FZj85oZ5JfJY/4Zwf/CWP2Ylw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=W3rPGqEUBOFPO/4b2CVaML0yj8othIL08IFUlfNKW78=; b=Hl8wjtXcddO/FEhYwoc9ao1AZWVKCF+8oYF3yf4x/TLWEYLT7ISDflj1ULQvUN2aJ2+C6Cd5hwWgb4XL08Yq3w6W3Iit7w6yB9wJ+WaKCCRfzlas3ZUOpO0SfKe7eckoh/YNWFbwEfDKKLjW+klyJtlxlgRTpv66qzvkngOZ8JfnZ4cVC7LqX7OcF/XtNm0V0jN2z91mbl5o2p292sM7z9DS1rJODkCn6FclpcQGg+/TuJnXlIXkXhgoeC7OZgEDLZ3vMcKJnkQ0GJpty2n3G89w3KpHtqjiTr+dBYS1JMk+7TUN1IOiZb91SYFFFu5u7Pc28vAqCijBl+4ssK1idg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=W3rPGqEUBOFPO/4b2CVaML0yj8othIL08IFUlfNKW78=; b=XvVXfXHn1PEhAxvPj6zxmfz4TjuXd3i1kRg9eJU66aq5S1cd56e77JL4LIgojnFAvTAl5f8y73UaCrsx95WlYNjZWQEzOyVv9bLyaJmCIKvZ0U5F3GTWtZep89jCER2rTmhp4xea+o60q8pd5XdPJ5fBYuWMyYyZLWX93b1UQ2I= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by BN7PR10MB2516.namprd10.prod.outlook.com (2603:10b6:406:c1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.17; Mon, 31 Jan 2022 16:12:43 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6%6]) with mapi id 15.20.4930.022; Mon, 31 Jan 2022 16:12:43 +0000 From: Alan Maguire To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, jolsa@kernel.org, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v3 bpf-next 2/4] libbpf: add auto-attach for uprobes based on section name Date: Mon, 31 Jan 2022 16:12:32 +0000 Message-Id: <1643645554-28723-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> References: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM0PR06CA0124.eurprd06.prod.outlook.com (2603:10a6:208:ab::29) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8f7bd759-6379-421f-87a4-08d9e4d48343 X-MS-TrafficTypeDiagnostic: BN7PR10MB2516:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1824; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Lh6INAFHuhPoqYK4uDdZ47Jk3yRzqZSHGYL59I185F7ME3Hec7jUxJGvxfrQhkq/WbKmylJZd5TFAMoYT4cNf+yDFQSZdNodZJxADIl6Jn749Bb0VXgtZzTfA4lHIAx8WFRhaN7OXz3ISKRj7tr4ayi0QRZ74vjAK7IvWvSvWNwYn/hC0EjVvOKl2+ibzM1pShPETtZ/p8/YrOS257ImImtNiIRNC9ebYQBE+wynaEeSdmJG33yyEf17BmD3aPI1IFKNYD4iQTPFX8Lb7Devq9ZsXvxlh42s3ohEmSMCSZtYSKJYlGXGUSpF6SbWP97YLH+Qn1u61Yas0jWNn0u8CU1mQpbW4AJ/dGZEFaiRpnMKgzWs6Ys/pXhRFu0cMSVh4e/gTDnlmeD7EVorzoTdPbgRX5bPn45yrZ5rjpWKfqICthkLibcrO6xdyhdupAMtmSJc0i3EbeNlFnlTOOfGvRp6nRDrTpJjU8g8qIrpXbW8oxSXXk2FfMinIb7TR8jTuDQiE72fVNTcIsevqOJUzXxVmuNy7wmsseDRYnuTIDovlwCa7qgpCMwQq6c+YtxQ5KAvxYWexy1XmucVOKV1ThgurlBA1ic/XZaIUUjwyQawfM2VdHTXD+eTlJfynryzytoOxOGKVU75i+EnEQV9RwFznlgJoE2VnGoxhcA/AJcbVwYeZ2vElGmAnrQctdbXn7gqQE8xA++8H9sg6uC+YQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(26005)(186003)(38350700002)(6486002)(38100700002)(5660300002)(6666004)(107886003)(2616005)(52116002)(86362001)(508600001)(6512007)(6506007)(316002)(83380400001)(44832011)(8936002)(4326008)(8676002)(66946007)(2906002)(66556008)(66476007)(7416002)(36756003)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: juahTHQV+ICSLy1GqlhxFtlCktqfKEr6kh1gFjy+3O2zeiDuB8aAe8//VqyafW2sjQjVEDEXMbYolG35GNS29kAQLLUSmQZW+bOVVN45kZs3mRKs7WDlRhfqtaFyofMt9Q7Dyxkr48tb/hGvzcNcNRghgOxm8gx+civ4XjdvZ3GdMvL84qRRbpsY5queF8FZiDiQxreqyXXQ4Q/spHJctfJo//1dHqjQxUv6EnR+/TV+l+U7IfefnwZM6eAJ3RcbGXfFSki/CCdFPzI39taaa8s4wv9s8fNjrRVAEg0jKveOLg702WoDFZ7W0i47zxhI5z+9aLRDjFyp6kP6rYpL3goKXv1JSr6SmIJRuuU/jClS+i0DGB5hjD6SdmXf8Wwq5sYG1AD/RH3HdBH3s6FU+ojKUudnqDlC7fTWAjJhcr+PTtu/kw+tZCcvxIEmKc7t7MyiZUHUmA7iJ+WfoLSPIoIkXYFfZXqO+dZ68INe9Yhr+hgVWmgnuuU7L/MVRHultuluZov/wW1KBz/UTlT4uZsU9dBxjEkEQ33lKV1ZRo3jjjjIriMnO/uxSQdJes+IrUSeFHc+1z3ilr7Y81ReyxvXt7TXa1zG2TZxho0SduVokcvv2G2anz7rAUk/a/VbDJ4OpZNWiZRRRYsDWm7xPo62+VQiPIcaq1oS5MOc9npmEp1WVBkhU0hPLBCf9t+XiZsBXictmCFS8hELO13Uxe4RKFVXF8VwkA9ytcLnyIoQHvRcgs/F5Ba1Ez4fhiucuaH+Cexcs0CuRHnBL5CnT4T6iuGtMauIiQofCqzTX8lBpcC20UeQWAU+u4mWFyLv+G0gc7uDd8HMxgyQjwLrEIXBzKVlo+MKZvGsk6U1K+3gIR/AP35kGLNj8QfxtvKddFucTOnZYVRmEof2QNh0JVROLDEAwIFQjRjZcsawf4zl9LOOa7d2P9MpYKxa6qFQEb/6RZcO6QItd1i5dtcm9VElpVaqYY/2Ed3dCBm3QlrtqXbd18V16TMwfg4+k6KDU8mEKcYZC+lk12KjtsXm8EYjrL+Dl82HIvwZ7LKF8huH8EGzw4hGeFyn4Rg4u/7mpkyoJVQeaWfWo1sQRtolRdgXSE6KpfrqrhACPI2QhKEX3zWXbDvpzBoKa11Tg0Rq0U4Pi5aGw6xfkrLtBb95nhCNEN0yzljRPLMNuCP5PsduNduzoywuwFR2+0dQEgmaSMYuNbcAbNJzi03+GeE0v6/liHufBEDb4cJKVC/ViW8VEGr5SZImb8xeCBOaCbBxRdwQLodmKU5Ese0/47dbEY1qSZnKvBmevCWqhLPBg0vBt+e6E2hrdhafOxbvhuq9FruhTDq23y/U/KKG2fUsedXiyJdTIo/M1e++erw+m8kVMrjlI0jyd9W1DDvAEhOEw3taq+5jOzntqKop7GCHdMPJdC9sV+Dew4FizGi+pJZtaCM1cFAU2Ssv7Jid65Y2xwNq7Mh2s9Qmtw1fd5FcI4t9BVcykJ1v4LZzzKFWT/ReZbNRlhfXt/brZS0m+0yvcdI9Ftd9KD0sdQNETUw2/RVQyvm7QhvtN4u+u1lyPdaPhTHyFDK5hwO5WiZRn1h0/aJKuPY6Znw/NH2CaZ7tH8wjQyZSpDXtJ4H3OepK4+g= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8f7bd759-6379-421f-87a4-08d9e4d48343 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jan 2022 16:12:43.8419 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 1NcQ87m2usxRBP5e+cRxIH11Ni+MK0VEGZLbE9OYSLil3LvfXz9npRGqUbV55ZuJa6xg1Zf4hNJTR5YvnqWNUw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7PR10MB2516 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10244 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 suspectscore=0 malwarescore=0 bulkscore=0 mlxscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2201310106 X-Proofpoint-ORIG-GUID: 9M76vjGPumXI1VWhQ2smERGdLYjWdo9l X-Proofpoint-GUID: 9M76vjGPumXI1VWhQ2smERGdLYjWdo9l Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Now that u[ret]probes can use name-based specification, it makes sense to add support for auto-attach based on SEC() definition. The format proposed is SEC("u[ret]probe//path/to/prog:[raw_offset|[function_name[+offset]]") For example, to trace malloc() in libc: SEC("uprobe//usr/lib64/libc.so.6:malloc") Auto-attach is done for all tasks (pid -1). Note that there is a backwards-compatibility issue here. Consider a BPF object consisting of a set of BPF programs, including a uprobe program. Because uprobes did not previously support auto-attach, it's possible that because the uprobe section name is not in auto-attachable form, overall BPF skeleton attach would now fail due to the failure of the uprobe program to auto-attach. So we need to handle the case of auto-attach failure where the form of the section name is not suitable for auto-attach without a complete attach failure. On surveying the code, bpf_program__attach() already returns -ESRCH in cases where no auto-attach function is supplied, so for consistency with that - and because that return value is less likely to collide with actual attach failures than -EOPNOTSUPP - it is used as the attach function return value signalling auto-attach is not possible. Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index eb95629..e2b4415 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -8581,6 +8581,7 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log } static struct bpf_link *attach_kprobe(const struct bpf_program *prog, long cookie); +static struct bpf_link *attach_uprobe(const struct bpf_program *prog, long cookie); static struct bpf_link *attach_tp(const struct bpf_program *prog, long cookie); static struct bpf_link *attach_raw_tp(const struct bpf_program *prog, long cookie); static struct bpf_link *attach_trace(const struct bpf_program *prog, long cookie); @@ -8592,9 +8593,9 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE | SEC_SLOPPY_PFX), SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE | SEC_SLOPPY_PFX), SEC_DEF("kprobe/", KPROBE, 0, SEC_NONE, attach_kprobe), - SEC_DEF("uprobe/", KPROBE, 0, SEC_NONE), + SEC_DEF("uprobe/", KPROBE, 0, SEC_NONE, attach_uprobe), SEC_DEF("kretprobe/", KPROBE, 0, SEC_NONE, attach_kprobe), - SEC_DEF("uretprobe/", KPROBE, 0, SEC_NONE), + SEC_DEF("uretprobe/", KPROBE, 0, SEC_NONE, attach_uprobe), SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE | SEC_SLOPPY_PFX), SEC_DEF("action", SCHED_ACT, 0, SEC_NONE | SEC_SLOPPY_PFX), @@ -10525,6 +10526,64 @@ static long elf_find_func_offset(const char *binary_path, const char *name) } +/* Format of u[ret]probe section definition supporting auto-attach: + * u[ret]probe//path/to/prog:function[+offset] + * + * Many uprobe programs do not avail of auto-attach, so we need to handle the + * case where the format is uprobe/myfunc by returning NULL rather than an + * error. + */ +static struct bpf_link *attach_uprobe(const struct bpf_program *prog, long cookie) +{ + DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); + char *func_name, binary_path[512]; + unsigned int raw_offset; + char *func, *probe_name; + struct bpf_link *link; + size_t offset = 0; + int n, err; + + opts.retprobe = str_has_pfx(prog->sec_name, "uretprobe/"); + if (opts.retprobe) + probe_name = prog->sec_name + sizeof("uretprobe/") - 1; + else + probe_name = prog->sec_name + sizeof("uprobe/") - 1; + + snprintf(binary_path, sizeof(binary_path), "%s", probe_name); + /* ':' should be prior to function+offset */ + func_name = strrchr(binary_path, ':'); + if (!func_name) { + pr_debug("section '%s' is old-style u[ret]probe/function, cannot auto-attach\n", + prog->sec_name); + return libbpf_err_ptr(-ESRCH); + } + func_name[0] = '\0'; + func_name++; + n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); + if (n < 1) { + err = -EINVAL; + pr_warn("uprobe name is invalid: %s\n", func_name); + return libbpf_err_ptr(err); + } + /* Is func a raw address? */ + if (n == 1 && sscanf(func, "%x", &raw_offset) == 1) { + free(func); + func = NULL; + offset = (size_t)raw_offset; + } + if (opts.retprobe && offset != 0) { + free(func); + err = -EINVAL; + pr_warn("uretprobes do not support offset specification\n"); + return libbpf_err_ptr(err); + } + opts.func_name = func; + + link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); + free(func); + return link; +} + struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe, pid_t pid, const char *binary_path, @@ -12041,7 +12100,19 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) *link = bpf_program__attach(prog); err = libbpf_get_error(*link); - if (err) { + switch (err) { + case 0: + break; + case -ESRCH: + /* -ESRCH is used as it is less likely to collide with other error + * cases in program attach while being consistent with the value + * returned by bpf_program__attach() where no auto-attach function + * is provided. + */ + pr_warn("auto-attach not supported for program '%s'\n", + bpf_program__name(prog)); + break; + default: pr_warn("failed to auto-attach program '%s': %d\n", bpf_program__name(prog), err); return libbpf_err(err); From patchwork Mon Jan 31 16:12:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12730851 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F416C433EF for ; Mon, 31 Jan 2022 16:13:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380117AbiAaQNb (ORCPT ); Mon, 31 Jan 2022 11:13:31 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:63844 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349709AbiAaQNK (ORCPT ); Mon, 31 Jan 2022 11:13:10 -0500 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20VFx5mI010613; Mon, 31 Jan 2022 16:12:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=IcvUWVkBXSJVXiG/GXrnRKhgiKEEGhz0Q5TSiYjSw2M=; b=qEdxRBRBMj+1+VwqC1YxtTZV8GyKoV0rs/XDtgiQRzX4cdO6vPFg630n29XjNCt5VMbr VdexQVM+Mhi3ffgLpC0fINmAXzI2IgnVgAcLlVHkur/lGrbM236WOXYCZBOlhRaiQIEF 21So2Y8dWjM8QNNGQSp8AoWAiq5Tt06DW3HBAaiO5BPbx/0YQGz2shP1tsjkqByN4XNz LyEiwgS1LoQ8u9/uw/I8kvUk3OiO6wDhez/hvyRWHW5npyfl3/nIpLdEtHQiKaiR3F9/ DhE9aQb6reR6bC+CvujJeoSAGbP1Qx7PptaJcnYDqjW9gFN+iy8BZe/naMX+dEV4OCIi dw== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3dxjatr65q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:49 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20VGBbKq115119; Mon, 31 Jan 2022 16:12:48 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2176.outbound.protection.outlook.com [104.47.56.176]) by aserp3030.oracle.com with ESMTP id 3dvumdt0p1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:48 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Xhz/BA/CEE3wTGb0f14qyTI6U9BhnVciQoChjuA75mTK4TjYRFNF8q6avxIVwL+5ofjaeed5Ie+1lhUh9fDFqgvVnI01ANhruAkdG+nZdHn8ZYO6+gNG9hHY6ghMmYFiDd6RAup0/UYgijfR+rsjxkLoNF6+MURYEbjeQDROsrSpOMwxVcKwsD1u6rkrs+DuazJ1XmO05JD55tJlVwYY17tbLaCN+PaWROIAYP/ufQn95FEjjmrjoAfE77YlDCq+iLMGCB3fVngSDWWghmOXJw2sH2JxZUJM6OvJOJ3VW0VDh2+bXJRuwuqxzIXuIgkSMbdN6iNCvr8VgdK6jK4rUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=IcvUWVkBXSJVXiG/GXrnRKhgiKEEGhz0Q5TSiYjSw2M=; b=IztMRcAltlrLJObDCVo/L0DjmvBf1QkfMUW7VHwjzWyD/kF9+BxR5E9NAsmFf7rInXtIudkFQTQvglAhQmGvegq3SyDtyWWjkGTmVGcRlNoxNqdVO5M0YE/XmtNo45nLt98f6lpGmLdWyJgf8zk0h2sM6ui2XvWduxblSVX51YMmO6KTZQ2zl3dMI2laKkT4mCKYDjMrHJrU5K8WCoH4JXksKWI5SfnmFkwhM7YktjVm4JnPn4aOiv47dlTj58xl2OTmzFzKSMw0tBWli3Ce5DDKSQSRwykCYCRP4QDXNWfbU7tY0iMYT9PU6i6xur8TuuV89SHkUIvjvWMbBfsKmg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IcvUWVkBXSJVXiG/GXrnRKhgiKEEGhz0Q5TSiYjSw2M=; b=gl3RUSwaWu2aZtAI+FV5vEF8h0q33lpa+DRpwYfkJrt+Qw5DHunMD3+kC4ItouS4ZX01etXfvq1vVw1PTBIQg9teBx71nKWqRp0/XLpvD+DZ9nzqwQsyClmATX7p36KheUcHhOsyhgmnZ8OZld7fUfcbin+BLYJlERWWjLv+tlg= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by BN7PR10MB2516.namprd10.prod.outlook.com (2603:10b6:406:c1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.17; Mon, 31 Jan 2022 16:12:46 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6%6]) with mapi id 15.20.4930.022; Mon, 31 Jan 2022 16:12:46 +0000 From: Alan Maguire To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, jolsa@kernel.org, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v3 bpf-next 3/4] selftests/bpf: add get_lib_path() helper Date: Mon, 31 Jan 2022 16:12:33 +0000 Message-Id: <1643645554-28723-4-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> References: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM0PR06CA0124.eurprd06.prod.outlook.com (2603:10a6:208:ab::29) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 431236d5-086d-4d57-f375-08d9e4d484a4 X-MS-TrafficTypeDiagnostic: BN7PR10MB2516:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:556; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3FBVObPIxtt/+te2ZyMDebOL3JzHHQmSicEYyK7cC2svXPGWw6YJFvXErhGiKpS2SH5YTQnUHqbgseyLPEPsLb8pVfi64/ZJzmue+Hh+4UcUWr/4ji0eqt81SULtoa1iSE6XPTUqSR616JSZ/6DZh+e0mDYkzgno4nn69lMHZkFj0CjB/6AvPp3XGric0P4XHOZK3g0MdW/bDEPJWJvRcCnCiS9CGYcKUQuza98KkbH59nO+PKsdi0Pyw7ZNw2J8r7NU24/x4DK3O+LnuBxTyoUon/wF5nAZyR8GqnvhQP5ot1++1YrXxC5dqqqNTOUyMWruaUbCt1qgrW9KxuVld5eQ3jkECmyqPXhMHZzumOguFUe7qNIQNvErMXImofMS1qFNAfjM+NsbsnmDikhdWeATGnUxQEaKuy2yN/LS6Dh8Fa6B3k2JSH2aCQu/yYBCs7Na+B6bcSkY+L0epmtVBdwepm9+Pp6lJVuKAMBg48+qS2BAndjtaWQwVGHHtrKkRYtqVjKLvdCg6qs0F2lFLyVxIshWVnNYEswcv1EmAZ/kMdej8r257naQGNkTstD3QrrqsoDoBMf53Bmjxxd9j8yy2SaGcrLIUbLh1yGixDZHnXXmpLq5lBC49MPuECQpyDbGhGK0iRd5Sfj3PItGSajU0/ISDWurGZW5TQuGXh4ciJgTTPtajuHQvEmTpDZaa0xNEJoz1SkGYnFaHNTV5A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(26005)(186003)(38350700002)(6486002)(38100700002)(5660300002)(6666004)(107886003)(2616005)(52116002)(86362001)(508600001)(6512007)(6506007)(316002)(83380400001)(44832011)(8936002)(4326008)(8676002)(66946007)(2906002)(66556008)(66476007)(7416002)(36756003)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: W34pPSGszZd0DEq/Fw1Tlzn5wmtJnj9Hz9+yAQP+jwLdrcjy0nijMZPSebjPVF9P8Zy2eUcHv1qpP9HI71DEBFSNZDJhcbIkYkLqDJ3zbXaYE3cUu09r0mB4P0Fr+YRCYG2gh0avIqRbeQCOtsZFjyftClHZW86znFFSKUTKYf0e+rL+v1KZokZ+3NVaKqGeniv2qhnqp8skoLGqFpWdKWZplFgKX43Be2g8GjCtPwPTxvXpNgXX3bnZuYEC9V4cDKC9AA1I1vwgsJHdXSg3fQCREeXnYXprQ1iwheFO4UmLJiWEqTPkkftNhRU26pFvEt/VTZLEg46C8HcfQp7ijubt3uc+QLa5IZRu8DwGbDISKhY8XfQFHnNSRgBwu2LFTFlQLMT6h6U8R5s6jrVy5C2dVDGkL7ZUfCFQv0csqFQHNCSyD2B1aoQA7hL+RPhE0j3y41PGdWnYwYkZJihjSp46ky6deFqbkdYJUwqaimo91iB/CPVfR4ary6ElO1eIzlYVWPViRd7wbU++vG6pvb2GG1foxPkWWTQnFl8Cqc/oCMthHHlXuqV4rNaKod8f1/2dj1+5vJdOH9dsFPZ7Mb26UrEm5QA20Kg38n0mvbGH3GCKfbuefA1uk4i97D2bB5rIPrtE8C/hq0nyjEGYGuGKfcN94gZnPstin6Do8smb7KuUBBAIWMNyJd4+b7MTrkC4oaANPxD0Dx/+C/EcaRlNC6eZQpiFAG8NWQ0gVwJ9ppVqbB4ESZfHByOBnQiOs7Ux77yxWIGPRv9hXC1IIuGjhPHHnlcS1g0zjoJ6jyt2q0GerfU4RGzWePXF0kE4xdyPxkv+d/ucEMgjMVG2RSWRF2JM3MkTrafCDFSdN55fURYGJVMp1LAPR3KCJ4O7TKMvmS3nRoCzI2Nw6uALEj7PCL0rqUraibt46BnvHnOr/oO+KLHVa75oA9Uj7AHnVSptQr6nZ06Uqd/4fVTBGPenXpgQC3Lfu69MRv3jBofPMeULW+JAzkz2EE9oorOz2giDMvqReSgi0Ea5Ms6kmPgUzct8a8zqBSWD5L0HUAF7WQu4edm1gs8/38Oa0nEQMQOIUb+g6IxnV9GhGMD11egeI7N36DQUE4IPHiAm2raNU7DymjQZG8IMwPzRu0ciCB37Cl/AdnBCirFFgI/YknXwFMZjl3nN0yH4YCQB0jDXbHY6s54ndym4U3aUzKaCWA8kzj97nuN86qkWo2DMoWv04xXwuLjhmh4Zt3Lqqdt6F9g6/j06wWkRSdBgNSwwli4d9eKUSGTpjdHUQRi15oC6az6JK0nCxir/raOrHkgvlyhqECoUwfby3s5m7G+Dh/I2XtS54xk4Sm2D83/JlDYEdTJSBC7/eadn+OYkp7amlSqxZBMBf0khy2A7fP9DARUkpckBbg9weOv7DNmshxTdKjbzDcsfz9171feS94MeakaF6bpt7vgOREvPDOBBZsw3z4fl4QDPYhGnssxstfeDXU3dKxKslt6xb4OFfXX1mKwJyYILgNDXRf11p4rH+Erj/80sjIapVtL0rnltdF2C4m+F/htvLXJgZ+dmdlj5K+aAGd7jqX12XgxQasaijlMvBSgDOn4a7FYZQGNfPsfg2jJpOoSEI60Lec2/ijY= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 431236d5-086d-4d57-f375-08d9e4d484a4 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jan 2022 16:12:46.1112 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uoF2Dmm0efgMDDtpHMF55pfB168/G1QSlCqojKoNg/cJ1omp1Zr/Dd2tXG5Y0QDWcZxfq8LOrEmM0xMtyVTrtQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7PR10MB2516 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10244 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 malwarescore=0 phishscore=0 mlxscore=0 adultscore=0 suspectscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2201310106 X-Proofpoint-GUID: Vyz-08KYLM50eTEweOKDlVteCpwt4xP3 X-Proofpoint-ORIG-GUID: Vyz-08KYLM50eTEweOKDlVteCpwt4xP3 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net get_lib_path(path_substr) returns full path to a library containing path_substr (such as "libc-") found via /proc/self/maps. Caller is responsible for freeing the returned string. Signed-off-by: Alan Maguire --- tools/testing/selftests/bpf/trace_helpers.c | 17 +++++++++++++++++ tools/testing/selftests/bpf/trace_helpers.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index ca6abae..49e5f0d 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -216,3 +216,20 @@ ssize_t get_rel_offset(uintptr_t addr) fclose(f); return -EINVAL; } + +char *get_lib_path(const char *path_substr) +{ + char *found = NULL; + char lib_path[512]; + FILE *f; + + f = fopen("/proc/self/maps", "r"); + while (fscanf(f, "%*s %*s %*s %*s %*s %[^\n]", lib_path) == 1) { + if (strstr(lib_path, path_substr) == NULL) + continue; + found = strdup(lib_path); + break; + } + fclose(f); + return found; +} diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h index 238a9c9..ff379f6 100644 --- a/tools/testing/selftests/bpf/trace_helpers.h +++ b/tools/testing/selftests/bpf/trace_helpers.h @@ -20,5 +20,7 @@ struct ksym { ssize_t get_uprobe_offset(const void *addr); ssize_t get_rel_offset(uintptr_t addr); +/* Return allocated string path to library that contains path_substr. */ +char *get_lib_path(const char *path_substr); #endif From patchwork Mon Jan 31 16:12:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12730848 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC0F3C433EF for ; Mon, 31 Jan 2022 16:13:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349733AbiAaQNJ (ORCPT ); Mon, 31 Jan 2022 11:13:09 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:42186 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232033AbiAaQNH (ORCPT ); Mon, 31 Jan 2022 11:13:07 -0500 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20VFx21o031412; Mon, 31 Jan 2022 16:12:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : mime-version; s=corp-2021-07-09; bh=qjdj5f6hn6Xa+VuuIRJZ5LY+kQsH0gu9JoqOGofCieY=; b=LEzwEgmjvXfLH+sdPRA1rJPCdWohgVxNaugDQfP0BpV2IPbXVUMalisI2bdR5lVfhf2x 9cH3dDc1QqKjgC6H40yELhlll0KB9t078uhoIFhBdoso+PBchTmiGnPDWaz4BDkXTcV+ rflTSz2txcR2Mzsf/0B0y1Gizr9TC533yDiAJZ9anA/0qDZxE9pgricf03yIj5BQLPET 6OmDnvbJhmQZ1HABs0JGkuVtbx8wvdk4e0VuO1jrcRoIqX9KwOOOFFkUC1IjlMVtiNC3 +SYd2KOPMYlHbPe5kXc3MIPGeaEylyv62u3Rbq69MsWIfd6G3I23fCodrRavL54qTShf MA== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3dxj9v86dq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:52 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20VGBL72164151; Mon, 31 Jan 2022 16:12:51 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2171.outbound.protection.outlook.com [104.47.56.171]) by userp3030.oracle.com with ESMTP id 3dvtpx912s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Jan 2022 16:12:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WDnuUY6Z11n5pkWw+f66NdgH2AOEGY+haPk/EQY5kn2Y2ZB7xtjzcfPSM/D7TjOScRr34xam/Ykw02Azd6E0svFvYmDec5J4AfX5ltpa8scw/4LSMecNLKlfbYVgCVjlj4tHUTNnOSuYgrxgRG6gPbxxreZlcyrX/+H6c4waXHizl7JkzzEoJ38X6j6oMig7zzDxTJkete2xFUjgPAihVDvNOCiwEcMNQ4fbDQJkxAeWcF/Fy9R1vnE/1lPc5lsDMPN8uf6vApCbE6i8q4qSneErGBTxE/Z7vixykDyVrRThnlm8VRifV6Svaoa7a9Dns4E57lI9Bm3XYBFk9fVLtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=qjdj5f6hn6Xa+VuuIRJZ5LY+kQsH0gu9JoqOGofCieY=; b=Q/BB/rkBFlj75Fb7dQ/nNrEc3CTyXUyMoc78J/CgPnURGk90JwrSN2rOCfqU1rlMvHFcJ7oorWG9tSyVHFtELyiGptLQl916YN14NmQf+SnqOdiCFxDNLVi9eU8z7MWYE6ceBDxsaCugSZSGjRfRiLkgB0em20POpXA8BHHbSJXzoaZYc7giq4qh7rursonCF4JAThHj/bUsspl3aoo3TPkAI+maYJhv4aJ3AU6nLW5aDVhHsIq5DVX5YgEw8fx6YIUKL85QQFHUuf/WhOMVx6SIXXVtx+mhjYxjpA5tftGewSYhnRUD1P15zs8+ZFih3j2Scid0UXzgfEPW8zT7ww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qjdj5f6hn6Xa+VuuIRJZ5LY+kQsH0gu9JoqOGofCieY=; b=Zcrvk2pIzzzhB3m52/9FFrLop1SiujrR/RVSD/2vy6M8t8+GiyRInoaG9T6qcDnZOwZuAd9fLPEeeDXtlQwg9qWT3R++3C81j8gPIPnaKtgXTPUp2c32TR15qyhLxWUd8fQBClc7o9UMtVrQqTe7EF0jOyKhySKZV18nD/eRKOU= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by BN7PR10MB2516.namprd10.prod.outlook.com (2603:10b6:406:c1::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.17; Mon, 31 Jan 2022 16:12:48 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6%6]) with mapi id 15.20.4930.022; Mon, 31 Jan 2022 16:12:48 +0000 From: Alan Maguire To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, jolsa@kernel.org, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v3 bpf-next 4/4] selftests/bpf: add tests for u[ret]probe attach by name Date: Mon, 31 Jan 2022 16:12:34 +0000 Message-Id: <1643645554-28723-5-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> References: <1643645554-28723-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: AM0PR06CA0124.eurprd06.prod.outlook.com (2603:10a6:208:ab::29) To BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6241d6b3-e94b-44eb-e5da-08d9e4d485d3 X-MS-TrafficTypeDiagnostic: BN7PR10MB2516:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3826; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cfPmgJYl/3q9+vNRLjDuxdsXRwWqV/hQxMUHX7L1kMqqBUY9BKYQUflF1aC2eStfhtvEQYr3sgzg1+hpRaGvlLcb0OCw25Rpw/RrnkjDTPalpAUXxVCmhA6FEqFCicKQNZLch6DGfND8AsV7GMFB81hetAH5VOYhQ9papcLD5SeLbVEgaiDy+d3Xv25rkWHwxaVmhoQy44COqFUu1kU9Wcz6xS6M6L4a0ZdsoIobtXaCS7rpHn/Mltv83/gqq8tGdgGW6s4idcpfbgtz2is9aVCQn/6N051F1wK5fY8PsM/UtymjvEcftn7GMIybLU2uqZm/LPvt1w9HRfzODv4zHaRZvMQ/W7XBTXXFdOD4gxNvjLY0WWBR0VvsagOqPARjcvVtbuU2eCBBj6w55Jht4zk+6TkwCc+nE9TVTWGdHm8nFzw6iWNnSAmpMqM/LTYFhfKSRTo4+VJg4rQwGbDCdDnOhLF4zejCGHmh5nbBKULEJEmq9x6Jq5Ttjl/KP5EX9bMgAiC9IOHHa0VIAcDkrYIDnh8/rSx4cGtqMIns+PEJlHPrxpbNL4KYnf7/SZlHa/fEJ9YHnAS8nC3Phr7pyyGGbgg0vRNX+EC2WLQeQTQ8GEvd9bdoGmSt6pP7mD0C6LS5vxjiCHaNJa27XvfIrVcCurRJWo0LiGREBx7LegtaZQc4p0rj+D88jUsc/jjzm+MSLM/bs+Fd+XOFeGRmFg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BLAPR10MB5267.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(26005)(186003)(38350700002)(6486002)(38100700002)(5660300002)(6666004)(107886003)(2616005)(52116002)(86362001)(508600001)(6512007)(6506007)(316002)(83380400001)(44832011)(8936002)(4326008)(8676002)(66946007)(2906002)(66556008)(66476007)(7416002)(36756003)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Hu3o0ZPG+uftrjlVle+D4OfCGYPwg/y4UCte+8vfzaJgG9HdVDc2RXfuFDjLIISkrqY/h9TMyXQRMr5SDDjCSviUUiFZkpYQmVxP4aIRLVXqqhJSYVEAcL+TOY6BDO4JwrDucvgDIIC5GKolmRS5sY122HeY+4FF/QkFbRn3iPufB7R3sDGLQ+tQzTRqtQfDq0Zu2or/r7n/uq89bZwBu/uL0jloOXXYIkCkeEPH8nUteRGDFjHRZKRZ8KK/qeLFyq71Do+6KuXy03XMPQkoweZYw1+r5TCDul7RJHeLRkf+GYcwSHFchz5ALT672cacgGuHC72OhgExFLhMpLh8zLxqH82GsMkjP3j+iF7LZ0VP3bY1Wwfjp6mrmlYI4msjUOpKTqy7OFfKQkhaC/hE6qmRbJyCXG6gULgqJmrNg8JoD7QzOfYBVK/Jdr1005JoXkPsY2rULvbQUlhTg9BSpXN4MOncNn0uUIIoNVv7lZ5MfgaepZAwKcC3EEtoygOEgjkZTa0eG/ArUruKQh57sux//d/XxGSxPPkBBrD0WJMIgMIDNuVYr7xn9OyC+4qWPiGQ2Hi1EJ+KMNFe2SR0vaJrdAH1P1f2Fa9Mi632So77QjNGlE9eaIYuLQksWW66oDUhf9VbT2pj8thK122liARL/7dJRr1OhMEbUCCyJKRaxzu9OyrPcBcZZtFeR+iaPCGpAt9CsG7oOWFYqTBVY7XQ/lBFxKJFOIuR4YTnfXc2d14gL69RIN8a+q0F84JDUMedHP5OI6oMhRFFPUkKrLBQmbKCxqJKA4+LZ9vDhVubrxNKJ/zIEMhD9ObqBmCZrEkmhiW27LwZdK9hrJv71roW59SfbkrFgQmk4zEGJ6nd0oZh58/Xy4aQOHosbfy3DQI4kxt9n4Mky3vJxHc/WJroU5CcjF0NKA+JtSEo2lHiC276eJc75heGfsqSm/8Z1vgOAxElMEUsk57XAH2bfd47BmGE2pcPHIN0VPqFNT1I9o/ZFIfTy+UJENoPKXiIxEjoQabr52onMb7aRllI4O8L4EECjTZ9NELlhfJz8EoYp2qcRq32IGYyB0JecDFDhElOZcJmA2rvA8Y033a512bO0pW3uR/L4HYYTo+mYjjGybtRFu+IzNdHUZEDUhMZn9byR0U0EC6DXRqSmcNhQATEQ3DSfdKYguR0EB95lqCJM20KHbYO7i3QTIW2935xhF6cGgKB0tcLWGrNg/NA7XYEirXsrrOWTcB8oZheZlX39QW9hHiOQFYkcBEATjuxa9k4Iul/CUqByGYexsbCBgfn6GPvkeyW7ROOFkOmTfUPxlKhFGvVXH1Bnq4Z2BZfATgik0yAutrK4rAOGBFbOCRVGOLkuHnyLR4HqcK3LZsPUSIA4ygPMBlN1YZV8UyCsW9r0zAlpDA1LHkA3AthRtxttARJUWkKYxWSsk+HAmUF56jFRFTyGhlnSDyWggbSTUgCnb0QEulxSy+tmlr/I3B6Aj1hg1000jbflBNA6sDRiE9CJUim/JaXRSGKCe8x77NeGMllDNTrvrzqexO9OmWws4gFqeLu3fsk3Kw2L5QQOlOMpDyp6y7qNk3VUvoWRrghCzevnpqHqGY26Er78D2U7glJFbLHfxQ2RxuUgtw= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6241d6b3-e94b-44eb-e5da-08d9e4d485d3 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Jan 2022 16:12:48.0966 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: OocT3/BX+JtoSwSjwd0zZxi7vjKv3OZGA4Ahq27Dx+HMhTs/XF32dfg3yluagZQWwCahyEo1oXh8iR+i1B6Z7A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN7PR10MB2516 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10244 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2201310106 X-Proofpoint-ORIG-GUID: HCfct17mV14w33ny5_oIyjJhOX2UPoYC X-Proofpoint-GUID: HCfct17mV14w33ny5_oIyjJhOX2UPoYC Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net add tests that verify attaching by name for 1. local functions in a program 2. library functions in a shared object; and 3. library functions in a program ...succeed for uprobe and uretprobes using new "func_name" option for bpf_program__attach_uprobe_opts(). Also verify auto-attach works where uprobe, path to binary and function name are specified, but fails with -ESRCH when the format does not match (the latter is to support backwards-compatibility). Signed-off-by: Alan Maguire --- .../selftests/bpf/prog_tests/attach_probe.c | 89 +++++++++++++++++++++- .../selftests/bpf/progs/test_attach_probe.c | 37 +++++++++ 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c index d48f6e5..127c347 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -11,6 +11,12 @@ static void trigger_func(void) asm volatile (""); } +/* attach point for byname uprobe */ +static void trigger_func2(void) +{ + asm volatile (""); +} + void test_attach_probe(void) { DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); @@ -19,7 +25,10 @@ void test_attach_probe(void) struct bpf_link *uprobe_link, *uretprobe_link; struct test_attach_probe* skel; ssize_t uprobe_offset, ref_ctr_offset; + struct bpf_link *uprobe_err_link; + char *libc_path; bool legacy; + char *mem; /* Check if new-style kprobe/uprobe API is supported. * Kernels that support new FD-based kprobe and uprobe BPF attachment @@ -90,9 +99,72 @@ void test_attach_probe(void) goto cleanup; skel->links.handle_uretprobe = uretprobe_link; + /* verify auto-attach fails for old-style uprobe definition */ + uprobe_err_link = bpf_program__attach(skel->progs.handle_uprobe_byname); + if (!ASSERT_EQ(libbpf_get_error(uprobe_err_link), -ESRCH, + "auto-attach should fail for old-style name")) + goto cleanup; + + uprobe_opts.func_name = "trigger_func2"; + uprobe_opts.retprobe = false; + uprobe_opts.ref_ctr_offset = 0; + skel->links.handle_uprobe_byname = + bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname, + 0 /* this pid */, + "/proc/self/exe", + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(skel->links.handle_uprobe_byname, "attach_uprobe_byname")) + goto cleanup; + + /* verify auto-attach works */ + skel->links.handle_uretprobe_byname = + bpf_program__attach(skel->progs.handle_uretprobe_byname); + if (!ASSERT_OK_PTR(skel->links.handle_uretprobe_byname, "attach_uretprobe_byname")) + goto cleanup; + + /* test attach by name for a library function, using the library + * as the binary argument. To do this, find path to libc used + * by test_progs via /proc/self/maps. + */ + libc_path = get_lib_path("libc-"); + if (!ASSERT_OK_PTR(libc_path, "get path to libc")) + goto cleanup; + if (!ASSERT_NEQ(strstr(libc_path, "libc-"), NULL, "find libc path in /proc/self/maps")) + goto cleanup; + + uprobe_opts.func_name = "malloc"; + uprobe_opts.retprobe = false; + skel->links.handle_uprobe_byname2 = + bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname2, + 0 /* this pid */, + libc_path, + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(skel->links.handle_uprobe_byname2, "attach_uprobe_byname2")) + goto cleanup; + + uprobe_opts.func_name = "free"; + uprobe_opts.retprobe = true; + skel->links.handle_uretprobe_byname2 = + bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe_byname2, + -1 /* any pid */, + "/proc/self/exe", + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(skel->links.handle_uretprobe_byname2, "attach_uretprobe_byname2")) + goto cleanup; + /* trigger & validate kprobe && kretprobe */ usleep(1); + /* trigger & validate shared library u[ret]probes attached by name */ + mem = malloc(1); + free(mem); + + /* trigger & validate uprobe & uretprobe */ + trigger_func(); + + /* trigger & validate uprobe attached by name */ + trigger_func2(); + if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res", "wrong kprobe res: %d\n", skel->bss->kprobe_res)) goto cleanup; @@ -100,9 +172,6 @@ void test_attach_probe(void) "wrong kretprobe res: %d\n", skel->bss->kretprobe_res)) goto cleanup; - /* trigger & validate uprobe & uretprobe */ - trigger_func(); - if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res", "wrong uprobe res: %d\n", skel->bss->uprobe_res)) goto cleanup; @@ -110,7 +179,21 @@ void test_attach_probe(void) "wrong uretprobe res: %d\n", skel->bss->uretprobe_res)) goto cleanup; + if (CHECK(skel->bss->uprobe_byname_res != 5, "check_uprobe_byname_res", + "wrong uprobe byname res: %d\n", skel->bss->uprobe_byname_res)) + goto cleanup; + if (CHECK(skel->bss->uretprobe_byname_res != 6, "check_uretprobe_byname_res", + "wrong uretprobe byname res: %d\n", skel->bss->uretprobe_byname_res)) + goto cleanup; + if (CHECK(skel->bss->uprobe_byname2_res != 7, "check_uprobe_byname2_res", + "wrong uprobe byname2 res: %d\n", skel->bss->uprobe_byname2_res)) + goto cleanup; + if (CHECK(skel->bss->uretprobe_byname2_res != 8, "check_uretprobe_byname2_res", + "wrong uretprobe byname2 res: %d\n", skel->bss->uretprobe_byname2_res)) + goto cleanup; + cleanup: + free(libc_path); test_attach_probe__destroy(skel); ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_cleanup"); } diff --git a/tools/testing/selftests/bpf/progs/test_attach_probe.c b/tools/testing/selftests/bpf/progs/test_attach_probe.c index 8056a4c..9942461c 100644 --- a/tools/testing/selftests/bpf/progs/test_attach_probe.c +++ b/tools/testing/selftests/bpf/progs/test_attach_probe.c @@ -10,6 +10,10 @@ int kretprobe_res = 0; int uprobe_res = 0; int uretprobe_res = 0; +int uprobe_byname_res = 0; +int uretprobe_byname_res = 0; +int uprobe_byname2_res = 0; +int uretprobe_byname2_res = 0; SEC("kprobe/sys_nanosleep") int handle_kprobe(struct pt_regs *ctx) @@ -39,4 +43,37 @@ int handle_uretprobe(struct pt_regs *ctx) return 0; } +SEC("uprobe/trigger_func_byname") +int handle_uprobe_byname(struct pt_regs *ctx) +{ + uprobe_byname_res = 5; + return 0; +} + +/* use auto-attach format for section definition. */ +SEC("uretprobe//proc/self/exe:trigger_func2") +int handle_uretprobe_byname(struct pt_regs *ctx) +{ + uretprobe_byname_res = 6; + return 0; +} + +SEC("uprobe/trigger_func_byname2") +int handle_uprobe_byname2(struct pt_regs *ctx) +{ + unsigned int size = PT_REGS_PARM1(ctx); + + /* verify malloc size */ + if (size == 1) + uprobe_byname2_res = 7; + return 0; +} + +SEC("uretprobe/trigger_func_byname2") +int handle_uretprobe_byname2(struct pt_regs *ctx) +{ + uretprobe_byname2_res = 8; + return 0; +} + char _license[] SEC("license") = "GPL";