hiview_hievent.c 14.8 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "hiview_hievent.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "los_memory.h"
#include "los_task_pri.h"
#include "los_process_pri.h"
#include "los_task_pri.h"
#include "los_mux.h"
#include "los_mp.h"
#include "los_typedef.h"

#include "hievent_driver.h"

#define INT_TYPE_MAX_LEN    21

#define MAX_PATH_LEN        256
#define MAX_STR_LEN         (10 * 1024)

/* 64K is max length of /dev/hwlog_exception */
#define EVENT_INFO_BUF_LEN         (64 * 1024)
#define EVENT_INFO_PACK_BUF_LEN    (2 * 1024)

#define HWLOG_ERR  PRINT_ERR
#define HWLOG_INFO PRINT_INFO

#define BUF_POINTER_FORWARD                     \
    do {                                        \
        if (tmplen < len) {                     \
            tmp += tmplen;                      \
            len -= tmplen;                      \
        } else {                                \
            HWLOG_ERR("string over length");    \
            tmp += len;                         \
            len = 0;                            \
        }                                       \
    } while (0)

struct HiviewHieventPayload {
    char *key;
    char *value;
    struct HiviewHieventPayload *next;
};

static int HiviewHieventConvertString(struct HiviewHievent *event, char **pbuf);

static struct HiviewHieventPayload *HiviewHieventPayloadCreate(void);

static void HiviewHieventPayloadDestroy(struct HiviewHieventPayload *p);

static struct HiviewHieventPayload *HiviewHieventGetPayload(struct HiviewHieventPayload *head,
                                                            const char *key);

static void HiviewHieventAddPayload(struct HiviewHievent *obj,
                                    struct HiviewHieventPayload *payload);

static struct HiviewHieventPayload *HiviewHieventPayloadCreate(void)
{
    struct HiviewHieventPayload *payload = NULL;

    payload = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR,
                           sizeof(struct HiviewHieventPayload));
    if (!payload) {
        return NULL;
    }

    payload->key = NULL;
    payload->value = NULL;
    payload->next = NULL;

    return payload;
}

static void HiviewHieventPayloadDestroy(struct HiviewHieventPayload *p)
{
    if (!p) {
        return;
    }

    if (p->value) {
        LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, p->value);
    }

    LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, p->key);
    LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, p);
}

static struct HiviewHieventPayload *HiviewHieventGetPayload(struct HiviewHieventPayload *head,
                                                            const char *key)
{
    struct HiviewHieventPayload *p = head;

    while (p) {
        if (key && p->key) {
            if (strcmp(p->key, key) == 0) {
                return p;
            }
        }
        p = p->next;
    }

    return NULL;
}

static void HiviewHieventAddPayload(struct HiviewHievent *obj,
                                    struct HiviewHieventPayload *payload)
{
    if (!obj->head) {
        obj->head = payload;
    } else {
        struct HiviewHieventPayload *p = obj->head;

        while (p->next) {
            p = p->next;
        }
        p->next = payload;
    }
}

struct HiviewHievent *HiviewHieventCreate(unsigned int eventid)
{
    struct HiviewHievent *event = NULL;

    /* combined event obj struct */
    event = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, sizeof(*event));
    if (!event) {
        return NULL;
    }

    (VOID)memset_s(event, sizeof(*event), 0, sizeof(*event));
    event->eventid = eventid;
    HWLOG_INFO("%s : %u\n", __func__, eventid);

    return (void *)event;
}

int HiviewHieventPutIntegral(struct HiviewHievent *event,
                             const char *key, long value)
{
    int ret;
    struct HiviewHieventPayload *payload = NULL;

    if ((!event) || (!key)) {
        HWLOG_ERR("Bad input event or key for %s", __func__);
        return -EINVAL;
    }

    payload = HiviewHieventGetPayload(event->head, key);
    if (!payload) {
        payload = HiviewHieventPayloadCreate();
        if (!payload) {
            return -ENOMEM;
        }
        payload->key = strdup(key);
        HiviewHieventAddPayload(event, payload);
    }

    if (payload->value) {
        LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, payload->value);
    }

    payload->value = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, INT_TYPE_MAX_LEN);
    if (!payload->value) {
        return -ENOMEM;
    }
    (VOID)memset_s(payload->value, INT_TYPE_MAX_LEN, 0, INT_TYPE_MAX_LEN);
    ret = snprintf_s(payload->value, INT_TYPE_MAX_LEN, INT_TYPE_MAX_LEN - 1,
                     "%d", (int)value);
    if (ret < 0) {
        return -ENOMEM;
    }

    return 0;
}

