From patchwork Fri Mar 11 12:10:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12777916 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 67BC3C433FE for ; Fri, 11 Mar 2022 12:11:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242868AbiCKMMa (ORCPT ); Fri, 11 Mar 2022 07:12:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239673AbiCKMM3 (ORCPT ); Fri, 11 Mar 2022 07:12:29 -0500 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98DB8122227; Fri, 11 Mar 2022 04:11:26 -0800 (PST) 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 22BBqp45024124; Fri, 11 Mar 2022 12:11:08 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=1zVQH+FuCIgl+lwGZ9pO04mlfe4Ad+U3ZJafV9THhLo=; b=Z/9lweFccaj4ye5Wg/ha6bVOZxTiEd6eITGOUyFr2LZYDNnMQnBxv7wnIrOu83AN6soq 7kv7gfXgUcIcHfsfIcmNkyaPLgipNv/WwHfjCmva1Yl+Ilbv8qhu3Pd8W8QCMz9UL2oD WXCNCHaRcsZWWGgjBtDXbGhqLFgx6AzAZVmO6hRsiTKPnklnSwSxZQp6QZpg4aXb5FiR /k3GaowfZmWOuJ5HVwAFfz3ihthMGXzbp6bueTvCt3/Mwo9irOVq0lXFEdlm25xdkWhq ajXzlJFRtno5d1UyUp/M1MQ19AasRMN7yURqWJVLB7nHQ430/1vibpHpjejkSBV5K9EZ pA== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3ekx9creun-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:08 +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 22BC1NZh078598; Fri, 11 Mar 2022 12:11:07 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2104.outbound.protection.outlook.com [104.47.55.104]) by aserp3020.oracle.com with ESMTP id 3ekyp47eh1-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:07 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZtjOYZANfIOq8vZF833CO40vY3y1HIB6TCjfpMEaEmLMZLYL8J8H1CiYkn+I0m27oCDsbYB+HigxXZbyEmONVRgqe9vADGFpJUvRj4lAeJ+kEb1s9QtKuf3DcxB1erbJV6V1HgxJKCWtDSBER5mqF7hE9PAOhPzyfznbzHWGRySCzAp5nELKgp1r5wCkwPyoPgGxfI4hz1WgEcwKqYG61g02dP5kyIgVXHRMjtodFwYusoQRq4GTCOToSCpFSYWg5UM+4Pp6ZrWyHOUP9160DT+U9SJ/qtDJqBt22mCfwXbsJ6+WQ7XLnvIDnB1J/93cIFPZJWPEpq4r1C67uZNFWQ== 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=1zVQH+FuCIgl+lwGZ9pO04mlfe4Ad+U3ZJafV9THhLo=; b=SklV4TrVtBHrVpNh8a0ENfKmGZgcOzm3BSt1eDo/JdhzSmu8wctSwtEz/sI+ATGTZsGcxDScQO/ZGfh2TPkr2uyJ71lccg+BRee3I9v8Sa2PzU5I5pnkB/jpwoqBNLgYU7hE9Q7+aRXftN0J5atCZ6oC8ADLFvaLqlk2DPWsp1GunIYhUTCCAfcutrKDQ9fFQVngHSslkKKPw3+vivgMtKGhiQDRw0eL7zDlvVBZ0Y88BjzmAlsPAtDJrRz8gy9kPBzA30mda0nd0In3nZ+TM0u/Tg9B3/T39dpoEvjEfpZW79mowPWFRkzWBPpsm3i5mSrMu1iRvdpZ4lhyKBNDmQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; 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=1zVQH+FuCIgl+lwGZ9pO04mlfe4Ad+U3ZJafV9THhLo=; b=k7AZoOvSbafFuVdxgUNmcE46hnrO+oGRhWd0/crDcsSJFYDKpFFheqXf1KYFhBd3FOs/R0HXSMPlayD7S/8FIQ01llqm+D4UjWNalBUdHihzAc8t6smZrpMfl62zbDnblZupfZYARsJMqVjOSL89W2eqFGVzgDLwfrhi60CTt4E= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by SJ0PR10MB4784.namprd10.prod.outlook.com (2603:10b6:a03:2d4::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.14; Fri, 11 Mar 2022 12:11:06 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931%3]) with mapi id 15.20.5061.024; Fri, 11 Mar 2022 12:11:06 +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, toke@redhat.com, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v4 bpf-next 1/5] libbpf: bpf_program__attach_uprobe_opts() should determine paths for programs/libraries where possible Date: Fri, 11 Mar 2022 12:10:54 +0000 Message-Id: <1647000658-16149-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> References: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0335.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18c::16) 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: 98e7a5ec-1773-4d25-9082-08da03583805 X-MS-TrafficTypeDiagnostic: SJ0PR10MB4784:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: et965yZQuftg5Tr1E4eAeF9rrBsYRnxO9w5rC4+Tk6Wqdt4UzZKIriroWtzz8LflbEpOivVzY6Tpl/51Y8UzsnNnO31cOadIegN9TLndxYl3CYGsZKmpicjm8Czr2JceEn+hqC6U3FOwECQjXckOUJKWKCJm+rLRD0s9qkmAJE1QSuxmFnt6ULZuP4jBZS0N3JKrmnIzagnuCwRivOzLTywjWKR3Z/KA87i/8Xv2FhEaOmCWr3FI0jL7jNdBrLJaQEwZ7D+gGJwSs/cp2rMLzMe5tQYKM+/3m85RFZFKjsvRRE4KF6MNUMhh/xH1OiWi/dOOipKBS77PbNy9u+EXtOn1h9XnhxWzUW7gkLbGKZ8odQ/ym+9ghdkXtKQHqtmhmavBXJ3GDoHUQB4Dcn0u6R++K/eNE+5D77H4WuEXTVbte6f16fw/0paycAtwBJcArpM+E1zfmiaMRKW/CuzZ2GeXZBQ8cB+IA7OSQL9YKN8g5Q2gmdbfHrh9HWQI9ApckJAe/v/wcZKq4l9KspHxsz7vnGKLR6czuxY1BKS7PEZBJd9KFmI/Dm4AVU3j0Ty2qzhH44WABH9MVRxOsP5A0/bQEpFBMKieSpokO7QQ5MxfobvzLp+CZEWiNCEXVuzQNHgMV1+UXenw4bPvCdMWs4YTGxTppsch06Z/ssC30RitBBhigoOeaJz9J9KToxWVZLbFxSlgO9E7H7tKt4pThhdBlUeCH72WESHB9ECbADtgLuBfNoVB0NldDDowPyBK 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)(38350700002)(2616005)(66946007)(36756003)(7416002)(83380400001)(316002)(38100700002)(6666004)(86362001)(508600001)(5660300002)(44832011)(6506007)(8936002)(52116002)(26005)(6512007)(186003)(2906002)(107886003)(66476007)(8676002)(4326008)(6486002)(66556008)(101420200003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: zoj7SXAiF4oU3LE0F0/tG4jZKdo0Um2t14bM1vN7149rhJPfCd/SoiNGPYIPTOqgoH0zqOZBnmz/Blg4WlNzCbx/d3SR3KGbOgvf4WXggJ/7IkBgUHUqW8ro9I9SUwt719cAggTA6RAhUGkHawYZ9Vy+WL9Jz2hJ/c8JW9kqYc41aFW17kbC5HELo5evt6k1M+/nQFsY5X/rs9rggxobgmdbz6PahDIZkYTitmbrkPIEhSh5f6sbVowB+5rJNeDLuuUqcvHjJEj/7MpNN5ZO1GFhnpugTTWdPIanlghOl35g6RAvvA4fN4aAs+CWGBYDHGRhp4yqax9elQb3t1tHpsEnrLMlZ8bntrQP1t0xIF9jBr9qSYUw8NM/OxMBuVQwnJsSxLYgTEdEDHLksllfVSPwbkNZjnOOOVpxyqm/F5BaTUfCXQNyuSSAumMwlw0z/WsgqXCS2y8SHDuCxGzvwdQiX9/ZTJzY4q17Lfgd7anFyf78ioPpWTlppKNcX2EYU1zaNwa7F+rxZwYbMlV81bF3iu8/THGzd/araB9ulPG3+4HfVdIMqZZ1hWxEI1m6b95trNNri4BHsZqj8bAGjAiwUutDdRQLZwn5xs3hthDFMSp5zpfwKIW3foMidM75UEhJpjZTuvM6P/JSjY1Zj5qUotTU5f/ItnuBfXmtXwEWYZMrIb9aXZxzrsohu41nLDs1bG+pBObCF5WpGxlB6FG4guj7hlL09Hb20iWZG8Q98VtXyQKrl+BvtXKTIsIMRXxzdhHhGrmWu65Yi0yCd9203y/npvGrOb1nHjX8QdyxIcZvn03zH6VfYVOkUiuJNux486l6YSnODpuzyPiX6UTvh3Qe/3XdjZ5Uafx45uFAhUHt8FuIjLqDUSOFJC4EhjmxL9UZm8mUSS6zjjgAwSTzzGtJ+xZe3Nv3ykaIVPERMhmey7ns2+HJNmD8bp9YxifMDNa9LB4Z55Oqj0KIArxeAclaXHDTeMhOzFpoj2asgMu0k/bQQTUQmKTGYLPU/Bmw/3bb78/PYR9W6XOXUXgMxNqd2QJUvXKK77iK+xgW2RPEgI+H0fe/6iscyAa5eN/oqsHRB/ky1PT9lkTshvkj8epe7JLYmqqMNNUiQHSg1PMNMKd59NpQsMFhtEuO/vUeDrGj+de4pvFZ+1R4VEWOGhy6qvHJc5xxfKuhmEH6tut7F/Z82wUGH+arzN+8gjSt6l1nfeH4TblHP+2NtQZNUKTh4DCLUYRMBZnQOZ4V1EA9uhd/hKjRlZQIo7Q3vm8x5MiZZ8dPTLDqsM7H8tLAdIF8Xe+qcpKrLnB1hEUTjV0NA85NGgpJULE+cJfi/Gls7zFugYNVQCwxQBkTsgzkBUn7C8fVL8GGMVCO1ogxb8RYmvCx2YCSyHvBm6CzjO73h7upthqZwrq5bVfIM/2ibq9quMV+l+W/W/o4dVRQvDWAN4f4EJ9hTeDHLQDxAnQ46Q46XFjeWPZNN6CDIB6FDzorWjHxePcyxgjEN65AftemkUt3la1PrO5uc0+JvneAGgvy4JBFqApn2oJksb+WrcYdVZOBIkYzZAGxvRymQbBhg8rz+aXo9pf2LuPu3bcCR+hUxGKWyRg90yFi5Xnu6IeG+TIKDqgkebLM5RY= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 98e7a5ec-1773-4d25-9082-08da03583805 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Mar 2022 12:11:06.0751 (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: oDg/qX1EHvicQUCzqgeROHKog50pLeA37dG9krWzTZy0jxzLYI91BR6x6Q3zEgPMceyhyvTosjO2qGL0FhmUeg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4784 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10282 signatures=692556 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 mlxscore=0 bulkscore=0 mlxlogscore=999 spamscore=0 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203110059 X-Proofpoint-ORIG-GUID: girn4a8Bz0H-Tk2B3n_oIizshzXf5r1y X-Proofpoint-GUID: girn4a8Bz0H-Tk2B3n_oIizshzXf5r1y Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net bpf_program__attach_uprobe_opts() requires a binary_path argument specifying binary to instrument. Supporting simply specifying "libc.so.6" or "foo" should be possible too. Library search checks LD_LIBRARY_PATH, then /usr/lib64, /usr/lib. This allows users to run BPF programs prefixed with LD_LIBRARY_PATH=/path2/lib while still searching standard locations. Similarly for non .so files, we check PATH and /usr/bin, /usr/sbin. Path determination will be useful for auto-attach of BPF uprobe programs using SEC() definition. Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 43161fd..b577577 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10320,6 +10320,45 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, return pfd; } +/* Get full path to program/shared library. */ +static int resolve_full_path(const char *file, char *result, size_t result_sz) +{ + char *search_paths[2]; + int i; + + if (strstr(file, ".so")) { + search_paths[0] = getenv("LD_LIBRARY_PATH"); + search_paths[1] = (char *)"/usr/lib64:/usr/lib"; + } else { + search_paths[0] = getenv("PATH"); + search_paths[1] = (char *)"/usr/bin:/usr/sbin"; + } + + for (i = 0; i < ARRAY_SIZE(search_paths); i++) { + char *s, *search_path, *currpath, *saveptr = NULL; + + if (!search_paths[i]) + continue; + search_path = strdup(search_paths[i]); + s = search_path; + while ((currpath = strtok_r(s, ":", &saveptr)) != NULL) { + struct stat sb; + + s = NULL; + snprintf(result, result_sz, "%s/%s", currpath, file); + /* ensure it is an executable file/link */ + if (stat(result, &sb) == 0 && (sb.st_mode & (S_IFREG | S_IFLNK)) && + (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) { + pr_debug("resolved '%s' to '%s'\n", file, result); + free(search_path); + return 0; + } + } + free(search_path); + } + return -ENOENT; +} + 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, @@ -10327,6 +10366,7 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, { DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts); char errmsg[STRERR_BUFSIZE], *legacy_probe = NULL; + char full_binary_path[PATH_MAX]; struct bpf_link *link; size_t ref_ctr_off; int pfd, err; @@ -10338,13 +10378,22 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, retprobe = OPTS_GET(opts, retprobe, false); ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0); pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); + if (binary_path && !strchr(binary_path, '/')) { + err = resolve_full_path(binary_path, full_binary_path, + sizeof(full_binary_path)); + if (err) { + pr_warn("could not find full path for %s\n", binary_path); + return libbpf_err_ptr(err); + } + binary_path = full_binary_path; + } legacy = determine_uprobe_perf_type() < 0; if (!legacy) { pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path, func_offset, pid, ref_ctr_off); } else { - char probe_name[512]; + char probe_name[PATH_MAX + 64]; if (ref_ctr_off) return libbpf_err_ptr(-EINVAL); From patchwork Fri Mar 11 12:10:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12777919 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 A8AB8C43219 for ; Fri, 11 Mar 2022 12:11:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344400AbiCKMMn (ORCPT ); Fri, 11 Mar 2022 07:12:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347050AbiCKMMk (ORCPT ); Fri, 11 Mar 2022 07:12:40 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F38F5141FE5; Fri, 11 Mar 2022 04:11:33 -0800 (PST) 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 22BBZcrE004098; Fri, 11 Mar 2022 12:11:12 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=5jF2kYrj8DqV5W8M0raDLkw8hX7kFHu9OdM920FxLC8=; b=C/FSc0CT5gCJfxQXtLDjMq31T6ieyux0qEcgrz3+H7l3Jhb17PzxN1qmAN2KtRUNNTYT J7P2UFdNMMXaa4fkNSaIKZR8NecMp5W/phjmYv89Gy2Pnp/fhD9eSZlNz35dfAk9BElQ TQMXdsy1adzsItO02NDNiuV6/0oXFeuXD/h6w8oQL7Jkus9s1nVVwGSRBixOjMkaTAnE o7SHAC5+6QE4nihCu/zU4Mqhhi7Ms1xGT6gVNPh2vgnaC/7VDG5VQ1tB8AcBtpBE8qLs wrrmla07o+YfOETYlCjGOdykbFxU91ah9Lt/Y3CzgUX6zRxclZHAIA1c0iJFryl0RX4D hQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3em0du8w9h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:11 +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 22BC1obs034517; Fri, 11 Mar 2022 12:11:10 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2105.outbound.protection.outlook.com [104.47.55.105]) by aserp3030.oracle.com with ESMTP id 3ekwwe2s0h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:10 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CG+LeG2GAKPFH9rJKj0iP93/kUXqzjRcV3WWmUirKRAZpNB4tvQLgDwSTSCz4fCFgVMokQ9T9ZsHPC2iX/JpN25OOdXTOgEqyOOqRkpt9eC4rsjcEyrzF8RjfPkHc17hkg/y+53NvKyaNPpeRuLuZt6dzgaIqXckWhUbEwJu3bOH983tGdBHxZQ3TG0mkVwcN5t0whdEmB5IH55j93L1/zcUf3V2a3b52jQyLqMCx5fPBq3hiANns1Y7Guys6jHILjUijdcP+OylxLZf0dlv/uDLqpq1d1gqvQo2280q29pg/X9mnpyzjax01W5HJXl3ymZPhL/NZDC1XVLCih6moQ== 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=5jF2kYrj8DqV5W8M0raDLkw8hX7kFHu9OdM920FxLC8=; b=nvzztSMizKCsBkkein7Gy0Cx3aipLXdf2F/rOcNjqXBqq3yrdUWqPkTTvLNB8J79eyq6g9sOmMD+Uze//8Xa37yOrzEG33LzCNoX7o0F7CzqbnDNil4AJY5ajV1TKnfu4Qdi+dn2aVg9p7SCj92yuth7TDywAlHLy48bNkAFljdcntpWJE1xdv137KvfpnhUM+52o6l5XuUDVQ34EH3LKKCVys4TOBDCfqAV9M4SKvOZMx5pKaKnHrBiWmfNumysKNYwYud5j0OTNN6McJUk4EOkQR4Q+8p6zWPRDv4eFRZnCkDIEoDCl/J2IhXwVkVrdoup+kH576kk6RY1J32jOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; 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=5jF2kYrj8DqV5W8M0raDLkw8hX7kFHu9OdM920FxLC8=; b=d0ztgn3TKWkEJnjUZzN5+k++jf+/IN0+SzPMgdr2rFNcoB7n+Jnd+TJeltf0dD+7NXYoZU56bhg8mwCJX9XSDg1CsIQQI6hLpBaploWKjFuZHs051pvV4sdLKJ7oO37zoPwX3joZNXY/+sKqqoM1sHOwMQSl9kk2DglhpS6ipeY= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by SJ0PR10MB4784.namprd10.prod.outlook.com (2603:10b6:a03:2d4::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.14; Fri, 11 Mar 2022 12:11:08 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931%3]) with mapi id 15.20.5061.024; Fri, 11 Mar 2022 12:11:07 +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, toke@redhat.com, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v4 bpf-next 2/5] libbpf: support function name-based attach uprobes Date: Fri, 11 Mar 2022 12:10:55 +0000 Message-Id: <1647000658-16149-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> References: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0335.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18c::16) 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: 78e706df-09c0-4aaf-3c51-08da03583921 X-MS-TrafficTypeDiagnostic: SJ0PR10MB4784:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eshRCV3l1+YVnh0n64/GSwnGazUDI4Xf0E6AHPo8/1FRp6+NtCtD5vLWBRGenC4+wH8vA6rJoh3vu2PUfMYLB++3vfxlHJXq0XaTfXv98ILfAzJufrV1ybvxfRnVr1oAJUNeD7HQ5x/XdcPX3KWSg+dfq2c3dSU6KN4KvUqjM7Yi4FEpq/jai4jfZn77RUJZhF5qbtgJtFC3bf3CmrlJ6BDZSZ6EzUc+nywWJdNDsctyoMqnXgJMQZ3s5IW0LYbjo3NrUfksbCtKOvtsmL6Z1MBF2TEhf/h/R72pgQDpgFHZ5+pEHxrMUPc0OeW/xadcrlKTzSK8faVnixw+zx+o0TVr18PDFFauc7IKwX46XiZ6FPukzwXzHACDBdfMQqk44v1z6iC91SY4kQuSmaP0UVp2WEqwxRiwFXkggRsv2N4BMLT61bE4RE+aZFAoLLpqXu8urAE4RMHgKGAte21tBGML+hZ06XFp5i/mifq/VhB1YLrFXQ06v9pySqh2i8G7Cue4xGf4lBKLuk6bQLgbfoNH7n7ib+NoGJOPOoK7eYB2MoF9cGmpdbvBVZugyZeAcg8Nkl215Lptelhch1upi+bi7tJSYvfRxu4oFJI03iunztZkCBBmwOlSX5Ye4bON04iRLtpn5RurF0zCFCE1gm4dC+Zc2ZyVl6oC1azEoQoKv1GA6uovKabOE+YXSNfq04LowBQNWmzICBI5IMCqo6AhYCsM0nLTSSI83fsk6OHLqvRAJmSxcYAaQYH19NMfpoC2VAk9YXlg+WogHcSJjH1sZnPC4S+yf3J0dCY78VY= 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)(38350700002)(2616005)(66946007)(36756003)(7416002)(966005)(83380400001)(316002)(38100700002)(30864003)(6666004)(86362001)(508600001)(5660300002)(44832011)(6506007)(8936002)(52116002)(26005)(6512007)(186003)(2906002)(107886003)(66476007)(8676002)(4326008)(6486002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SGa/ad40t4IYk3OpfIhHP7D5xP62RuZsQ7m8dh5s1J6b92qsurNGmeDBSRDjMUwlLsDuyDYGReEnKLL8u5y0vA0ja0dY+twD65lEjSIhJXAYSzylPl39Jo4e2pLx6cpg92JZHQc3gkF/C7+p0a2dmpu5f70kJqNM5pPpTYNwWHrOcm2A0n8Rl7bcn/9t0jBvgzFnya6kkpD7Wr22zY5h0i73tF75E7h7nfM5F5ggeIT2lLaT/7V/4B7JIY/MlGJZcwB3H1oAWAEAlsKrqo08Fthrhx+ThQg7OXAD6vzeohL7953wsbSHBMk9rK64nHYi4f0P5aeT+OH+vwx0u7hgcI84upd0Th6OReAEyY/753V7dvrLMqsRk9jhyzhY8FlWZdCfcoakZQwC32IeiV+Xj2Oe9yw2iT86vL+y2JR0UinQtTmedzJaYoiwbxrNPlcU9Jm5vqJGikKy95OpRUU9fFl5XWGgLV7fI07nc3nLdPM8z/U91F2ohI0/dn56P2v3ZNPxcJiJwwUdNM9+LXiWluTa6w4JFU5S3Xv/ibEQtch5CPEFz/QTZ7HfC38hkhXIbYtPLZbiWuBSHOub5m36vgjI8RASmmEvpZPLN9/1CtLep3cTQWT3biJ+OY9GRD9gHjxhnYYQ/EoDOt3nxGEdrwXuxXHBosMa0eisi8JccjgSDIyJeZDPNSkLUSS/1Wwwa946neBLaRp9G4jujgU4XHTs7F0RwrJsZEYNw53JJ5tCGcvHIEl0ViHRT8a7ElGTPRs+dJp89mewaZsw1ykiK4mHbByCrVauh07WngMDXh4oCkVmttONy37+ermc2nP0DysgUajCZJxl6LiWNLWzrvyS2NfsrlzRc6a+9/VXwpF/jwx9/yqQDGvFUsfJB8xgQ25Cp9NflTorR2LPh8Uf84k4j05HqNhSlHb7vKFynh9aEEo87vyVNV/H+Gkxuo9Q1nJlE/N8rJGczR3tmjwy5dIp5d/W4iUFsPTs9TKf//wj2RPt3akdBWbxfx4ZXnA6FRgAWOWcNCbD88ygRLgM7w4kray8/yI8jWIXLBF2P4OSAH/ZW0I+EYPQQqd1TgaVERLpjpGRy1/oTFyllCc199Uy7HCET/zQo3s4Frsxiec69o8kkuOhoXczWNUiZjHS1OL0S+QFFAXSVK14U1o67y7+I1ymTmkw4XFAKvkYiueHBR+ElJ8hhjMBslmByRe6AhAT7VRv9X6t/OWWICjyN8Yk7+NhjXOKl7BHzoNq8q9wXsmMVMoRFOTpGe5OCJKnEQP2nE5Qo8yyDn0guctDKKmWs72/g4Hm1w6MhZyppIXW2oQJaErit6hCNDhF1gyNH0mcp/JAkOasI8Y+msUTKKSbE0yfTpCj8jUY5F64mvuqLMbFUTvfGB3dcfF9CFgDj+HSskak87REl7uFsTz6nr4Vb5Nc4V/iGgaI54n0WiXWr+WHCKhmhhdE/kqJ5wEXlNTX3jqteh+Ug+z6/tznR95ogCAaOtoWUushx0aLsBHo7+oT/M7da0+1Ge+BnfCiwXf6YoyvenecZGw8c7LLQfK9yUouDgC29pNdAZUJKiFGzqrXXsR631L4epQ+ePnNTskL1fZzqIiq2PjKuw0wddRf2Yz8cd3Y4D2R1+GjzmM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 78e706df-09c0-4aaf-3c51-08da03583921 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Mar 2022 12:11:07.8705 (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: pMTrC8t0K17wNk04HnLYSYi787CLdbSO6bI2dIv04zMXGwj4FpOk8f6jAYv6YtdAP7DDJDUBx5Gw9xtxbQyKmA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4784 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10282 signatures=692556 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 spamscore=0 phishscore=0 bulkscore=0 adultscore=0 malwarescore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203110059 X-Proofpoint-ORIG-GUID: b0pYeu4J0mzmNYf-uCuYXBFPPtstNWre X-Proofpoint-GUID: b0pYeu4J0mzmNYf-uCuYXBFPPtstNWre 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 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 | 310 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 10 +- 2 files changed, 319 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index b577577..2b50b01 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10320,6 +10320,302 @@ 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, and if name is non-NULL, both name and type must match. + */ +static Elf_Scn *elf_find_next_scn_by_type_name(Elf *elf, int sh_type, const char *name, + Elf_Scn *scn) +{ + size_t shstrndx; + + if (name && elf_getshdrstrndx(elf, &shstrndx)) { + pr_debug("elf: failed to get section names section index: %s\n", + elf_errmsg(-1)); + return NULL; + } + + while ((scn = elf_nextscn(elf, scn)) != NULL) { + const char *sname; + GElf_Shdr sh; + + if (!gelf_getshdr(scn, &sh)) + continue; + if (sh.sh_type != sh_type) + continue; + + if (!name) + return scn; + + sname = elf_strptr(elf, shstrndx, sh.sh_name); + if (sname && strcmp(sname, name) == 0) + return scn; + } + return NULL; +} + +/* 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, since consumers of the + * library function within the binary will jump to the plt table entry (malloc@plt) first. + * If we can instrument that .plt entry locally in the specific binary (rather than instrumenting + * glibc say), overheads are greatly reduced. + * + * However, we need to find the index into the .plt table. There are two parts to this. + * + * First, we have to find the index that the .plt entry (malloc@plt) lives at. To do that, + * we use the .rela.plt table, which consists of entries in the same order as the .plt, + * but crucially each entry contains the symbol index from the symbol table. This allows + * us to match the index of the .plt entry to the desired library function. + * + * Second, we need to find the address associated with that indexed .plt entry. + * The .plt section provides section information about the overall section size and the + * size of each .plt entry. However prior to the entries themselves, there is code + * that carries out the dynamic linking, and this code may not be the same size as the + * .plt entry size (it happens to be on x86_64, but not on aarch64). So we have to + * determine that initial code size so we can index into the .plt entries that follow it. + * To do this, we simply subtract the .plt table size (nr_plt_entries * entry_size) + * from the overall section size, and then use that offset as the base into the array + * of .plt entries. + */ +static ssize_t elf_find_plt_offset(Elf *elf, size_t sym_idx) +{ + long plt_entry_sz, plt_sz, plt_base; + Elf_Scn *rela_plt_scn, *plt_scn; + size_t plt_idx, nr_plt_entries; + Elf_Data *rela_data; + GElf_Shdr sh; + + /* First get .plt index and table size via .rela.plt */ + rela_plt_scn = elf_find_next_scn_by_type_name(elf, SHT_RELA, ".rela.plt", NULL); + if (!rela_plt_scn) { + pr_debug("elf: failed to find .rela.plt section\n"); + return -LIBBPF_ERRNO__FORMAT; + } + if (!gelf_getshdr(rela_plt_scn, &sh)) { + pr_debug("elf: failed to get shdr for .rela.plt section\n"); + return -LIBBPF_ERRNO__FORMAT; + } + rela_data = elf_getdata(rela_plt_scn, 0); + if (!rela_data) { + pr_debug("elf: failed to get data for .rela.plt section\n"); + return -LIBBPF_ERRNO__FORMAT; + } + nr_plt_entries = sh.sh_size / sh.sh_entsize; + for (plt_idx = 0; plt_idx < nr_plt_entries; plt_idx++) { + GElf_Rela rela; + + if (!gelf_getrela(rela_data, plt_idx, &rela)) + continue; + if (GELF_R_SYM(rela.r_info) == sym_idx) + break; + } + if (plt_idx == nr_plt_entries) { + pr_debug("elf: could not find sym index %ld in .rela.plt section\n", + sym_idx); + return -LIBBPF_ERRNO__FORMAT; + } + + /* Now determine base of .plt table and calculate where entry for function is */ + plt_scn = elf_find_next_scn_by_type_name(elf, SHT_PROGBITS, ".plt", NULL); + if (!plt_scn || !gelf_getshdr(plt_scn, &sh)) { + pr_debug("elf: failed to find .plt section\n"); + return -LIBBPF_ERRNO__FORMAT; + } + plt_base = sh.sh_addr; + plt_entry_sz = sh.sh_entsize; + plt_sz = sh.sh_size; + if (nr_plt_entries * plt_entry_sz >= plt_sz) { + pr_debug("elf: failed to calculate base .plt entry size with %ld plt entries\n", + nr_plt_entries); + return -LIBBPF_ERRNO__FORMAT; + } + plt_base += plt_sz - (nr_plt_entries * plt_entry_sz); + + return plt_base + (plt_idx * plt_entry_sz); +} + +/* 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_idx = 0; + 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 itself. + * 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 nr_syms, strtabidx, idx; + 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_name(elf, sh_types[i], NULL, 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 (idx = 0; idx < nr_syms; idx++) { + int curr_bind; + GElf_Sym sym; + + if (!gelf_getsym(symbols, idx, &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) { + /* 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_idx = idx; + } + /* The index of the entry in SHT_DYNSYM helps find .plt entry */ + if (ret == 0 && sh_types[i] == SHT_DYNSYM) + ret = elf_find_plt_offset(elf, sym_idx); + /* 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; +} + /* Get full path to program/shared library. */ static int resolve_full_path(const char *file, char *result, size_t result_sz) { @@ -10371,6 +10667,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz) 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); @@ -10387,6 +10684,19 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz) } binary_path = full_binary_path; } + 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) { diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index c1b0c2e..85c93e2 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -436,9 +436,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 Fri Mar 11 12:10:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12777918 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 17018C433FE for ; Fri, 11 Mar 2022 12:11:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345664AbiCKMMl (ORCPT ); Fri, 11 Mar 2022 07:12:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345125AbiCKMMk (ORCPT ); Fri, 11 Mar 2022 07:12:40 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4CF914EF68; Fri, 11 Mar 2022 04:11:31 -0800 (PST) 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 22BAsIEZ005961; Fri, 11 Mar 2022 12:11:13 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=6D7nt7OzBOEA4QTwKj2un6TQjCBXMl3jObir2mlHN8U=; b=dy21cfRvQM9DxQoEGZulA64M4h900YOr+8T0cdT1l2jpgpu7PBTAnnujH6dz25YiRscZ 0722yCmplwBTxs+1XR3CB3GQQuVXyFWvVGNMu42pM+rdId+EEraCUrVXH3CnpXtb6nZQ jLvb1jwDRivtYEIww40ngYTPVrm7Df31Yat2rVgfGgoeUvaNf14ieavjVgZH0quH6S6u 1yWmUbiHNRUMsg2Tc8qh5Sapmyc2XQaMnSCw9upZ7tZu2iL6DsXc/N/UAPYEKEvUDgsR xT7wbhkG31DmDbE/h51YXFg+TMFOWsIk9B365ng8DDiK8a3ZB4GHQ1CStK1Xrez3ZpVe Ng== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3ekyrayw5m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:12 +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 22BC1obu034517; Fri, 11 Mar 2022 12:11:11 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2105.outbound.protection.outlook.com [104.47.55.105]) by aserp3030.oracle.com with ESMTP id 3ekwwe2s0h-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T93UCjfPYxk00G9X+3yf5DYCcjRgmH/tDPT63ME6olxkj5Ux+/pSbKCy8fV0KRS3EL1FLASN62mNHJfnVcHHMfRFZHaoFMtW6UjkBsLzLDetss6+qkdFElnjqDW/TmML0C+OF/bZex2DCdj797EmEvYWWIvWLThe08TVOzYv7jZKH8otl3ZEtb0HpXV6qM9b2sGRuWTp7a9qqJgU0dCP5oRVrnld69+LXkV7UlIuAcOKipEvHAl6GkVjnn1ojgeYEpg4EaQ1HSji9xgjDW5R0uYI1frAtbRjN0QQLvkl7dFVcn1wvRuD6OFb6zY/p8K+jBBhBWtMMi3QxixSKaiiig== 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=6D7nt7OzBOEA4QTwKj2un6TQjCBXMl3jObir2mlHN8U=; b=VWlwrQq6aK7e9wR46KayWNA/ERW2Dg+7LlGfYVyIS2vw7TkY+mcrZ9CM4bHuUCV4yNdjacpr5VUqtqtPkOLJtZDam6L8+uSVxsBISork6xANu6jj0gFlqTsjp7s3bolb1TcK/8+rMe9FJf99MjM39GQ1QDP0qxaMnbGozpBbMww1iZ5FC/9Z1V0yWfrML3muzbTtditXxAOOR+ByzVRSRc2N7drItx9kw3hdIbEOe/6U+m70OnPAB4DBT68ZgcKEqzp9VhhnRVajQw/dqVE8IhsrbqcjGN9dR+6VBHQuvf8R9nz2aL81593Tc7+2q6niBSWNB72f7HW1MuPh45v3nQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; 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=6D7nt7OzBOEA4QTwKj2un6TQjCBXMl3jObir2mlHN8U=; b=ztRm29S4S6iRmP3VAoAHsIlu54SgewGR47zzIQ0cegiOONV4Ut7HxRVlbLiQRB+wlM8rA1em8wb2Op4HAg7Em+49rP9axVMfQgOjLEQY57Aldyq9IBI7MLGXEwIxCHtW7xNMjcKGOs8emX7MPlphSkwIcpaVLD5PX7ke4Z799m8= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by SJ0PR10MB4784.namprd10.prod.outlook.com (2603:10b6:a03:2d4::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.14; Fri, 11 Mar 2022 12:11:09 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931%3]) with mapi id 15.20.5061.024; Fri, 11 Mar 2022 12:11:09 +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, toke@redhat.com, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v4 bpf-next 3/5] libbpf: add auto-attach for uprobes based on section name Date: Fri, 11 Mar 2022 12:10:56 +0000 Message-Id: <1647000658-16149-4-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> References: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0335.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18c::16) 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: 0274200e-88c4-4b73-bc6d-08da03583a3a X-MS-TrafficTypeDiagnostic: SJ0PR10MB4784:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /JDBVq7UTHpojywrECJ4IpKk7M1InM1cxKj3PK7kOFjBUP1+l3KFd5rkTsP0YWsfbqRaMzbBJ1rotoX7I50KlnJMNOChmLhGBFNplxXLBnmrx71QJkSWvu9ExSIMswkrgS83kG/BmN4yMGAqExN9lSyG5qSpx8+B+HskuAHcdYU3WexSxGwKzL5s/VraXJUHaArm52Lp8bx5Qu/gaArUNZqIz+G5wR9oHjU3ruI7DM0BM4+51krToULH/p3XB8lORUVexMkpTsf8eiEObxPRVXtwCCvJEpcUv598Wo4g6+TEj5IclJfH6ldCYQ3X11EZ5etaJOFPUtxxSmV096MQhxM0dSNHNZpPLdkhxw88VOJBLydiSHJLc4DZW1U9ALP6mE5FQiE8zVYLqRwe8ZUoHNaPCyBtJDEC9SBERq3Aj0seV38RaAd7NqqdlEbmbjfxK+2unW6rTJwvhnuC2Ia1cm1zGKGcq8HMiWhFUQgDvnN9dfmpUTLL3b4oYte5wm2FEnmJBg0XD8ZT/S6Nn2akAq6y3gCVhApjpqu1KMuuYegNNQI32QOlH29sIP9WosSQFXn9vM2QV1rvqMtu9IkNyoBukNFVBCpIU5QUTheqv/e/RR7s5+L1KyGXJXO0L9YWzKLBHNeZP0j9kuKdA+liRdLe686eRGGLFqsRDNNx8smIaqOHyfSgDhcSRUYEg5YsDXSzFQJg+IIkOPAsRRnXJA== 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)(38350700002)(2616005)(66946007)(36756003)(7416002)(83380400001)(316002)(38100700002)(6666004)(86362001)(508600001)(5660300002)(44832011)(6506007)(8936002)(52116002)(26005)(6512007)(186003)(2906002)(107886003)(66476007)(8676002)(4326008)(6486002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pBDJkn44GEcFNyaUyOQ8RcEq1U/aac+ir+90kpqV+1SYpgNLGcExfZl17h/U6W+0jDvH7rpNreEz1aWtfRvXkxMmuID7WdlX1XevmORz5PvPhRnE8W3yVkrsOqnd5dniiM8G2V0ibcFu14pB0pdDkFBH8n0JMWBaOe61ZWMcuzliiN8xGdUslIn+2Zy0DOpnU4PgD3vxVBsamV7hKDEhC8Fh/1xhxz1POY2zfqjTmtQWrxABCr/JnyPKH+t8UUCu9C7nKBk6rtACVsbiRZLMHsdfmI/ASEn6pdK2SIfyc+Ux8k4p3cJZfTCJmt0MikC9sKcjz9HqFrg8cfQuzy9cvFg/7SEWtsNWe5oHvcghbdRKwovqnmDuDABrWnRfP4eMHk1SQPb5ee+cZFa+TPF8ZKvr+fTx31kXYjAKNQZS03j0iNGNRdqcQF0y4YK9RNtGqHBINs3gMj7DxaXOuC7TILfQxFc99IpsB9xVq13nUT6gyevB+m6jf5D0ItRM2BDc2p4T1IOpNdM8yG8JQOgyU79yOOaS5o7Mff3PkzY1T8CeB7+TGArZnRoUyNWC4cjE5exifSAxDwU96IcuTflSjzHiTun3WLG0CP5l8K+IoD0M6sxL+tTCkliEBSMAdDZyJUVXFCjIOyjfOpofaaNYjl1xawZILz38Xp7rEVEBFGe8vIK8Pgtaap9gp28AdeEu4FB5DmQu/eE3+XjKOsAaEMaj47m3F68YBRTEc3XtjjXxbapl39CWR4/xxla52YVxHztrLebByDw1FDneOPM9P4kCuoYuEYl5BUHbu/JmvMxiXtC78UfC2zR43LVCtRzY8yVQQk9YCQr3LWCp/ihSZIQjspvigkYpzV0QykNbu0VqCoBpMS8Jrd5DlzcsfDErUfm8qPk02ilvceaqTUpOi6V/6FqURGpZjmXzG+J7Vf9Lw0cHs/PTSEVb/EY8baSe5jQxveRE+kTPSuPEA1qS7tXLS/5OoUtOur/tja/m8mxiv6/D8qcsgEy71PMnr+L4Y3tiHPz7c0QlAcQEB/VB/+aLs3ljdew9DeJGyH/7LxECTYkDq0TXTtTRPHehlf6t2W/0ioRYepqDyZuSEsEB/DUuW6YiQZRJM93/dxHJLijuUT/ZLQXB4Lq54D87ZlwUNHqOkVlfmgnWBb2QeBOSoCnrxE+VEiJj5t0Cev/MahTx0UQv1SKl+Ri1GgZqTw7G5d70Z8G/1S0pShb/lIHGBEyilz4AqLGBWvJnsshs3mHR6lFJaD5ZENO70rYftDoq6mIuwSh9js2zb8jLg8bxUcgnMga22iRFthWX8UBCeUVHANc1UemXmCkdyJJRbzVH3kbO7b/ROGfr72UBfPetOoP54NYsNeEusFqD6oL7+bRyoz/m0gWWCJ36kez+iYdCcyD0RHXfQOSqJzZDjiSPuf8MRBtzlidhHt6Bos8/DMQ8ydk4Ua8sZzObpoMMbUOM1GWET/86uzUkYehwdsAGhLGtaTFIiJiKAac5oaBzk3q6jD2XiYFaOZh99L1NxUg8/1WFClaLZPwADeAWibgR+TPx9NKGA/32PeoN0qEdLmJOk8vFieyGmphPuFkVIJCUBZcF3fXLSzkAlCuqPUdIGeNhWfIcSkPbQ1INpKrLeKI= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0274200e-88c4-4b73-bc6d-08da03583a3a X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Mar 2022 12:11:09.7153 (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: LCJ7wZoY6NvyJUBjDab2S4J8Z40to1D3pNvszLhyC2j+y8FnBrVecFH/e/gZJ+vFbvwa8g9vBiTzdZmRXCiagg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4784 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10282 signatures=692556 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 spamscore=0 phishscore=0 bulkscore=0 adultscore=0 malwarescore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203110059 X-Proofpoint-GUID: Ck9gYYNjXA635bD0nfwiavPGyigya2eX X-Proofpoint-ORIG-GUID: Ck9gYYNjXA635bD0nfwiavPGyigya2eX 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/prog:[raw_offset|[function_name[+offset]]") For example, to trace malloc() in libc: SEC("uprobe/libc.so.6:malloc") ...or to trace function foo2 in /usr/bin/foo: SEC("uprobe//usr/bin/foo:foo2") Auto-attach is done for all tasks (pid -1). prog can be an absolute path or simply a program/library name; in the latter case, we use PATH/LD_LIBRARY_PATH to resolve the full path, falling back to standard locations (/usr/bin:/usr/sbin or /usr/lib64:/usr/lib) if the file is not found via environment-variable specified locations. Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2b50b01..0dcbca8 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -8593,6 +8593,7 @@ int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log } static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); +static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link); static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link); static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link); @@ -8604,9 +8605,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_DEPRECATED), SEC_DEF("action", SCHED_ACT, 0, SEC_NONE | SEC_SLOPPY_PFX), @@ -10761,6 +10762,69 @@ struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog, return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts); } +/* Format of u[ret]probe section definition supporting auto-attach: + * u[ret]probe/prog:function[+offset] + * + * prog can be an absolute/relative path or a filename; the latter is resolved to a + * full path via bpf_program__attach_uprobe_opts. + * + * Many uprobe programs do not avail of auto-attach, so we need to handle the + * case where the format is uprobe/myfunc by returning 0 with *link set to NULL + * to identify the case where auto-attach is not supported. + */ +static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link) +{ + DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts); + char *func, *probe_name, *func_end; + char *func_name, binary_path[512]; + unsigned long long raw_offset; + size_t offset = 0; + int n; + + *link = NULL; + + 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 0; + } + func_name[0] = '\0'; + func_name++; + n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); + if (n < 1) { + pr_warn("uprobe name '%s' is invalid\n", func_name); + return -EINVAL; + } + if (opts.retprobe && offset != 0) { + free(func); + pr_warn("uretprobes do not support offset specification\n"); + return -EINVAL; + } + + /* Is func a raw address? */ + errno = 0; + raw_offset = strtoull(func, &func_end, 0); + if (!errno && !*func_end) { + free(func); + func = NULL; + offset = (size_t)raw_offset; + } + opts.func_name = func; + + *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts); + free(func); + return 0; +} + static int determine_tracepoint_id(const char *tp_category, const char *tp_name) { From patchwork Fri Mar 11 12:10:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12777920 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 A7360C433FE for ; Fri, 11 Mar 2022 12:11:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348103AbiCKMMo (ORCPT ); Fri, 11 Mar 2022 07:12:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347911AbiCKMMk (ORCPT ); Fri, 11 Mar 2022 07:12:40 -0500 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0619817AEE1; Fri, 11 Mar 2022 04:11:34 -0800 (PST) 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 22BBeGs9024109; Fri, 11 Mar 2022 12:11:15 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=o+crwmIaRiur1B8tccY50Nwg6siAf4qC4X9Mad9uEjw=; b=oWqF0E7d7OwQswuv8wszRQtcTjWuSRs8P1MkcSxL/A0efyBRF2WPZhKLC6CABSHdfm5b mov4MQXoN2wmzkqLsn4hwH1wvVi0zL9N2Jii7hvRws315VnePslf6IEeBlNCRs6br2HW +I+JWNwRVIVr3kdlh/uZrQmsDTu5leSUI0yWy5UXREqEavTtMK+YajCcciwZYxcKjcV8 ApQ74oqAVW5OWbMvn8vGvLW4CZxKM7z/xX7Tcv0VRN0dhhU9DRLb7mxMS5QzJeeYZmKh 4/ZekW5fYFrawxOfugw0JU6S1H0FBlPdkZFwGCwS/SWzM7t0AakcIjgSTa2Id0jOMvBL jw== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3ekx9creuu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:15 +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 22BC0UM6117764; Fri, 11 Mar 2022 12:11:14 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2101.outbound.protection.outlook.com [104.47.58.101]) by userp3030.oracle.com with ESMTP id 3ekvyxjce0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:14 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LMRdp6dU8HiSsA+15FGJZdE8TJjPH/MEhqU1DyE7LZwZ/BgN9OqdoNoVYvIiIP0nKF5YFFJ5ZgBT5ojV7j34X99KW8eo8+LM5a5XgnVdKKSqkhxT8PNkksTkuovDxhs/GSitAPnq0sWdBNrHbsewwFBiFoXBzHNcqwuh/MvGCIEiBjdtb5FSak5kIUDAgz9YeS1yCSU/X+YWCgrG3NR7JB+sy1Jbd965sJNNk3HIJyDcW7bL6fo136wLDO2QHH0zeh6I8/YxVhwx8fByHTDM8eX9OHbWR6nFdc9EGa8LWYgpBTwj+L+c9Fzwe3k+RMz1ms1UHLw182PJHSP2+gBSBg== 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=o+crwmIaRiur1B8tccY50Nwg6siAf4qC4X9Mad9uEjw=; b=f3lO6u0lj9+thC/dOgGqQekufJUFSBNEH9lD0Ej1m0GEE5lb3HptHiQufh/4JgNQcd9ag8Z/R+xdoodAfqfjJUyR27Ja61ku4s67rMpHLexzktd83zLMNSa2StBIO5ZaEup52lbk0JF/t5Jnp7ONnv0/oINI+vJ7ntwtFVnfy4mPDcjxE30SXsYlHW4qcOCKfLOwicO0Qt76Xw9MBFkw09KNX07+J/P2eXWeBgum9pNg5bbzV5u3PB82B0rzYMeO7778LnsB2rf/94a59pLgJaoSjPOoo8yz9iMRHbe/v6SjM/1tVIyXS+rymPaEFqQGq3B/O7RcXuIje0ElOtF0xg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; 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=o+crwmIaRiur1B8tccY50Nwg6siAf4qC4X9Mad9uEjw=; b=toHr3i3juz6jPildNiAQf4u33IbeXSJcenyjXWYXgqOyxv22nBOSP/yA4/IiYrkySZZ/eO+CWvlTraxQtfcPduKu3BlQ+TwbLxWhqhzxKaWLNNB2rmtZxiXKH569LOGI0O0Mn0gxnZ4NQA2Cpp8eQfANDIDuZSCJjZ79qKFyVmw= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by SJ0PR10MB4784.namprd10.prod.outlook.com (2603:10b6:a03:2d4::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.14; Fri, 11 Mar 2022 12:11:11 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931%3]) with mapi id 15.20.5061.024; Fri, 11 Mar 2022 12:11:11 +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, toke@redhat.com, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v4 bpf-next 4/5] selftests/bpf: add tests for u[ret]probe attach by name Date: Fri, 11 Mar 2022 12:10:57 +0000 Message-Id: <1647000658-16149-5-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> References: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0335.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18c::16) 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: b3d2775f-67dc-4a2e-a2ba-08da03583b4f X-MS-TrafficTypeDiagnostic: SJ0PR10MB4784:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LLLeAJNAQnum0e+X5EWHmJc2alR+w/W0hpmHbCIfRZTGwWklLc8CtVww0H1R3ClcNA/spuvK9ROYjtV2lw8iq0AsUgN4AkSEnYeDMWb6c5SAbUIvZgZIeiMT2bd+/97rtCQTBdjIEa3TKOs/6yAJQn2hFvk7A/YfV5x6izKfP+9A0MHCmNCVKLThmXq4YXpBvpWdjsijMPjeXtfXfuDLSSuOML7r6A9q+rA6znS1BK4TGlIcTJBHVd6Eg5PwfnJF6f+bUmVP4fjrrXnKaOsCMcf/GFF90dY3ms7XnYbpYZS836b0jUyCxETgrSFQyMeqVl+jhiTssj4bZYD7FSFVwryO0ihm5pGj+GhccKB3hMqS6lP2XiTodsn4cAfyqiKmzDf8ZzZ9J/jjxSCupdblQcJK+FHTp1eHAitK1XzZ0cxk70m2P072Z434E6gvaT2m2h2Rn/gmRjitxLkawg+ak6o+8AHiVeblRoWGx3IietORHR4MpYH6WIJJskL/suMlBqfrdkX8Obztw3WBC/dnkLeQkU5+JiR6fqW+yk7ps39hjwfQBOX7yRRvdLfI4f/5sbvFcjLmmwugfIaRXwRKReH+GmuYfyGTTzh+lrl7MC+QbZsLeV/WZSRR16lkhwXKmxP2UUFG1LNAqV2JqVXQSaYWmL6eEEyoqVwH5l5jwT23X5yNh/sg/9lMdEW9o4aCJcdFvKya5/63dR746efhNw== 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)(38350700002)(2616005)(66946007)(36756003)(7416002)(83380400001)(316002)(38100700002)(6666004)(86362001)(508600001)(5660300002)(44832011)(6506007)(8936002)(52116002)(26005)(6512007)(186003)(2906002)(107886003)(66476007)(8676002)(4326008)(6486002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ruBgXkj/km9jRYiIv+lR0mLWcFS+H1QJZ2Lej4/o5tVJxwRbP6dRtp/IG9Zh6TT0B58uGcivDEMogH6LxdLCiJa6yMMcDrYfHLkHtd5ePH6S02R7JjDJ13Jpo7Hx6RtbMJZxkz0bUDHg/ecQH/vfkITA0IIgV7d4M06ONJNmtceeBsXwBmTKAGk6Kfm4dhP1gASTbSYf7XiLNoLqQ3v+rqOyTBzABqYzlZXLbaIqs3yIEzotc2pkeSe1ILeJ3ZJJirt4E+cYnHobdsQkszR1kUB+bgY/C3w7DL/LQ9i9duum9TFEHNHVwRq3Kgb1sac6aCE22ivCwV6pyK8arGY3wGxebdK+lXCFjdlfOmkO1xguqvFETbCwe1tWR8NMZqQkfYhuc1Ma14kl2I5xzOhuk5JagRyIbIu+SLLj+b6v0im2ZX4yUgxtt99DPCO1CM53yWSYg+lqIuE2N8vohf4cuM2zTYmnbGhQSePDVlkp63IweDwSh2wx2r+QiAtnXKiQp49HJcL7LH0mdeESxxnoOdMOd9AHZatmwK8oSI4m2jCFulVXnlH8E6zWqDgdpom+J9/r8dXIyTLXznz4BruYL7+d0RYkN61LwpGhdfE+NBNj6V1ynBOsgk95l0TW2RJdlb3sBM0gCkYuKvsSv+LYppwl38K5ePfVrX7h10mkFGvw6NxJChpWTlTawC+ijR9DSAEgrGG7H425QqU4n4aR7Yh6aVTEjywYCTv7Cfl9CY85J7W+svVmebKd596xhNfsRf84axYvbjYz70rPJbQponrguNqbRWLb8A2dCL/EUrTWP1eXbzVDoCuVmV7fkAFckMOewQd/MNYJSixnRN4Hfi3Z1KZ0exbtTT8AcTOD8SatP3AkfVWLimak3BfFkXLqTOnwfYu4epy3AVDUFkCLuOATZF2BTV1Pf0qe9v4HBfybzjRDL+UHXhmvpuDXGw2MG4yW+lrChcz7CduK6rnFis9xY2ApD55D1yNC2jB0rd3quqRZXnwNo6VLF/7POIOZ6VhT2sGFAgkARgqy8fdULUIFDUBltX8phziHW0L2jVhnGVxx7PC/RizH8LbKfEPONPrQa9DhsMxMa10gYsUhpJHoZtRJYPGpUWcesa5PyFrw81bokrkr29+emO9YMr20uDYBsOLpg2Kg+JOV5LlAG+DTC8HtHr1f7bk/Y71DshM8WeZdSR9dWGL55dNYULHQUki6JmIvvecSTbciEeS+4XMQpzelXKI1Gu9MDJafOINzhzlXRBC5yGRV7v8GInDE/YxBoFhAaAzREfGPCuvWYaWzmdmIf9ECfutKHAhNONsuzTZTyMrh/S0KPW5uXLOwBj5N5kuIji3b32m5SbcpAb8uDJ73cUVgLzJEa5JChEKds/ySFXNCjrSQCh9VD/vuXiRpPvMGROm+BNlLdbtYhk7U6KJGkCHqHOyaO65C9+F/UpL7GQq8I/614OFCtgLGDNZ/o2HthmHATAmQ0pI/vldiYE/ptnYziRncr2TAEozN3NX/oJW0UxBIKw91j7Q/tLRYoQXpA5AglIwB1Yjs/SX8mABoURhPPKGcb8pX5bEY9EPF0vWX81I1QXl20SkSmBnpMtyXFvC2ziyBHodB5DX734lva0Jy8o0lK4cZPXM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b3d2775f-67dc-4a2e-a2ba-08da03583b4f X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Mar 2022 12:11:11.5926 (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: haIAZiyAS8JRkcjEvGpPEcwqwZfNy4DeC34PqRjlrLLBTF2mCLnX87JRFHZNmQBhEZDH9Kny1uNqogvifHBk5A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4784 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10282 signatures=692556 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxscore=0 suspectscore=0 bulkscore=0 mlxlogscore=999 adultscore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203110059 X-Proofpoint-ORIG-GUID: rpKjM3Ogzj13YCX5ZWTbVbCJIRuBHPNW X-Proofpoint-GUID: rpKjM3Ogzj13YCX5ZWTbVbCJIRuBHPNW 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 -EOPNOTSUPP 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, 113 insertions(+), 13 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..b770e0e 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -11,15 +11,22 @@ 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); - int duration = 0; struct bpf_link *kprobe_link, *kretprobe_link; 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; bool legacy; + char *mem; /* Check if new-style kprobe/uprobe API is supported. * Kernels that support new FD-based kprobe and uprobe BPF attachment @@ -43,9 +50,9 @@ void test_attach_probe(void) return; skel = test_attach_probe__open_and_load(); - if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) + if (!ASSERT_OK_PTR(skel, "skel_open")) return; - if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n")) + if (!ASSERT_OK_PTR(skel->bss, "check_bss")) goto cleanup; kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe, @@ -90,24 +97,80 @@ void test_attach_probe(void) goto cleanup; skel->links.handle_uretprobe = uretprobe_link; - /* trigger & validate kprobe && kretprobe */ - usleep(1); + /* 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), -EOPNOTSUPP, + "auto-attach should fail for old-style name")) + goto cleanup; - if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res", - "wrong kprobe res: %d\n", skel->bss->kprobe_res)) + 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; - if (CHECK(skel->bss->kretprobe_res != 2, "check_kretprobe_res", - "wrong kretprobe res: %d\n", skel->bss->kretprobe_res)) + + /* 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. libc.so.6 will be resolved via dlopen()/dlinfo(). + */ + 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.so.6", + 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(); - if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res", - "wrong uprobe res: %d\n", skel->bss->uprobe_res)) + /* trigger & validate uprobe attached by name */ + trigger_func2(); + + if (!ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->kretprobe_res, 2, "check_kretprobe_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uprobe_res, 3, "check_uprobe_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uretprobe_res, 4, "check_uretprobe_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uprobe_byname_res, 5, "check_uprobe_byname_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uretprobe_byname_res, 6, "check_uretprobe_byname_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uprobe_byname2_res, 7, "check_uprobe_byname2_res")) goto cleanup; - if (CHECK(skel->bss->uretprobe_res != 4, "check_uretprobe_res", - "wrong uretprobe res: %d\n", skel->bss->uretprobe_res)) + if (!ASSERT_EQ(skel->bss->uretprobe_byname2_res, 8, "check_uretprobe_byname2_res")) goto cleanup; 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"; From patchwork Fri Mar 11 12:10:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12777921 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 D1CE7C433EF for ; Fri, 11 Mar 2022 12:11:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348522AbiCKMMp (ORCPT ); Fri, 11 Mar 2022 07:12:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243342AbiCKMMk (ORCPT ); Fri, 11 Mar 2022 07:12:40 -0500 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85E5E188A07; Fri, 11 Mar 2022 04:11:36 -0800 (PST) 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 22BBj56b011326; Fri, 11 Mar 2022 12:11:17 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=FT7SMbPO1INVUF/yRTWt5h2zstuOkZoZ8gcrTSqKgHs=; b=zQGRDdj5Uxg9BXhp4Km4ZJIYFULJ+sXh3Dqp6jAwB7aOopYzLo+HjC//Li8HilhOv2Y8 MrZ9BbmzhyHggILEfuAK9CX6RZE3G106yUZIAPi8XcH5F4/u51Et7lVHARZXhayMAiaz IJMBXiWVFj36UGV+wIZjr8IijL3raRRA83z6tPPccuKQXhbxY9pYMn6aPIwVOXq25mzP /PpymsqyW/Oesu+55U4fheZGE7yN/5A9K2bi+hzqq8SR90e+AGGTxRweAjhprEVmKGjy FrwTnhRWAexDv+3VFzFDCzJ0KrRdVbtLindx1cI7JKbPLkQr+FsOsv+nmxwoKZ2OML10 RA== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3ekxf10mtj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:17 +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 22BCBBJK123780; Fri, 11 Mar 2022 12:11:15 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2109.outbound.protection.outlook.com [104.47.58.109]) by userp3020.oracle.com with ESMTP id 3envvp44yk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 11 Mar 2022 12:11:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UJkGkOZIw4Q17C888U6tY4bC6jmtZwYkgFqc3KJLQDhsLuJIdtMCe7IN7lgKZhs2b9PZq6fJU0l4bRDMdS35VkiFtp7QrB0VJWzvAxJchc7L6xOpOeGCcvn/V1fxgKt87tHxAojHaGR0cmuLjXVk4l5hb0chGQoEqiycs+N7t83bJKrxM1rB5wNfzobr7QiqbBOOhGtzMedekGYDyD47Pm8vJ6vFIXpxgiA+U6Wp5SqajQps4hOUKVycuVw8WtJ/Er7Aby3tZAOYwy8w0/f/eG+3DI9yr8C1T4ZJAcCte2S6VIHd4I98gFv2ZuNzee9nZiCa4CGGN/Kw++SDnBrdhg== 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=FT7SMbPO1INVUF/yRTWt5h2zstuOkZoZ8gcrTSqKgHs=; b=GEobsctf8Euwndz/txIn6n+reGLYdSvHok0G7AbVOBuLAUAYO8Wsdu8oHSvR/LKJOdSRmJlw30XPainsUlHCtx1VtSADtHVpVzsf55LQwm6GvzX95gzLBiYm2lYfZ3q86YJPY6yF+R/U8kqdvex+e5MlpzEzbv1i5GG+FcpdpWsxw41xkh5yxqIdwiRGDPd8ug1KrwUaT0uJDKKjmNNX+Dzu5aWMknxqKdgQprkjp2+kq1yXzyLcBpJj1lThWzMumizI954OFOESaPmCG2fzitsIrdRuQCx61cUtSwn/NJyz16gLsbCMnhdlMQWA0aKfHoeTMS8LtqYkVBB6McUBew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; 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=FT7SMbPO1INVUF/yRTWt5h2zstuOkZoZ8gcrTSqKgHs=; b=GaExePgszMhLMOHzmaI73XRcRNR6zyunHi5Uqj3ayui2JLZ9QqaBF1Ug0+NByM/V2KiRujS6O40mRQbTVf5wpJ76j1/a1qYJpGWtdo7hDeipAOCUG2ql0OkzyE5421q2+Ht/vjU29uyC0TMbQqGXjByDl/IlWiw8FXEr4n9G6uo= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by SJ0PR10MB4784.namprd10.prod.outlook.com (2603:10b6:a03:2d4::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5038.14; Fri, 11 Mar 2022 12:11:13 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::517b:68:908d:b931%3]) with mapi id 15.20.5061.024; Fri, 11 Mar 2022 12:11:13 +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, toke@redhat.com, sunyucong@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, Alan Maguire Subject: [PATCH v4 bpf-next 5/5] selftests/bpf: add tests for uprobe auto-attach via skeleton Date: Fri, 11 Mar 2022 12:10:58 +0000 Message-Id: <1647000658-16149-6-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> References: <1647000658-16149-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO4P123CA0335.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18c::16) 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: 02e1ad76-608c-4d74-9753-08da03583c66 X-MS-TrafficTypeDiagnostic: SJ0PR10MB4784:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m9nBKVo2tdqtN2hSlqpweG5yx2fkHhBC/apqlz+8PgMzTLOL2k3VSejDc2HvT5HbG9nZAoNOrAfsqW1QTl9BsjTrhg9LvTEBfau6NjV53NcwJXVi1uVmuJH7eiIEze7G1V5Gfkuiv3X0a+0zyikWvecSGUssoMakUZAfGn7CMsjEJlUqw/bokmdKBnk+1fuLLhat+FqxXUWvvX7QoYOV9Fcsn5mdGUsDKRuhti+IRRKxYQ6hjnCS8a2YRq+1EqSE02nFkneYqwuCJuxh56hLK7jIpfN1L33jwwa7kfTI2UnheRjOofPDdIHkM3FgSTEFkikJp/VxG/wOLvrDer3prL5VzO+HIvog0TVXT0Q/jNCmEVFN3GwFCz9IDgV1DszYdUBPKUHMn5JlMMZiAbXyc9SbIluyOsvk8jI3hWgyukpU4txkaTES8j3Sn2RAEfrdDiMxc/Ewp206334DvWRP3O3zLSl9UKbhWy68eg0C1pw/+WorqPN2LILGpeMbxXvynpdVga2JfNRPiYbehJl61xmjWmrPfZV1jLY4ze6kEbgU9mjB+H5f9dbVmbZbKHDLE6tZkmVsLHpLOVyh9RaYbjwhJvBKtwPhgCCib44jcGOo7EZONPkGIW/Dhc6ktwGluwGAoR2DtYPHgT9vEcz/6P5pwPR6IEsRl75FKIOy/Hfkhxz1lAsE0QueGHzwvKr3oYVDxhqu9nfz8is64fCo+g== 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)(38350700002)(2616005)(66946007)(36756003)(7416002)(83380400001)(316002)(38100700002)(6666004)(86362001)(508600001)(5660300002)(44832011)(6506007)(8936002)(52116002)(26005)(6512007)(186003)(2906002)(107886003)(66476007)(8676002)(4326008)(6486002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GJX7JYSloYU4gGL0sSDdHnI9oUYw6lpZtsHYSJMStAa4gOGQHfWtrmpdMVAaHxZ8+cLsRU0IIAhaa6WccXoVZ0sBvU24aASRqAi39XSHRVEGenfvMOv4y23dfypKntAG5hrphRbtvqkafOtzWBLNiVfM6Yxdm77aNuReCmzgghw9QM2iLbZbGM8XPW7ixj6fhw1vnTrUEz6SIGwDmjmKHqAEPpAqlUi+eW9lLXSEFV31EK95HSS89cfVHBk5/0Nwvxk8RmrwVpQlYBP+EvvluAld19bH6kaOAZm5pmV8FvmFFh8vDpYNCBIBOgzQgwSLTd4SLi64ONNmublYq7aRj9zSbimyloWuOEx2PZUUvDlkczJQfxZ6lWlGuG/DxFi3CdVfbyCUvFc2Ydp4YGufqhE729tNVQXPvWyPZ2IwRS8ps8q5qGGxlEexxpiGv2YxxdTWfBSo5oS3QdOgPidUBGSZCY70rvN7YdIRH+1OUz0D0waGHb7lCpv82Y+Bfaopc2SzJEmbx/EiWoydEpO9Ldc7mypJ7GmREIYeBFWSIitcSd99rC/ecx7FBDWWELtA0vn0P0Aptq1x6ceW7NcwbpuCMy6I8qmplLLgS37cZ/KUefnmZsDYlBDqVZOmI1C2ivnxzTOoAtukxM93ZAV3Zas6mpCms/rXChsLmwrqxvd+FTjK4ITxNQ4Q5nwyfzHOmSStaJHDtMW59XMPRqivq1XV1n/QrQujgW0ASsiRnBEs45phEKPWQ4+Ai2rnn5i4/tIePtpfWj130erXs60Ii9vBHNFKtR+DSc0SxsEHrGRSHjW4G6JwiDkbUxwEW++1u4JKKqyi3lAJJ9J3LUEY/HKJxxIMi1MU/k4W2YKMyuty5wIaNdf7Ovy+1YOOQ7LDRiVJKF/xtv7rFbqTOmUNPyzMMzAyFkJRZt62XwnuJQeriQi04Y6jZI5c2YkPXfjeLxhiC388eHA/T6eLu7wN0btaDb+/J/pz1kvX2oQuYG3tdLYQh9e29GN2EGwvr8R5okceDRPBrLyfqcKsLEeD20G2blI5sUTJE1ZqwEorN1jS06Fkz774YyD6x4x5wqLrqHCrVxDfpAAUn1BoIk/ATMCH/R0+3mFHI8nAa1VtyHiEyzvw/2eHj2Knv5EpoeVdeZdu1Z67TO1ezITDoC/r9q8yAX95DWMep1Bwbxxi56ooVvNgBi4/37iwdBYXSMh9rrqDwDfofnpF6MyGVL/0VS4exTATHdfWArq2tzjtjZp9BbmAQvWITkHrqDSxxabl6eBc9/hpi5As6DRmmBR9DltB8+C0wnv7dldYnAzihFbLu6HNeCoRGgRNKIODMRg2+Xfyr+59sY3wkRosbVtPyKet+P2RKht4y0LCop61N95vYImalS1tMxQCNFPRlNXQYvAcCRhKTV23jBm9AbJvw0AzD8rZa16gtEc3VJ/XyhLVGO7KqEjGtY+IVsX4Ci0mSUmFn2XReP3VXJ4CnYPYcKYa4iPn6VeA9etSH2fE2Wxgu/BsEvRllr/ZdsrWqYBi6nX9OP4EfWQGErkJnQp0w9hZcUoJIVK096vDVmFGYOJlIve2jqQVHLaqiaitk8Q42j3jd3g0mEASjdK6xhg9V/7pHTxEeALPZcaLSmWC0aE= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 02e1ad76-608c-4d74-9753-08da03583c66 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Mar 2022 12:11:13.3895 (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: xkAnJn0Q55DV4gVa2TdXSvXgXkGifGlSvf/t+WJrnHfueZFli+i8sOaQw48sHWwm1ADEMV9CoGDDAKeorVHPlw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4784 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10282 signatures=692556 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 malwarescore=0 suspectscore=0 mlxscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203110060 X-Proofpoint-ORIG-GUID: u7A6CWQ0ke3TJjK5jdDPejj7sGSbm2eC X-Proofpoint-GUID: u7A6CWQ0ke3TJjK5jdDPejj7sGSbm2eC Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net tests that verify auto-attach works for function entry/return for local functions in program, library functions in program and library functions in library. Signed-off-by: Alan Maguire --- .../selftests/bpf/prog_tests/uprobe_autoattach.c | 48 +++++++++++++++ .../selftests/bpf/progs/test_uprobe_autoattach.c | 69 ++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c create mode 100644 tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c b/tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c new file mode 100644 index 0000000..57ed636 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022, Oracle and/or its affiliates. */ + +#include +#include "test_uprobe_autoattach.skel.h" + +/* uprobe attach point */ +static void autoattach_trigger_func(void) +{ + asm volatile (""); +} + +void test_uprobe_autoattach(void) +{ + struct test_uprobe_autoattach *skel; + char *mem; + + skel = test_uprobe_autoattach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + if (!ASSERT_OK_PTR(skel->bss, "check_bss")) + goto cleanup; + + if (!ASSERT_OK(test_uprobe_autoattach__attach(skel), "skel_attach")) + goto cleanup; + + /* trigger & validate uprobe & uretprobe */ + autoattach_trigger_func(); + + /* trigger & validate shared library u[ret]probes attached by name */ + mem = malloc(1); + free(mem); + + if (!ASSERT_EQ(skel->bss->uprobe_byname_res, 1, "check_uprobe_byname_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uretprobe_byname_res, 2, "check_uretprobe_byname_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uprobe_byname2_res, 3, "check_uprobe_byname2_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uretprobe_byname2_res, 4, "check_uretprobe_byname2_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uprobe_byname3_res, 5, "check_uprobe_byname3_res")) + goto cleanup; + if (!ASSERT_EQ(skel->bss->uretprobe_byname3_res, 6, "check_uretprobe_byname3_res")) + goto cleanup; +cleanup: + test_uprobe_autoattach__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c b/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c new file mode 100644 index 0000000..a808bcf --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022, Oracle and/or its affiliates. */ + +#include +#include +#include +#include + +int uprobe_byname_res = 0; +int uretprobe_byname_res = 0; +int uprobe_byname2_res = 0; +int uretprobe_byname2_res = 0; +int uprobe_byname3_res = 0; +int uretprobe_byname3_res = 0; + +/* This program cannot auto-attach, but that should not stop other + * programs from attaching. + */ +SEC("uprobe/no_autoattach") +int handle_uprobe_noautoattach(struct pt_regs *ctx) +{ + return 0; +} + +SEC("uprobe//proc/self/exe:autoattach_trigger_func") +int handle_uprobe_byname(struct pt_regs *ctx) +{ + uprobe_byname_res = 1; + return 0; +} + +SEC("uretprobe//proc/self/exe:autoattach_trigger_func") +int handle_uretprobe_byname(struct pt_regs *ctx) +{ + uretprobe_byname_res = 2; + return 0; +} + + +SEC("uprobe//proc/self/exe:malloc") +int handle_uprobe_byname2(struct pt_regs *ctx) +{ + uprobe_byname2_res = 3; + return 0; +} + +SEC("uretprobe//proc/self/exe:malloc") +int handle_uretprobe_byname2(struct pt_regs *ctx) +{ + uretprobe_byname2_res = 4; + return 0; +} + + +SEC("uprobe/libc.so.6:free") +int handle_uprobe_byname3(struct pt_regs *ctx) +{ + uprobe_byname3_res = 5; + return 0; +} + +SEC("uretprobe/libc.so.6:free") +int handle_uretprobe_byname3(struct pt_regs *ctx) +{ + uretprobe_byname3_res = 6; + return 0; +} + +char _license[] SEC("license") = "GPL";