Verified Commit ff6d9423 authored by Marc Vef's avatar Marc Vef
Browse files

Hand formatting syscall code

parent 5615c4aa
Loading
Loading
Loading
Loading
+59 −80
Original line number Diff line number Diff line
@@ -84,9 +84,7 @@ struct printable_arg {

/** All arg formatters must follow this prototype */
template <typename FmtBuffer>
using formatter = 
    std::add_pointer_t<void(FmtBuffer&, const printable_arg&)>;

using formatter = std::add_pointer_t<void(FmtBuffer&, const printable_arg&)>;


/** forward declare formatters */
@@ -209,9 +207,7 @@ typedef struct {

template <typename FmtBuffer, typename FlagDescriptorArray>
static void
format_flag(FmtBuffer& buffer, 
            long flag,
		    FlagDescriptorArray&& desc) {
format_flag(FmtBuffer& buffer, long flag, FlagDescriptorArray&& desc) {

    // we assume that if a flag value is zero, its printable
    // name will always be at position 0 in the array
@@ -237,9 +233,7 @@ format_flag(FmtBuffer& buffer,

template <typename FmtBuffer, typename FlagDescriptorArray>
static void
format_flag_set(FmtBuffer& buffer, 
                long flags,
		        FlagDescriptorArray&& desc) {
format_flag_set(FmtBuffer& buffer, long flags, FlagDescriptorArray&& desc) {

    // we assume that if a flag value is zero, its printable
    // name will always be at position 0 in the array
@@ -291,8 +285,7 @@ format_flag_set(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_whence_arg_to(FmtBuffer& buffer, 
                     const printable_arg& parg) {
format_whence_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    /* Names for lseek() whence arg */
    const auto flag_names =
@@ -315,8 +308,7 @@ format_whence_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_mmap_prot_arg_to(FmtBuffer& buffer, 
                        const printable_arg& parg) {
format_mmap_prot_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    /* Names for mmap() prot arg */
    const auto flag_names =
@@ -341,8 +333,7 @@ format_mmap_prot_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_mmap_flags_arg_to(FmtBuffer& buffer, 
                         const printable_arg& parg) {
format_mmap_flags_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    /* Names for mmap() flags arg */
    const auto flag_names =
@@ -382,8 +373,7 @@ format_mmap_flags_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_clone_flags_arg_to(FmtBuffer& buffer, 
                          const printable_arg& parg) {
format_clone_flags_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    /* Names for clone() flags arg */
    const auto flag_names =
@@ -435,8 +425,7 @@ format_clone_flags_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_signum_arg_to(FmtBuffer& buffer, 
                     const printable_arg& parg) {
format_signum_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    /* Names for signum args */
    const auto flag_names =
@@ -490,8 +479,7 @@ format_signum_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_sigproc_how_arg_to(FmtBuffer& buffer, 
                          const printable_arg& parg) {
format_sigproc_how_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    /* Names for sigproc how args */
    const auto flag_names =
@@ -513,8 +501,7 @@ format_sigproc_how_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_none_arg_to(FmtBuffer& buffer,
                   const printable_arg& parg) {
format_none_arg_to(FmtBuffer& buffer, const printable_arg& parg) {
    fmt::format_to(buffer, "void");
}

@@ -527,8 +514,7 @@ format_none_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_fd_arg_to(FmtBuffer& buffer,
                 const printable_arg& parg) {
format_fd_arg_to(FmtBuffer& buffer, const printable_arg& parg) {
    fmt::format_to(buffer, "{}={}", parg.name, static_cast<int>(parg.value));
}

@@ -541,8 +527,7 @@ format_fd_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_atfd_arg_to(FmtBuffer& buffer,
                   const printable_arg& parg) {
format_atfd_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    if(static_cast<int>(parg.value) == AT_FDCWD) {
        fmt::format_to(buffer, "{}=AT_FDCWD", parg.name);
@@ -561,8 +546,7 @@ format_atfd_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_cstr_arg_to(FmtBuffer& buffer,
                   const printable_arg& parg) {
format_cstr_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    if(LIKELY(reinterpret_cast<const char*>(parg.value) != nullptr)) {
        fmt::format_to(buffer, "{}=\"{}\"", parg.name,
@@ -649,8 +633,7 @@ format_open_flags_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_octal_mode_to(FmtBuffer& buffer,
                     const printable_arg& parg) {
format_octal_mode_to(FmtBuffer& buffer, const printable_arg& parg) {
    fmt::format_to(buffer, "{}={:#04o}", parg.name, parg.value);
}

@@ -662,8 +645,7 @@ format_octal_mode_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_ptr_arg_to(FmtBuffer& buffer,
                  const printable_arg& parg) {
format_ptr_arg_to(FmtBuffer& buffer, const printable_arg& parg) {

    if(LIKELY(reinterpret_cast<const void*>(parg.value) != nullptr)) {
        fmt::format_to(buffer, "{}={}", parg.name,
@@ -683,8 +665,7 @@ format_ptr_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_dec_arg_to(FmtBuffer& buffer,
                  const printable_arg& parg) {
format_dec_arg_to(FmtBuffer& buffer, const printable_arg& parg) {
    fmt::format_to(buffer, "{}={}", parg.name, parg.value);
}

@@ -697,8 +678,7 @@ format_dec_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_dec32_arg_to(FmtBuffer& buffer,
                    const printable_arg& parg) {
format_dec32_arg_to(FmtBuffer& buffer, const printable_arg& parg) {
    fmt::format_to(buffer, "{}={}", parg.name, static_cast<int>(parg.value));
}

@@ -711,8 +691,7 @@ format_dec32_arg_to(FmtBuffer& buffer,
 */
template <typename FmtBuffer>
inline void
format_arg_to(FmtBuffer& buffer,
              const printable_arg& parg) {
format_arg_to(FmtBuffer& buffer, const printable_arg& parg) {
    fmt::format_to(buffer, "{}={:#x}", parg.name, parg.value);
}

+7 −19
Original line number Diff line number Diff line
@@ -11,9 +11,6 @@
  SPDX-License-Identifier: MIT
*/

// This file uses special C formatting for a better overview
// clang-format off

#ifndef GKFS_SYSCALLS_DECODER_HPP
#define GKFS_SYSCALLS_DECODER_HPP

@@ -28,8 +25,7 @@ namespace detail {

/** a RAII saver/restorer of errno values */
struct errno_saver {
    errno_saver(int errnum) :
        saved_errno_(errnum) { }
    errno_saver(int errnum) : saved_errno_(errnum) {}

    ~errno_saver() {
        errno = saved_errno_;
@@ -42,8 +38,7 @@ struct errno_saver {

template <typename FmtBuffer>
inline void
decode(FmtBuffer& buffer, 
       const long syscall_number,
decode(FmtBuffer& buffer, const long syscall_number,
       const long argv[MAX_ARGS]) {

    detail::errno_saver _(errno);
@@ -67,9 +62,7 @@ decode(FmtBuffer& buffer,

template <typename FmtBuffer>
inline void
decode(FmtBuffer& buffer, 
       const long syscall_number,
       const long argv[MAX_ARGS],
decode(FmtBuffer& buffer, const long syscall_number, const long argv[MAX_ARGS],
       const long result) {

    detail::errno_saver _(errno);
@@ -94,22 +87,17 @@ decode(FmtBuffer& buffer,
    }

    if(error_code(result) != 0) {
        fmt::format_to(buffer, ") = {} {} ({})", 
                static_cast<int>(-1), 
                errno_name(-result),
                errno_message(-result));
        fmt::format_to(buffer, ") = {} {} ({})", static_cast<int>(-1),
                       errno_name(-result), errno_message(-result));
        return;
    }

    fmt::format_to(buffer, ") = ");
    const auto& ret = sc.return_type();
    ret.formatter<FmtBuffer>()(buffer, result);

}

} // namespace syscall
} // namespace gkfs

#endif // GKFS_SYSCALLS_DECODER_HPP
 No newline at end of file

// clang-format on
 No newline at end of file
+533 −533
Original line number Diff line number Diff line
@@ -575,7 +575,7 @@ errno_message(int errno_value) {
    return std::string{msg};
}

} // namespace syscalls
} // namespace syscall
} // namespace gkfs

#endif // GKFS_SYSCALLS_ERRNO_HPP
+19 −20
Original line number Diff line number Diff line
@@ -71,8 +71,7 @@ lookup_by_number(const long syscall_number) {
}

static inline descriptor
lookup_by_number(const long syscall_number,
               const long argv[MAX_ARGS]) {
lookup_by_number(const long syscall_number, const long argv[MAX_ARGS]) {
    const auto* info = ::get_syscall_info(syscall_number, argv);
    return *reinterpret_cast<const descriptor*>(info);
}
+14 −17
Original line number Diff line number Diff line
@@ -487,8 +487,7 @@ requires_mode_arg(int flags) {
 * appropriately describes the system call identified by 'syscall_number'.
 */
const struct syscall_info*
get_syscall_info(const long syscall_number, 
                 const long* argv) {
get_syscall_info(const long syscall_number, const long* argv) {

    if(syscall_number < 0 ||
       syscall_number >= (long) ARRAY_SIZE(syscall_table)) {
@@ -900,8 +899,8 @@ compare_named_entries(const void *k, const void *e) {
const struct syscall_info*
get_syscall_info_by_name(const char* syscall_name) {

    struct named_syscall_entry* res = 
        bsearch(syscall_name, &syscalls_by_name[0], ARRAY_SIZE(syscalls_by_name),
    struct named_syscall_entry* res = bsearch(
            syscall_name, &syscalls_by_name[0], ARRAY_SIZE(syscalls_by_name),
            sizeof(struct named_syscall_entry), compare_named_entries);

    if(res == NULL) {
@@ -911,8 +910,7 @@ get_syscall_info_by_name(const char* syscall_name) {
    return res->s_info;
}

#define RETURN_TYPE(scinfo) \
    (scinfo)->s_return_type.r_type
#define RETURN_TYPE(scinfo) (scinfo)->s_return_type.r_type

bool
syscall_never_returns(long syscall_number) {
@@ -920,7 +918,6 @@ syscall_never_returns(long syscall_number) {
}



#undef SYSCALL
#undef S_NOARGS
#undef S_UARG