1 %{ 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 * 22 */ 23 /* 24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 /* 29 * Copyright (c) 2015, Joyent, Inc. All rights reserved. 30 */ 31 32 #include <mdb/mdb_types.h> 33 #include <mdb/mdb_debug.h> 34 #include <mdb/mdb_shell.h> 35 #include <mdb/mdb_string.h> 36 #include <mdb/mdb_frame.h> 37 #include <mdb/mdb_lex.h> 38 #include <mdb/mdb_io.h> 39 #include <mdb/mdb_nv.h> 40 #include <mdb/mdb.h> 41 42 /* 43 * Utility routines to fetch values from the target's virtual address space 44 * and object file, respectively. These are called from the handlers for 45 * the * /.../ and % /.../ code below. 46 */ 47 48 static void 49 vfetch(void *buf, size_t nbytes, uintptr_t addr) 50 { 51 if (mdb_tgt_vread(mdb.m_target, buf, nbytes, addr) != nbytes) 52 yyperror("failed to read from address %p", addr); 53 } 54 55 static void 56 ffetch(void *buf, size_t nbytes, uintptr_t addr) 57 { 58 if (mdb_tgt_fread(mdb.m_target, buf, nbytes, addr) != nbytes) 59 yyperror("failed to read from address %p", addr); 60 } 61 62 /* 63 * Because we define YYMAXDEPTH as zero below, we have to provide a YYEXPAND() 64 * function to expand our yys and yyv variables. For simplicity, we currently 65 * define these structures statically; a more complex solution can be defined if 66 * it is ever needed. If we return 'val', yacc assumes resize has failed. 67 */ 68 static int 69 yyexpand(int val) 70 { 71 return (val ? val : YYMAXDEPTH); 72 } 73 #define YYEXPAND yyexpand 74 75 /* 76 * This will cause the rest of the yacc code to assume that yys and yyv are 77 * pointers, not static arrays. 78 */ 79 #undef YYMAXDEPTH 80 #define YYMAXDEPTH 0 81 %} 82 83 %union { 84 char *l_string; 85 char l_char; 86 uintmax_t l_immediate; 87 mdb_var_t *l_var; 88 mdb_idcmd_t *l_dcmd; 89 } 90 91 %token <l_string> MDB_TOK_SYMBOL 92 %token <l_string> MDB_TOK_STRING 93 %token <l_char> MDB_TOK_CHAR 94 %token <l_immediate> MDB_TOK_IMMEDIATE 95 %token <l_dcmd> MDB_TOK_DCMD 96 %token <l_var> MDB_TOK_VAR_REF 97 %token <l_immediate> MDB_TOK_LEXPR 98 %token <l_immediate> MDB_TOK_REXPR 99 %token <l_immediate> MDB_TOK_COR1_DEREF 100 %token <l_immediate> MDB_TOK_COR2_DEREF 101 %token <l_immediate> MDB_TOK_COR4_DEREF 102 %token <l_immediate> MDB_TOK_COR8_DEREF 103 %token <l_immediate> MDB_TOK_OBJ1_DEREF 104 %token <l_immediate> MDB_TOK_OBJ2_DEREF 105 %token <l_immediate> MDB_TOK_OBJ4_DEREF 106 %token <l_immediate> MDB_TOK_OBJ8_DEREF 107 108 %left '|' 109 %left '^' 110 %left '&' 111 %left MDB_TOK_EQUAL MDB_TOK_NOTEQUAL 112 %left MDB_TOK_LSHIFT MDB_TOK_RSHIFT 113 %left '-' '+' 114 %left '*' '%' '#' 115 116 %right MDB_COR_VALUE 117 %right MDB_OBJ_VALUE 118 %right MDB_INT_NEGATE 119 %right MDB_BIT_COMPLEMENT 120 %right MDB_LOG_NEGATE 121 %right MDB_VAR_REFERENCE 122 123 %type <l_immediate> expression 124 %type <l_dcmd> command 125 126 %% 127 statement_list: /* Empty */ 128 | statement_list statement { return (0); } 129 ; 130 131 terminator: '\n' 132 | ';' 133 134 statement: pipeline shell_pipe terminator { 135 if (!mdb_call(mdb_nv_get_value(mdb.m_dot), 1, 0)) 136 return (0); 137 } 138 139 | expression pipeline shell_pipe terminator { 140 if (!mdb_call($1, 1, DCMD_ADDRSPEC)) 141 return (0); 142 } 143 144 | expression ',' expression pipeline shell_pipe terminator { 145 if (!mdb_call($1, $3, DCMD_ADDRSPEC | DCMD_LOOP)) 146 return (0); 147 } 148 149 | ',' expression pipeline shell_pipe terminator { 150 if (!mdb_call(mdb_nv_get_value(mdb.m_dot), $2, 151 DCMD_LOOP)) 152 return (0); 153 } 154 155 | expression terminator { 156 mdb_frame_t *pfp = mdb_frame_pipe(); 157 /* 158 * The handling of naked expressions is slightly tricky: 159 * in a string context, we want to just set dot to the 160 * expression value. In a pipe context, we also set 161 * dot but need to record the address in the right- 162 * hand command's addrv and update any vcbs that are 163 * active. Otherwise, on the command-line, we have to 164 * support this as an alias for executing the previous 165 * command with the new value of dot. Sigh. 166 */ 167 if (mdb_iob_isastr(mdb.m_in)) { 168 mdb_nv_set_value(mdb.m_dot, $1); 169 mdb.m_incr = 0; 170 } else if (pfp != NULL && pfp->f_pcmd != NULL) { 171 mdb_addrvec_unshift(&pfp->f_pcmd->c_addrv, 172 (uintptr_t)$1); 173 mdb_vcb_update(pfp, (uintptr_t)$1); 174 mdb_nv_set_value(mdb.m_dot, $1); 175 } else { 176 mdb_list_move(&mdb.m_lastc, 177 &mdb.m_frame->f_cmds); 178 if (!mdb_call($1, 1, DCMD_ADDRSPEC)) 179 return (0); 180 } 181 } 182 183 | expression ',' expression shell_pipe terminator { 184 mdb_list_move(&mdb.m_lastc, &mdb.m_frame->f_cmds); 185 if (!mdb_call($1, $3, DCMD_ADDRSPEC | DCMD_LOOP)) 186 return (0); 187 } 188 189 | ',' expression shell_pipe terminator { 190 uintmax_t dot = mdb_dot_incr(","); 191 mdb_list_move(&mdb.m_lastc, &mdb.m_frame->f_cmds); 192 if (!mdb_call(dot, $2, DCMD_LOOP)) 193 return (0); 194 } 195 196 | '!' MDB_TOK_STRING terminator { 197 if (mdb_iob_isapipe(mdb.m_in)) 198 yyerror("syntax error"); 199 mdb_shell_exec($2); 200 } 201 202 | terminator { 203 if ((mdb.m_flags & MDB_FL_REPLAST) && 204 !mdb_iob_isastr(mdb.m_in)) { 205 uintmax_t dot = mdb_dot_incr("\\n"); 206 /* 207 * If a bare terminator is encountered, execute 208 * the previous command if -o repeatlast is set 209 * and stdin is not an mdb_eval() string. 210 */ 211 mdb_list_move(&mdb.m_lastc, 212 &mdb.m_frame->f_cmds); 213 if (!mdb_call(dot, 1, 0)) 214 return (0); 215 } 216 } 217 ; 218 219 pipeline: pipeline '|' command { mdb_cmd_create($3, &mdb.m_frame->f_argvec); } 220 | command { mdb_cmd_create($1, &mdb.m_frame->f_argvec); } 221 ; 222 223 command: '?' format_list { $$ = mdb_dcmd_lookup("?"); } 224 | '/' format_list { $$ = mdb_dcmd_lookup("/"); } 225 | '\\' format_list { $$ = mdb_dcmd_lookup("\\"); } 226 | '@' format_list { $$ = mdb_dcmd_lookup("@"); } 227 | '=' format_list { $$ = mdb_dcmd_lookup("="); } 228 | MDB_TOK_DCMD argument_list { $$ = $1; } 229 | '$' { $$ = mdb_dcmd_lookup("$?"); } 230 ; 231 232 shell_pipe: /* Empty */ 233 | '!' MDB_TOK_STRING { mdb_shell_pipe($2); } 234 ; 235 236 format_list: /* Empty */ 237 | format_list MDB_TOK_LEXPR expression MDB_TOK_REXPR { 238 mdb_arg_t arg; 239 240 arg.a_type = MDB_TYPE_IMMEDIATE; 241 arg.a_un.a_val = $3; 242 243 mdb_argvec_append(&mdb.m_frame->f_argvec, &arg); 244 } 245 246 | format_list MDB_TOK_IMMEDIATE { 247 mdb_arg_t arg; 248 249 arg.a_type = MDB_TYPE_IMMEDIATE; 250 arg.a_un.a_val = $2; 251 252 mdb_argvec_append(&mdb.m_frame->f_argvec, &arg); 253 } 254 255 | format_list MDB_TOK_STRING { 256 mdb_arg_t arg; 257 258 arg.a_type = MDB_TYPE_STRING; 259 arg.a_un.a_str = $2; 260 261 mdb_argvec_append(&mdb.m_frame->f_argvec, &arg); 262 } 263 264 | format_list MDB_TOK_CHAR { 265 mdb_arg_t arg; 266 267 arg.a_type = MDB_TYPE_CHAR; 268 arg.a_un.a_char = $2; 269 270 mdb_argvec_append(&mdb.m_frame->f_argvec, &arg); 271 } 272 ; 273 274 argument_list: /* Empty */ 275 | argument_list MDB_TOK_LEXPR expression MDB_TOK_REXPR { 276 mdb_arg_t arg; 277 278 arg.a_type = MDB_TYPE_IMMEDIATE; 279 arg.a_un.a_val = $3; 280 281 mdb_argvec_append(&mdb.m_frame->f_argvec, &arg); 282 } 283 284 | argument_list MDB_TOK_STRING { 285 mdb_arg_t arg; 286 287 arg.a_type = MDB_TYPE_STRING; 288 arg.a_un.a_str = $2; 289 290 mdb_argvec_append(&mdb.m_frame->f_argvec, &arg); 291 } 292 ; 293 294 expression: expression '+' expression { $$ = $1 + $3; } 295 | expression '-' expression { $$ = $1 - $3; } 296 | expression '*' expression { $$ = $1 * $3; } 297 298 | expression '%' expression { 299 if ($3 == 0UL) 300 yyerror("attempted to divide by zero"); 301 302 /* 303 * Annoyingly, x86 generates a #DE when dividing 304 * LONG_MIN by -1; check for this case explicitly. 305 */ 306 if ($1 == LONG_MIN && $3 == -1L) 307 yyerror("divide overflow"); 308 309 $$ = (intmax_t)$1 / (intmax_t)$3; 310 } 311 312 | expression '&' expression { $$ = $1 & $3; } 313 | expression '|' expression { $$ = $1 | $3; } 314 | expression '^' expression { $$ = $1 ^ $3; } 315 316 | expression MDB_TOK_EQUAL expression { $$ = ($1 == $3); } 317 | expression MDB_TOK_NOTEQUAL expression { $$ = ($1 != $3); } 318 319 | expression MDB_TOK_LSHIFT expression { $$ = $1 << $3; } 320 | expression MDB_TOK_RSHIFT expression { $$ = $1 >> $3; } 321 322 | expression '#' expression { 323 if ($3 == 0UL) 324 yyerror("attempted to divide by zero"); 325 326 $$ = ((intptr_t)($1 + ($3 - 1)) / (intptr_t)$3) * $3; 327 } 328 329 | '*' expression %prec MDB_COR_VALUE { 330 uintptr_t value; 331 332 vfetch(&value, sizeof (value), $2); 333 $$ = value; 334 } 335 336 | MDB_TOK_COR1_DEREF expression %prec MDB_COR_VALUE { 337 uint8_t value; 338 339 vfetch(&value, sizeof (value), $2); 340 $$ = value; 341 } 342 343 | MDB_TOK_COR2_DEREF expression %prec MDB_COR_VALUE { 344 uint16_t value; 345 346 vfetch(&value, sizeof (value), $2); 347 $$ = value; 348 } 349 350 | MDB_TOK_COR4_DEREF expression %prec MDB_COR_VALUE { 351 uint32_t value; 352 353 vfetch(&value, sizeof (value), $2); 354 $$ = value; 355 } 356 357 | MDB_TOK_COR8_DEREF expression %prec MDB_COR_VALUE { 358 uint64_t value; 359 360 vfetch(&value, sizeof (value), $2); 361 $$ = value; 362 } 363 364 | '%' expression %prec MDB_OBJ_VALUE { 365 uintptr_t value; 366 367 ffetch(&value, sizeof (value), $2); 368 $$ = value; 369 } 370 371 | MDB_TOK_OBJ1_DEREF expression %prec MDB_OBJ_VALUE { 372 uint8_t value; 373 374 ffetch(&value, sizeof (value), $2); 375 $$ = value; 376 } 377 378 | MDB_TOK_OBJ2_DEREF expression %prec MDB_OBJ_VALUE { 379 uint16_t value; 380 381 ffetch(&value, sizeof (value), $2); 382 $$ = value; 383 } 384 385 | MDB_TOK_OBJ4_DEREF expression %prec MDB_OBJ_VALUE { 386 uint32_t value; 387 388 ffetch(&value, sizeof (value), $2); 389 $$ = value; 390 } 391 392 | MDB_TOK_OBJ8_DEREF expression %prec MDB_OBJ_VALUE { 393 uint64_t value; 394 395 ffetch(&value, sizeof (value), $2); 396 $$ = value; 397 } 398 399 | '-' expression %prec MDB_INT_NEGATE { $$ = -$2; } 400 | '~' expression %prec MDB_BIT_COMPLEMENT { $$ = ~$2; } 401 | '#' expression %prec MDB_LOG_NEGATE { $$ = !$2; } 402 | '(' expression ')' { $$ = $2; } 403 404 | MDB_TOK_VAR_REF %prec MDB_VAR_REFERENCE { 405 $$ = mdb_nv_get_value($1); 406 } 407 408 | MDB_TOK_SYMBOL { 409 if (strcmp($1, ".") == 0) { 410 $$ = mdb_nv_get_value(mdb.m_dot); 411 strfree($1); 412 413 } else { 414 const char *obj = MDB_TGT_OBJ_EVERY, *name = $1; 415 char *s = (char *)$1; 416 GElf_Sym sym; 417 418 if ((s = strrsplit(s, '`')) != NULL) { 419 name = s; 420 obj = $1; 421 } 422 423 if (mdb_tgt_lookup_by_name(mdb.m_target, 424 obj, name, &sym, NULL) == -1) { 425 strfree($1); 426 yyperror("failed to dereference " 427 "symbol"); 428 } 429 430 strfree($1); 431 $$ = (uintmax_t)sym.st_value; 432 } 433 } 434 435 | '+' { $$ = mdb_dot_incr("+"); } 436 | '^' { $$ = mdb_dot_decr("^"); } 437 | '&' { $$ = mdb.m_raddr; } 438 | MDB_TOK_IMMEDIATE 439 ; 440 441 %% 442