From patchwork Thu Jan 20 11:42:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12718563 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 32B79C43219 for ; Thu, 20 Jan 2022 11:43:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231940AbiATLnF (ORCPT ); Thu, 20 Jan 2022 06:43:05 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:43186 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231917AbiATLnD (ORCPT ); Thu, 20 Jan 2022 06:43:03 -0500 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20K9n4Tg014084; Thu, 20 Jan 2022 11:42:48 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=Xu6tZXLtUyH5CpypomziSHMovOGpMO5K+/+yjHShI3g=; b=EAZuYgyWsJCYz9Gs0RpyFCa/H9r7tLSzYNv6RlmS4FbXA9VOmh3hDurUEMqyJbc/iyTD UATGDHfNHC/vEO55fStFx9NiH0NdvVZnleeIS6KV1B0tizMAsMdyUoR/p8xHh0m5DIaB KKr67wBMWLAtB/zxMgLY8YcSA/ZmxSZGAbLzGHuXYvnOQvYiuwf8d6rcJR2gDfWi5/wf q+1D7p6/p53ATzyMZxLL6Ui1ZB6VP0e/HRz161Wwhi4Kd95tmnSxUNrmo5uBIJtGEvbI xRl+eD4hfvYBWWOeT8gfAuaUdCkDvfWRn84LOpRlESSMiwoNiVdpRuP9l+CBCkK9C4AF hg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc51fr25-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Jan 2022 11:42:48 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20KBaB31151224; Thu, 20 Jan 2022 11:42:47 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2171.outbound.protection.outlook.com [104.47.55.171]) by userp3020.oracle.com with ESMTP id 3dkqqs9385-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Jan 2022 11:42:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E8Hjbt5RfGpAR2HQ1rtXVUGOJsZSuIo13gG5s7dg1EU8fRvfa4h2RVtBQoKZAoBToJQaSAcgZpOpD/kfWfF/z1LTV6zu3DbeC/VFMTsnafTb7dBqMcqcV8h3NEomN5hxcPbzHpXo8SvKwKPE2JrRYRTeXdd3toDZvQU4zzpN0lYLaVW/AMT4E1buHs2P19XAwhm4DbPAXnRn8vG0MF6bgsCmSi4jXka3ahsvlNrSirQC4QU45Bw0inqqFx9NwM3WsMZ67hrixMslpuyVchTLR+Mu49ReHvELYEOJxAqWmSzfxGoCkLlgRcf29XpR83DXpH0XxlKmmfd43iPZo+KL7g== 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=Xu6tZXLtUyH5CpypomziSHMovOGpMO5K+/+yjHShI3g=; b=bGf9qCxdCWoPJGK5D4OcWDaly08TVzoBt9rBgpQ6hrfnmoddNgHFxaWpGNJRKPiHTJHBmX3DBC24RfCInOvZyYOYSGw8RlqbVB8ZTYXwA92KTs5CLFChBCkSMIzYOJoq3W8FmP1MTSN0dIPX9zqZTC+mkOp4u9ZNqZDQcd7nrkDIStezDAhZALzI9TELKawxlTJhi7UkytM8wxSZDRiSQWxHsjDgq1s+Kk3/f+4Ud8RNHcc3AHmShf+kh8cy5AXxMQsdyunj5W1JAF2NYLNU0ZpNIdUAKbkY6N2V5KHQGDsYbEV42rD7wgPtZO8Hn9KCfX3/yFGis3+e4CwPKCLnQw== 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=Xu6tZXLtUyH5CpypomziSHMovOGpMO5K+/+yjHShI3g=; b=WLAU1R/4Hpsj7uMMxTcI5OL99mF12PRQwb81/AHffs0rOU476hoAji70CCejagQmmgWcQ2M5iokNhlPL+CMCEO1uzufnTWJmI6w/8BpmBzUD86zoYYwkvf9l7liarnR16F6axYWGyyAwLOLYoRYnRgdSXR4kj5IyayZIQ7BTcWc= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM6PR10MB2923.namprd10.prod.outlook.com (2603:10b6:5:70::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.7; Thu, 20 Jan 2022 11:42: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%4]) with mapi id 15.20.4909.010; Thu, 20 Jan 2022 11:42:43 +0000 From: Alan Maguire To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org 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: [RFC bpf-next 1/3] libbpf: support function name-based attach for uprobes Date: Thu, 20 Jan 2022 11:42:28 +0000 Message-Id: <1642678950-19584-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1642678950-19584-1-git-send-email-alan.maguire@oracle.com> References: <1642678950-19584-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0358.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18d::21) 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: 666e68f4-4ee8-47be-62ee-08d9dc09f52e X-MS-TrafficTypeDiagnostic: DM6PR10MB2923:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1923; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lIHSJlE/MlHJ3ULugKfnb43wX/9XS6o4sf3/Viyt8Db0H0bBa7akbePEP3juY37wjrWZjjdSndfj1iFPb3m4/rG1mzkooj1IFE1KLc52lRNguEykazLBNduKet62Q/7EpL1P3vLuHIvS4fGSTXYZ7xLBfSjMssBkFAl/UBcbEPWfY2Qw6YHbWfqYqzARYpPAN5k49F7qpm2UtIgcwzHv4yn3hkDd+z8BusCpS6QRpSAOprmywxTOvp83eNYegWOWXDbp6KM+OJLtmltJRvVPn9HzHXMjma25pAA2SDkVTWOP6kkHdpgHBeT0gW1U3RTwmNPOeHq/lP4BURmYEGXA7VIXgcWZ4DmZCV7cYtsA0n5X39nJ0Phe+pMujFaFExJ4bwESzFo4Pa6CkCrUES+bJDFsZlU4STp0tJ1hl7Ql0HC9xw7jTHhMBz3vR/FuIdi3kl6q3AzDZFVVzkvlxq+W/GnsDYwZ++Sfke3XFi7lAwJDF8G+iq3eRTSAPnZ4UEEv73tkYt1NKBxKt+yZ6lnVwk1T1eI8z9YHYWfuAdrmAcCWD3Qjt+buRACJeZE0vA6dKulxWtzFBvqCwEcgCTaJEyZggnRnsZEccu2Ddp+XLSCNpSFb4+P5+ufkGkoxk5hrK9h0SfJJPb8P4av+5D7l9tGmUhPtUDhQ/D6Hd5DnekrRdvoQh2l1ctHoQo9aFNAULjmm4MiMJQkqWP4rhJtbrp0Ie7KxYhIu4hZuBuQ9gMHM9a990a7SzY8EENVFo74OnH8Kxcv3M2gCWQPrO5mPLGN/Lo6BJi/wD+6O42o1oW4= 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:(366004)(316002)(2616005)(8936002)(186003)(86362001)(6666004)(6506007)(2906002)(44832011)(8676002)(26005)(107886003)(508600001)(36756003)(83380400001)(66476007)(66556008)(966005)(66946007)(52116002)(38350700002)(38100700002)(4326008)(6486002)(6512007)(5660300002)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2fqdB+wAIHDQerepgwJLNTJAVu0KanOpEJcza8no3NA2Dak4awVSWQpsXAJ4cVKumBKzh1vMP9zjEXDkscCJoFPSNo9jwdRTvT4HkdJoBwKHSrsznwQL76HEFHPoeLmiTutHmdL9MJWpnywGDWugZB9M9IENtkARff9kNu//r6h+1GM3EqsGLHZ6afZcpH/yKjyue/PWl7NYBeZBcsLKeVTuTuMjjvn+lo4SvwPQrhzPiFOaN68ZxdKgyOs+Y6gkxTXjQMLL6TrEBNMouRnIwq+KLHU6516+/R/nTYeAPPKbpNsFk2DxApJVsYJymNwSah11qEP0aXwozDf/wNusTkUeIlTll6E6fwpRzMAQSwDsWfgeydHZfd6ru8UDMC7mcTzlwjOhLYm52PeAfj55P3WvlSfe4498uG8OrRrIkxwaGxc8CpKe05N0cDNSPO8ALBrhQQq78ullxtty1/H7wIeclsCRV6qsdx4AB7Cbn+7Au8YfgKTGj36P7NXhTiTJLQGYt+vsNZqTKNHjNa2qXe3g8J4zi/N9EcTO1wfzgyN7Jal7pBiW435pI9sLIqvylKhgdLIP6gZ6MFKnYRDICYlDG0xaqlg727Zu/ugcd6+mM7rPyyAek437ClQ9g6MA5PmangohydJcUqSQ31TZTJeE9o4saC6U3kFeBtqi354fCE1Cp258HA5StvpkvWvEu1TUnXJWvqtA6YkyxpfNsY4NTrlBgIsu0nFK9svI/eW1aIqiZmtfO3g6zU63EHkMlv3gnyNCANtGJ757mcCQ+5Oe7JFVs3XrCK+37g9Cf8DRLhU+DPQ7tV31D0IkDikScymK5yeP/R5KsCZGlIVm8lEQ1JiTO1XApYJjGqatJ83BIEnJaUSchcw2TraBVScuXiTAiMJspkpvDdyS9g6lFF7F4khSb7PNKWrkbMy8imCV/b9eCrrOv6+mvw0GLZd1K0vwb7FpZ5YmGsrzBqlQ0rzkDFOxkolkL7f30XMWbqPSZUgUNZ/v674X0QhbjtV+BSV476w1hFuG/TCpvkI3ABxZ5tDxUdTaDB71r715f2FZ0G4FDE7czu3GC4LNJQosuGM0VRKjtPCFTp9XfzCYSDLPncmncYV2d5yvm6drNygUvqqClRSvBoPL29v1tqI/z2FjnqTrf4kmWuv6zpiosJuKH8SPSOcjQHFPOZNewnkpH9cwj0sMwFAsQi/rD/u7+231L/abDpawn4oIY4hU03v8gdWnyoj1NrSmwHObozKJqHJLhsmaj+AwNnRGF7UP/BZgj4rFDWFsbQIm42DIOmzlY0Z3X/M82/Z0Zm7jY0QXoo5/ybWLnJmzkoZSeBWmL2m9ef9ryqTPq/2E0NbdmoddngSsLJoCU2UZsE0SaooDx/hOWikgg5vUSwXvWnS+gjc9te1ntiUvH2RrIcnQrIHLNA+wRJfDAvLJ/VFTPPz3ZZ7BA7Pe7S+EJWWIMz81Pc7Mrau4j93mYIqjE1d6PKnNpy9+1aRCApK9upoFCtkAYZ/u5Bl/A0w1jGhNccXKPNpurYT8OdEu14cBn+tG0KsdM9ZQK+dhd751YumS9Bs1YanmQreianIOBv9P1HHBrBHJ/6azl1YCbTx1NTQmszzacTff/t5+zZK4eLGox7Y= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 666e68f4-4ee8-47be-62ee-08d9dc09f52e X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jan 2022 11:42:37.7784 (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: OpRf6VL6e1VyRookyldnTkHEsVfznmxzs7kmAx4hywQGNGCiSJDk0rLTxPrbyDVZ3zv+yyzVKCvdDH47GGoMeQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2923 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10232 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201200059 X-Proofpoint-GUID: NeEPMH9r_1XETNfZ82ypNQRVDqjga7Rr X-Proofpoint-ORIG-GUID: NeEPMH9r_1XETNfZ82ypNQRVDqjga7Rr Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 two steps: - first, determine the symbol address using libelf; this gives us the offset as reported by objdump; then, in the case of local functions - 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 to attach to a local function in a binary - function "foo1" in /usr/bin/foo - or to attach to a library function in a shared object - function "malloc" in /usr/lib64/libc.so.6. Because the symbol table values of shared object functions in a binary will be 0, we cannot attach to a shared object function in a binary ("malloc" in /usr/bin/foo). [1] https://www.kernel.org/doc/html/latest/trace/uprobetracer.html Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 10 ++- 2 files changed, 208 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index fdb3536..6479aae 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10183,6 +10183,191 @@ 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 ssize_t find_elf_relative_offset(Elf *elf, ssize_t 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 (ssize_t)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 *find_elfscn(Elf *elf, int sh_type, Elf_Scn *scn) +{ + Elf64_Shdr *sh; + + while ((scn = elf_nextscn(elf, scn)) != NULL) { + sh = elf64_getshdr(scn); + if (sh && sh->sh_type == sh_type) + break; + } + return scn; +} + +/* Find offset of function name in object specified by path. "name" matches + * symbol name or name@@LIB for library functions. + */ +static ssize_t find_elf_func_offset(const char *binary_path, const char *name) +{ + size_t si, strtabidx, nr_syms; + bool dynamic, is_shared_lib; + char errmsg[STRERR_BUFSIZE]; + Elf_Data *symbols = NULL; + int lastbind = -1, fd; + ssize_t ret = -ENOENT; + Elf_Scn *scn = NULL; + const char *sname; + Elf64_Shdr *sh; + GElf_Ehdr ehdr; + Elf *elf; + + if (!binary_path) { + pr_warn("name-based attach requires binary_path\n"); + return -EINVAL; + } + if (elf_version(EV_CURRENT) == EV_NONE) { + pr_warn("elf: failed to init libelf for %s\n", binary_path); + return -LIBBPF_ERRNO__LIBELF; + } + 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; + } + is_shared_lib = ehdr.e_type == ET_DYN; + dynamic = is_shared_lib; +retry: + scn = find_elfscn(elf, dynamic ? SHT_DYNSYM : SHT_SYMTAB, NULL); + if (!scn) { + pr_warn("elf: failed to find symbol table ELF section in %s\n", + binary_path); + ret = -ENOENT; + goto out; + } + + sh = elf64_getshdr(scn); + strtabidx = sh->sh_link; + symbols = elf_getdata(scn, 0); + if (!symbols) { + pr_warn("elf: failed to get symtab section in %s: %s\n", + binary_path, elf_errmsg(-1)); + ret = -LIBBPF_ERRNO__FORMAT; + goto out; + } + + lastbind = -1; + nr_syms = symbols->d_size / sizeof(Elf64_Sym); + for (si = 0; si < nr_syms; si++) { + Elf64_Sym *sym = (Elf64_Sym *)symbols->d_buf + si; + size_t matchlen; + int currbind; + + if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) + continue; + + sname = elf_strptr(elf, strtabidx, sym->st_name); + if (!sname) { + pr_warn("elf: failed to get sym name string in %s\n", + binary_path); + ret = -EIO; + goto out; + } + currbind = ELF64_ST_BIND(sym->st_info); + + /* If matching on func@@LIB, match on everything prior to + * the '@@'; otherwise match on full string. + */ + matchlen = strstr(sname, "@@") ? strstr(sname, "@@") - sname : + strlen(sname); + + if (strlen(name) == matchlen && + strncmp(sname, name, matchlen) == 0) { + if (ret >= 0 && lastbind != -1) { + /* handle multiple matches */ + if (lastbind != STB_WEAK && currbind != STB_WEAK) { + /* Only accept one non-weak bind. */ + pr_warn("elf: additional match for '%s': %s\n", + sname, name); + ret = -LIBBPF_ERRNO__FORMAT; + goto out; + } else if (currbind == STB_WEAK) { + /* already have a non-weak bind, and + * this is a weak bind, so ignore. + */ + continue; + } + } + ret = sym->st_value; + lastbind = currbind; + } + } + if (ret == 0) { + if (!dynamic) { + dynamic = true; + goto retry; + } + pr_warn("elf: '%s' is 0 in symbol table; try using shared library path instead of '%s'\n", + name, binary_path); + ret = -ENOENT; + } + if (ret > 0) { + pr_debug("elf: symbol table match for '%s': 0x%lx\n", + name, ret); + if (!is_shared_lib) + ret = find_elf_relative_offset(elf, ret); + } +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, @@ -10194,6 +10379,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); @@ -10202,6 +10388,19 @@ 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) { + ssize_t sym_off; + + sym_off = find_elf_func_offset(binary_path, func_name); + if (sym_off < 0) { + pr_debug("could not find sym offset for %s in %s\n", + func_name, binary_path); + 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 9728551..4675586 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -431,9 +431,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 name, 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 Thu Jan 20 11:42:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12718564 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 4539CC43217 for ; Thu, 20 Jan 2022 11:43:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231962AbiATLnH (ORCPT ); Thu, 20 Jan 2022 06:43:07 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:43578 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231919AbiATLnE (ORCPT ); Thu, 20 Jan 2022 06:43:04 -0500 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20K95NJN010655; Thu, 20 Jan 2022 11:42:49 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=LruUyHGx4otHaBQ7FNiHJ1DCkL7YZ+thL2LVuvJpG7s=; b=dTWljpykGMu/pfrYbvQq5SN6czAygJfbX0EJWFrqVHpXdNPCPHtYX4/VGlbZpcw1MAWI 0sgj7rZLfMNA7NoiX9hei7Wy+SoquPnRJxX2evqcRlRHQ278W9ntPDmzR2+3VzpXuGwc xkgAZq6/XImzXCw++O/rOUA/SzZ4hfZMSzvtXbWlbdYy2Qjj+oalHjMMKPTmk+t7dNVr E957D1Whb3p7dIWuOgGobpLk6McqM6mhPxyh9hKWxw701ZaDljcwUAxZsHpBVNjtPGf1 zqTog3Biv1YqnZBKDnMd9np9fbjaDU4mjZaJVBKXdK9dRtWdfDn5TfUat83ghOhynHhc uQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc5302fj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Jan 2022 11:42:48 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20KBaB33151224; Thu, 20 Jan 2022 11:42:47 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2171.outbound.protection.outlook.com [104.47.55.171]) by userp3020.oracle.com with ESMTP id 3dkqqs9385-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Jan 2022 11:42:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YdLqwY/ltqxF52tES8y83XRglIMrj7BTPpfswqxLhWuDPHg+IVw2WCAjQcp+GDRaxtQ3wMEs2W7fwDcIv9M6v+WZYmmkJOmmYmqR23q812+EVL39USDuSwXOfW1QMTOgYbgDjF0YMv/YbbSKGkosLTnOkdV3NOjsftXyTOuDS15T1QK4SSFwwVHvmSVrg6jz5IzfkUHhAOmEA3AFT12M3GDtgxa4LTpE9y7D1dWWPYpuu6MPK6EB+PVQl9bL/HDD+3Xcm5MBqZzyEG0Mr4e/IEbidXLa0zA+PyMCVvxtV9JDudW2hYE0/IfBOOnHD3axRcyMNuIvoFV/HzfjtaRQwg== 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=LruUyHGx4otHaBQ7FNiHJ1DCkL7YZ+thL2LVuvJpG7s=; b=H/LJxMZm4fDpnEooCHbalZClQk+N4x/tlWoP7B3zotOkRp6hFbWzhyHMkixW52GrorF94LEIaJ2ZBMHU/+oX22yHJxHSyHtGkISF0zv6P64EpZ7D/Us0btLRQT4S3GbB+wKcX50Hx/D7yIdsWeth81IcDlzuwyylWAWjHKdyEBo1sISfOD6Dg5rdNLtD5epa49Vs7vCueoEdZk3EnLAatyXQRcmJoVeByTckVZEhlGvOCGLJ4YcqGxSP3CW7bKnpeAMj5avJboZEXT5INoNxLd70GG7YqTmQQJyy25vMyZe83o+Zuyz0DzFvRU0O2yCL3GGSiypEOytI7XBjfFBcog== 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=LruUyHGx4otHaBQ7FNiHJ1DCkL7YZ+thL2LVuvJpG7s=; b=f/J1EwtFEWBlbp7uFEjguBHNgEstplh6syovjzggOw9J3Ua7aNbLoLljvLlN3ySITCbsUWrMOliXMNjT08Dd/RqpzqAK6WF8ktQvWwnjV942+Ln14qkAvEEumra+AY60zgpNXZ+i/orcnWvYAv3vs/e+cc6sL3zXUj9pfPYLkX8= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM6PR10MB2923.namprd10.prod.outlook.com (2603:10b6:5:70::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.7; Thu, 20 Jan 2022 11:42:44 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6%4]) with mapi id 15.20.4909.010; Thu, 20 Jan 2022 11:42:44 +0000 From: Alan Maguire To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org 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: [RFC bpf-next 2/3] libbpf: add auto-attach for uprobes based on section name Date: Thu, 20 Jan 2022 11:42:29 +0000 Message-Id: <1642678950-19584-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1642678950-19584-1-git-send-email-alan.maguire@oracle.com> References: <1642678950-19584-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0358.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18d::21) 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: 9715f633-35cc-4332-4fff-08d9dc09f639 X-MS-TrafficTypeDiagnostic: DM6PR10MB2923:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1284; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QUN799/+GcgTbdrhF2VtQMVx1/iDsu9b42g65hbPIREu/TTS8uiAhw64pp5eH4o4oOHC/nEQkgVFoSfIe8Qp6CebsjCAcBd9driBo2GN27LSKmYm4vkoNC/IaWhzJwMS5kZvH3t0BNIKQhB9d+URNrLyQqqZ0PwqmuJk9Jno7lQfx9JTnCUzwJbfK+UQyV0zzMx2WI0esVUEhrpPRs2O8TXlh8sdoicZ3wWeSUgrZH2yUKJg55V4dVT3Ks3uQkCUw3KG07nK23Z+rUWwQLjgFhtoPnpx2HPUcV9yUwGFR2p+RImeZR4CFkY4PUB/O4HijMTPkkPdlpV7vzpE/yyJuhGizIlDVwaknW2iQpXYdQlKCnB9VyHc8xYAU7xa76akLOxTxoesDOQA+lYS8OjgoqRfTCf0WSN6kqNiRQqRzy4aJGTRhawOiakZOkgYZOklVR3A2WxaQ3E22FFyFu1q5jyPC8kKaiO3KEjAmLPntCFfmty/3DlLkIC/gPoCaJ5yQLJge9rqBPmGv/ftrVUuxREKou1jONCe1ccHnSQQA5hUEXCSPD7awHhkTOUmG7bHJIkv9ztSKVHByOozIGq4p3AVL9JgfbSWarGxxTAZJCxR/CySTy050GX/4azbWc2Ocryr2bVOIx+3Bm7waaf02nIhIOlUri2SaRkrv+3zeKU3D54drQTCGMPMUKutY4pkbRNk7cTKmaoubJX5ostIDg== 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:(366004)(316002)(2616005)(8936002)(186003)(86362001)(6666004)(6506007)(2906002)(44832011)(8676002)(26005)(107886003)(508600001)(36756003)(83380400001)(66476007)(66556008)(66946007)(52116002)(38350700002)(38100700002)(4326008)(6486002)(6512007)(5660300002)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gs7+mLseZMCaLbRgKPe++ttKmCjOO6FrQHxNhpe0IqCO6hQnoqqvo74Wgc/QWZFBgLdR3QcmjUIWhMk8JHLyy02IWizsHQHypbrgXKkTs6Te6nlEX2PPo/q2z6N/BwCtVLaJwvjST5G5dx7/t8UmMzwqP7cF2EMLyAmcc6BMicfntVPWkRP4xYAUxOLZU0dnqjz1+R+vMfwuHFy9nYxXVp48YDyaAeR5m7sC1s9+ZiMGlWbxTltx2jO3cQKI3m+bhTGj9dH3Z2Xa25pzKzskWvtsFKKvf25HY1xE9mggmT19M2UbqkY0pAfekYPRly7drN+l4pMSPTtnzO7OgnWMqkL6V1OFv4udkWCjRJedfhcQMWFs3Pbfrvea5F5Kkxx5CiXHp3NZCTCidhpYWMGekVYMPZFf9canKMH1p3A3AN++8RQ40SJkrj1v4oj2oxElboens1Ohc9R5NX0NHYsn5vy8N77ABt+6PV4bYJEfPz7EfIuFPIkK/LDGeiYq5ZHwPVworsszZaows/xdexoC9Y633tRF1FIo8jWBXjtt8F1HE3lm1hQW+HKbYXZucwZ3ucuQ+I5+j7pkXnAnxm9BJnTpp0ZQWBa36Cd15YuBNHFYN6GR+MGc9HlK7kjBZY6MbcYcpsya/HqQw/j5EqgYvcME2zTqWoELxNRw/CRFikV57FRSyuDoHpcLnoc1BaoK1yWuryLzYq7zKSrbTFT/LtFn/0zydhikXZz1IRy+BfYPBTcPQxFHIZmpoWs+BBqrPDZHEX3G45fCP58L/Eho009Rm/Ey7+5CxGHE5ZL7tbxfUp+aTLyZVyMVdDu2UD0Dy/OzgW30UkGNMmXYiJ1HPpPMspeGUdciDuVx2Q2e31c6HFbqwmngsK9OzQFDA9t7K5xBcnx+e36z2ys2pGehFEBC2MowNvz6QJqTUe27ETamGoQsp94qK7tsi7meeoUyMtF+CsgrK93c13ZdFAcuSsr8prK6xBaCxTBqu5DQsqQJOlh1t6myUcgESudABGA571R1US0oyEBZlwdBYzKq1vYAeMHiaIOk5kUG6tB1DGwXfzEf4tnR/+4y1kwKtqTOctvWgw02oIhk+wkB03rHb1CPdShdJ9Pa9gVp+Dvk9Xb47jtAtXtr6bqcIivA6OHztEXMQfgLmgIyVXOwLLIxbK0c1sxhShxWNdQyvfPwj8gcygAd1GpeACF/nLG3hCm9ORbb3IieO9yrVNsiYvJJKPpivA1uiUdiwum86u06JRrwjF0wumzbj2c4McaXy7kXDU/nTXOP/bGxqqKi7KMKHdwTOo7ayUm27GWyng+5ey26BzephihgpO5bYGw5QA9InANufJRKhyBdBVG1UAtecLEyQkyLk3tHHRbtx89yEO2sI/MIFC8p61JIoADNY3R3mSC8SgpHAa6xbf2B332ckceC98sPVHUYhzWIy7UplloiGwVVEctjY4CTMitJw5IaaFMiSC/+8WC1qtiej3fMs6W179n82lW6tmy/zLMLTMGha4kbtIxtpVyG7ocrvzFKFwmAE4fi62QNivWWxh8Qoy0JAuCt0OxshcuFPI9hkkswk052RNvcBn50SGAhq+V1FDziQRucJ3yaKVBZf2osH23w8YjzsG+/mGyzvhI0WKQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9715f633-35cc-4332-4fff-08d9dc09f639 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jan 2022 11:42:39.5278 (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: Fq8Il5OARnPdte+wuKJu9ce5H8yLslU9zsuLQlLdImTr2tNdSS3avJyoXMnmqM+ENd55w/8NxJ2gOO5L/Mz8IQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2923 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10232 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201200059 X-Proofpoint-GUID: xdanE6X9M2Ctc0oFpnqojpnO-e-sYHPn X-Proofpoint-ORIG-GUID: xdanE6X9M2Ctc0oFpnqojpnO-e-sYHPn Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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/function[+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). Future work will look at generalizing section specification to substitute ':' for '/'. Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 6479aae..1c8c8f0 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -8565,6 +8565,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); @@ -8576,9 +8577,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), @@ -10454,6 +10455,61 @@ static ssize_t find_elf_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]; + 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; + + /* First char in binary_path is a '/'; this allows us to support + * uprobe/path/2/prog and uprobe//path/2/prog, while also + * distinguishing between old-style uprobe/something definitions. + */ + snprintf(binary_path, sizeof(binary_path) - 1, "/%s", probe_name); + /* last '/' should be prior to function+offset */ + func_name = strrchr(binary_path + 1, '/'); + if (!func_name) { + pr_debug("section '%s' is old-style u[ret]probe/function, cannot auto-attach\n", + prog->sec_name); + return NULL; + } + 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); + } + 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, From patchwork Thu Jan 20 11:42:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12718565 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 02CF8C433FE for ; Thu, 20 Jan 2022 11:43:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231969AbiATLnJ (ORCPT ); Thu, 20 Jan 2022 06:43:09 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:46764 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231917AbiATLnG (ORCPT ); Thu, 20 Jan 2022 06:43:06 -0500 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20K95NJP010655; Thu, 20 Jan 2022 11:42:51 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=8KpIfaUV1V83wTyXH6nruUoLhygLaTvbNRKodASm1Fs=; b=KmFSRHmFECktxshVCMLpkB5yU+tqwunJhQsYFhLEDzpdc5cG1Wvod8j8vX4V+7bgpH1r +/LZRnGZS3jxHqWflADw4YB8TCqD/i68VxbzZ5t/5k+KJKeapF1lAhuhJppix4pXoKVf 1rcNRDTbpG21z5kPuB1jrbZQ4GD05oPuDttmspKAwOP+u9d6KalGiQ36w7UF10+sZAG6 tyEiBQxCJdon5uatwgbHKHNwOfvLOOT6077e5iTLRdEbbMi3j4fg6r1sfaK/sV2RJpbJ h0hJwOqFbe5KHCmMbpvW6YjBj2dUCrhpntl8XuntYs6dHLGrsKSQu1ndkTYBQAM1DYaV Bg== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dnc5302fm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Jan 2022 11:42:51 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20KBaB34151224; Thu, 20 Jan 2022 11:42:49 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2171.outbound.protection.outlook.com [104.47.55.171]) by userp3020.oracle.com with ESMTP id 3dkqqs9385-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Jan 2022 11:42:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Tsdc9uksMLx2l/tY1kWIspRxkT4e/Y+IUp8mkRm0KdQJCMmo4vsBFMP5FO2Zhd/KEBCLufhrA7BInq/wgZu1ZxL+fRQ9najoiMgIbSsu1e4k16gbckUCuXBQNGVtPE/QvNuGGimtmv3LRnZcHggaGmQYI4/uwlmhb85aByfiCRo1+SAIZUIawBT+7NAogUvQSlV4uG42uDjuFNKAEjdw4lTYkSeTlHrd7CaKOUjFlbneoE5GVz4c4iG4lKXkhuRUDVdsn5Bgr0DYyzokhODQqfJj99AKOquqAUyUX1+H9Av3vheR9d3PEnoB82wrg+JIefXgVw17yQyQu2IgMVjhdg== 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=8KpIfaUV1V83wTyXH6nruUoLhygLaTvbNRKodASm1Fs=; b=Yk2sExb+zWzXMPZ63fRREuS4qaBJgQKcYHf7RHPJhiAbu3ObetKrp4xKB3uIM17lQg6A0wo+1YlYC6kK5SBh8Q4AP35x2AlkSGkIVszrEkdw+e/ywnXeJPdaPKq2TM2nsgsjRcN4CR3HfnjD0UcUol0X8nTXIaqzeZAyzuY/21NbdOIgfWmx/BJq5wz01gJydY4//Pam/2oD4luMXBjkyQjsmuQSLsac0dxQQTGB/2WXwTa7aN+xWn0GDVsPTOLAr0cYjARhY/a55xxUOgKlGCy9zKWncW0WrnqkpcYJL04JEo3Jd7wS5SDnBScJNvoyF+vN4sH+7X1WmzylVQikgQ== 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=8KpIfaUV1V83wTyXH6nruUoLhygLaTvbNRKodASm1Fs=; b=r8DE+8wyG+kE2Xxgd4MDGNV/IfvO9Hqt0wEO+ZfrkIVFo1B5cfm8iuP2aJYzc157fbfWZFMyYW7GQhOqe5y3MFpinOUMwYaI7STSOHUl7wmNVLhWlpMUiVp5tW7f5J/KuZ6mlqQifNAD/j/XnEsajZmnKTu09aBXrz0Jd6DPVXw= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by DM6PR10MB2923.namprd10.prod.outlook.com (2603:10b6:5:70::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.7; Thu, 20 Jan 2022 11:42:45 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::49df:513e:bdbb:33a6%4]) with mapi id 15.20.4909.010; Thu, 20 Jan 2022 11:42:45 +0000 From: Alan Maguire To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org 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: [RFC bpf-next 3/3] selftests/bpf: add tests for u[ret]probe attach by name Date: Thu, 20 Jan 2022 11:42:30 +0000 Message-Id: <1642678950-19584-4-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1642678950-19584-1-git-send-email-alan.maguire@oracle.com> References: <1642678950-19584-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0358.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18d::21) 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: f5e53557-660e-45b0-0163-08d9dc09f753 X-MS-TrafficTypeDiagnostic: DM6PR10MB2923:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:234; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: j5qmzOafIqsZIWNT2xlCz5xj0XLDA7G+VFsoz4qfJWuMiVQyZvgx1wXrlKtdyyjKgWHCsmKBpnhR/FIq9HyZYL51hqT0aoYOyzFskUaWjMiDdRuFsU/l8IuUpcu6RrPMNefhmkeH+vUA+6o84ksGKFX3Gg1lkkT7yBzlhGOY25IGdpplyuYfFNFPLgsPLPjzKbcKpg2bzdYAd9Gq60ClwKAWF4DIveTB7kCIwLfjqSVjfCm0dtGYmZUcXTazTaPFxs164p82mXH2iK3SgWKKTMyiVRktrtMSp9lcIaa+uW/bSn3OtOIUN0J/s0DCqGplXYAXIRHh9TCvDdneVrd+gmathbspHStR/HxKNKK9gIkW7FBC1rxRHXvgbqpiQr+kzmZEAxQ6UbaaM49BUzh8C31uzlFiFx17Gwd4ckCQQ79Qwhx/RYhK2ZGITzU96B+/bpD5vlyFPi27oCsY62aqGr4O2KAhuNVueqE3o37IL1s/+GnNt1c+MWw170Cd7eRW3/zZ05j1/Kou8jU3f13wLYfX5TsKQT4/zaXLuMGbySeauKpiLaoOJArBv+dbLdfjlL5FfCInHg+bP4pNPtw6xeSprvSVutfjRzhHnhndG4MlGBDYGOiOTZ+lyQasMRWFNtpXiXo89DqzL7kOLOpjC/S0ymyPezsnqBqmriF1C3lRIfGo8pu/TFmjl9/3zm/LaBF6WZe9QYWjcVP/BrXJlQ== 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:(366004)(316002)(2616005)(8936002)(186003)(86362001)(6666004)(6506007)(2906002)(44832011)(8676002)(26005)(107886003)(508600001)(36756003)(83380400001)(66476007)(66556008)(66946007)(52116002)(38350700002)(38100700002)(4326008)(6486002)(6512007)(5660300002)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4KczZzymyz8bhsQhWou2MXWSgVibLNJB9mGBskbv4B07dchfJ/ZqZswTpnhmWHovZ6/DmqiAA2H4jR5gE1U3SyV8nHXaNx9PgagL891g4U0PftkZUSVbL10jqbKRCwHuqGFx0/ufvZTzIsZQG66sliMoIedUMFkXqQsmfnR1SAnKTUDpiUOsQK5Voi2bmCB55XjfyIesjMpTuSjF/2PZxieVKeBzCzt9VxIE54nvgU8bC6Db6WYA5vd12IdSWDNoP7nZqrqwW1EqjRNIGtPhCF6YTt0mfATwyaMKZqQzj3pu3CMkjt6s4UMiWLkdABO/Enptac3Alj1dhvn1RzxGTrq2r+MyKmGRYjEAr3RvNLliiu9L3RTef4e0Ch/LSaFe5CLuVsD9uhz2d5KjNibBZduUvHJs3kl2YSyP12akpJlWUMjzwCDZqiujSOCiireVU4RB3/GDWCWu2ZbZwC/uHffEL6bmJ3cn8IL7xRixGC3xiMUF8DUePtX1smzGZP7+1EOcUUIzhmGZnvyllgVT+a6t6u6vs2b5VZD2ScxvYrnoAUG7wxJ5qywymncsJe3k36YxMPljMa4QtKTvb7iuDZGgFlk9ZJhQHOMUbbRNirxdMY6mnemVbgpnrCW3RClyOntOSAj4DUvOh3SM/IcpqTWwYAwE00Z4Dx/382eG5pD8OwUJNZ4mIpW7nIBZrz2LV2RxWYN4ToCucDIDAHcasRi94JBO1uiIsrIwXqWrVaiO7Cl4aY0hddUl1XXPiqnsn4XQY1gnNtqQM+6vl0zsAv2b7PhJCHcbbQQRzIsN3EDmjHrQt1hGjyXsH7E5AIeHzt1zzh6VglpudJ2wO5bso2fGHCoU7H3qo3gUCZFaShmpWFiBr5G4kzjgyEK6vUyZexsVb9L8whkpzkimc/6h4QkuC9twJ5pFjJozdmrTjOYz9FjXPiO7MM3UZs7GIv8lGwj9rYZr77iOxpRo07tQO6vpbnmOHbZ/oPCiIbszT37qMEBU8fwPzucnQoSySAgVvF4qOBUjILReqJWLY9gf9C8jOhhlchdvLvnoHFKFpqNh1fUywitHOu80vrtXMHqmeUcpwuFP5GLmmlqhnomnwSbKj5w5Q4Tr6Aq6VASAxa+h3GZgOZ2fthpuVZY7/glWzqJkuLZv3sILu61HKTAKn9xEwPKMczTsvT3ddrWnNZBKcVaSjMVzYXnoDV5MrY3Fdv6V4O2fiBHWew2H//U+KkWmskyfnTNPHr9/7tCikSGF7TV4xn9V1+cYi8JeHItGENlx6TSXqb9i2zpBQ26TCuzy/SB7hVG64f/kod43CZsp1GqovRz0QDpShjIRBZPnjtSNyEp+i8EO0Llvtn5OasTRpI8X+YFEsWLOwoGpMheA0eDzBQLNxeBKYEgE7xDVmb4JxNxY0bPxYmjKJTYJ2P3gP4Ir9jSQNnCGugkJSsiMk5olz+rFtKw2ksXcNiH7mbgrMRP+xURfbLPhKb1GA9H0aA7BEwey7v9HnM3uL/rIjXX7hWlq1F2Q/RsZ1VcwQ+hLEmSmh05T0QsPpUPERiaa2QGkFJdcOQEc6DlRqvN23XD4OAgTmkEZNWEiX/8EAm38y0ZrPt8SZDdB+EH41wj3b9MPz3eLjdgshlvgmSk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f5e53557-660e-45b0-0163-08d9dc09f753 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jan 2022 11:42:41.3114 (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: M70sImw3SeaAQroovMcOP7H2JYxfuuXnQ7831AL0OwOlKWWlIvhYV/H8TneFzlpWojFRakIj2OF+MQtKkJSTlw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2923 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10232 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxlogscore=999 malwarescore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201200059 X-Proofpoint-GUID: edEbTGD_MfCsFd-Yw1AWBKa8dE3mYJqb X-Proofpoint-ORIG-GUID: edEbTGD_MfCsFd-Yw1AWBKa8dE3mYJqb Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC add tests that verify attaching by name for local functions in a program and library functions in a shared object 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. Signed-off-by: Alan Maguire --- .../selftests/bpf/prog_tests/attach_probe.c | 114 ++++++++++++++++++--- .../selftests/bpf/progs/test_attach_probe.c | 33 ++++++ 2 files changed, 130 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c index d0bd51e..1bfb09e 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -10,16 +10,24 @@ static void method(void) { return ; } +/* attach point for byname uprobe */ +static void method2(void) +{ +} + void test_attach_probe(void) { DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); int duration = 0; struct bpf_link *kprobe_link, *kretprobe_link; - struct bpf_link *uprobe_link, *uretprobe_link; + struct bpf_link *uprobe_link[3], *uretprobe_link[3]; struct test_attach_probe* skel; size_t uprobe_offset; ssize_t base_addr, ref_ctr_offset; + char libc_path[256]; bool legacy; + char *mem; + FILE *f; /* Check if new-style kprobe/uprobe API is supported. * Kernels that support new FD-based kprobe and uprobe BPF attachment @@ -69,14 +77,14 @@ void test_attach_probe(void) uprobe_opts.retprobe = false; uprobe_opts.ref_ctr_offset = legacy ? 0 : ref_ctr_offset; - uprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe, - 0 /* self pid */, - "/proc/self/exe", - uprobe_offset, - &uprobe_opts); - if (!ASSERT_OK_PTR(uprobe_link, "attach_uprobe")) + uprobe_link[0] = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe, + 0 /* self pid */, + "/proc/self/exe", + uprobe_offset, + &uprobe_opts); + if (!ASSERT_OK_PTR(uprobe_link[0], "attach_uprobe")) goto cleanup; - skel->links.handle_uprobe = uprobe_link; + skel->links.handle_uprobe = uprobe_link[0]; if (!legacy) ASSERT_GT(uprobe_ref_ctr, 0, "uprobe_ref_ctr_after"); @@ -84,17 +92,79 @@ void test_attach_probe(void) /* if uprobe uses ref_ctr, uretprobe has to use ref_ctr as well */ uprobe_opts.retprobe = true; uprobe_opts.ref_ctr_offset = legacy ? 0 : ref_ctr_offset; - uretprobe_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe, - -1 /* any pid */, + uretprobe_link[0] = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe, + -1 /* any pid */, + "/proc/self/exe", + uprobe_offset, &uprobe_opts); + if (!ASSERT_OK_PTR(uretprobe_link[0], "attach_uretprobe")) + goto cleanup; + skel->links.handle_uretprobe = uretprobe_link[0]; + + uprobe_opts.func_name = "method2"; + uprobe_opts.retprobe = false; + uprobe_opts.ref_ctr_offset = 0; + uprobe_link[1] = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname, + 0 /* this pid */, "/proc/self/exe", - uprobe_offset, &uprobe_opts); - if (!ASSERT_OK_PTR(uretprobe_link, "attach_uretprobe")) + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(uprobe_link[1], "attach_uprobe_byname")) + goto cleanup; + skel->links.handle_uprobe_byname = uprobe_link[1]; + + /* verify auto-attach works */ + uretprobe_link[1] = bpf_program__attach(skel->progs.handle_uretprobe_byname); + if (!ASSERT_OK_PTR(uretprobe_link[1], "attach_uretprobe_byname")) + goto cleanup; + skel->links.handle_uretprobe_byname = uretprobe_link[1]; + + /* 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. + */ + f = fopen("/proc/self/maps", "r"); + if (!ASSERT_OK_PTR(f, "read /proc/self/maps")) + goto cleanup; + while (fscanf(f, "%*s %*s %*s %*s %*s %[^\n]", libc_path) == 1) { + if (strstr(libc_path, "libc-")) + break; + } + fclose(f); + 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; + uprobe_link[2] = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname2, + 0 /* this pid */, + libc_path, + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(uprobe_link[2], "attach_uprobe_byname2")) goto cleanup; - skel->links.handle_uretprobe = uretprobe_link; + skel->links.handle_uprobe_byname2 = uprobe_link[2]; - /* trigger & validate kprobe && kretprobe */ + uprobe_opts.func_name = "free"; + uprobe_opts.retprobe = true; + uretprobe_link[2] = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe_byname2, + -1 /* any pid */, + libc_path, + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(uretprobe_link[2], "attach_uretprobe_byname2")) + goto cleanup; + skel->links.handle_uretprobe_byname2 = uretprobe_link[2]; + + /* trigger & validate kprobe && kretprobe && uretprobe by name */ usleep(1); + /* trigger & validate shared library u[ret]probes attached by name */ + mem = malloc(1); + free(mem); + + /* trigger & validate uprobe & uretprobe */ + method(); + + /* trigger & validate uprobe attached by name */ + method2(); + if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res", "wrong kprobe res: %d\n", skel->bss->kprobe_res)) goto cleanup; @@ -102,9 +172,6 @@ void test_attach_probe(void) "wrong kretprobe res: %d\n", skel->bss->kretprobe_res)) goto cleanup; - /* trigger & validate uprobe & uretprobe */ - method(); - if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res", "wrong uprobe res: %d\n", skel->bss->uprobe_res)) goto cleanup; @@ -112,6 +179,19 @@ 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: 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..c176c89 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,33 @@ 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/method2") +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) +{ + 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";