xref: /freebsd-14.2/lib/libsysdecode/linux.c (revision b69ae1a3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Dmitry Chagin <[email protected]>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/types.h>
32 #include <sys/proc.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <sysdecode.h>
36 
37 #include "support.h"
38 
39 #ifdef __aarch64__
40 #include <arm64/linux/linux.h>
41 #elif __i386__
42 #include <i386/linux/linux.h>
43 #elif __amd64__
44 #ifdef COMPAT_32BIT
45 #include <amd64/linux32/linux.h>
46 #else
47 #include <amd64/linux/linux.h>
48 #endif
49 #else
50 #error "Unsupported Linux arch"
51 #endif
52 
53 #include <compat/linux/linux.h>
54 #include <compat/linux/linux_timer.h>
55 
56 #define	X(a,b)	{ a, #b },
57 #define	XEND	{ 0, NULL }
58 
59 #define	TABLE_START(n)	static struct name_table n[] = {
60 #define	TABLE_ENTRY	X
61 #define	TABLE_END	XEND };
62 
63 #include "tables_linux.h"
64 
65 #undef TABLE_START
66 #undef TABLE_ENTRY
67 #undef TABLE_END
68 
69 void
70 sysdecode_linux_clockid(FILE *fp, clockid_t which)
71 {
72 	const char *str;
73 	clockid_t ci;
74 	pid_t pid;
75 
76 	if (which >= 0) {
77 		str = lookup_value(clockids, which);
78 		if (str == NULL)
79 			fprintf(fp, "UNKNOWN(%d)", which);
80 		else
81 			fputs(str, fp);
82 		return;
83 	}
84 	if ((which & LINUX_CLOCKFD_MASK) == LINUX_CLOCKFD_MASK) {
85 		fputs("INVALID PERTHREAD|CLOCKFD", fp);
86 		goto pidp;
87 	}
88 	ci = LINUX_CPUCLOCK_WHICH(which);
89 	if (LINUX_CPUCLOCK_PERTHREAD(which) == true)
90 		fputs("THREAD|", fp);
91 	else
92 		fputs("PROCESS|", fp);
93 	str = lookup_value(clockcpuids, ci);
94 	if (str != NULL)
95 		fputs(str, fp);
96 	else {
97 		if (ci == LINUX_CLOCKFD)
98 			fputs("CLOCKFD", fp);
99 		else
100 			fprintf(fp, "UNKNOWN(%d)", which);
101 	}
102 
103 pidp:
104 	pid = LINUX_CPUCLOCK_ID(which);
105 	fprintf(fp, "(%d)", pid);
106 }
107