~aleteoryx/muditaos

ref: 4804015a7012e7bdf8d9b58d4b5ae2260efa34d1 muditaos/board/rt1051/crashdump/crashdumpwriter_vfs.cpp -rw-r--r-- 3.2 KiB
4804015a — Mateusz Piesta [MOS-496] Music player broken UI 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "crashdumpwriter_vfs.hpp"
#include <crashdump-serial-number/crashdump_serial_number.hpp>
#include <cstdio>
#include <fcntl.h>
#include "purefs/vfs_subsystem.hpp"
#include <purefs/filesystem_paths.hpp>
#include <exit_backtrace.h>

#include <log/log.hpp>
#include <filesystem>
#include <stdint.h>
#include <unistd.h>
#include <chrono>
#include <set>

namespace
{
    constexpr inline auto suffix = "_crashdump.hex";

    // Crashdump filename pattern:
    // [serial-number]_[timestamp-in-seconds]_crashdump.hex

    inline std::string generate_crashdump_filename()
    {
        const auto crash_time =
            std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
                .count();
        auto filename = std::string("/") + crashdump::getSerialNumber() + "_" + std::to_string(crash_time) + suffix;
        return filename;
    }
} // namespace

namespace crashdump
{

    void CrashDumpWriterVFS::openDump()
    {
        const auto crashDumpFilePath = purefs::dir::getCrashDumpsPath().string() + generate_crashdump_filename();

        LOG_INFO("Crash dump %s preparing ...", crashDumpFilePath.c_str());
        file = std::fopen(crashDumpFilePath.c_str(), "w");
        if (!file) {
            LOG_FATAL("Failed to open crash dump file errno %i", errno);
            _exit_backtrace(-1, false);
        }
    }

    void CrashDumpWriterVFS::saveDump()
    {
        LOG_INFO("Crash dump create finished.");
        fflush(file);
        fsync(fileno(file));
        std::fclose(file);
    }

    void CrashDumpWriterVFS::deleteOldDump()
    {
        std::set<std::filesystem::path> crashdumps{};
        for (const auto &entry : std::filesystem::directory_iterator(purefs::dir::getCrashDumpsPath())) {
            std::cout << entry.path() << std::endl;
            crashdumps.insert(entry.path());
        }

        if (crashdumps.size() > maxFilesCount) {
            auto crashdump_to_delete = crashdumps.begin();
            LOG_INFO("Deleting %s ...", crashdump_to_delete->c_str());
            if (not std::filesystem::remove(crashdump_to_delete->c_str())) {
                LOG_WARN("File: %s was not deleted.", crashdump_to_delete->c_str());
            }
        }
    }

    void CrashDumpWriterVFS::writeBytes(const uint8_t *buff, std::size_t size)
    {
        if (std::fwrite(buff, sizeof(*buff), size, file) != size) {
            LOG_FATAL("Unable to write crash dump errno: %i", errno);
            _exit_backtrace(-1, false);
        }
    }

    void CrashDumpWriterVFS::writeHalfWords(const uint16_t *buff, std::size_t size)
    {
        if (std::fwrite(buff, sizeof(*buff), size, file) != size) {
            LOG_FATAL("Unable to write crash dump errno: %i", errno);
            _exit_backtrace(-1, false);
        }
    }

    void CrashDumpWriterVFS::writeWords(const uint32_t *buff, std::size_t size)
    {
        if (std::fwrite(buff, sizeof(*buff), size, file) != size) {
            LOG_FATAL("Unable to write crash dump errno: %i", errno);
            _exit_backtrace(-1, false);
        }
    }

} // namespace crashdump