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 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 448 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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
|
/*
* Copyright (C) 2006-2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <pthread.h>
#include <sched.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cdefs.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <atomic>
#include <memory>
#include <string>
#include <vector>
#include <android-base/file.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/sched_policy.h>
#include <cutils/sockets.h>
#include <log/event_tag_map.h>
#include <log/getopt.h>
#include <log/logcat.h>
#include <log/logprint.h>
#include <private/android_logger.h>
#include <system/thread_defs.h>
#include <pcrecpp.h>
#define DEFAULT_MAX_ROTATED_LOGS 4
struct log_device_t {
const char* device;
bool binary;
struct logger* logger;
struct logger_list* logger_list;
bool printed;
log_device_t* next;
log_device_t(const char* d, bool b) {
device = d;
binary = b;
next = nullptr;
printed = false;
logger = nullptr;
logger_list = nullptr;
}
};
struct android_logcat_context_internal {
// status
volatile std::atomic_int retval; // valid if thread_stopped set
// Arguments passed in, or copies and storage thereof if a thread.
int argc;
char* const* argv;
char* const* envp;
std::vector<std::string> args;
std::vector<const char*> argv_hold;
std::vector<std::string> envs;
std::vector<const char*> envp_hold;
int output_fd; // duplication of fileno(output) (below)
int error_fd; // duplication of fileno(error) (below)
// library
int fds[2]; // From popen call
FILE* output; // everything writes to fileno(output), buffer unused
FILE* error; // unless error == output.
pthread_t thr;
volatile std::atomic_bool stop; // quick exit flag
volatile std::atomic_bool thread_stopped;
bool stderr_null; // shell "2>/dev/null"
bool stderr_stdout; // shell "2>&1"
// global variables
AndroidLogFormat* logformat;
const char* outputFileName;
// 0 means "no log rotation"
size_t logRotateSizeKBytes;
// 0 means "unbounded"
size_t maxRotatedLogs;
size_t outByteCount;
int printBinary;
int devCount; // >1 means multiple
pcrecpp::RE* regex;
log_device_t* devices;
EventTagMap* eventTagMap;
// 0 means "infinite"
size_t maxCount;
size_t printCount;
bool printItAnyways;
bool debug;
bool hasOpenedEventTagMap;
};
// Creates a context associated with this logcat instance
android_logcat_context create_android_logcat() {
android_logcat_context_internal* context;
context = (android_logcat_context_internal*)calloc(
1, sizeof(android_logcat_context_internal));
if (!context) return nullptr;
context->fds[0] = -1;
context->fds[1] = -1;
context->output_fd = -1;
context->error_fd = -1;
context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
context->argv_hold.clear();
context->args.clear();
context->envp_hold.clear();
context->envs.clear();
return (android_logcat_context)context;
}
// logd prefixes records with a length field
#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
namespace android {
enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
// if showHelp is set, newline required in fmt statement to transition to usage
static void logcat_panic(android_logcat_context_internal* context,
enum helpType showHelp, const char* fmt, ...)
__printflike(3, 4);
static int openLogFile(const char* pathname) {
return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
}
static void close_output(android_logcat_context_internal* context) {
// split output_from_error
if (context->error == context->output) {
context->output = nullptr;
context->output_fd = -1;
}
if (context->error && (context->output_fd == fileno(context->error))) {
context->output_fd = -1;
}
if (context->output_fd == context->error_fd) {
context->output_fd = -1;
}
// close output channel
if (context->output) {
if (context->output != stdout) {
if (context->output_fd == fileno(context->output)) {
context->output_fd = -1;
}
if (context->fds[1] == fileno(context->output)) {
context->fds[1] = -1;
}
fclose(context->output);
}
context->output = nullptr;
}
if (context->output_fd >= 0) {
if (context->output_fd != fileno(stdout)) {
if (context->fds[1] == context->output_fd) {
context->fds[1] = -1;
}
close(context->output_fd);
}
context->output_fd = -1;
}
}
static void close_error(android_logcat_context_internal* context) {
// split error_from_output
if (context->output == context->error) {
context->error = nullptr;
context->error_fd = -1;
}
if (context->output && (context->error_fd == fileno(context->output))) {
context->error_fd = -1;
}
if (context->error_fd == context->output_fd) {
context->error_fd = -1;
}
// close error channel
if (context->error) {
if ((context->error != stderr) && (context->error != stdout)) {
if (context->error_fd == fileno(context->error)) {
context->error_fd = -1;
}
if (context->fds[1] == fileno(context->error)) {
context->fds[1] = -1;
}
fclose(context->error);
}
context->error = nullptr;
}
if (context->error_fd >= 0) {
if ((context->error_fd != fileno(stdout)) &&
(context->error_fd != fileno(stderr))) {
if (context->fds[1] == context->error_fd) context->fds[1] = -1;
close(context->error_fd);
}
context->error_fd = -1;
}
}
static void rotateLogs(android_logcat_context_internal* context) {
int err;
// Can't rotate logs if we're not outputting to a file
if (!context->outputFileName) return;
close_output(context);
// Compute the maximum number of digits needed to count up to
// maxRotatedLogs in decimal. eg:
// maxRotatedLogs == 30
// -> log10(30) == 1.477
// -> maxRotationCountDigits == 2
int maxRotationCountDigits =
(context->maxRotatedLogs > 0)
? (int)(floor(log10(context->maxRotatedLogs) + 1))
: 0;
for (int i = context->maxRotatedLogs; i > 0; i--) {
std::string file1 = android::base::StringPrintf(
"%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
std::string file0;
if (!(i - 1)) {
file0 = android::base::StringPrintf("%s", context->outputFileName);
} else {
file0 =
android::base::StringPrintf("%s.%.*d", context->outputFileName,
maxRotationCountDigits, i - 1);
}
if (!file0.length() || !file1.length()) {
perror("while rotating log files");
break;
}
err = rename(file0.c_str(), file1.c_str());
if (err < 0 && errno != ENOENT) {
perror("while rotating log files");
}
}
context->output_fd = openLogFile(context->outputFileName);
if (context->output_fd < 0) {
logcat_panic(context, HELP_FALSE, "couldn't open output file");
return;
}
context->output = fdopen(context->output_fd, "web");
if (!context->output) {
logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
return;
}
if (context->stderr_stdout) {
close_error(context);
context->error = context->output;
context->error_fd = context->output_fd;
}
context->outByteCount = 0;
}
void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
size_t size = buf->len();
TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
}
static bool regexOk(android_logcat_context_internal* context,
const AndroidLogEntry& entry) {
if (!context->regex) return true;
std::string messageString(entry.message, entry.messageLen);
return context->regex->PartialMatch(messageString);
}
static void processBuffer(android_logcat_context_internal* context,
log_device_t* dev, struct log_msg* buf) {
int bytesWritten = 0;
int err;
AndroidLogEntry entry;
char binaryMsgBuf[1024];
if (dev->binary) {
if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
context->eventTagMap = android_openEventTagMap(nullptr);
context->hasOpenedEventTagMap = true;
}
err = android_log_processBinaryLogBuffer(
&buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
sizeof(binaryMsgBuf));
// printf(">>> pri=%d len=%d msg='%s'\n",
// entry.priority, entry.messageLen, entry.message);
} else {
err = android_log_processLogBuffer(&buf->entry_v1, &entry);
}
if ((err < 0) && !context->debug) return;
if (android_log_shouldPrintLine(
context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
entry.priority)) {
bool match = regexOk(context, entry);
context->printCount += match;
if (match || context->printItAnyways) {
bytesWritten = android_log_printLogLine(context->logformat,
context->output_fd, &entry);
if (bytesWritten < 0) {
logcat_panic(context, HELP_FALSE, "output error");
return;
}
}
}
context->outByteCount += bytesWritten;
if (context->logRotateSizeKBytes > 0 &&
(context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
rotateLogs(context);
}
}
static void maybePrintStart(android_logcat_context_internal* context,
log_device_t* dev, bool printDividers) {
if (!dev->printed || printDividers) {
if (context->devCount > 1 && !context->printBinary) {
char buf[1024];
snprintf(buf, sizeof(buf), "--------- %s %s\n",
dev->printed ? "switch to" : "beginning of", dev->device);
if (write(context->output_fd, buf, strlen(buf)) < 0) {
logcat_panic(context, HELP_FALSE, "output error");
return;
}
}
dev->printed = true;
}
}
static void setupOutputAndSchedulingPolicy(
android_logcat_context_internal* context, bool blocking) {
if (!context->outputFileName) return;
if (blocking) {
// Lower priority and set to batch scheduling if we are saving
// the logs into files and taking continuous content.
if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
fprintf(context->error,
"failed to set background scheduling policy\n");
}
struct sched_param param;
memset(¶m, 0, sizeof(param));
if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) {
fprintf(stderr, "failed to set to batch scheduler\n");
}
if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
context->error) {
fprintf(context->error, "failed set to priority\n");
}
}
close_output(context);
context->output_fd = openLogFile(context->outputFileName);
if (context->output_fd < 0) {
logcat_panic(context, HELP_FALSE, "couldn't open output file");
return;
}
struct stat statbuf;
if (fstat(context->output_fd, &statbuf) == -1) {
close_output(context);
logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
return;
}
if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
close_output(context);
logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
return;
}
context->output = fdopen(context->output_fd, "web");
context->outByteCount = statbuf.st_size;
}
// clang-format off
static void show_help(android_logcat_context_internal* context) {
if (!context->error) return;
const char* cmd = strrchr(context->argv[0], '/');
cmd = cmd ? cmd + 1 : context->argv[0];
fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
fprintf(context->error, "options include:\n"
" -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
" -f <file>, --file=<file> Log to file. Default is stdout\n"
" -r <kbytes>, --rotate-kbytes=<kbytes>\n"
" Rotate log every kbytes. Requires -f option\n"
" -n <count>, --rotate-count=<count>\n"
" Sets max number of rotated logs to <count>, default 4\n"
" --id=<id> If the signature id for logging to file changes, then clear\n"
" the fileset and continue\n"
" -v <format>, --format=<format>\n"
" Sets log print format verb and adverbs, where <format> is:\n"
" brief help long process raw tag thread threadtime time\n"
" and individually flagged modifying adverbs can be added:\n"
" color descriptive epoch monotonic printable uid\n"
" usec UTC year zone\n"
" Multiple -v parameters or comma separated list of format and\n"
" format modifiers are allowed.\n"
// private and undocumented nsec, no signal, too much noise
// useful for -T or -t <timestamp> accurate testing though.
" -D, --dividers Print dividers between each log buffer\n"
" -c, --clear Clear (flush) the entire log and exit\n"
" if Log to File specified, clear fileset instead\n"
" -d Dump the log and then exit (don't block)\n"
" -e <expr>, --regex=<expr>\n"
" Only print lines where the log message matches <expr>\n"
" where <expr> is a regular expression\n"
// Leave --head undocumented as alias for -m
" -m <count>, --max-count=<count>\n"
" Quit after printing <count> lines. This is meant to be\n"
" paired with --regex, but will work on its own.\n"
" --print Paired with --regex and --max-count to let content bypass\n"
" regex filter but still stop at number of matches.\n"
// Leave --tail undocumented as alias for -t
" -t <count> Print only the most recent <count> lines (implies -d)\n"
" -t '<time>' Print most recent lines since specified time (implies -d)\n"
" -T <count> Print only the most recent <count> lines (does not imply -d)\n"
" -T '<time>' Print most recent lines since specified time (not imply -d)\n"
" count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
" 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
" -g, --buffer-size Get the size of the ring buffer.\n"
" -G <size>, --buffer-size=<size>\n"
" Set size of log ring buffer, may suffix with K or M.\n"
" -L, --last Dump logs from prior to last reboot\n"
// Leave security (Device Owner only installations) and
// kernel (userdebug and eng) buffers undocumented.
" -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
" 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
" Multiple -b parameters or comma separated list of buffers are\n"
" allowed. Buffers interleaved. Default -b main,system,crash.\n"
" -B, --binary Output the log in binary.\n"
" -S, --statistics Output statistics.\n"
" -p, --prune Print prune white and ~black list. Service is specified as\n"
" UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
" with ~, otherwise weighed for longevity if unadorned. All\n"
" other pruning activity is oldest first. Special case ~!\n"
" represents an automatic quicker pruning for the noisiest\n"
" UID as determined by the current statistics.\n"
" -P '<list> ...', --prune='<list> ...'\n"
" Set prune white and ~black list, using same format as\n"
" listed above. Must be quoted.\n"
" --pid=<pid> Only prints logs from the given pid.\n"
// Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
" --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
" comes first. Improves efficiency of polling by providing\n"
" an about-to-wrap wakeup.\n");
fprintf(context->error, "\nfilterspecs are a series of \n"
" <tag>[:priority]\n\n"
"where <tag> is a log component tag (or * for all) and priority is:\n"
" V Verbose (default for <tag>)\n"
" D Debug (default for '*')\n"
" I Info\n"
" W Warn\n"
" E Error\n"
" F Fatal\n"
" S Silent (suppress all output)\n"
"\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
"If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
"eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
"\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
"\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
"or defaults to \"threadtime\"\n\n");
}
static void show_format_help(android_logcat_context_internal* context) {
if (!context->error) return;
fprintf(context->error,
"-v <format>, --format=<format> options:\n"
" Sets log print format verb and adverbs, where <format> is:\n"
" brief long process raw tag thread threadtime time\n"
" and individually flagged modifying adverbs can be added:\n"
" color descriptive epoch monotonic printable uid usec UTC year zone\n"
"\nSingle format verbs:\n"
" brief — Display priority/tag and PID of the process issuing the message.\n"
" long — Display all metadata fields, separate messages with blank lines.\n"
" process — Display PID only.\n"
" raw — Display the raw log message, with no other metadata fields.\n"
" tag — Display the priority/tag only.\n"
" thread — Display priority, PID and TID of process issuing the message.\n"
" threadtime — Display the date, invocation time, priority, tag, and the PID\n"
" and TID of the thread issuing the message. (the default format).\n"
" time — Display the date, invocation time, priority/tag, and PID of the\n"
" process issuing the message.\n"
"\nAdverb modifiers can be used in combination:\n"
" color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
" \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
" descriptive — events logs only, descriptions from event-log-tags database.\n"
" epoch — Display time as seconds since Jan 1 1970.\n"
" monotonic — Display time as cpu seconds since last boot.\n"
" printable — Ensure that any binary logging content is escaped.\n"
" uid — If permitted, display the UID or Android ID of logged process.\n"
" usec — Display time down the microsecond precision.\n"
" UTC — Display time as UTC.\n"
" year — Add the year to the displayed time.\n"
" zone — Add the local timezone to the displayed time.\n"
" \"<zone>\" — Print using this public named timezone (experimental).\n\n"
);
}
// clang-format on
static int setLogFormat(android_logcat_context_internal* context,
const char* formatString) {
AndroidLogPrintFormat format;
format = android_log_formatFromString(formatString);
// invalid string?
if (format == FORMAT_OFF) return -1;
return android_log_setPrintFormat(context->logformat, format);
}
static const char multipliers[][2] = { { "" }, { "K" }, { "M" }, { "G" } };
static unsigned long value_of_size(unsigned long value) {
for (unsigned i = 0;
(i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
value /= 1024, ++i)
;
return value;
}
static const char* multiplier_of_size(unsigned long value) {
unsigned i;
for (i = 0;
(i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
value /= 1024, ++i)
;
return multipliers[i];
}
// String to unsigned int, returns -1 if it fails
static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
size_t max = SIZE_MAX) {
if (!ptr) return false;
char* endp;
errno = 0;
size_t ret = (size_t)strtoll(ptr, &endp, 0);
if (endp[0] || errno) return false;
if ((ret > max) || (ret < min)) return false;
*val = ret;
return true;
}
static void logcat_panic(android_logcat_context_internal* context,
enum helpType showHelp, const char* fmt, ...) {
context->retval = EXIT_FAILURE;
if (!context->error) {
context->stop = true;
return;
}
va_list args;
va_start(args, fmt);
vfprintf(context->error, fmt, args);
va_end(args);
switch (showHelp) {
case HELP_TRUE:
show_help(context);
break;
case HELP_FORMAT:
show_format_help(context);
break;
case HELP_FALSE:
default:
break;
}
context->stop = true;
}
static char* parseTime(log_time& t, const char* cp) {
char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
if (ep) return ep;
ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
if (ep) return ep;
return t.strptime(cp, "%s.%q");
}
// Find last logged line in <outputFileName>, or <outputFileName>.1
static log_time lastLogTime(const char* outputFileName) {
log_time retval(log_time::EPOCH);
if (!outputFileName) return retval;
std::string directory;
const char* file = strrchr(outputFileName, '/');
if (!file) {
directory = ".";
file = outputFileName;
} else {
directory = std::string(outputFileName, file - outputFileName);
++file;
}
std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
closedir);
if (!dir.get()) return retval;
log_time now(android_log_clockid());
size_t len = strlen(file);
log_time modulo(0, NS_PER_SEC);
struct dirent* dp;
while (!!(dp = readdir(dir.get()))) {
if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
(dp->d_name[len] && ((dp->d_name[len] != '.') ||
(strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
continue;
}
std::string file_name = directory;
file_name += "/";
file_name += dp->d_name;
std::string file;
if (!android::base::ReadFileToString(file_name, &file)) continue;
bool found = false;
for (const auto& line : android::base::Split(file, "\n")) {
log_time t(log_time::EPOCH);
char* ep = parseTime(t, line.c_str());
if (!ep || (*ep != ' ')) continue;
// determine the time precision of the logs (eg: msec or usec)
for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
if (t.tv_nsec % (mod * 10)) {
modulo.tv_nsec = mod;
break;
}
}
// We filter any times later than current as we may not have the
// year stored with each log entry. Also, since it is possible for
// entries to be recorded out of order (very rare) we select the
// maximum we find just in case.
if ((t < now) && (t > retval)) {
retval = t;
found = true;
}
}
// We count on the basename file to be the definitive end, so stop here.
if (!dp->d_name[len] && found) break;
}
if (retval == log_time::EPOCH) return retval;
// tail_time prints matching or higher, round up by the modulo to prevent
// a replay of the last entry we have just checked.
retval += modulo;
return retval;
}
const char* getenv(android_logcat_context_internal* context, const char* name) {
if (!context->envp || !name || !*name) return nullptr;
for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
if (strncmp(context->envp[i], name, len)) continue;
if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
}
return nullptr;
}
} // namespace android
void reportErrorName(const char** current, const char* name,
bool blockSecurity) {
if (*current) return;
if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
*current = name;
}
}
static int __logcat(android_logcat_context_internal* context) {
using namespace android;
int err;
bool hasSetLogFormat = false;
bool clearLog = false;
bool allSelected = false;
bool getLogSize = false;
bool getPruneList = false;
bool printStatistics = false;
bool printDividers = false;
unsigned long setLogSize = 0;
const char* setPruneList = nullptr;
const char* setId = nullptr;
int mode = ANDROID_LOG_RDONLY;
std::string forceFilters;
log_device_t* dev;
struct logger_list* logger_list;
size_t tail_lines = 0;
log_time tail_time(log_time::EPOCH);
size_t pid = 0;
bool got_t = false;
// object instantiations before goto's can happen
log_device_t unexpected("unexpected", false);
const char* openDeviceFail = nullptr;
const char* clearFail = nullptr;
const char* setSizeFail = nullptr;
const char* getSizeFail = nullptr;
int argc = context->argc;
char* const* argv = context->argv;
context->output = stdout;
context->error = stderr;
for (int i = 0; i < argc; ++i) {
// Simulate shell stderr redirect parsing
if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
// Append to file not implemented, just open file
size_t skip = (argv[i][2] == '>') + 2;
if (!strcmp(&argv[i][skip], "/dev/null")) {
context->stderr_null = true;
} else if (!strcmp(&argv[i][skip], "&1")) {
context->stderr_stdout = true;
} else {
// stderr file redirections are not supported
fprintf(context->stderr_stdout ? stdout : stderr,
"stderr redirection to file %s unsupported, skipping\n",
&argv[i][skip]);
}
// Only the first one
break;
}
const char* filename = nullptr;
for (int i = 0; i < argc; ++i) {
// Simulate shell stdout redirect parsing
if (argv[i][0] != '>') continue;
// Append to file not implemented, just open file
filename = &argv[i][(argv[i][1] == '>') + 1];
// Only the first one
break;
}
// Deal with setting up file descriptors and FILE pointers
if (context->error_fd >= 0) { // Is an error file descriptor supplied?
if (context->error_fd == context->output_fd) {
context->stderr_stdout = true;
} else if (context->stderr_null) { // redirection told us to close it
close(context->error_fd);
context->error_fd = -1;
} else { // All Ok, convert error to a FILE pointer
context->error = fdopen(context->error_fd, "web");
if (!context->error) {
context->retval = -errno;
fprintf(context->stderr_stdout ? stdout : stderr,
"Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
strerror(errno));
goto exit;
}
}
}
if (context->output_fd >= 0) { // Is an output file descriptor supplied?
if (filename) { // redirect to file, close supplied file descriptor.
close(context->output_fd);
context->output_fd = -1;
} else { // All Ok, convert output to a FILE pointer
context->output = fdopen(context->output_fd, "web");
if (!context->output) {
context->retval = -errno;
fprintf(context->stderr_stdout ? stdout : context->error,
"Failed to fdopen(output_fd=%d) %s\n",
context->output_fd, strerror(errno));
goto exit;
}
}
}
if (filename) { // We supplied an output file redirected in command line
context->output = fopen(filename, "web");
}
// Deal with 2>&1
if (context->stderr_stdout) context->error = context->output;
// Deal with 2>/dev/null
if (context->stderr_null) {
context->error_fd = -1;
context->error = nullptr;
}
// Only happens if output=stdout or output=filename
if ((context->output_fd < 0) && context->output) {
context->output_fd = fileno(context->output);
}
// Only happens if error=stdout || error=stderr
if ((context->error_fd < 0) && context->error) {
context->error_fd = fileno(context->error);
}
context->logformat = android_log_format_new();
if (argc == 2 && !strcmp(argv[1], "--help")) {
show_help(context);
context->retval = EXIT_SUCCESS;
goto exit;
}
// meant to catch comma-delimited values, but cast a wider
// net for stability dealing with possible mistaken inputs.
static const char delimiters[] = ",:; \t\n\r\f";
struct getopt_context optctx;
INIT_GETOPT_CONTEXT(optctx);
optctx.opterr = !!context->error;
optctx.optstderr = context->error;
for (;;) {
int ret;
int option_index = 0;
// list of long-argument only strings for later comparison
static const char pid_str[] = "pid";
static const char debug_str[] = "debug";
static const char id_str[] = "id";
static const char wrap_str[] = "wrap";
static const char print_str[] = "print";
// clang-format off
static const struct option long_options[] = {
{ "binary", no_argument, nullptr, 'B' },
{ "buffer", required_argument, nullptr, 'b' },
{ "buffer-size", optional_argument, nullptr, 'g' },
{ "clear", no_argument, nullptr, 'c' },
{ debug_str, no_argument, nullptr, 0 },
{ "dividers", no_argument, nullptr, 'D' },
{ "file", required_argument, nullptr, 'f' },
{ "format", required_argument, nullptr, 'v' },
// hidden and undocumented reserved alias for --regex
{ "grep", required_argument, nullptr, 'e' },
// hidden and undocumented reserved alias for --max-count
{ "head", required_argument, nullptr, 'm' },
{ "help", no_argument, nullptr, 'h' },
{ id_str, required_argument, nullptr, 0 },
{ "last", no_argument, nullptr, 'L' },
{ "max-count", required_argument, nullptr, 'm' },
{ pid_str, required_argument, nullptr, 0 },
{ print_str, no_argument, nullptr, 0 },
{ "prune", optional_argument, nullptr, 'p' },
{ "regex", required_argument, nullptr, 'e' },
{ "rotate-count", required_argument, nullptr, 'n' },
{ "rotate-kbytes", required_argument, nullptr, 'r' },
{ "statistics", no_argument, nullptr, 'S' },
// hidden and undocumented reserved alias for -t
{ "tail", required_argument, nullptr, 't' },
// support, but ignore and do not document, the optional argument
{ wrap_str, optional_argument, nullptr, 0 },
{ nullptr, 0, nullptr, 0 }
};
// clang-format on
ret = getopt_long_r(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
long_options, &option_index, &optctx);
if (ret < 0) break;
switch (ret) {
case 0:
// only long options
if (long_options[option_index].name == pid_str) {
// ToDo: determine runtime PID_MAX?
if (!getSizeTArg(optctx.optarg, &pid, 1)) {
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
long_options[option_index].name,
optctx.optarg);
goto exit;
}
break;
}
if (long_options[option_index].name == wrap_str) {
mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
ANDROID_LOG_NONBLOCK;
// ToDo: implement API that supports setting a wrap timeout
size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
if (optctx.optarg &&
!getSizeTArg(optctx.optarg, &dummy, 1)) {
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
long_options[option_index].name,
optctx.optarg);
goto exit;
}
if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
context->error) {
fprintf(context->error,
"WARNING: %s %u seconds, ignoring %zu\n",
long_options[option_index].name,
ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
}
break;
}
if (long_options[option_index].name == print_str) {
context->printItAnyways = true;
break;
}
if (long_options[option_index].name == debug_str) {
context->debug = true;
break;
}
if (long_options[option_index].name == id_str) {
setId = (optctx.optarg && optctx.optarg[0]) ? optctx.optarg
: nullptr;
}
break;
case 's':
// default to all silent
android_log_addFilterRule(context->logformat, "*:s");
break;
case 'c':
clearLog = true;
mode |= ANDROID_LOG_WRONLY;
break;
case 'L':
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
ANDROID_LOG_NONBLOCK;
break;
case 'd':
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
break;
case 't':
got_t = true;
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
// FALLTHRU
case 'T':
if (strspn(optctx.optarg, "0123456789") !=
strlen(optctx.optarg)) {
char* cp = parseTime(tail_time, optctx.optarg);
if (!cp) {
logcat_panic(context, HELP_FALSE,
"-%c \"%s\" not in time format\n", ret,
optctx.optarg);
goto exit;
}
if (*cp) {
char c = *cp;
*cp = '\0';
if (context->error) {
fprintf(
context->error,
"WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
ret, optctx.optarg, c, cp + 1);
}
*cp = c;
}
} else {
if (!getSizeTArg(optctx.optarg, &tail_lines, 1)) {
if (context->error) {
fprintf(context->error,
"WARNING: -%c %s invalid, setting to 1\n",
ret, optctx.optarg);
}
tail_lines = 1;
}
}
break;
case 'D':
printDividers = true;
break;
case 'e':
context->regex = new pcrecpp::RE(optctx.optarg);
break;
case 'm': {
char* end = nullptr;
if (!getSizeTArg(optctx.optarg, &context->maxCount)) {
logcat_panic(context, HELP_FALSE,
"-%c \"%s\" isn't an "
"integer greater than zero\n",
ret, optctx.optarg);
goto exit;
}
} break;
case 'g':
if (!optctx.optarg) {
getLogSize = true;
break;
}
// FALLTHRU
case 'G': {
char* cp;
if (strtoll(optctx.optarg, &cp, 0) > 0) {
setLogSize = strtoll(optctx.optarg, &cp, 0);
} else {
setLogSize = 0;
}
switch (*cp) {
case 'g':
case 'G':
setLogSize *= 1024;
// FALLTHRU
case 'm':
case 'M':
setLogSize *= 1024;
// FALLTHRU
case 'k':
case 'K':
setLogSize *= 1024;
// FALLTHRU
case '\0':
break;
default:
setLogSize = 0;
}
if (!setLogSize) {
logcat_panic(context, HELP_FALSE,
"ERROR: -G <num><multiplier>\n");
goto exit;
}
} break;
case 'p':
if (!optctx.optarg) {
getPruneList = true;
break;
}
// FALLTHRU
case 'P':
setPruneList = optctx.optarg;
break;
case 'b': {
std::unique_ptr<char, void (*)(void*)> buffers(
strdup(optctx.optarg), free);
char* arg = buffers.get();
unsigned idMask = 0;
char* sv = nullptr; // protect against -ENOMEM above
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
if (!strcmp(arg, "default")) {
idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
(1 << LOG_ID_CRASH);
} else if (!strcmp(arg, "all")) {
allSelected = true;
idMask = (unsigned)-1;
} else {
log_id_t log_id = android_name_to_log_id(arg);
const char* name = android_log_id_to_name(log_id);
if (!!strcmp(name, arg)) {
logcat_panic(context, HELP_TRUE,
"unknown buffer %s\n", arg);
goto exit;
}
if (log_id == LOG_ID_SECURITY) allSelected = false;
idMask |= (1 << log_id);
}
arg = nullptr;
}
for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
const char* name = android_log_id_to_name((log_id_t)i);
log_id_t log_id = android_name_to_log_id(name);
if (log_id != (log_id_t)i) continue;
if (!(idMask & (1 << i))) continue;
bool found = false;
for (dev = context->devices; dev; dev = dev->next) {
if (!strcmp(name, dev->device)) {
found = true;
break;
}
if (!dev->next) break;
}
if (found) continue;
bool binary =
!strcmp(name, "events") || !strcmp(name, "security");
log_device_t* d = new log_device_t(name, binary);
if (dev) {
dev->next = d;
dev = d;
} else {
context->devices = dev = d;
}
context->devCount++;
}
} break;
case 'B':
context->printBinary = 1;
break;
case 'f':
if ((tail_time == log_time::EPOCH) && !tail_lines) {
tail_time = lastLogTime(optctx.optarg);
}
// redirect output to a file
context->outputFileName = optctx.optarg;
break;
case 'r':
if (!getSizeTArg(optctx.optarg, &context->logRotateSizeKBytes,
1)) {
logcat_panic(context, HELP_TRUE,
"Invalid parameter \"%s\" to -r\n",
optctx.optarg);
goto exit;
}
break;
case 'n':
if (!getSizeTArg(optctx.optarg, &context->maxRotatedLogs, 1)) {
logcat_panic(context, HELP_TRUE,
"Invalid parameter \"%s\" to -n\n",
optctx.optarg);
goto exit;
}
break;
case 'v': {
if (!strcmp(optctx.optarg, "help") ||
!strcmp(optctx.optarg, "--help")) {
show_format_help(context);
context->retval = EXIT_SUCCESS;
goto exit;
}
std::unique_ptr<char, void (*)(void*)> formats(
strdup(optctx.optarg), free);
char* arg = formats.get();
unsigned idMask = 0;
char* sv = nullptr; // protect against -ENOMEM above
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
err = setLogFormat(context, arg);
if (err < 0) {
logcat_panic(context, HELP_FORMAT,
"Invalid parameter \"%s\" to -v\n", arg);
goto exit;
}
arg = nullptr;
if (err) hasSetLogFormat = true;
}
} break;
case 'Q':
#define LOGCAT_FILTER "androidboot.logcat="
#define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
#define CONSOLE_OPTION "androidboot.console="
#define QEMU_PROPERTY "ro.kernel.qemu"
#define QEMU_CMDLINE "qemu.cmdline"
// This is a *hidden* option used to start a version of logcat
// in an emulated device only. It basically looks for
// androidboot.logcat= on the kernel command line. If
// something is found, it extracts a log filter and uses it to
// run the program. The logcat output will go to consolepipe if
// androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
// it goes to androidboot.console (e.g. tty)
{
// if not in emulator, exit quietly
if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
context->retval = EXIT_SUCCESS;
goto exit;
}
std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
if (cmdline.empty()) {
android::base::ReadFileToString("/proc/cmdline", &cmdline);
}
const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
// if nothing found or invalid filters, exit quietly
if (!logcatFilter) {
context->retval = EXIT_SUCCESS;
goto exit;
}
const char* p = logcatFilter + strlen(LOGCAT_FILTER);
const char* q = strpbrk(p, " \t\n\r");
if (!q) q = p + strlen(p);
forceFilters = std::string(p, q);
// redirect our output to the emulator console pipe or console
const char* consolePipe =
strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
const char* console =
strstr(cmdline.c_str(), CONSOLE_OPTION);
if (consolePipe) {
p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
} else if (console) {
p = console + strlen(CONSOLE_OPTION);
} else {
context->retval = EXIT_FAILURE;
goto exit;
}
q = strpbrk(p, " \t\n\r");
int len = q ? q - p : strlen(p);
std::string devname = "/dev/" + std::string(p, len);
std::string pipePurpose("pipe:logcat");
if (consolePipe) {
// example: "qemu_pipe,pipe:logcat"
// upon opening of /dev/qemu_pipe, the "pipe:logcat"
// string with trailing '\0' should be written to the fd
size_t pos = devname.find(",");
if (pos != std::string::npos) {
pipePurpose = devname.substr(pos + 1);
devname = devname.substr(0, pos);
}
}
cmdline.erase();
if (context->error) {
fprintf(context->error, "logcat using %s\n",
devname.c_str());
}
FILE* fp = fopen(devname.c_str(), "web");
devname.erase();
if (!fp) break;
if (consolePipe) {
// need the trailing '\0'
if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(),
pipePurpose.size() + 1)) {
fclose(fp);
context->retval = EXIT_FAILURE;
goto exit;
}
}
// close output and error channels, replace with console
android::close_output(context);
android::close_error(context);
context->stderr_stdout = true;
context->output = fp;
context->output_fd = fileno(fp);
if (context->stderr_null) break;
context->stderr_stdout = true;
context->error = fp;
context->error_fd = fileno(fp);
}
break;
case 'S':
printStatistics = true;
break;
case ':':
logcat_panic(context, HELP_TRUE,
"Option -%c needs an argument\n", optctx.optopt);
goto exit;
case 'h':
show_help(context);
show_format_help(context);
goto exit;
default:
logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
optctx.optopt);
goto exit;
}
}
if (context->maxCount && got_t) {
logcat_panic(context, HELP_TRUE,
"Cannot use -m (--max-count) and -t together\n");
goto exit;
}
if (context->printItAnyways && (!context->regex || !context->maxCount)) {
// One day it would be nice if --print -v color and --regex <expr>
// could play with each other and show regex highlighted content.
// clang-format off
if (context->error) {
fprintf(context->error, "WARNING: "
"--print ignored, to be used in combination with\n"
" "
"--regex <expr> and --max-count <N>\n");
}
context->printItAnyways = false;
}
if (!context->devices) {
dev = context->devices = new log_device_t("main", false);
context->devCount = 1;
if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
dev = dev->next = new log_device_t("system", false);
context->devCount++;
}
if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
dev = dev->next = new log_device_t("crash", false);
context->devCount++;
}
}
if (!!context->logRotateSizeKBytes && !context->outputFileName) {
logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
goto exit;
}
if (!!setId) {
if (!context->outputFileName) {
logcat_panic(context, HELP_TRUE,
"--id='%s' requires -f as well\n", setId);
goto exit;
}
std::string file_name = android::base::StringPrintf(
"%s.id", context->outputFileName);
std::string file;
bool file_ok = android::base::ReadFileToString(file_name, &file);
android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
getuid(), getgid());
if (!file_ok || !file.compare(setId)) setId = nullptr;
}
if (!hasSetLogFormat) {
const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
if (!!logFormat) {
std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
free);
char* sv = nullptr; // protect against -ENOMEM above
char* arg = formats.get();
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
err = setLogFormat(context, arg);
// environment should not cause crash of logcat
if ((err < 0) && context->error) {
fprintf(context->error,
"invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
}
arg = nullptr;
if (err > 0) hasSetLogFormat = true;
}
}
if (!hasSetLogFormat) {
setLogFormat(context, "threadtime");
}
}
if (forceFilters.size()) {
err = android_log_addFilterString(context->logformat,
forceFilters.c_str());
if (err < 0) {
logcat_panic(context, HELP_FALSE,
"Invalid filter expression in logcat args\n");
goto exit;
}
} else if (argc == optctx.optind) {
// Add from environment variable
const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
if (!!env_tags_orig) {
err = android_log_addFilterString(context->logformat,
env_tags_orig);
if (err < 0) {
logcat_panic(context, HELP_TRUE,
"Invalid filter expression in ANDROID_LOG_TAGS\n");
goto exit;
}
}
} else {
// Add from commandline
for (int i = optctx.optind ; i < argc ; i++) {
// skip stderr redirections of _all_ kinds
if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
// skip stdout redirections of _all_ kinds
if (argv[i][0] == '>') continue;
err = android_log_addFilterString(context->logformat, argv[i]);
if (err < 0) {
logcat_panic(context, HELP_TRUE,
"Invalid filter expression '%s'\n", argv[i]);
goto exit;
}
}
}
dev = context->devices;
if (tail_time != log_time::EPOCH) {
logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
} else {
logger_list = android_logger_list_alloc(mode, tail_lines, pid);
}
// We have three orthogonal actions below to clear, set log size and
// get log size. All sharing the same iteration loop.
while (dev) {
dev->logger_list = logger_list;
dev->logger = android_logger_open(logger_list,
android_name_to_log_id(dev->device));
if (!dev->logger) {
reportErrorName(&openDeviceFail, dev->device, allSelected);
dev = dev->next;
continue;
}
if (clearLog || setId) {
if (context->outputFileName) {
int maxRotationCountDigits =
(context->maxRotatedLogs > 0) ?
(int)(floor(log10(context->maxRotatedLogs) + 1)) :
0;
for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
std::string file;
if (!i) {
file = android::base::StringPrintf(
"%s", context->outputFileName);
} else {
file = android::base::StringPrintf("%s.%.*d",
context->outputFileName, maxRotationCountDigits, i);
}
if (!file.length()) {
perror("while clearing log files");
reportErrorName(&clearFail, dev->device, allSelected);
break;
}
err = unlink(file.c_str());
if (err < 0 && errno != ENOENT && !clearFail) {
perror("while clearing log files");
reportErrorName(&clearFail, dev->device, allSelected);
}
}
} else if (android_logger_clear(dev->logger)) {
reportErrorName(&clearFail, dev->device, allSelected);
}
}
if (setLogSize) {
if (android_logger_set_log_size(dev->logger, setLogSize)) {
reportErrorName(&setSizeFail, dev->device, allSelected);
}
}
if (getLogSize) {
long size = android_logger_get_log_size(dev->logger);
long readable = android_logger_get_log_readable_size(dev->logger);
if ((size < 0) || (readable < 0)) {
reportErrorName(&getSizeFail, dev->device, allSelected);
} else {
std::string str = android::base::StringPrintf(
"%s: ring buffer is %ld%sb (%ld%sb consumed),"
" max entry is %db, max payload is %db\n",
dev->device,
value_of_size(size), multiplier_of_size(size),
value_of_size(readable), multiplier_of_size(readable),
(int)LOGGER_ENTRY_MAX_LEN,
(int)LOGGER_ENTRY_MAX_PAYLOAD);
TEMP_FAILURE_RETRY(write(context->output_fd,
str.data(), str.length()));
}
}
dev = dev->next;
}
context->retval = EXIT_SUCCESS;
// report any errors in the above loop and exit
if (openDeviceFail) {
logcat_panic(context, HELP_FALSE,
"Unable to open log device '%s'\n", openDeviceFail);
goto close;
}
if (clearFail) {
logcat_panic(context, HELP_FALSE,
"failed to clear the '%s' log\n", clearFail);
goto close;
}
if (setSizeFail) {
logcat_panic(context, HELP_FALSE,
"failed to set the '%s' log size\n", setSizeFail);
goto close;
}
if (getSizeFail) {
logcat_panic(context, HELP_FALSE,
"failed to get the readable '%s' log size", getSizeFail);
goto close;
}
if (setPruneList) {
size_t len = strlen(setPruneList);
// extra 32 bytes are needed by android_logger_set_prune_list
size_t bLen = len + 32;
char* buf = nullptr;
if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
buf[len] = '\0';
if (android_logger_set_prune_list(logger_list, buf, bLen)) {
logcat_panic(context, HELP_FALSE,
"failed to set the prune list");
}
free(buf);
} else {
logcat_panic(context, HELP_FALSE,
"failed to set the prune list (alloc)");
}
goto close;
}
if (printStatistics || getPruneList) {
size_t len = 8192;
char* buf;
for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
delete[] buf, buf = nullptr, --retry) {
if (getPruneList) {
android_logger_get_prune_list(logger_list, buf, len);
} else {
android_logger_get_statistics(logger_list, buf, len);
}
buf[len - 1] = '\0';
if (atol(buf) < 3) {
delete[] buf;
buf = nullptr;
break;
}
size_t ret = atol(buf) + 1;
if (ret <= len) {
len = ret;
break;
}
len = ret;
}
if (!buf) {
logcat_panic(context, HELP_FALSE, "failed to read data");
goto close;
}
// remove trailing FF
char* cp = buf + len - 1;
*cp = '\0';
bool truncated = *--cp != '\f';
if (!truncated) *cp = '\0';
// squash out the byte count
cp = buf;
if (!truncated) {
while (isdigit(*cp)) ++cp;
if (*cp == '\n') ++cp;
}
len = strlen(cp);
TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
delete[] buf;
goto close;
}
if (getLogSize || setLogSize || clearLog) goto close;
setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
if (context->stop) goto close;
// LOG_EVENT_INT(10, 12345);
// LOG_EVENT_LONG(11, 0x1122334455667788LL);
// LOG_EVENT_STRING(0, "whassup, doc?");
dev = nullptr;
while (!context->stop &&
(!context->maxCount || (context->printCount < context->maxCount))) {
struct log_msg log_msg;
int ret = android_logger_list_read(logger_list, &log_msg);
if (!ret) {
logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
break;
}
if (ret < 0) {
if (ret == -EAGAIN) break;
if (ret == -EIO) {
logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
break;
}
if (ret == -EINVAL) {
logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
break;
}
logcat_panic(context, HELP_FALSE, "logcat read failure\n");
break;
}
log_device_t* d;
for (d = context->devices; d; d = d->next) {
if (android_name_to_log_id(d->device) == log_msg.id()) break;
}
if (!d) {
context->devCount = 2; // set to Multiple
d = &unexpected;
d->binary = log_msg.id() == LOG_ID_EVENTS;
}
if (dev != d) {
dev = d;
maybePrintStart(context, dev, printDividers);
if (context->stop) break;
}
if (context->printBinary) {
printBinary(context, &log_msg);
} else {
processBuffer(context, dev, &log_msg);
}
}
close:
// Short and sweet. Implemented generic version in android_logcat_destroy.
while (!!(dev = context->devices)) {
context->devices = dev->next;
delete dev;
}
android_logger_list_free(logger_list);
exit:
// close write end of pipe to help things along
if (context->output_fd == context->fds[1]) {
android::close_output(context);
}
if (context->error_fd == context->fds[1]) {
android::close_error(context);
}
if (context->fds[1] >= 0) {
// NB: should be closed by the above
int save_errno = errno;
close(context->fds[1]);
errno = save_errno;
context->fds[1] = -1;
}
context->thread_stopped = true;
return context->retval;
}
// Can block
int android_logcat_run_command(android_logcat_context ctx,
int output, int error,
int argc, char* const* argv,
char* const* envp) {
android_logcat_context_internal* context = ctx;
context->output_fd = output;
context->error_fd = error;
context->argc = argc;
context->argv = argv;
context->envp = envp;
context->stop = false;
context->thread_stopped = false;
return __logcat(context);
}
// starts a thread, opens a pipe, returns reading end.
int android_logcat_run_command_thread(android_logcat_context ctx,
int argc, char* const* argv,
char* const* envp) {
android_logcat_context_internal* context = ctx;
int save_errno = EBUSY;
if ((context->fds[0] >= 0) || (context->fds[1] >= 0)) goto exit;
if (pipe(context->fds) < 0) {
save_errno = errno;
goto exit;
}
pthread_attr_t attr;
if (pthread_attr_init(&attr)) {
save_errno = errno;
goto close_exit;
}
struct sched_param param;
memset(¶m, 0, sizeof(param));
pthread_attr_setschedparam(&attr, ¶m);
pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
int save_errno = errno;
goto pthread_attr_exit;
}
context->stop = false;
context->thread_stopped = false;
context->output_fd = context->fds[1];
// save off arguments so they remain while thread is active.
for (int i = 0; i < argc; ++i) {
context->args.push_back(std::string(argv[i]));
}
// save off environment so they remain while thread is active.
if (envp) for (size_t i = 0; envp[i]; ++i) {
context->envs.push_back(std::string(envp[i]));
}
for (auto& str : context->args) {
context->argv_hold.push_back(str.c_str());
}
context->argv_hold.push_back(nullptr);
for (auto& str : context->envs) {
context->envp_hold.push_back(str.c_str());
}
context->envp_hold.push_back(nullptr);
context->argc = context->argv_hold.size() - 1;
context->argv = (char* const*)&context->argv_hold[0];
context->envp = (char* const*)&context->envp_hold[0];
#ifdef DEBUG
fprintf(stderr, "argv[%d] = {", context->argc);
for (auto str : context->argv_hold) {
fprintf(stderr, " \"%s\"", str ?: "nullptr");
}
fprintf(stderr, " }\n");
fflush(stderr);
#endif
context->retval = EXIT_SUCCESS;
if (pthread_create(&context->thr, &attr,
(void*(*)(void*))__logcat, context)) {
int save_errno = errno;
goto argv_exit;
}
pthread_attr_destroy(&attr);
return context->fds[0];
argv_exit:
context->argv_hold.clear();
context->args.clear();
context->envp_hold.clear();
context->envs.clear();
pthread_attr_exit:
pthread_attr_destroy(&attr);
close_exit:
close(context->fds[0]);
context->fds[0] = -1;
close(context->fds[1]);
context->fds[1] = -1;
exit:
errno = save_errno;
context->stop = true;
context->thread_stopped = true;
context->retval = EXIT_FAILURE;
return -1;
}
// test if the thread is still doing 'stuff'
int android_logcat_run_command_thread_running(android_logcat_context ctx) {
android_logcat_context_internal* context = ctx;
return context->thread_stopped == false;
}
// Finished with context
int android_logcat_destroy(android_logcat_context* ctx) {
android_logcat_context_internal* context = *ctx;
if (!context) return -EBADF;
*ctx = nullptr;
context->stop = true;
while (context->thread_stopped == false) {
// Makes me sad, replace thread_stopped with semaphore. Short lived.
sched_yield();
}
delete context->regex;
context->argv_hold.clear();
context->args.clear();
context->envp_hold.clear();
context->envs.clear();
if (context->fds[0] >= 0) {
close(context->fds[0]);
context->fds[0] = -1;
}
android::close_output(context);
android::close_error(context);
if (context->fds[1] >= 0) {
// NB: could be closed by the above fclose(s), ignore error.
int save_errno = errno;
close(context->fds[1]);
errno = save_errno;
context->fds[1] = -1;
}
android_closeEventTagMap(context->eventTagMap);
// generic cleanup of devices list to handle all possible dirty cases
log_device_t* dev;
while (!!(dev = context->devices)) {
struct logger_list* logger_list = dev->logger_list;
if (logger_list) {
for (log_device_t* d = dev; d; d = d->next) {
if (d->logger_list == logger_list) d->logger_list = nullptr;
}
android_logger_list_free(logger_list);
}
context->devices = dev->next;
delete dev;
}
int retval = context->retval;
free(context);
return retval;
}
|