| 1 | /*
|
|---|
| 2 | * Copyright (c) 2013 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup taskmon
|
|---|
| 30 | * @brief
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <ipc/services.h>
|
|---|
| 40 | #include <sys/typefmt.h>
|
|---|
| 41 | #include <task.h>
|
|---|
| 42 | #include <event.h>
|
|---|
| 43 | #include <ipc/corecfg.h>
|
|---|
| 44 | #include <loc.h>
|
|---|
| 45 | #include <macros.h>
|
|---|
| 46 | #include <errno.h>
|
|---|
| 47 | #include <str_error.h>
|
|---|
| 48 |
|
|---|
| 49 | #define NAME "taskmon"
|
|---|
| 50 |
|
|---|
| 51 | static bool write_core_files;
|
|---|
| 52 |
|
|---|
| 53 | static void corecfg_client_conn(ipc_callid_t , ipc_call_t *, void *);
|
|---|
| 54 |
|
|---|
| 55 | static void fault_event(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 56 | {
|
|---|
| 57 | const char *fname;
|
|---|
| 58 | char *s_taskid;
|
|---|
| 59 | char *dump_fname;
|
|---|
| 60 | int rc;
|
|---|
| 61 |
|
|---|
| 62 | task_id_t taskid;
|
|---|
| 63 | uintptr_t thread;
|
|---|
| 64 |
|
|---|
| 65 | taskid = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
|
|---|
| 66 | thread = IPC_GET_ARG3(*call);
|
|---|
| 67 |
|
|---|
| 68 | if (asprintf(&s_taskid, "%" PRIu64, taskid) < 0) {
|
|---|
| 69 | printf("Memory allocation failed.\n");
|
|---|
| 70 | return;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | printf(NAME ": Task %" PRIu64 " fault in thread %p.\n", taskid,
|
|---|
| 74 | (void *) thread);
|
|---|
| 75 |
|
|---|
| 76 | fname = "/app/taskdump";
|
|---|
| 77 |
|
|---|
| 78 | if (write_core_files) {
|
|---|
| 79 | if (asprintf(&dump_fname, "/data/core%" PRIu64, taskid) < 0) {
|
|---|
| 80 | printf("Memory allocation failed.\n");
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | printf(NAME ": Executing %s -c %s -t %s\n", fname, dump_fname, s_taskid);
|
|---|
| 85 | rc = task_spawnl(NULL, fname, fname, "-c", dump_fname, "-t", s_taskid,
|
|---|
| 86 | NULL);
|
|---|
| 87 | } else {
|
|---|
| 88 | printf(NAME ": Executing %s -t %s\n", fname, s_taskid);
|
|---|
| 89 | rc = task_spawnl(NULL, fname, fname, "-t", s_taskid, NULL);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | if (rc != EOK) {
|
|---|
| 93 | printf("%s: Error spawning %s (%s).\n", NAME, fname,
|
|---|
| 94 | str_error(rc));
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | static void corecfg_get_enable_srv(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 99 | {
|
|---|
| 100 | async_answer_1(iid, EOK, write_core_files);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | static void corecfg_set_enable_srv(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 104 | {
|
|---|
| 105 | write_core_files = IPC_GET_ARG1(*icall);
|
|---|
| 106 | async_answer_0(iid, EOK);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static void corecfg_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 110 | {
|
|---|
| 111 | /* Accept the connection */
|
|---|
| 112 | async_answer_0(iid, EOK);
|
|---|
| 113 |
|
|---|
| 114 | while (true) {
|
|---|
| 115 | ipc_call_t call;
|
|---|
| 116 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 117 | sysarg_t method = IPC_GET_IMETHOD(call);
|
|---|
| 118 |
|
|---|
| 119 | if (!method) {
|
|---|
| 120 | /* The other side has hung up */
|
|---|
| 121 | async_answer_0(callid, EOK);
|
|---|
| 122 | return;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | switch (method) {
|
|---|
| 126 | case CORECFG_GET_ENABLE:
|
|---|
| 127 | corecfg_get_enable_srv(callid, &call);
|
|---|
| 128 | break;
|
|---|
| 129 | case CORECFG_SET_ENABLE:
|
|---|
| 130 | corecfg_set_enable_srv(callid, &call);
|
|---|
| 131 | break;
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | int main(int argc, char *argv[])
|
|---|
| 137 | {
|
|---|
| 138 | printf("%s: Task Monitoring Service\n", NAME);
|
|---|
| 139 |
|
|---|
| 140 | #ifdef CONFIG_WRITE_CORE_FILES
|
|---|
| 141 | write_core_files = true;
|
|---|
| 142 | #else
|
|---|
| 143 | write_core_files = false;
|
|---|
| 144 | #endif
|
|---|
| 145 | if (event_subscribe(EVENT_FAULT, 0) != EOK) {
|
|---|
| 146 | printf("%s: Error registering fault notifications.\n", NAME);
|
|---|
| 147 | return -1;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | async_set_client_connection(corecfg_client_conn);
|
|---|
| 151 |
|
|---|
| 152 | int rc = loc_server_register(NAME);
|
|---|
| 153 | if (rc != EOK) {
|
|---|
| 154 | printf("%s: Failed registering server (%d).\n",
|
|---|
| 155 | NAME, rc);
|
|---|
| 156 | return -1;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | service_id_t sid;
|
|---|
| 160 | rc = loc_service_register(SERVICE_NAME_CORECFG, &sid);
|
|---|
| 161 | if (rc != EOK) {
|
|---|
| 162 | printf("%s: Failed registering service (%d).\n",
|
|---|
| 163 | NAME, rc);
|
|---|
| 164 | return -1;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | async_set_interrupt_received(fault_event);
|
|---|
| 168 | task_retval(0);
|
|---|
| 169 | async_manager();
|
|---|
| 170 |
|
|---|
| 171 | return 0;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /** @}
|
|---|
| 175 | */
|
|---|