int HiviewHieventPutString(struct HiviewHievent *event,
                           const char *key, const char *value)
{
    struct HiviewHieventPayload *payload = NULL;
    int len;

    if ((!event) || (!key) || (!value)) {
        HWLOG_ERR("Bad key for %s", __func__);
        return -EINVAL;
    }

    payload = HiviewHieventGetPayload(event->head, key);
    if (!payload) {
        payload = HiviewHieventPayloadCreate();
        if (!payload) {
            return -ENOMEM;
        }
        payload->key = strdup(key);
        HiviewHieventAddPayload(event, payload);
    }

    if (payload->value) {
        LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, payload->value);
    }

    len = strlen(value);
    /* prevent length larger than MAX_STR_LEN */
    if (len > MAX_STR_LEN) {
        len = MAX_STR_LEN;
    }
    payload->value = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, len + 1);
    if (!payload->value) {
        return -ENOMEM;
    }
    (VOID)memset_s(payload->value, len + 1, 0, len + 1);
    if (EOK == strncpy_s(payload->value, len + 1, value, len)) {
        payload->value[len] = '\0';
    }

    return 0;
}

int HiviewHieventSetTime(struct HiviewHievent *event, long long seconds)
{
    if ((!event) || (seconds == 0)) {
        HWLOG_ERR("Bad input for %s", __func__);
        return -EINVAL;
    }
    event->time = seconds;
    return 0;
}

static int AppendArrayItem(char **pool, int poolLen, const char *path)
{
    int i;

    if ((!path) || (path[0] == 0)) {
        HWLOG_ERR("Bad path %s", __func__);
        return -EINVAL;
    }

    if (strlen(path) > MAX_PATH_LEN) {
        HWLOG_ERR("file path over max: %d", MAX_PATH_LEN);
        return -EINVAL;
    }

    for (i = 0; i < poolLen; i++) {
        if (pool[i] != 0) {
            continue;
        }
        pool[i] = strdup(path);
        if (pool[i] == NULL) {
            return -ENOMEM;
        }
        break;
    }

    if (i == MAX_PATH_NUMBER) {
        HWLOG_ERR("Too many pathes");
        return -EINVAL;
    }

    return 0;
}

int HiviewHieventAddFilePath(struct HiviewHievent *event, const char *path)
{
    if (!event) {
        HWLOG_ERR("Bad path %s", __func__);
        return -EINVAL;
    }
    return AppendArrayItem(event->filePath, MAX_PATH_NUMBER, path);
}

/* make string ":" to "::", ";" to ";;", and remove newline character
 * for example: "abc:def;ghi" transfer to "abc::def;;ghi"
 */
static char *HiviewHieventMakeRegular(char *value)
{
    int count = 0;
    int len = 0;
    char *temp = value;
    char *regular = NULL;
    char *regularTemp = NULL;

    while (*temp != '\0') {
        if (*temp == ':') {
            count++;
        } else if (*temp == ';') {
            count++;
        } else if ((*temp == '\n') || (*temp == '\r')) {
            *temp = ' ';
        }
        temp++;
        len++;
    }

    /* no need to transfer, just return old value */
    if (count == 0) {
        return value;
    }

    size_t regularLen = len + count * 2 + 1; // 2 char in a byte
    regular = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, regularLen);
    if (!regular) {
        return NULL;
    }
    (VOID)memset_s(regular, regularLen, 0, regularLen);
    regularTemp = regular;
    temp = value;
    while (*temp != 0) {
        if ((*temp == ':') || (*temp == ';')) {
            *regularTemp++ = *temp;
        }
        *regularTemp++ = *temp;
        temp++;
    }
    *regularTemp = '\0';

    return regular;
}

int LogBufToException(char category, int level, char logType,
                      char sn, const char *msg, int msglen)
{
    struct IdapHeader *hdr;
    size_t bufLen = sizeof(int) + sizeof(struct IdapHeader) + msglen;
    char *buffer = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, bufLen);
    if (!buffer) {
        return -ENOMEM;
    }

    int *checkCode = (int *)buffer;
    *checkCode = CHECK_CODE;

    hdr = (struct IdapHeader *)(buffer + sizeof(int));
    hdr->level = level;
    hdr->category = category;
    hdr->logType = logType;
    hdr->sn = sn;

    int ret = memcpy_s(buffer + sizeof(int) + sizeof(struct IdapHeader),
                       msglen, msg, msglen);
    if (ret != EOK) {
        goto out;
    }

    ret = HieventWriteInternal(buffer, bufLen);

out:
    LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, buffer);

    return ret;
}

