[a074b4f] | 1 | /*
|
---|
[92c1680] | 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
[a074b4f] | 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>
|
---|
[1ccafee] | 40 | #include <sys/typefmt.h>
|
---|
[a074b4f] | 41 | #include <task.h>
|
---|
[92c1680] | 42 | #include <ipc/corecfg.h>
|
---|
| 43 | #include <loc.h>
|
---|
[a074b4f] | 44 | #include <macros.h>
|
---|
| 45 | #include <errno.h>
|
---|
[d9fae235] | 46 | #include <str_error.h>
|
---|
[a074b4f] | 47 |
|
---|
| 48 | #define NAME "taskmon"
|
---|
| 49 |
|
---|
[92c1680] | 50 | static bool write_core_files;
|
---|
| 51 |
|
---|
| 52 | static void corecfg_client_conn(ipc_callid_t , ipc_call_t *, void *);
|
---|
| 53 |
|
---|
[8820544] | 54 | static void fault_event(ipc_callid_t callid, ipc_call_t *call, void *arg)
|
---|
[a074b4f] | 55 | {
|
---|
[a000878c] | 56 | const char *fname;
|
---|
[a074b4f] | 57 | char *s_taskid;
|
---|
[92c1680] | 58 | char *dump_fname;
|
---|
[0485135] | 59 | int rc;
|
---|
[a074b4f] | 60 |
|
---|
| 61 | task_id_t taskid;
|
---|
| 62 | uintptr_t thread;
|
---|
| 63 |
|
---|
| 64 | taskid = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
|
---|
| 65 | thread = IPC_GET_ARG3(*call);
|
---|
| 66 |
|
---|
[7e752b2] | 67 | if (asprintf(&s_taskid, "%" PRIu64, taskid) < 0) {
|
---|
[a074b4f] | 68 | printf("Memory allocation failed.\n");
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[7e752b2] | 72 | printf(NAME ": Task %" PRIu64 " fault in thread %p.\n", taskid,
|
---|
| 73 | (void *) thread);
|
---|
[0485135] | 74 |
|
---|
| 75 | fname = "/app/taskdump";
|
---|
| 76 |
|
---|
[92c1680] | 77 | if (write_core_files) {
|
---|
| 78 | if (asprintf(&dump_fname, "/data/core%" PRIu64, taskid) < 0) {
|
---|
| 79 | printf("Memory allocation failed.\n");
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
[0485135] | 82 |
|
---|
[92c1680] | 83 | printf(NAME ": Executing %s -c %s -t %s\n", fname, dump_fname, s_taskid);
|
---|
[1c635d6] | 84 | rc = task_spawnl(NULL, NULL, fname, fname, "-c", dump_fname, "-t", s_taskid,
|
---|
[92c1680] | 85 | NULL);
|
---|
| 86 | } else {
|
---|
| 87 | printf(NAME ": Executing %s -t %s\n", fname, s_taskid);
|
---|
[1c635d6] | 88 | rc = task_spawnl(NULL, NULL, fname, fname, "-t", s_taskid, NULL);
|
---|
[03333bc] | 89 | }
|
---|
| 90 |
|
---|
[0485135] | 91 | if (rc != EOK) {
|
---|
[d9fae235] | 92 | printf("%s: Error spawning %s (%s).\n", NAME, fname,
|
---|
[0485135] | 93 | str_error(rc));
|
---|
| 94 | }
|
---|
[a074b4f] | 95 | }
|
---|
| 96 |
|
---|
[92c1680] | 97 | static void corecfg_get_enable_srv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 98 | {
|
---|
| 99 | async_answer_1(iid, EOK, write_core_files);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | static void corecfg_set_enable_srv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 103 | {
|
---|
| 104 | write_core_files = IPC_GET_ARG1(*icall);
|
---|
| 105 | async_answer_0(iid, EOK);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | static void corecfg_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 109 | {
|
---|
| 110 | /* Accept the connection */
|
---|
| 111 | async_answer_0(iid, EOK);
|
---|
| 112 |
|
---|
| 113 | while (true) {
|
---|
| 114 | ipc_call_t call;
|
---|
| 115 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 116 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 117 |
|
---|
| 118 | if (!method) {
|
---|
| 119 | /* The other side has hung up */
|
---|
| 120 | async_answer_0(callid, EOK);
|
---|
| 121 | return;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | switch (method) {
|
---|
| 125 | case CORECFG_GET_ENABLE:
|
---|
| 126 | corecfg_get_enable_srv(callid, &call);
|
---|
| 127 | break;
|
---|
| 128 | case CORECFG_SET_ENABLE:
|
---|
| 129 | corecfg_set_enable_srv(callid, &call);
|
---|
| 130 | break;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[a074b4f] | 135 | int main(int argc, char *argv[])
|
---|
| 136 | {
|
---|
[79ae36dd] | 137 | printf("%s: Task Monitoring Service\n", NAME);
|
---|
| 138 |
|
---|
[92c1680] | 139 | #ifdef CONFIG_WRITE_CORE_FILES
|
---|
| 140 | write_core_files = true;
|
---|
| 141 | #else
|
---|
| 142 | write_core_files = false;
|
---|
| 143 | #endif
|
---|
[8820544] | 144 | if (async_event_subscribe(EVENT_FAULT, fault_event, NULL) != EOK) {
|
---|
[79ae36dd] | 145 | printf("%s: Error registering fault notifications.\n", NAME);
|
---|
[a074b4f] | 146 | return -1;
|
---|
| 147 | }
|
---|
[79ae36dd] | 148 |
|
---|
[92c1680] | 149 | async_set_client_connection(corecfg_client_conn);
|
---|
| 150 |
|
---|
| 151 | int rc = loc_server_register(NAME);
|
---|
| 152 | if (rc != EOK) {
|
---|
| 153 | printf("%s: Failed registering server (%d).\n",
|
---|
| 154 | NAME, rc);
|
---|
| 155 | return -1;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | service_id_t sid;
|
---|
| 159 | rc = loc_service_register(SERVICE_NAME_CORECFG, &sid);
|
---|
| 160 | if (rc != EOK) {
|
---|
| 161 | printf("%s: Failed registering service (%d).\n",
|
---|
| 162 | NAME, rc);
|
---|
| 163 | return -1;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[79ae36dd] | 166 | task_retval(0);
|
---|
[a074b4f] | 167 | async_manager();
|
---|
[79ae36dd] | 168 |
|
---|
[a074b4f] | 169 | return 0;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /** @}
|
---|
| 173 | */
|
---|