18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: LGPL-2.1
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * trace/beauty/pkey_alloc.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2017, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "trace/beauty/beauty.h"
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/log2.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cisize_t strarray__scnprintf_flags(struct strarray *sa, char *bf, size_t size, bool show_prefix, unsigned long flags)
138c2ecf20Sopenharmony_ci{
148c2ecf20Sopenharmony_ci	int i, printed = 0;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	if (flags == 0) {
178c2ecf20Sopenharmony_ci		const char *s = sa->entries[0];
188c2ecf20Sopenharmony_ci		if (s)
198c2ecf20Sopenharmony_ci			return scnprintf(bf, size, "%s%s", show_prefix ? sa->prefix : "", s);
208c2ecf20Sopenharmony_ci		return scnprintf(bf, size, "%d", 0);
218c2ecf20Sopenharmony_ci	}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	for (i = 1; i < sa->nr_entries; ++i) {
248c2ecf20Sopenharmony_ci		unsigned long bit = 1UL << (i - 1);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci		if (!(flags & bit))
278c2ecf20Sopenharmony_ci			continue;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci		if (printed != 0)
308c2ecf20Sopenharmony_ci			printed += scnprintf(bf + printed, size - printed, "|");
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci		if (sa->entries[i] != NULL)
338c2ecf20Sopenharmony_ci			printed += scnprintf(bf + printed, size - printed, "%s%s", show_prefix ? sa->prefix : "", sa->entries[i]);
348c2ecf20Sopenharmony_ci		else
358c2ecf20Sopenharmony_ci			printed += scnprintf(bf + printed, size - printed, "0x%#", bit);
368c2ecf20Sopenharmony_ci	}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	return printed;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic size_t pkey_alloc__scnprintf_access_rights(int access_rights, char *bf, size_t size, bool show_prefix)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci#include "trace/beauty/generated/pkey_alloc_access_rights_array.c"
448c2ecf20Sopenharmony_ci	static DEFINE_STRARRAY(pkey_alloc_access_rights, "PKEY_");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	return strarray__scnprintf_flags(&strarray__pkey_alloc_access_rights, bf, size, show_prefix, access_rights);
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cisize_t syscall_arg__scnprintf_pkey_alloc_access_rights(char *bf, size_t size, struct syscall_arg *arg)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	unsigned long cmd = arg->val;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return pkey_alloc__scnprintf_access_rights(cmd, bf, size, arg->show_string_prefix);
548c2ecf20Sopenharmony_ci}
55