static int HiviewHieventFillPayload(struct HiviewHievent *event, char **pbuf,
                                    char *tmp, int length)
{
    struct HiviewHieventPayload *p = event->head;
    int len = length;
    int tmplen = 0;
    unsigned int keycount = 0;
    while (p) {
        char *value = NULL;
        char *regularValue = NULL;
        int needFree = 1;

        if (!p->value) {
            p = p->next;
            continue;
        }
        if (keycount == 0) {
            tmplen = snprintf_s(tmp, len, len - 1, " --extra ");
            BUF_POINTER_FORWARD;
        }
        keycount++;

        /* fill key */
        if (p->key) {
            tmplen = snprintf_s(tmp, len, len - 1, "%s:", p->key);
        }
        BUF_POINTER_FORWARD;
        /* fill value */
        tmplen = 0;

        value = p->value;
        regularValue = HiviewHieventMakeRegular(value);
        if (!regularValue) {
            regularValue = "NULL";
            needFree = 0;
        }
        tmplen = snprintf_s(tmp, len, len - 1, "%s;", regularValue);
        if ((value != regularValue) && needFree) {
            free(regularValue);
        }
        BUF_POINTER_FORWARD;
        p = p->next;
    }
    return len;
}

static int HiviewHieventConvertString(struct HiviewHievent *event, char **pbuf)
{
    int len;
    char *tmp = NULL;
    int tmplen = 0;
    unsigned int i;

    char *buf = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, EVENT_INFO_BUF_LEN);
    if (!buf) {
        *pbuf = NULL;
        return 0;
    }
    (VOID)memset_s(buf, EVENT_INFO_BUF_LEN, 0, EVENT_INFO_BUF_LEN);
    len = EVENT_INFO_BUF_LEN;
    tmp = buf;

    /* fill eventid */
448
    tmplen = snprintf_s(tmp, len, len - 1, "eventid %u", event->eventid);
W
wenjun 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    BUF_POINTER_FORWARD;

    /* fill the path */
    for (i = 0; i < MAX_PATH_NUMBER; i++) {
        if (!event->filePath[i]) {
            break;
        }
        tmplen = snprintf_s(tmp, len, len - 1, " -i %s",
                            event->filePath[i]);
        BUF_POINTER_FORWARD;
    }

    /* fill time */
    if (event->time) {
        tmplen = snprintf_s(tmp, len, len - 1, " -t %lld",  event->time);
        BUF_POINTER_FORWARD;
    }

    /* fill the payload info */
    len = HiviewHieventFillPayload(event, pbuf, tmp, len);
    *pbuf = buf;
    return (EVENT_INFO_BUF_LEN - len);
}

#define IDAP_LOGTYPE_CMD 1
static int HiviewHieventWriteLogException(char *str, const int strlen)
{
    char tempchr;
    char *strptr = str;
    int leftBufLen = strlen + 1;
    int sentcnt = 0;

    while (leftBufLen > 0) {
        if (leftBufLen > EVENT_INFO_PACK_BUF_LEN) {
            tempchr = strptr[EVENT_INFO_PACK_BUF_LEN - 1];
            strptr[EVENT_INFO_PACK_BUF_LEN - 1] = '\0';
            LogBufToException(0, 0, IDAP_LOGTYPE_CMD, 1,
                              strptr, EVENT_INFO_PACK_BUF_LEN);
            leftBufLen -= (EVENT_INFO_PACK_BUF_LEN - 1);
            strptr += (EVENT_INFO_PACK_BUF_LEN - 1);
            strptr[0] = tempchr;
            sentcnt++;
        } else {
            LogBufToException(0, 0, IDAP_LOGTYPE_CMD, 0,
                              strptr, leftBufLen);
            sentcnt++;
            break;
        }
    }

    return sentcnt;
}

int HiviewHieventReport(struct HiviewHievent *obj)
{
    char *str = NULL;
    int bufLen;
    int sentPacket;

    if (!obj) {
        HWLOG_ERR("Bad event %s", __func__);
        return -EINVAL;
    }

    bufLen = HiviewHieventConvertString(obj, &str);
    if (!str) {
        return -EINVAL;
    }
    sentPacket = HiviewHieventWriteLogException(str, bufLen);
    HWLOG_INFO("report: %s", str);
    LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, str);

    return sentPacket;
}

void HiviewHieventDestroy(struct HiviewHievent *event)
{
    int i;
    struct HiviewHieventPayload *p = NULL;

    if (!event) {
        return;
    }
    p = event->head;
    while (p) {
        struct HiviewHieventPayload *del = p;

        p = p->next;
        HiviewHieventPayloadDestroy(del);
    }
    event->head = NULL;
    for (i = 0; i < MAX_PATH_NUMBER; i++) {
        LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, event->filePath[i]);
        event->filePath[i] = NULL;
    }
    LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, event);
}
546 547 548 549 550 551 552 553

void HiviewHieventFlush(void)
{
    // magic number 0x7BBE69BD for notify hiview to flush hievent file
    struct HiviewHievent *hievent = HiviewHieventCreate(0x7BBE69BD);
    HiviewHieventReport(hievent);
    HiviewHieventDestroy(hievent);
}