| 1 | /*************************************************************************** |
|---|
| 2 | * (c)2011 Broadcom Corporation |
|---|
| 3 | * |
|---|
| 4 | * This program is the proprietary software of Broadcom Corporation and/or its licensors, |
|---|
| 5 | * and may only be used, duplicated, modified or distributed pursuant to the terms and |
|---|
| 6 | * conditions of a separate, written license agreement executed between you and Broadcom |
|---|
| 7 | * (an "Authorized License"). Except as set forth in an Authorized License, Broadcom grants |
|---|
| 8 | * no license (express or implied), right to use, or waiver of any kind with respect to the |
|---|
| 9 | * Software, and Broadcom expressly reserves all rights in and to the Software and all |
|---|
| 10 | * intellectual property rights therein. IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU |
|---|
| 11 | * HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY |
|---|
| 12 | * NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE. |
|---|
| 13 | * |
|---|
| 14 | * Except as expressly set forth in the Authorized License, |
|---|
| 15 | * |
|---|
| 16 | * 1. This program, including its structure, sequence and organization, constitutes the valuable trade |
|---|
| 17 | * secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof, |
|---|
| 18 | * and to use this information only in connection with your use of Broadcom integrated circuit products. |
|---|
| 19 | * |
|---|
| 20 | * 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" |
|---|
| 21 | * AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR |
|---|
| 22 | * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO |
|---|
| 23 | * THE SOFTWARE. BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES |
|---|
| 24 | * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, |
|---|
| 25 | * LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION |
|---|
| 26 | * OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF |
|---|
| 27 | * USE OR PERFORMANCE OF THE SOFTWARE. |
|---|
| 28 | * |
|---|
| 29 | * 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS |
|---|
| 30 | * LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR |
|---|
| 31 | * EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR |
|---|
| 32 | * USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF |
|---|
| 33 | * THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT |
|---|
| 34 | * ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE |
|---|
| 35 | * LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF |
|---|
| 36 | * ANY LIMITED REMEDY. |
|---|
| 37 | * |
|---|
| 38 | * $brcm_Workfile: logger.c $ |
|---|
| 39 | * $brcm_Revision: 9 $ |
|---|
| 40 | * $brcm_Date: 8/24/11 4:29p $ |
|---|
| 41 | * |
|---|
| 42 | * Module Description: |
|---|
| 43 | * |
|---|
| 44 | * Revision History: |
|---|
| 45 | * |
|---|
| 46 | * $brcm_Log: /nexus/utils/logger/logger.c $ |
|---|
| 47 | * |
|---|
| 48 | * 9 8/24/11 4:29p vsilyaev |
|---|
| 49 | * SW7346-466: Take 3 on fixing tool false positives |
|---|
| 50 | * |
|---|
| 51 | * 8 8/22/11 11:30a vsilyaev |
|---|
| 52 | * SW7346-466: Fixed tool false positives |
|---|
| 53 | * |
|---|
| 54 | * 7 8/19/11 11:40a vsilyaev |
|---|
| 55 | * SW7346-466: Added assert |
|---|
| 56 | * |
|---|
| 57 | * 6 8/15/11 12:12p vsilyaev |
|---|
| 58 | * SW7420-1465, SW7405-5221: Use -I to point deep into the platform |
|---|
| 59 | * directory |
|---|
| 60 | * |
|---|
| 61 | * 5 8/9/11 12:50p vsilyaev |
|---|
| 62 | * SW7420-1465: Improved handshake protocol |
|---|
| 63 | * |
|---|
| 64 | * 4 8/5/11 1:02p vsilyaev |
|---|
| 65 | * SW7420-1465: Added code to handle condition when logger was running |
|---|
| 66 | * prior to parent have activated the debug log |
|---|
| 67 | * |
|---|
| 68 | * 3 8/4/11 8:02p vsilyaev |
|---|
| 69 | * SW7420-1465: Added protocol to ensure that all buffered debug output |
|---|
| 70 | * gets written out |
|---|
| 71 | * |
|---|
| 72 | * 2 8/3/11 5:33p vsilyaev |
|---|
| 73 | * SW7420-1465, SW7405-5221: Route debug output to the special FIFO |
|---|
| 74 | * instead of syslog buffer |
|---|
| 75 | * |
|---|
| 76 | * 1 6/3/11 5:35p vsilyaev |
|---|
| 77 | * SW7405-4477: nexus logger tool |
|---|
| 78 | * |
|---|
| 79 | **************************************************************************/ |
|---|
| 80 | #include "bstd.h" |
|---|
| 81 | #include "bkni.h" |
|---|
| 82 | #include "bdbg_fifo.h" |
|---|
| 83 | #include "bdbg_log.h" |
|---|
| 84 | #include <stdio.h> |
|---|
| 85 | #include <stdlib.h> |
|---|
| 86 | #include <sys/mman.h> |
|---|
| 87 | #include <sys/types.h> |
|---|
| 88 | #include <sys/stat.h> |
|---|
| 89 | #include <fcntl.h> |
|---|
| 90 | #include <signal.h> |
|---|
| 91 | #include <sys/ioctl.h> |
|---|
| 92 | #include "nexus_driver_ioctl.h" |
|---|
| 93 | |
|---|
| 94 | BDBG_MODULE(logger); |
|---|
| 95 | |
|---|
| 96 | static bool sigusr1_fired=false; |
|---|
| 97 | |
|---|
| 98 | static void sigusr1_handler(int unused) |
|---|
| 99 | { |
|---|
| 100 | BSTD_UNUSED(unused); |
|---|
| 101 | sigusr1_fired = true; |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | static void usage(const char *name) |
|---|
| 106 | { |
|---|
| 107 | fprintf(stderr, "Usage:\n%s log_file [device_node]\n", name); |
|---|
| 108 | exit(0); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | int main(int argc, const char *argv[]) |
|---|
| 112 | { |
|---|
| 113 | BERR_Code rc; |
|---|
| 114 | BDBG_FifoReader_Handle logReader; |
|---|
| 115 | BDBG_Fifo_Handle logWriter; |
|---|
| 116 | int fd; |
|---|
| 117 | int device_fd=-1; |
|---|
| 118 | bool driver_ready = false; |
|---|
| 119 | const char *fname; |
|---|
| 120 | size_t size; |
|---|
| 121 | void *shared; |
|---|
| 122 | struct stat st; |
|---|
| 123 | int urc; |
|---|
| 124 | pid_t parent; |
|---|
| 125 | PROXY_NEXUS_Log_Instance instance; |
|---|
| 126 | |
|---|
| 127 | if(argc<2) { |
|---|
| 128 | usage(argv[0]); |
|---|
| 129 | } |
|---|
| 130 | BSTD_UNUSED(argv); |
|---|
| 131 | |
|---|
| 132 | rc = BKNI_Init(); |
|---|
| 133 | BDBG_ASSERT(rc==BERR_SUCCESS); |
|---|
| 134 | rc = BDBG_Init(); |
|---|
| 135 | BDBG_ASSERT(rc==BERR_SUCCESS); |
|---|
| 136 | |
|---|
| 137 | /* coverity[var_assign_var] */ |
|---|
| 138 | fname = argv[1]; |
|---|
| 139 | BDBG_ASSERT(fname); |
|---|
| 140 | /* coverity[tainted_string] */ |
|---|
| 141 | fd = open(fname, O_RDONLY); |
|---|
| 142 | if(fd<0) { |
|---|
| 143 | perror(fname); |
|---|
| 144 | usage(argv[0]); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | if(argc>2) { |
|---|
| 148 | int ready; |
|---|
| 149 | /*( device_fd = open(argv[2],O_RDWR); */ |
|---|
| 150 | device_fd = atoi(argv[2]); |
|---|
| 151 | if(device_fd<0) { |
|---|
| 152 | perror(argv[2]); |
|---|
| 153 | usage(argv[0]); |
|---|
| 154 | } |
|---|
| 155 | urc = ioctl(device_fd, IOCTL_PROXY_NEXUS_Log_Test, &ready); |
|---|
| 156 | if(urc!=0) { |
|---|
| 157 | perror(argv[2]); |
|---|
| 158 | usage(argv[0]); |
|---|
| 159 | } |
|---|
| 160 | if(ready) { |
|---|
| 161 | urc = ioctl(device_fd, IOCTL_PROXY_NEXUS_Log_Create, &instance); |
|---|
| 162 | if(urc==0) { |
|---|
| 163 | driver_ready = true; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | /* unlink(fname); don't remove file, allow multiple copies of logger */ |
|---|
| 168 | urc = fstat(fd, &st); |
|---|
| 169 | if(urc<0) { |
|---|
| 170 | perror("stat"); |
|---|
| 171 | usage(argv[0]); |
|---|
| 172 | } |
|---|
| 173 | parent = getppid(); |
|---|
| 174 | signal(SIGUSR1,sigusr1_handler); |
|---|
| 175 | size = st.st_size; |
|---|
| 176 | shared = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); |
|---|
| 177 | if(shared==MAP_FAILED) { |
|---|
| 178 | perror("mmap"); |
|---|
| 179 | usage(argv[0]); |
|---|
| 180 | } |
|---|
| 181 | logWriter = (BDBG_Fifo_Handle)shared; |
|---|
| 182 | rc = BDBG_FifoReader_Create(&logReader, logWriter); |
|---|
| 183 | BDBG_ASSERT(rc==BERR_SUCCESS); |
|---|
| 184 | for(;;) { |
|---|
| 185 | unsigned timeout; |
|---|
| 186 | |
|---|
| 187 | for(;;) { |
|---|
| 188 | char dbgStr[257]; |
|---|
| 189 | size_t dbgStrLen; |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | timeout = 0; |
|---|
| 193 | rc = BDBG_Log_Dequeue(logReader, &timeout, dbgStr, sizeof(dbgStr)-1, &dbgStrLen); |
|---|
| 194 | if(rc!=BERR_SUCCESS) {rc=BERR_TRACE(rc);break;} |
|---|
| 195 | if(dbgStrLen) { |
|---|
| 196 | /* BDBG_ERR(("%u %u", dbgStrLen, strlen(dbgStr))); */ |
|---|
| 197 | dbgStr[dbgStrLen] = '\n'; |
|---|
| 198 | dbgStrLen++; |
|---|
| 199 | urc = write(STDERR_FILENO, dbgStr, dbgStrLen); |
|---|
| 200 | /* BDBG_ERR(("%d %d %d", urc, dbgStrLen, strlen(dbgStr))); */ |
|---|
| 201 | /* BDBG_ASSERT(urc==(int)dbgStrLen); */ |
|---|
| 202 | } |
|---|
| 203 | if(driver_ready) { |
|---|
| 204 | PROXY_NEXUS_Log_Dequeue dequeue; |
|---|
| 205 | dequeue.instance = instance; |
|---|
| 206 | dequeue.buffer_size = sizeof(dbgStr)-1; |
|---|
| 207 | dequeue.buffer = dbgStr; |
|---|
| 208 | dequeue.timeout = 0; |
|---|
| 209 | urc = ioctl(device_fd, IOCTL_PROXY_NEXUS_Log_Dequeue, &dequeue); |
|---|
| 210 | if(urc!=0) { |
|---|
| 211 | BDBG_MSG(("Can't read data from the driver")); |
|---|
| 212 | dequeue.timeout = timeout; |
|---|
| 213 | dequeue.buffer_size = 0; |
|---|
| 214 | } |
|---|
| 215 | dbgStrLen = dequeue.buffer_size; |
|---|
| 216 | if(dbgStrLen) { |
|---|
| 217 | dbgStr[dbgStrLen] = '\n'; |
|---|
| 218 | dbgStrLen++; |
|---|
| 219 | urc = write(STDERR_FILENO, dbgStr, dbgStrLen); |
|---|
| 220 | } |
|---|
| 221 | if(timeout>dequeue.timeout) { |
|---|
| 222 | timeout = dequeue.timeout; |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | if(timeout!=0) { |
|---|
| 226 | break; |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | if(timeout) { |
|---|
| 230 | if(sigusr1_fired) { |
|---|
| 231 | goto done; |
|---|
| 232 | } |
|---|
| 233 | if(kill(parent, 0)) { |
|---|
| 234 | static const char terminated[] = "_____ TERMINATED _____\n"; |
|---|
| 235 | write(STDERR_FILENO, terminated, sizeof(terminated)-1); |
|---|
| 236 | break; |
|---|
| 237 | } |
|---|
| 238 | BKNI_Sleep(timeout); |
|---|
| 239 | if(device_fd>=0 && !driver_ready) { |
|---|
| 240 | int ready; |
|---|
| 241 | /* if parent was dealyed keep trying to attach */ |
|---|
| 242 | urc = ioctl(device_fd, IOCTL_PROXY_NEXUS_Log_Test, &ready); |
|---|
| 243 | if(urc!=0) { |
|---|
| 244 | goto done; |
|---|
| 245 | } |
|---|
| 246 | if(ready) { |
|---|
| 247 | urc = ioctl(device_fd, IOCTL_PROXY_NEXUS_Log_Create, &instance); |
|---|
| 248 | if(urc!=0) { |
|---|
| 249 | goto done; |
|---|
| 250 | } |
|---|
| 251 | driver_ready = true; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | continue; |
|---|
| 255 | } |
|---|
| 256 | break; |
|---|
| 257 | } |
|---|
| 258 | done: |
|---|
| 259 | if(device_fd>=0) { |
|---|
| 260 | close(device_fd); |
|---|
| 261 | } |
|---|
| 262 | close(fd); |
|---|
| 263 | BDBG_FifoReader_Destroy(logReader); |
|---|
| 264 | |
|---|
| 265 | BDBG_Uninit(); |
|---|
| 266 | /* BKNI_Uninit(); Don't call it since this would print memory allocation stats */ |
|---|
| 267 | return 0; |
|---|
| 268 | } |
|---|
| 269 | |
|---|