From patchwork Wed Jan 12 16:18:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12711560 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 F0D3EC433F5 for ; Wed, 12 Jan 2022 16:20:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355073AbiALQUL (ORCPT ); Wed, 12 Jan 2022 11:20:11 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:8116 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355108AbiALQTW (ORCPT ); Wed, 12 Jan 2022 11:19:22 -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 20CG8KaX001321; Wed, 12 Jan 2022 16:19:01 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=ZexGXTclLJjvBdSCrhjcVcDCZ+XNVIEJ82Fb3JEoMHQ=; b=X2I23Wf/vys6Oy/GzrwIP0jh+8HjiFxEPNvgNcTVuBgw2Ne+aOonUvVFJafD2OeBdycW HvDpB2FGnZb/pNBS3kjp6h7o8Zad7qjhqT0k5R9jg6DEW/yG6kVAIq1qgnTHq/dwRGEY 30lG8ff6mU9zjI2IGJMsEfAu9oRzBcIXZLJnfovrrfzg8Pc9HqqqYmNaAFYz+NkTTsyW ew1JcVzPN+Snx4lmfKr3jrPibKxQ+fv6WyCuJIwFAJQdL1HgI5/uCAnVu/S3JU5AMUHz Blz3JjsrS0z6rzmE8GLE1cDklmzkVLjAkXe0nB0qN7Mnv7fIYTxJgN/Ik6WKL/ZThuY+ +w== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3dgp7npq41-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:19:01 +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 20CG8DFs073509; Wed, 12 Jan 2022 16:18:59 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2176.outbound.protection.outlook.com [104.47.57.176]) by userp3030.oracle.com with ESMTP id 3deyr051ae-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:18:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n7aG4Nm6xR6MjhZFVj07nsM6AokatU7mtnJ6gPGlob0w64vqF5Irc/QWlULsheO10hfAtJvEc5mC6z3j8mmOwFHk5OfzaIkW7W+iKxH6HVZ4qWXtZXU2KFFXmpG88ckmkzyGehgv2xgdTj0v+zwwdwNSsX/eOzFYUtm3BRq2h1Z7UtH+IUFVzxqjAtifRkLkRpqlaA0mnd6UfKCtAlWzZ6aH30zuquECzdCOfrfLc1DuaclfzLVu7S0Czfl+Nyz/prDRtcJrOMTBo5cCueS7zqif93KVBJcz0A+6uP/crdaBrLsbassPBKXWYYWY4lZZNFI+40r2QNp9mDLxWkMy6Q== 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=ZexGXTclLJjvBdSCrhjcVcDCZ+XNVIEJ82Fb3JEoMHQ=; b=Avz31YCHBLtrsTeYCFo8L8PBSs6nOoM0Bo6PN4DzaqULactVVmEo4JRizvAPg8YiRzOvCQn15ccpPY2Tbx2xhoDvM0TG/WIJJIx5A1ID2AHxRa96r07O0jFPRU1WjysLV6/PYrKlQbIoSdUG5Irr7fL6Jp1+Oitzc+ArIB/FyWpHbHvV2X+bXofWxh7ykbs5t+Jlf/uiHMqikqYHVpMGzAh1w4GJBMgszje1tK4EXSnk8E1tsNT/kMDeYaAX67qivEHnE2tcRKsHgV3J7JViS79CnD/QbLqaqMlyzz7slk6aiRwUZ/7vSwTWUu1a/Hy0YvZfWXNzlOGXixOqfAk+Wg== 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=ZexGXTclLJjvBdSCrhjcVcDCZ+XNVIEJ82Fb3JEoMHQ=; b=XStBpRrIkjl1UUcz44DlsSg0pBOrL3Krk8ymUhf+Y5Rqg6yewiFvq7BF+4uAYQPSL1MC7yWSyAOOGC5dd66dFtjJtIMYQ03RKMwOdI21kMqq8f7uzE2E4ndPPO7AFkxcBcRtgdYyLVbc1IdhaiU8kPdARjELx9F+9CNsewP4Y44= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by MN2PR10MB3600.namprd10.prod.outlook.com (2603:10b6:208:114::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.9; Wed, 12 Jan 2022 16:18:57 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c%5]) with mapi id 15.20.4888.009; Wed, 12 Jan 2022 16:18:57 +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/4] libbpf: support function name-based attach for uprobes Date: Wed, 12 Jan 2022 16:18:46 +0000 Message-Id: <1642004329-23514-2-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> References: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO2P123CA0080.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::13) 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: ef39e37d-92ba-44ce-cdee-08d9d5e73bd9 X-MS-TrafficTypeDiagnostic: MN2PR10MB3600:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3276; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /87Cuklmt5B+vSdg304Zj6Wr1kW/Ex5cRyBCvK/1qRzJw1IPX9H+lKL82gjm2esuK9BEVwOP0lRMWXyqZjT6fHHZKjuwX0WgqHghe87H9jLYOeDvLrR1bhtUeIBAPf5Z1CfUR7QXth7zRvVCRjEayovQk0yGRyCIocpVP7XEYKoMxP4zwfZsGy4pSvtQb6Yui5btUXA3XTemt183LBklaUz9PHCklvhLNQ2s1jfIQbcuxtfD9gRw4SMmlUpNR4dXhPXB/AGmgqYfzOekzf/K3eHjsXeQajYhnxBReT64tV7N9vn6L9iKjgtmUkb+Xc3KoQ0Nw/cWS0XvD42+OTxomsuyIep/V0mNxE+i/cOL5kXZujYe5rY4GZkmQ/XMHCXtDbs826NmdWNrtpz/NXXhq7pRCrTD3MkaKBk249S+rB9kwPwlgsHLzVZXLIvfjnP7N5bAL0Y6pGIhv4jHS9PnXktJangUzBDmUaHdn7/PoKU01q2YxAotMgQKDOitgjL0Cs3tbTykexVoqbNYO0grAU7lfPXS5S0GF8oAqtiiMrefn6CbPYU64N9QYS0MCo6lCLhqb8C99gd6vLX9WM+jlbjKolMEri+q30JvitWeWeRVW4hyAOUSseF4I4RdhdDGOiYp2QNzE0JgEA55BSOne5nu+8PFTMrGsZvLCyKV6LvIVSGjMdHfP6tB88AQOldYp99kKb3EQFtANXj19YcrbsRGGUAY9dFLXjLSxcWVyO5KFKrKEvYzLR2BwbT3tvwykgFPg975t7302xpOMLdfje1yRvKRTZpdzNXmrBKON+Q= 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)(38350700002)(8676002)(66476007)(8936002)(6486002)(36756003)(66946007)(2616005)(5660300002)(44832011)(107886003)(6512007)(66556008)(316002)(86362001)(7416002)(966005)(83380400001)(2906002)(4326008)(52116002)(26005)(38100700002)(6506007)(6666004)(186003)(508600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +xSxiD2dGg10R2Jga0cRSmmuIb5fsjaNGfTfuYkoocHpw+17ygPYxo6wqAHODgcU0LFK9kzMopuiwNs6/Vv0tuFiIz5Zonr3rn7tN8ucls9IIK7DW+PkZ+3UoHAU1y9ikQjmR3x3swE0cTva1sQPvbYvrOJGMsV+DDjvkbFdCd3DsOalzNlB1Js924t7G2o0NYYnhQI/RHKRaoz1zSy1WdQaxdC13jjTD7mn+CfiCFPDpGY+MUEBDyDIrsgvn03DiuHXj+R1t60t+uHFc8UfIHF2khYJbkZ3A24ypjSqAD0fkSnsK/01oXUGD1/pUrhRkP7Mji8aQFvO95kMjQLu9sa3M7a7c+LvpiY90HKxQpxmcp8PBP/d6s2gHBKINrBdKYOHk+F5zi42fAXtjWUk0gGL086oisH99IO072uQgFXnKO8yZmAIFYXXhG0mv4NndzWpGdCOS0eL61uzFEO+YUVG5S5WHXlPG0xJ6roF2anS2Dvggk1MMt+loMy3dVq0MEeoygC49ngRo8zjkjzRfq7GUAcwT70c/uDfmtAhVCw3zGVd65LuTQvwMLULGB0aBRzhBpM0p+X42hUytIdhRBIW4WSm/ejyYOer7l3PuoCtee6wrXB5FeQjjdlYGpAhPLa7SJPlXfO8ofLvQ9o1l4hzfR1WLmT1SS1Bm/65I6pyswYfR4b/z/QJU33Bw6akyzLY5rIMUW1SeXGq5RUFyWYuys1+7GEcIKYMhyTrdlyhsgRt+Bgw8JRjKumaKgmwrHAn0kioA09fqn5YsDpQMCePRMyIajw+D3XW9U9WvEyhBKaQqpQWXSBL41KHNW9S4Q43fVPkbY2UATxDdaQOXfZ6LS2GAP3MF80RD8OM7a5BMvtnyWG6k6EGxL2PnjTpvGUgPqIdAf8M/oFqoPuLZte+/sLPJL3lEgCsX/sddnoreF2bI/DZXemGdQWwsrGyM7QChzjL7eE70Tkr3pH4R8ztvWcWaHSz3L2mD7nWQugUVZkasKsZvWUFv3lEgFGyIJ6DVhHO2yVdx1ve9WeTIjxmk76KRoBcDbJ7IRnHqAMMygvmkcUM3bFJV9IYY70wx1IHbya8xQshY/A/t0um+aTMG+gbsKgWN10zmHna5IzEhUrSTj/HHwak2HHfqYUkPOTKo01GuP6QH2w1yAWxsWcLVlM09vM/GvPpvzZKOWGtqfFln+3YKNjfqOob53j+N3WAFmmvhyg/mNYlfMX6Slv9CBTpx8K2+biNZM/8L8b6nEFhpvU4I+PBKkZkmKQzeAR4Yaoi0xMVrbOSiInE4CIv+NzbPI983T0HOYHnfweEobmxdxq8jf7k9bZN1/51s2gqwNhba8tHC6r4jhyUQI3x4fWAHlUJTI2/jt3iJ6sqpyZT26bbVdtN2UhOT23fWWSOlNiIgs33xmTCqHwdyItIriFlmO4E6Pz3vSkJBaEOaoV0Q7X+Bzmzhi6k3/teQN6Uh07RwTvL/JK429HJbE9fhBiQ6zakZDj5u1jmYWiVVsr3fDheebRTXmM/v326muyohfmWc0yaH04e1VkNR9UtWsF0gW6EFfI69V6ct/DDiCKnDVHZVu3in677pv5Bqjm4n69wYUQedv9AENqazDBvYV5kYTW51Cqpc9VCTsI= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef39e37d-92ba-44ce-cdee-08d9d5e73bd9 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2022 16:18:57.0582 (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: BZSNPCrTF77Fu1w6v7N3bbS0s3NnU5ys+CoW1cziTCSr39GHvvo6aOAkMOobVBlK00sI88qxAE2h4Hm/i225iQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3600 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10225 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 adultscore=0 phishscore=0 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201120103 X-Proofpoint-GUID: 9LHRPjdL9k7Rbezm8819AMN1l51zmorA X-Proofpoint-ORIG-GUID: 9LHRPjdL9k7Rbezm8819AMN1l51zmorA 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 - subtract the base address associated with the object. 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. [1] https://www.kernel.org/doc/html/latest/trace/uprobetracer.html Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 10 ++- 2 files changed, 181 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index cf862a1..bccc26a 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10155,6 +10155,126 @@ 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 get_rel_offset(pid_t pid, uintptr_t addr) +{ + size_t start, end, offset; + char msg[STRERR_BUFSIZE]; + char maps[64]; + char buf[256]; + FILE *f; + int err; + + /* pid 0 implies "this process" */ + snprintf(maps, sizeof(maps), "/proc/%d/maps", pid ? pid : getpid()); + f = fopen(maps, "r"); + if (!f) { + err = -errno; + pr_warn("could not open %s: %s\n", + maps, libbpf_strerror_r(err, msg, sizeof(msg))); + return err; + } + + while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &offset) == 4) { + if (addr >= start && addr < end) { + fclose(f); + return (size_t)addr - start + offset; + } + } + fclose(f); + return -ENOENT; +} + +/* find next ELF section of sh_type, returning fd and setting elfp and scnp to + * point at Elf and next Elf_Scn. + */ +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(Elf *elf, const char *name) +{ + size_t si, strtabidx, nr_syms; + Elf_Data *symbols = NULL; + ssize_t ret = -ENOENT; + Elf_Scn *scn = NULL; + const char *sname; + Elf64_Shdr *sh; + int bind; + + scn = find_elfscn(elf, SHT_SYMTAB, NULL); + if (!scn) { + pr_debug("elf: failed to find symbol table ELF section\n"); + return -ENOENT; + } + + sh = elf64_getshdr(scn); + strtabidx = sh->sh_link; + symbols = elf_getdata(scn, 0); + if (!symbols) { + pr_debug("elf: failed to get symtab section: %s\n", elf_errmsg(-1)); + return -LIBBPF_ERRNO__FORMAT; + } + + 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_debug("failed to get sym name string for var %s\n", name); + return -EIO; + } + 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) { + /* handle multiple matches */ + if (bind != STB_WEAK && currbind != STB_WEAK) { + /* Only accept one non-weak bind. */ + pr_debug("got additional match for symbol %s: %s\n", + sname, name); + return -LIBBPF_ERRNO__FORMAT; + } else if (currbind == STB_WEAK) { + /* already have a non-weak bind, and + * this is a weak bind, so ignore. + */ + continue; + } + } + ret = sym->st_value; + bind = currbind; + } + } + 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, @@ -10166,6 +10286,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); @@ -10174,6 +10295,57 @@ 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, rel_off; + Elf *elf; + int fd; + + if (pid == -1) { + /* system-wide probing is not supported; we need + * a running process to determine offsets. + */ + pr_warn("name-based attach does not work for pid -1 (all processes)\n"); + return libbpf_err_ptr(-EINVAL); + } + if (!binary_path) { + pr_warn("name-based attach requires binary_path\n"); + return libbpf_err_ptr(-EINVAL); + } + if (elf_version(EV_CURRENT) == EV_NONE) { + pr_debug("failed to init libelf for %s\n", binary_path); + return libbpf_err_ptr(-LIBBPF_ERRNO__LIBELF); + } + fd = open(binary_path, O_RDONLY | O_CLOEXEC); + if (fd < 0) { + err = -errno; + pr_debug("failed to open %s: %s\n", binary_path, + libbpf_strerror_r(err, errmsg, sizeof(errmsg))); + return libbpf_err_ptr(err); + } + elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (!elf) { + pr_debug("could not read elf from %s: %s\n", + binary_path, elf_errmsg(-1)); + close(fd); + return libbpf_err_ptr(-LIBBPF_ERRNO__FORMAT); + } + sym_off = find_elf_func_offset(elf, func_name); + close(fd); + elf_end(elf); + if (sym_off < 0) { + pr_debug("could not find sym offset for %s\n", func_name); + return libbpf_err_ptr(sym_off); + } + rel_off = get_rel_offset(pid, sym_off); + if (rel_off < 0) { + pr_debug("could not find relative offset for %s at 0x%lx\n", + func_name, sym_off); + return libbpf_err_ptr(rel_off); + } + func_offset += (size_t)rel_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 85dfef8..40cb5ae 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 Wed Jan 12 16:18:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12711562 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 81461C433F5 for ; Wed, 12 Jan 2022 16:20:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355089AbiALQUQ (ORCPT ); Wed, 12 Jan 2022 11:20:16 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:5710 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355112AbiALQTW (ORCPT ); Wed, 12 Jan 2022 11:19:22 -0500 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20CGGNpJ032143; Wed, 12 Jan 2022 16:19:06 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=kgwAE1HzLg25tBBXKQOdkt3l1uzTwkz8y2i+uqH/jao=; b=x63DPobPzn94FHzbFacq2OtKZ+764zaenrfYYfSEiYHd2LrHJ/6uzdSB2VZGbPEwJG7X ysJi8MOgwSru6rpYKVC7eaUhiiFKSlyP4zXYEfxAITP1y+CzSGFh3axL3ViCv0FGR6n/ eLnVVbMeTAy2dO4LjhY+A4MEigW7KWAMSURlrQHzT5Ns9L5OPWZnEVhpC7WLlmCJw91J OdCkyW3CQAw2a8YtojR8PTCi3F1BsLYHpv8lPz7T46lxCaPqabHEF3ADxZ6BB/qLkDeM wDPSB6l4dU8WNeUnI5G8PkRi58yvGuiOpRH4zeY8A2+Ejh6WjSMZaMOFlHozrbyGohf3 ZQ== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3dgmk9esu4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:19:04 +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 20CG8EKC073597; Wed, 12 Jan 2022 16:19:01 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2177.outbound.protection.outlook.com [104.47.57.177]) by userp3030.oracle.com with ESMTP id 3deyr051cd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:19:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gpzqA/nTPXlF+9tlS72TaGRwT8m81wP5l/3ic3XuH5IutWnSJiZHfN6Exe/FnQ8J98pfKdG9gTMjmcEgGjtEM9tHFX0rUjF9T0hZDyUjwrXIjDzI07TJQKUiO3Q2kmTqUcV7x8iVrDZNc8PkQnnETXAIwgJcKcTRYJOhlFpUOCyBP6rh61E4PN821sqebQOh5Tktkw5R/Lbhp3APQ1Au6dCN//TX5T8/Jn7lFSkTaQfJVAHZYijjAHO9F617VP+sLQrSqyuwBk9bvgZ2Qnz0sM931koY/qESguhfpEM1H1zMq+2vgw9TAnZ0jz49wX5qUrE+QNHMdND0AFDULo7kCA== 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=kgwAE1HzLg25tBBXKQOdkt3l1uzTwkz8y2i+uqH/jao=; b=D4F/VNRTZooccZCUh/CcysD441+Q/HxiZWyMfHYp+8VQ1iXVWe6i7FH21i3unNSphCjr+ZgtQrgmBRPpV8EKCsXdMeyOG6CP55tYboj7Wh9wA19xsKANVOwe3rl+OHgdd9ws3jxG6IIFOs+Z/WuJRXJcVUGAqgpwz4uNZVTiS9xYhCXecRCThlXKPYSdxyHL4ZE+nDxJIYHQEycmU278oJeiMpSCUVHDewajlClhdaB9zx7uj8yDavYh6g84OgWUISkQLntpKX4YqozCLOw+9BkIank7YQMRMI2Eaos5xsKsZk74YpT9bPXQdjB5C+rnucxo2tQmTuEE2pWgyzPPNw== 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=kgwAE1HzLg25tBBXKQOdkt3l1uzTwkz8y2i+uqH/jao=; b=QpCKxnxbiXdVuW+7LajfH1MOY7VUviciJItNmd5Y/JBubP2sGWk/ErWcagUiDlyBY4XGbXRy9iKMzxgPfsTqLtye53mZWZEfQ56OoH0tEh/a24wOhOx3OgIJUTZU+/iLZ33KWQkN9ZZraoOiaYVt19usJkZAmEUsEkZ/Lyiv9oc= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by MN2PR10MB3600.namprd10.prod.outlook.com (2603:10b6:208:114::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.9; Wed, 12 Jan 2022 16:18:59 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c%5]) with mapi id 15.20.4888.009; Wed, 12 Jan 2022 16:18:59 +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/4] libbpf: support usdt provider/probe name-based attach for uprobes Date: Wed, 12 Jan 2022 16:18:47 +0000 Message-Id: <1642004329-23514-3-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> References: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO2P123CA0080.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::13) 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: 58505e61-abc5-470f-23ec-08d9d5e73d0b X-MS-TrafficTypeDiagnostic: MN2PR10MB3600:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:792; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: P3YXobPPftARA+lGH7pJ3jjz7I4BbpdiQbJoi8bwfi8KPY3gk0MAhYgdpVmiIaEZt80SD1vjZ01Kff1HKh43q8bOGrymkUDQxEs21DcAevHpRqXhblkGjNEhsxL5ep7AHOTEtbht/kvjsFbrP7RgYiJDVT3skAYKiXdb95dTyhPeuI1zwHfIx9Txl1yQKah++rlSvp+XGstParGf10WOBKpEWoMJeO3F9x1xblcAUhci1b43OKkqQ4HRwEutjp585RBbr8mZP+x4KJnYjH2n+z5cRBJUMNIfRlfivfNIvyPhY/ImB4fbk5dIq0AAWy2oi+BfKi0/abu+sXRmaJlb3oSrtMQVHFob0Vocmn+OWywuus6D6bo5Zw8b6vpcl/ZztxZQ1oCkzztdRl6UkI9z/9dK5Va9flI3crWut540oMBoxad+vUXa9drIo3XG3JkfVVgxLaSpnu87SElPLQNFGml0aQ4bzvYL+doU2cG9bBYYidDRNz+3g3V3jp25vzHspgA/xEfcyfG0ZskX754o0BrFlJkpN3aR+8BR/6SKBhkEla6tsUu145kO3PebgoKvaMVvfQjEKjLIHY9y+/Dpkpd4DqS65wyTgePOUOkxf2DGtnu/o1Z7Vg6mjzM+XgLCyiE6F9My+S2JPktiMJQqoi7bq8YX03Hj27U+aHaQi9D5CQjcVDbrPXesQyulPq0NqqqUnU7vg6E1rDMjgQeUbw== 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)(38350700002)(8676002)(66476007)(8936002)(6486002)(36756003)(66946007)(2616005)(5660300002)(44832011)(107886003)(6512007)(66556008)(316002)(86362001)(7416002)(83380400001)(2906002)(4326008)(52116002)(26005)(38100700002)(6506007)(6666004)(186003)(508600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZZS2mtFs6w9wFKgXyqrZedjMikA0xwjOgUXjf0sKLmCNojuUh/xUVfR3rDnduUAACGicgpjj0R/G+HElKCG2YEX/EtsSkSkYkdB66VWh9YFtVgVqDHna+EDp3WxT4gk46ioZZHHGtZSwKfOX+1hpXtXyhxcteGccNFXrHGl8vUROw2m2/dec8CNpFAein0ScPPOi04yhiF4uWZLzBs/eRjLRNRpuGK+/LrxT1njiCayhX+q1PdBL9JgBYQdLxAcOl5CB1HYCp5ymsIZ4wHc7ECcHFzYJUF2QGbOZTBzWDOw+kBLWq9kW6+/scCEfvc4u27+i1PIhNN8bsvgrjYdzSmdhGBf91ZY+fYbRPkOZE5dIg/6NcRwuaSnbpmZhlt+VSMuuahm0gaHKdW9DHgqxd6fv5Sxo3Zlkq9iVPXeqeJnMs6CYNK3c3cX7jl/oE0II10qIrZNFOGXyMTEX3WoXlkEH22LSYRHuD337EtgB2Q7woYIsiwqoftD4Yw/AAZH8FGEcgznVFLvd/tEXjxKKHrrSfbhuD4uRqaY3J08eNoBf0VsiggOQoD8aadT6VxWVKxv3ICd+8Tvq1r1G33biyfRoGBXPSmtZ7kZ9W/inZbHkzWDhIQKD5cm/f/qeQWs+EPSRP2LMgHKqPMkMSotNhuLv4iUOCTHafHksEUw7rG47AQZJG7wQMftb6au0w6zUvW8k2E4wiSaX6mA0Y1febxBoTErXvF037YXciUsDoTDVc5KoVLEXI7VABRjsUMtB7UAcfplOP/KgJi9FS8kq9PrYVeUYs7Hp+0VQMXV17mdU4S5eEedLs7fxoP+05rH3p4x3mVHEGbe/lyFw0gjXpOy/ZMbAfrVolBv8xijcQrh5ws2lfVE9o80BTocOT8jIo9eUcLoB8GjN5VXk+/zNZ9+gKkKF5t4SifU5dzeDv5S0klhw45Rhrw3jM3wLvNAubjl0XI/sWpn9W2yYOrzr/BI8qb6Dejv7or3s+mpjjOFdR1MnstGNRZuOkgC0DUxjoDQJyVXop5Gsbw5rXnexVB2ouLsJn1lG3+x3pKsH0XGU11wdpDUIy2oI20eIwqh7Ixhhwag0zYyqVUi2w/DifxTYdlKiUkGns3lfxL5fp5quVi+Xm8wfMYGKHOXV+0BcusJ/ma8HfFl9nfuY/2L62Uu2FGKdLcVlsgPLo1TvnV+NFbbSS9eBjr+ZYRoBp4RS1E2FHGfPNwl+vobaj+RsNj1rgaFl3vQBBy57t7KxeSqIZWhODrVvtdBZ7kbaHj42JgQAWdPFoF1mN1fIh5eDZfGIKBPVimivnXCGvgm8xQCkiLCknoKrIvrRApg63jI+P0Bdb4zXK8T95aWYuw6FiHrZou//TgOy/eBTvsPbFXk+kPddY1LdF4TA3fEoqLzSDfKifERLmXWFSMP1L8Bv7YP+ZDlK3a+hhTxEUpSXyi/rxJnGlKTyEIPRMw+RFaLHn5kg/9HvIT8PAs/mrluuM3kgFF5B82H9Sa6W8mwjKmCAVVQAVSqlqs3p8RSNAIj6byucZfRe8BlH+sqjC3bhzXwerGkQx19fpTTzQeuf6yq0S92gzUO1bPFMciY0SJ2jwySUSqEdnODz00r+bUx9oawYW5WiZLWaHvzsGR6Utd4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 58505e61-abc5-470f-23ec-08d9d5e73d0b X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2022 16:18:59.0281 (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: eu4has1jT0YL5s2lPlqfB7JH0PgJNI8you+nrxm3r8+H1Hl9J6vGU2zZLBZim4lOc/IsWGUkudSnnTQbZ70M/A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3600 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10225 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 adultscore=0 phishscore=0 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201120103 X-Proofpoint-GUID: uRRvYqjRGXq7jxJQBmIdtAEv63Uy5YTy X-Proofpoint-ORIG-GUID: uRRvYqjRGXq7jxJQBmIdtAEv63Uy5YTy Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Add support for name-based attach to Userland Static-Defined Tracing (USDT) probes via lookup of ELF notes associated with probe definition. ELF notes are consulted for probe offset, and - if "is-enabled" style of probing is in use - semaphore offset. Signed-off-by: Alan Maguire --- tools/lib/bpf/libbpf.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.h | 9 +++++- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index bccc26a..cdcd799 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10275,6 +10275,52 @@ static ssize_t find_elf_func_offset(Elf *elf, const char *name) return ret; } +struct sdt_note { + uint64_t pc; + uint64_t base_addr; + uint64_t semaphore; + const char provider_probe[256]; +}; + +/* Find offset of USDT probe in object specified in ELF notes. + * Note may also specify semaphore offset, record value in *semaphore_offp. + */ +static ssize_t find_elf_usdt_offset(Elf *elf, const char *provider, + const char *name, ssize_t *semaphore_offp) +{ + Elf_Data *data = NULL; + Elf_Scn *scn = NULL; + + while ((scn = find_elfscn(elf, SHT_NOTE, scn)) > 0) { + while ((data = elf_getdata(scn, data)) != 0) { + size_t name_off, desc_off, off; + GElf_Nhdr nhdr; + + while ((off = gelf_getnote(data, off, &nhdr, + &name_off, &desc_off)) != 0) { + struct sdt_note *sdt_note; + const char *probe; + + if (nhdr.n_namesz != 8 || + memcmp((char *)data->d_buf + name_off, "stapsdt", 8) != 0) + continue; + sdt_note = (struct sdt_note *)(data->d_buf + desc_off); + if (strcmp(provider, sdt_note->provider_probe) != 0) + continue; + /* probe is after null-terminated provider */ + probe = sdt_note->provider_probe + + strlen(sdt_note->provider_probe) + 1; + if (strcmp(probe, name) != 0) + continue; + + *semaphore_offp = (ssize_t)sdt_note->semaphore; + return (ssize_t)sdt_note->pc; + } + } + } + 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, @@ -10286,7 +10332,7 @@ static ssize_t find_elf_func_offset(Elf *elf, const char *name) size_t ref_ctr_off; int pfd, err; bool retprobe, legacy; - const char *func_name; + const char *func_name, *usdt_name, *usdt_provider; if (!OPTS_VALID(opts, bpf_uprobe_opts)) return libbpf_err_ptr(-EINVAL); @@ -10296,11 +10342,25 @@ static ssize_t find_elf_func_offset(Elf *elf, const char *name) 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, rel_off; + usdt_provider = OPTS_GET(opts, usdt_provider, NULL); + usdt_name = OPTS_GET(opts, usdt_name, NULL); + if (func_name || usdt_name) { + ssize_t sym_off, rel_off, semaphore_off = 0; Elf *elf; int fd; + if (func_name && usdt_name) { + pr_warn("both func_name and usdt_name cannot be specified\n"); + return libbpf_err_ptr(-EINVAL); + } + if (usdt_name && (func_offset || ref_ctr_off)) { + pr_warn("func_offset argument, ref_ctr_off option should be 0 when usdt_name is used\n"); + return libbpf_err_ptr(-EINVAL); + } + if (usdt_name && !usdt_provider) { + pr_warn("usdt_provider and usdt_name must be supplied\n"); + return libbpf_err_ptr(-EINVAL); + } if (pid == -1) { /* system-wide probing is not supported; we need * a running process to determine offsets. @@ -10330,20 +10390,32 @@ static ssize_t find_elf_func_offset(Elf *elf, const char *name) close(fd); return libbpf_err_ptr(-LIBBPF_ERRNO__FORMAT); } - sym_off = find_elf_func_offset(elf, func_name); + if (func_name) + sym_off = find_elf_func_offset(elf, func_name); + else + sym_off = find_elf_usdt_offset(elf, usdt_provider, usdt_name, + &semaphore_off); close(fd); elf_end(elf); if (sym_off < 0) { - pr_debug("could not find sym offset for %s\n", func_name); + pr_debug("could not find sym offset for %s\n", func_name ?: usdt_name); return libbpf_err_ptr(sym_off); } rel_off = get_rel_offset(pid, sym_off); if (rel_off < 0) { pr_debug("could not find relative offset for %s at 0x%lx\n", - func_name, sym_off); + func_name ?: usdt_name, sym_off); return libbpf_err_ptr(rel_off); } func_offset += (size_t)rel_off; + if (semaphore_off) { + ref_ctr_off = get_rel_offset(pid, semaphore_off); + if (ref_ctr_off < 0) { + pr_debug("could not find relative offset for semaphore for %s at 0x%lx\n", + usdt_name, semaphore_off); + return libbpf_err_ptr(ref_ctr_off); + } + } } legacy = determine_uprobe_perf_type() < 0; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 40cb5ae..fcad6b1 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -439,9 +439,16 @@ struct bpf_uprobe_opts { * argument to specify argument _within_ the function. */ const char *func_name; + /* name of USDT (Userland Static-Defined Tracing) provider/probe. + * Offsets of USDT probe and associated semaphore (if any) are found + * in ELF notes. Note that if usdt_name is specified, func_offset + * argument and ref_ctr_offset values should be zero. + */ + const char *usdt_provider; + const char *usdt_name; size_t :0; }; -#define bpf_uprobe_opts__last_field func_name +#define bpf_uprobe_opts__last_field usdt_name /** * @brief **bpf_program__attach_uprobe()** attaches a BPF program From patchwork Wed Jan 12 16:18:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12711561 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 E95D4C4332F for ; Wed, 12 Jan 2022 16:20:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355167AbiALQUO (ORCPT ); Wed, 12 Jan 2022 11:20:14 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:5354 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355064AbiALQTW (ORCPT ); Wed, 12 Jan 2022 11:19:22 -0500 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20CGEQjM015186; Wed, 12 Jan 2022 16:19:06 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=QMZQyE0n1W2ZW+XIn7A0LzylfKOc5TG2wnSwt58nmfs=; b=Nm9bgW0WiQ9CTCR4a03JysbarquS3UbTEq1aLiXy6duBUlUpSxphCgY44m+Q4syxBTJ1 QVezU4hO8a2sSlZcgFqQfanogmZEOmfmNG9jyTMeoxFZdU/J8cV5nDBca6S1g9wWLDxS QOI+6pbKwZ8E3rRjQyes+YTKowOH6Ne/lopWi1lfSbgoljB3snSuK99DU8u1GC6do0/S I1NAlpDQezK1VN+aLWlDMnbpFeBg4sF+IPn9P3cERfrExX7B/e8upxcp6HzKT9+hgbWA p8psKR9GquZB2RqScFcxYzQIdbCsPp7tNS2TZn3LtkUIJ1FOOC6BX6dbGKlUt7kSQIf1 0g== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3dgjtgf4bu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:19:06 +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 20CFuAVJ018162; Wed, 12 Jan 2022 16:19:03 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2106.outbound.protection.outlook.com [104.47.70.106]) by aserp3020.oracle.com with ESMTP id 3df2e6fut0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:19:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jhS59WoZeOqfjFo5tDklSzWz3o4TsGb2DjQGNojVqNQ8wEa5KhfGDu8WjhMBm6Gf5vJiMbd/05wXo6rTnMEJFADBGKEtqFrWWdmsQs3uHIJSPXHHb3+HdXCfpIjw43w3cf64e5dUrACej+OhbSlvTJ0fSogIPE2R50nwwbale+pkXin2LdAYAUMUwLr6oyXwVIOBDTVsOOs6CQZtFZNv4mTq7k1pgnuLnGB1/xtaKLfFG95GKmNUPXxpCGssYGPTjqIP4Ko6j3qUeZMgoira6i8RHreUDRmEQk4rZYZeSTGO56Q3kJgsprjin0bSLOuSBu8ANtpsLFDLJTWddf4mUw== 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=QMZQyE0n1W2ZW+XIn7A0LzylfKOc5TG2wnSwt58nmfs=; b=feRxDu3YZqhL4wGebnVJ7KS3iE7okyEe+J9sCaLNH4s9ECz1JhZVHSQq6nS1oM+L1fHTGCfF8Uic3yi69v7juLbRWAAMklS8meXnS6OVO2LVPEhaabk0fv9tc/+/ydHcJEtlMse4aeoUGM5dWEFOuDLXZ8qtDnmo3eBeLuu01lyvfdFF1qg9xgHgqSuYhwZUIgO3IkjZIvgjgC4hoMu0S9ebe/NEGOwhxBezyPCFAk12RwH9BEbQw41OAJuQEEM3aMfiS6b4xH/jGNkhOeuqDxOMorGF8J1PpfCg8aBr0szi/WcX9p20ws2jyyT8yBd9uYZAs2B0oVUPCxYLAKLquQ== 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=QMZQyE0n1W2ZW+XIn7A0LzylfKOc5TG2wnSwt58nmfs=; b=iP2BVMEKkWLpX8dHylTAf7jBwf+ZwwkECqAwWTNPy43aYf/YcTYZj9JqPbFwe94KMpeMFfUUyXBjvgnO3GPzfxIJT4Q4TiWwkTt2jZ+RUlnV5lcld6hA9jLu09qBI5nz9Q6p9jhl2U3pvgzUgiS6F8iTyLW5HWohipUPrC79NxI= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by MN2PR10MB3103.namprd10.prod.outlook.com (2603:10b6:208:128::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.9; Wed, 12 Jan 2022 16:19:01 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c%5]) with mapi id 15.20.4888.009; Wed, 12 Jan 2022 16:19:01 +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/4] selftests/bpf: add tests for u[ret]probe attach by name Date: Wed, 12 Jan 2022 16:18:48 +0000 Message-Id: <1642004329-23514-4-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> References: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO2P123CA0080.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::13) 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: 4ca9fb68-3dbc-4f36-f180-08d9d5e73e32 X-MS-TrafficTypeDiagnostic: MN2PR10MB3103:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:93; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cmTBGrhwMi4i6yZgF9BzBtR8mo9V6i3NW9FiCq5ef/rrXM2P6Q9zEIdeY5B3WMYWnGwyaWfG8tsDbuN9SLvqeBYsWjZn2Vup47X18P72alovvubFedLEVvEujQvL1xvzKT/oTiIFdFRdg3+hOwyZEUZoRlPStAgxsm1HMBHmCeW0JXwF/3+7tbS4ML0v9HMUzdQLdoaaW8LPmJd80RK4YsXEejFhXp/31X7YYeS5DIxuVKPbfBDoOCdmNTfpthes+jYbvvicOQ+r9TDdVcdV7vy4vNV6An0v7DbJKFRljyQuuXYJwZ6TdVbJA6YXmfxkEN/TSeV6Ddq8HTcdUajDMvFyMJ82X+JfuwjZG659u7NE+W6xnKHrglI16Q7AVtvyxRPbHUVRyJOleBRZp6GDcej5rLtId5BX+8TbomFZ+GBppAGvOpQjau3DLwWBSmZZALEN2CSB8ZjfB7vBs9jKHIFZJxGg+E/fq0VgaJIvqJxRF6vT4uLOdT8MMzw7I1WyMWqwQ9lPSOtAA0IpD8k5ub0XFCWKa4sHsIZWFzY4jcDvJC+2OLGzwlIZHnkh/nsTMIp0Zp0WgboBG4VonnADd5GfeJt7jOYqiX7REtsE3JfHAgnL43XO6gaI0Vcp+jsvT5Io3S6/zGTf7/HqtvY+ocrEtdKIGtg/jCI5Ug+VwYptRK+FthzrmwyLyURUETjJfu5a5M1g64ETkqafqv1cZA== 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)(6666004)(2906002)(86362001)(508600001)(6486002)(6512007)(66556008)(66476007)(66946007)(52116002)(5660300002)(7416002)(2616005)(38350700002)(38100700002)(8676002)(44832011)(83380400001)(36756003)(186003)(26005)(6506007)(4326008)(107886003)(316002)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KFrwUUQo/MIxoDhKVAxknpHpeWxiN8p4fvSh+yX4GOkrIs4SggKUL0/7mpvKqPzxmvvDvIHxW7xBDpnF6qyy/gJiw5yGuJ5qIq41Xc5lp7Uk57sRomjkrLdEtQBjouJM6oqALkV6yTAFJMDTIvk46ErWmYE4TfJydhFz/7mEc8tAwrO/Sok+d+ISxWgh6J82mT8CsoukbYlN4SYuXx770cy0h6jdBwdw+lD2UOBGGhJFX6LdLxhdlfGHhDtQUkXRSL49VwvG2TlVDu/QjLqTlv25QU8Ue049Kz5UGQ70KomnuZXkF3+5hXj6+ZX/TsaXZYCFF5SMNXnGtGs366I+ujwN61W9wzJO0dL76zm/779EotyOPtjCAsnD04gZj0a72tvhuJ8t9IMh1TOj4BlBtlIeSrUQ8317bS8m9JyVRydfOZI8SfmNCHWyqWsHYbLrKUiWznzux+dt1y+mkrv20Cw66Hzn92i5SsP2RD2ZvZpMhp4FFerjKa3hqU59MivWBguvpYFECt3Ia0K8YasHDP47uF5KtC8Lo1D3drw1KrvRbYZefmG87X4olJzkKvRInTbm1IlYwVq+MFDPgqeXq2+ARp9uMPJaWd9OSOYxh9gPwxPG/RyAh1l/ncbFiA8DeAVDdKJ2WAzgvhY6ZKIngG7BpRZa1vLc4aBJsSRo1nuiHix5axGPBsEifOdug9WGP2pqCpGWhqtWV8PGOA9QRI6m7pZB3BKFIXjGfvf0CXTQ3xRL5w1r82bifaDHQK/6RO5hPXg+gJh2cB5ug8msmZCXZ9DFEFMAmIxhdeye+HNziRQ5zN1ckgp6PrmwJUnlUv9yFcWYaZQWtgMNcud1V4zVK5XFXTqTaWuqLBtqf6nvYHpndcx0RF5zr6s7yG1hFMbvtNUCj7E7zmWp87hotcf2cNOZ8t4AIY6KiS4Mppm0imEz3kA0L44vTq6LluHElH+KhApJtHSebO/GoBSVVvhdOlyfuL6/00515CR5Dd4B7qwKuaQNy8wkgLgzz8xvBYrchTsHtciwS6qeulEEMYrTvMX/adpGpPD/WcsahfCJyDrs89KUixZ50zv1+nhSNuS6DzXj9HvCTHUlLtk9+JFQRAgZoDYt6Qq1Z5NIlxb9Oam0uS7vtgaGHT/cNrpJRzByOzJA1AX84tShoJO8+CwJORp3dQrI6gDBfevQf8DXDqZ38jyBvvWE7YU0uuzBsEEi+87chqCjeFRJMwlOJXosl8q6UHsout23+xkoUSN28JKobjqePQ9zHSHX3EdnMHykeBTbwe8N1Le6hRAV6eH8Qzr+bM5diZDRD82cnygHpFlBlB7+L1ttvzfTuIAk7ZAXm1wYoKoLEnD5RszEZ16hymKrdNz/C5wSSV4yfMKDeWlIa4mQBQbuftP9tPs/hNEpuVNgRbfC3EAJmWWLYf03wiIS3+tzmhNicA/9LO7SSnPiVrYwizdXo/YoNzme3CSOu1e1FAzTPPDSMaau8d1pBvYzMtOUmpD5RGjkRoGQJvPtlLgAamLKmjFPh8NnNFua9CD1dDUHuVa0xB5Kv8GrOz+6woYVshliVbOa69us/Adv/3wVc6DToCzq9DcuPTmq0lGOazWmRzkfIyEGJItvcqj7I9cE112T/2zGm4E= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4ca9fb68-3dbc-4f36-f180-08d9d5e73e32 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2022 16:19:01.2010 (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: G5cCEri1qG/h+SyzfM++k6vDXJs3VnecKy+TUeQJzqYSKMRmfC0pOTfXu8x0WPptAVVaXe8j+A3sirbRgo/gAw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3103 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10225 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxscore=0 adultscore=0 suspectscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201120102 X-Proofpoint-GUID: 2Y-4iVZeqFwsRTVwtjIGeso6YaPqtsGA X-Proofpoint-ORIG-GUID: 2Y-4iVZeqFwsRTVwtjIGeso6YaPqtsGA 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 a local and library function succeed for uprobe and uretprobe using new "func_name" option for bpf_program__attach_uprobe_opts(). Signed-off-by: Alan Maguire --- .../selftests/bpf/prog_tests/attach_probe.c | 41 +++++++++++++++++++++- .../selftests/bpf/progs/test_attach_probe.c | 16 +++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c index d0bd51e..521d7bd 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -10,12 +10,18 @@ static void method(void) { return ; } +/* attach point for byname uprobe */ +static void method2(void) { + return; +} + 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_byname_link, *uretprobe_byname_link; struct test_attach_probe* skel; size_t uprobe_offset; ssize_t base_addr, ref_ctr_offset; @@ -92,7 +98,30 @@ void test_attach_probe(void) goto cleanup; skel->links.handle_uretprobe = uretprobe_link; - /* trigger & validate kprobe && kretprobe */ + uprobe_opts.func_name = "method2"; + uprobe_opts.retprobe = false; + uprobe_opts.ref_ctr_offset = 0; + uprobe_byname_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname, + 0 /* this pid */, + "/proc/self/exe", + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(uprobe_byname_link, "attach_uprobe_byname")) + goto cleanup; + skel->links.handle_uprobe_byname = uprobe_byname_link; + + /* test attach by name for a library function */ + uprobe_opts.func_name = "usleep"; + uprobe_opts.retprobe = true; + uprobe_opts.ref_ctr_offset = 0; + uretprobe_byname_link = bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe_byname, + 0 /* this pid */, + "/proc/self/exe", + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(uretprobe_byname_link, "attach_uretprobe_byname")) + goto cleanup; + skel->links.handle_uretprobe_byname = uretprobe_byname_link; + + /* trigger & validate kprobe && kretprobe && uretprobe by name */ usleep(1); if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res", @@ -105,6 +134,9 @@ void test_attach_probe(void) /* trigger & validate uprobe & uretprobe */ method(); + /* trigger & validate uprobe attached by name */ + method2(); + if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res", "wrong uprobe res: %d\n", skel->bss->uprobe_res)) goto cleanup; @@ -112,6 +144,13 @@ 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; + 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..efa56bd 100644 --- a/tools/testing/selftests/bpf/progs/test_attach_probe.c +++ b/tools/testing/selftests/bpf/progs/test_attach_probe.c @@ -10,6 +10,8 @@ int kretprobe_res = 0; int uprobe_res = 0; int uretprobe_res = 0; +int uprobe_byname_res = 0; +int uretprobe_byname_res = 0; SEC("kprobe/sys_nanosleep") int handle_kprobe(struct pt_regs *ctx) @@ -39,4 +41,18 @@ 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; +} + +SEC("uretprobe/trigger_func_byname") +int handle_uretprobe_byname(struct pt_regs *ctx) +{ + uretprobe_byname_res = 6; + return 0; +} + char _license[] SEC("license") = "GPL"; From patchwork Wed Jan 12 16:18:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Maguire X-Patchwork-Id: 12711563 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 82E4FC433F5 for ; Wed, 12 Jan 2022 16:20:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355095AbiALQUU (ORCPT ); Wed, 12 Jan 2022 11:20:20 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:31368 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355114AbiALQT3 (ORCPT ); Wed, 12 Jan 2022 11:19:29 -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 20CGJBbj009449; Wed, 12 Jan 2022 16:19: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=1g4DumJDGdbQ1l13iMGBSErBU8FnprHqZ80LsebhFWQ=; b=vAly4v5M0DSeRrtAYg5H3CyjWn3MQt9Hg1fCm1x7Zm3zQrKApTcFCqlG1+N01x5crtA6 6jrYAJ5Km0w/RANyIQnhKzYTZne4hF+mi2Au2ZGDWf4TZYXJxrNdgSaEZQ64kEaPXfca o4zxbCmQjX+Fkc7wty/43Xlk8gq45W2j3cX81mVBNXFjMixpkunqUij2lidXGFaeS6K1 KQe40e87Y97w8P181JTrJu/VLsjIH38c1YnomgkA3S4jMmObGZonnXn96KeWV2P6/1kl 4yrcs1VdePhQdd1xyZRMY/Qu5d3nd3mJjOMTwKt+P2I0FGM4F5Zs6MiDDsC3InbY8uTq IQ== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3dgn74ekud-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:19:13 +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 20CG8DbC073456; Wed, 12 Jan 2022 16:19:06 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2177.outbound.protection.outlook.com [104.47.57.177]) by userp3030.oracle.com with ESMTP id 3deyr051gm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jan 2022 16:19:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JrCxLtl8QTNfFX/NYPr51z44YFCRbbN0GgIR+hAsCCgew5j4KRo4CSTZRfa3vSb/+W8fcTuXFjEib3Z7vFbMQyCgl+F1m1f937SCEIz9cpVPs0qVpBRlUyIC6/vn0EF0UmC3F+RTTzzRMZ7gNCya0zrIysb7rG0Bd6RCihHG/PQF7vBKmh9PLaln6473NcjbRmnz3T0hZZh7rgvWulylev3HE8GONMIYApfM6opJi9RayeBDmE7g4pHuNtgscv/XgDHtNFcGc/fjnI4ELjoBRqVVHdj6srJNy7ylwhyvFXznP7NlBzrfAYqpqc7Fx0wOe5CiCApbx8HjO6wSiZd0Hg== 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=1g4DumJDGdbQ1l13iMGBSErBU8FnprHqZ80LsebhFWQ=; b=jXquegx/F5PcnDIE3vm9HxzzaWCZYi6jJtt32imKz8ZHDiYD58HGX3iaUP8CiAdNTyDO03f+6zOMNru6N3foZ3i3Lz9z+1XAaDJlscYauA4WwqTpV+J6f7sz+UmzmYHWF3qOdmBM8hO5L85scWJ1HELEUoe2TWd1CuwG8N+6sFlyhgSzBh35oaEJGVntLOTpmJxa1BjtFWObzt2yFj+QwKCT2UJD0t0JQcsRkyrB3xWxPuzfn8697Q+wCBuFL58NvTHE0uoJ5TITwqBD16Htou8gMvabuO1+wLVZSRO7OTO/VeIxBsKvRuDM6DVbRWCC1XailxHFejT0ful3ZBXL1g== 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=1g4DumJDGdbQ1l13iMGBSErBU8FnprHqZ80LsebhFWQ=; b=fCZePrfIFF3jKnkcH5Zdm0UG6uEQ7OF0IA9G267/4J2tSbv66OlT6m8k/4t8t6arnrWEZ+et0Iif1bHJ1Wevd+udwXjh3/NVxtIgSl23tnzl8qNXDEMBGVRZ5rwcu0w7+ZTeodtawqhlm7otqGQA/CLdng0IYsFJ2UPxV16erL8= Received: from BLAPR10MB5267.namprd10.prod.outlook.com (2603:10b6:208:30e::22) by MN2PR10MB3600.namprd10.prod.outlook.com (2603:10b6:208:114::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.9; Wed, 12 Jan 2022 16:19:03 +0000 Received: from BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c]) by BLAPR10MB5267.namprd10.prod.outlook.com ([fe80::31c1:e20b:33a1:ba8c%5]) with mapi id 15.20.4888.009; Wed, 12 Jan 2022 16:19:03 +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 4/4] selftests/bpf: add test for USDT uprobe attach by name Date: Wed, 12 Jan 2022 16:18:49 +0000 Message-Id: <1642004329-23514-5-git-send-email-alan.maguire@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> References: <1642004329-23514-1-git-send-email-alan.maguire@oracle.com> X-ClientProxiedBy: LO2P123CA0080.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::13) 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: 719886aa-b66d-4f5e-7639-08d9d5e73f85 X-MS-TrafficTypeDiagnostic: MN2PR10MB3600:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4502; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OyegCkHxduNODGXVp9Ttf2fjToEfRPfPzSr39MIAVVUUYKi30IhxufJ5DSP2j0TIRUL7QZ6erJERKbpZmpBkgZ/pq6gaFcYiYTE6zmn+d/prdi/0Dpw76PwRSGn3cSHPSp3/NB09ScFCfUQu4k3K+0EdDox5tIHXyik9BikvnhQLHf0BHgPQaVOlApCIy3pNO4d/DAUnJTsMrG0aJ4bg11KaJfy8MQD3PwsPWGi3cw3TKMpRZ/5GiMd0Nup5eAuK2NHRS8KFxqU5BkvVGVNMugpX+R597qisgNyY87eE3KchpwyF/vvhjn3RjrscYpPUh8fa8SsmQiWj/bMGiw8Z3UjlNyKRGx17lGzj5NBwgSE2rC2BWFpVp9fLLl/cW74wXhAqPD6tUBEv4FcBERto/gCxKIslru29BX5KSSGhxWluiALTvMpU0zoUO782wURv9p/CCq4wBqr//juyxQYkbsNki+5RrJP6HtHRX7nyhN6QIOgMNCXmlRLRygbQCERAVK7t+0fgubmZYEezJmBHzmd4wbX1ou+0dZnGetD2evp1fxFLaNjDY1vt4AvGyhhOzdBdmivZNBz465yaqCtyyCIn1cXUYhJ6KMdAPhbLEO7RG25DblNos1vGmzPtTajeEC53KIHBP8uDIKPpuatzs2VEoLXlu4Fn0mFIFIFyu+V3sS30fZUNg+zIfuE7LI4r43Yxff/QmI9x1E6R8LQIow== 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)(38350700002)(8676002)(66476007)(8936002)(6486002)(36756003)(66946007)(2616005)(5660300002)(44832011)(107886003)(6512007)(66556008)(316002)(86362001)(7416002)(83380400001)(2906002)(4326008)(52116002)(26005)(38100700002)(6506007)(6666004)(186003)(508600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 85+N3TvmLHj2ZDEK8U2N1+Clv2M2Qa1ggoTUgMBL8PFcznrdkZFMBcb/7P/SjBAmosuzZTRGRMq0lr0/X87M5+DRLKymi9RElryp1hNcyGZRt1W+M5S6fZ7hHwoktElkCuXqzt93vUFfHpYHchhGTpz6ev4KS/0VCH5WRMcYb5pdKzh2suA2N1YyZgCGnb7O7N7ywL/4lCG4lPWzYGicrGMNNNUVNrRd9+X6VUz6R7E3wO7ylQ1Sw3tFZnU1UmgKlMHQHABSofl/dAm4hRlTX5oEz1VMeHSUnwbdQEywk77yxBNjbuVfv5rTmyafCAj/WSg+Y19mdA/oT66RQAooPV81H4xM5xPIHkBvv0rhIc/jXdAJstRmOvj1Qd9MITLjNscd4QeAS24ZnwWkq4x3ROYYYpoJ/piSHcVqcezdMnM0Aaw+NBKOWKtC26Lz5qbtfygko/Z9jNodYhRwR0UptNi3P9AddrsgZJKQktsiAVI/lQjvsXf8NR4NBq5y2Opi4PS2v+I719IaNxqmvbPK80evnx/nVpuhjSeAQ92MqkwaMxXctYpzszdRfvcS75HmZsnXCD7wpHcPIKx5OEE/oySc/hwORbIstAp6Wnv2wIoCTo6aN1khvUGI3W99mavN2fKqHWEOOJm/JohJt1JHjCNpk2xENty5nzHgjhDXEgjFYuuIzVmFDedzrPzBfW7koqiWnkMl9Ts8JThBSZm6TgvS1kuio9ybZ5rzIhm8lCfqk/0m0DZOcUaVqv98zVnnj8ZPshx2131/ZEVMEIRsGc66PkCXEaKLK7NhnwobMlPQssKkWoBfQgsTOOebr1IVsnj08hYcLtcjAQvHiokHyb/LrmI1NwxeEu1VUK3VbyXY4kWe4rWW5fj/CiPmeb/ABtk+dTs+0eRt+yEkUrIQ4GN9VJPQBI7004/obKwU4mn2oPEpiNDi9lpfmdaJ/PFz7UlNmFcGAD0ucvBmRRTuWUtgdmvwz0fM5PYgYlBF+FVfzCJKiVeJ8T+0dmlOAhcQVSRISWK2Ho4L0nmplwkKyOcnHMOHh/r0YlPHofYwpad9XrO9kP5T7yKVKPf0f6aS3gWzAPFOcYGiRVr3Ldw4qAtmLeQKGTHy4pNGj0ZEumsyfUva0/5OzH2Pzy2+ex9TMff4Q/vdvsDFzGVVXw3gfrsT9J9+Y2UdqC0xlBrByjxuiyvhLxC7sW6GQVvlSqCwCfwyokCmiIyCJoZZnRpaOEhJnH3XdyOvPXq97rqbhTYvafAu1d08H3IpC+66pzfohWAOebMtl6C0kGsSYRC/6/yFpgCd0NWR4f7URA4xwmuHm6j0JTYsH5iHDvCioZQbeniEBSbfqMzCjfi3HxAbmPXBImwwQhoN0LDYQn2C9ZwL5eyXwgHw0z4fQ8pedR3Rf3Ol3VW6ZpbsNMCahCsQkDTjEkMCj16dIrYwcd4+uH7QCp7cg4yCDPS4N002JpfA3VflenUs2cn59Az9L1KfcoUFhwJiPo4VQ+IusJUpGsonvBz3j9kPctNg7y3ERQApMH15bwYkkeoVgyzmvgkezdAqYSxnPDs5fTe7xXPL+Jyv1z8nBHtAHYpCejTAb/keqI1UbCdKVMu5z+pVzY94pYEbx9dIU7fVCIwSm01cVKY= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 719886aa-b66d-4f5e-7639-08d9d5e73f85 X-MS-Exchange-CrossTenant-AuthSource: BLAPR10MB5267.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jan 2022 16:19:03.2164 (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: G6XJ6D8CZa3LbQDTT/3L/tsTA+Pory8BQzcskKycoVLLPv7ZN5Vfg95u3MShHZapoLpuXXJm6T75MX40hTCU3A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR10MB3600 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10225 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 adultscore=0 phishscore=0 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201120103 X-Proofpoint-ORIG-GUID: tSlmSaKyZA5fGQE5h9-E78I69wh12SPv X-Proofpoint-GUID: tSlmSaKyZA5fGQE5h9-E78I69wh12SPv Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC add test to verify attaching to USDT probe via specification of provider/name succeeds and probe fires. Depends on presence of as verified via feature-sdt Signed-off-by: Alan Maguire --- tools/testing/selftests/bpf/Makefile | 34 ++++++++++++++++++++++ .../selftests/bpf/prog_tests/attach_probe.c | 33 +++++++++++++++++++++ .../selftests/bpf/progs/test_attach_probe.c | 8 +++++ 3 files changed, 75 insertions(+) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 42ffc24..3a7e3f2 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -3,6 +3,14 @@ include ../../../build/Build.include include ../../../scripts/Makefile.arch include ../../../scripts/Makefile.include +# needed for Makile.feature +ifeq ($(srctree),) +srctree := $(patsubst %/,%,$(dir $(CURDIR))) +srctree := $(patsubst %/,%,$(dir $(srctree))) +srctree := $(patsubst %/,%,$(dir $(srctree))) +srctree := $(patsubst %/,%,$(dir $(srctree))) +endif + CXX ?= $(CROSS_COMPILE)g++ CURDIR := $(abspath .) @@ -32,6 +40,26 @@ ifneq ($(LLVM),) CFLAGS += -Wno-unused-command-line-argument endif +FEATURE_USER = .bpftest +FEATURE_TESTS = sdt +FEATURE_DISPLAY = sdt + +check_feat := 1 +NON_CHECK_FEAT_TARGETS := clean docs docs-clean +ifdef MAKECMDGOALS +ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) + check_feat := 0 +endif +endif + +ifeq ($(check_feat),1) +ifeq ($(FEATURES_DUMP),) +include $(srctree)/tools/build/Makefile.feature +else +include $(FEATURES_DUMP) +endif +endif + # Order correspond to 'make run_tests' order TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ test_verifier_log test_dev_cgroup \ @@ -108,6 +136,7 @@ override define CLEAN $(Q)$(RM) -r $(TEST_GEN_PROGS) $(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED) $(Q)$(RM) -r $(TEST_GEN_FILES) + $(Q)$(RM) -r $(OUTPUT)FEATURE-DUMP.bpf $(Q)$(RM) -r $(EXTRA_CLEAN) $(Q)$(MAKE) -C bpf_testmod clean $(Q)$(MAKE) docs-clean @@ -434,6 +463,11 @@ $(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c ) > $$@) endif +# support for adding USDT probes? +ifeq ($(feature-sdt), 1) +CFLAGS += -DHAVE_SDT_EVENT +endif + # compile individual test files # Note: we cd into output directory to ensure embedded BPF object is found $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c index 521d7bd..5bb24927 100644 --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c @@ -2,6 +2,17 @@ #include #include "test_attach_probe.skel.h" +#if defined(HAVE_SDT_EVENT) +#include + +static void usdt_method(void) +{ + DTRACE_PROBE(bpftest, probe1); + return; +} + +#endif /* HAVE_SDT_EVENT */ + /* this is how USDT semaphore is actually defined, except volatile modifier */ volatile unsigned short uprobe_ref_ctr __attribute__((unused)) __attribute((section(".probes"))); @@ -22,6 +33,7 @@ void test_attach_probe(void) struct bpf_link *kprobe_link, *kretprobe_link; struct bpf_link *uprobe_link, *uretprobe_link; struct bpf_link *uprobe_byname_link, *uretprobe_byname_link; + struct bpf_link *usdtprobe_byname_link; struct test_attach_probe* skel; size_t uprobe_offset; ssize_t base_addr, ref_ctr_offset; @@ -121,6 +133,27 @@ void test_attach_probe(void) goto cleanup; skel->links.handle_uretprobe_byname = uretprobe_byname_link; +#if defined(HAVE_SDT_EVENT) + uprobe_opts.usdt_provider = "bpftest"; + uprobe_opts.usdt_name = "probe1"; + uprobe_opts.func_name = NULL; + uprobe_opts.retprobe = false; + usdtprobe_byname_link = bpf_program__attach_uprobe_opts(skel->progs.handle_usdtprobe_byname, + 0 /* this pid */, + "/proc/self/exe", + 0, &uprobe_opts); + if (!ASSERT_OK_PTR(usdtprobe_byname_link, "attach_usdtprobe_byname")) + goto cleanup; + skel->links.handle_usdtprobe_byname = usdtprobe_byname_link; + + /* trigger and validate usdt probe */ + usdt_method(); + + if (CHECK(skel->bss->usdtprobe_byname_res != 7, "check_usdtprobe_byname_res", + "wrong usdtprobe_byname res: %d\n", skel->bss->usdtprobe_byname_res)) + goto cleanup; +#endif /* HAVE_SDT_EVENT */ + /* trigger & validate kprobe && kretprobe && uretprobe by name */ usleep(1); diff --git a/tools/testing/selftests/bpf/progs/test_attach_probe.c b/tools/testing/selftests/bpf/progs/test_attach_probe.c index efa56bd..0c93191 100644 --- a/tools/testing/selftests/bpf/progs/test_attach_probe.c +++ b/tools/testing/selftests/bpf/progs/test_attach_probe.c @@ -12,6 +12,7 @@ int uretprobe_res = 0; int uprobe_byname_res = 0; int uretprobe_byname_res = 0; +int usdtprobe_byname_res = 0; SEC("kprobe/sys_nanosleep") int handle_kprobe(struct pt_regs *ctx) @@ -55,4 +56,11 @@ int handle_uretprobe_byname(struct pt_regs *ctx) return 0; } +SEC("uprobe/trigger_usdt_byname") +int handle_usdtprobe_byname(struct pt_regs *ctx) +{ + usdtprobe_byname_res = 7; + return 0; +} + char _license[] SEC("license") = "GPL";