DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (fbecf6c8a86f)

VCS Links

Line Code
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
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is [Open Source Virtual Machine.].
 *
 * The Initial Developer of the Original Code is
 * Adobe System Incorporated.
 * Portions created by the Initial Developer are Copyright (C) 2004-2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Adobe AS3 Team
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "avmshell.h"

#ifdef DEBUGGER
namespace avmshell
{
	/**
	 * The array of top level commands that we support.
	 * They are placed into a Nx2 array, whereby the first component
	 * is a String which is the command and the 2nd component is the
	 * integer identifier for the command.
	 *
	 * The StringIntArray object provides a convenient wrapper class
	 * that implements the List interface.
	 *
	 * NOTE: order matters!  For the case of a single character
	 *       match, we let the first hit act like an unambiguous match.
	 */
	DebugCLI::StringIntArray DebugCLI::commandArray[] =
	{
		{ "break", CMD_BREAK },
		{ "bt", INFO_STACK_CMD },
        { "continue", CMD_CONTINUE },
		{ "delete", CMD_DELETE },
		{ "finish", CMD_FINISH },
		{ "help", CMD_HELP },
		{ "info", CMD_INFO },
		{ "list", CMD_LIST },
		{ "next", CMD_NEXT },
        { "print", CMD_PRINT },
        { "pwd", CMD_PWD },
		{ "quit", CMD_QUIT },
		{ "step", CMD_STEP },
		{ "set", CMD_SET },
		{ "show", CMD_SHOW },
		{ "where", INFO_STACK_CMD },
		{ NULL, 0 }
	};

	/**
	 * Info sub-commands
	 */
	DebugCLI::StringIntArray DebugCLI::infoCommandArray[] =
	{
		{ "arguments", INFO_ARGS_CMD },
		{ "breakpoints", INFO_BREAK_CMD },
		{ "files", INFO_FILES_CMD },
		{ "functions", INFO_FUNCTIONS_CMD },
		{ "locals", INFO_LOCALS_CMD },
		{ "stack", INFO_STACK_CMD },
		{ NULL, 0 }		
	};

	/**
	 * Show sub-commands
	 */
	DebugCLI::StringIntArray DebugCLI::showCommandArray[] =
	{
		{ "break", SHOW_BREAK_CMD },
		{ "functions", SHOW_FUNC_CMD },
		{ "memory", SHOW_MEM_CMD },
		{ "variable", SHOW_VAR_CMD },
		{ NULL, 0 }		
	};

	DebugCLI::DebugCLI(AvmCore *core, Debugger::TraceLevel tracelevel)
		: Debugger(core, tracelevel)
	{
		currentSourceLen = 0;
        warnMissingSource = true;
	}

	DebugCLI::~DebugCLI()
	{
		if (currentSource) {
			delete [] currentSource;
			currentSource = NULL;
		}
	}
		
	char* DebugCLI::nextToken()
	{
		char *out = currentToken;
		if (currentToken) {
			while (*currentToken) {
				if (*currentToken == ' ' || *currentToken == '\r' || *currentToken == '\n' || *currentToken == '\t') {
					*currentToken++ = 0;
					break;
				}
				currentToken++;
			}
			currentToken = *currentToken ? currentToken : NULL;
		}
		return out;
	}

	/**
	 * Attempt to match given the given string against our set of commands
	 * @return the command code that was hit.
	 */
	int DebugCLI::determineCommand(DebugCLI::StringIntArray cmdList[],
								   const char *input,
								   int defCmd)
	{
        if (!input) return INFO_UNKNOWN_CMD;
        
		int inputLen = (int)VMPI_strlen(input);
		
		// first check for a comment
		if (input[0] == '#') {
			return CMD_COMMENT;
		}

		int match = -1;
		bool ambiguous = false;
		
		for (int i=0; cmdList[i].text; i++) {
			if (!VMPI_strncmp(input, cmdList[i].text, inputLen)) {
				if (match != -1) {
					ambiguous = true;
					break;
				}
				match = i;
			}
		}

		/**
		 * 3 cases:
		 *  - No hits, return unknown and let our caller
		 *    dump the error.
		 *  - We match unambiguously or we have 1 or more matches
		 *    and the input is a single character. We then take the
		 *    first hit as our command.
		 *  - If we have multiple hits then we dump a 'ambiguous' message
		 *    and puke quietly.
		 */
		if (match == -1) {
			// no command match return unknown
			return defCmd;
		}
		// only 1 match or our input is 1 character or first match is exact
		else if (!ambiguous || inputLen == 1 || !VMPI_strcmp(cmdList[match].text, input)) {
			return cmdList[match].id;
		}
		else {
			// matches more than one command dump message and go
			core->console << "Ambiguous command '" << input << "': ";
			bool first = true;
			for (int i=0; cmdList[i].text; i++) {
				if (!VMPI_strncmp(cmdList[i].text, input, inputLen)) {
					if (!first) {
						core->console << ", ";
					} else {
						first = false;
					}
					core->console << cmdList[i].text;
				}
			}
			core->console << ".\n";
			return -1;
		}
	}

    const char* DebugCLI::commandNumberToCommandName(DebugCLI::StringIntArray cmdList[],
													 int cmdNumber)
    {
        for (int i = 0; cmdList[i].text; i++)
        {
            if (cmdList[i].id == cmdNumber)
                return cmdList[i].text;
        }

        return "?";
    }

	void DebugCLI::bt()
	{
		//core->stackTrace->dump(core->console);
		//core->console << '\n';

		// obtain information about each frame 
		int frameCount = core->debugger()->frameCount();
		for(int k=0; k<frameCount; k++)
		{
			Atom* ptr;
			int count, line; 
			SourceInfo* src;
			DebugFrame* frame = core->debugger()->frameAt(k);

			// source information
			frame->sourceLocation(src, line);

			core->console << "#" << k << "   ";

			// this 
			Atom a = nullObjectAtom;
			frame->dhis(a);
			core->console << core->format(a) << ".";

			// method
			MethodInfo* info = functionFor(src, line);
			if (info)
				core->console << info->getMethodName();
			else
				core->console << "<unknown>";

			core->console << "(";

			// dump args
			frame->arguments(ptr, count);
			for(int i=0; i<count; i++)
			{
				// write out the name
                Stringp nm = info->getArgName(i);
				if (info && (nm != core->kundefined))
					core->console << nm << "=";

				core->console << core->format(*ptr++);
				if (i<count-1)
					core->console << ",";
			}
			core->console << ") at ";
			if (src)
				core->console << src->name();
			else
				core->console << "???";

			core->console << ":" << (line) << "\n";
		}
	}

	MethodInfo* DebugCLI::functionFor(SourceInfo* src, int line)
	{
		MethodInfo* info = NULL;
		if (src)
		{
			// find the function at this location
			int size = src->functionCount();
			for(int i=0; i<size; i++)
			{
				MethodInfo* m = src->functionAt(i);
				if (line >= m->firstSourceLine() && line <= m->lastSourceLine())
				{
					info = m;
					break;
				}
			}
		}
		return info;
	}

	// zero based
	char* DebugCLI::lineStart(int linenum)
	{
		if (!currentSource && currentFile)
			setCurrentSource(currentFile);

		if (!currentSource) {
			return NULL;
		}

		// linenumbers map to zero based array entry
		char *ptr = currentSource;
		for (int i=0; i<linenum; i++) {
			// Scan to end of line
			while (*ptr != '\n') {
				if (!*ptr) {
					return NULL;
				}
				ptr++;
			}
			// Advance past newline
			ptr++;
		}
		return ptr;
	}
	
	void DebugCLI::displayLines(int line, int count)
	{
		if (!lineStart(0)) {
            if (currentFile) 
                core->console << currentFile;
            else
                core->console << "<unknown>";
			core->console <<":"<<line<<" ";
		} else {
			int lineAt = line;
			while(count-- > 0)
			{
				char* ptr = lineStart(lineAt-1);
				if (!ptr)
				{
					#if WRAP_AROUND
					lineAt = 1;
					count++; // reset line number to beginning skip this iteration
					#else
					break;
					#endif
				}
				else
				{
					core->console << (lineAt) << ": ";
					while (*ptr && *ptr != '\n')
						core->console << *ptr++;
					core->console << '\n';
					lineAt++;
				}
			}
		}
	}

	void DebugCLI::list(const char* line)
	{
		int currentLine = (core->callStack) ? core->callStack->linenum() : 0;
		int linenum = (line) ? VMPI_atoi(line) : currentLine;
		displayLines(linenum, 10);
	}
	
	void DebugCLI::printIP()
	{
		int line = (core->callStack) ? core->callStack->linenum() : 0;
		displayLines(line, 1);
	}

	void DebugCLI::breakpoint(char *location)
	{
		Stringp filename = currentFile;
		char *colon = VMPI_strchr(location, ':');

		if (colon) {
			*colon = 0;
			filename = core->internStringLatin1(location);
			location = colon+1;
		}

		if (abcCount() == 0) {
			core->console << "No abc file loaded\n";
			return;
		}

		SourceFile* sourceFile = NULL;
		for (int i = 0, n = abcCount(); i < n; ++i)
		{
			AbcFile* abcFile = (AbcFile*)abcAt(i);
			sourceFile = abcFile->sourceNamed(filename);
			if (sourceFile)
				break;
		}

		if (sourceFile == NULL) {
			core->console << "No source available; can't set breakpoint.\n";
			return;
		}

		int targetLine = VMPI_atoi(location);

		int breakpointId = ++breakpointCount;
		
		if (breakpointSet(sourceFile, targetLine)) {
			core->console << "Breakpoint " << breakpointId << ": file "
						  << filename
						  << ", " << (targetLine) << ".\n";

			BreakAction *breakAction = new (core->GetGC()) BreakAction(sourceFile,
																  breakpointId,
																  filename,
																  targetLine);
			breakAction->prev = lastBreakAction;
			if (lastBreakAction) {
				lastBreakAction->next = breakAction;
			} else {
				firstBreakAction = breakAction;
			}
			lastBreakAction = breakAction;
		} else {
			core->console << "Could not locate specified line.\n";
		}
	}

	void DebugCLI::showBreakpoints()
	{
		BreakAction *breakAction = firstBreakAction;
		while (breakAction) {
			breakAction->print(core->console);
			breakAction = breakAction->next;
		}
	}
	
	void DebugCLI::deleteBreakpoint(char *idstr)
	{
		int id = VMPI_atoi(idstr);

		BreakAction *breakAction = firstBreakAction;
		while (breakAction) {
			if (breakAction->id == id) {
				break;
			}
			breakAction = breakAction->next;
		}

		if (breakAction) {
			if (breakAction->prev) {
				breakAction->prev->next = breakAction->next;
			} else {
				firstBreakAction = breakAction->next;
			}
			if (breakAction->next) {
				breakAction->next->prev = breakAction->prev;
			} else {
				lastBreakAction = breakAction->prev;
			}
			if (breakpointClear(breakAction->sourceFile, breakAction->linenum)) {
				core->console << "Breakpoint " << id << " deleted.\n";
			} else {
				core->console << "Internal error; could not delete breakpoint.\n";
			}
		} else {
			core->console << "Could not find breakpoint.\n";
		}
	}
	
	void DebugCLI::locals()
	{
		Atom* ptr;
		int count, line; 
		SourceInfo* src;
		DebugFrame* frame = core->debugger()->frameAt(0);

		// source information
		frame->sourceLocation(src, line);

		// method
		MethodInfo* info = functionFor(src, line);
		if (info)
		{
			frame->locals(ptr, count);
			for(int i=0; i<count; i++)
			{
				// write out the name
                Stringp nm = info->getLocalName(i);
                if (nm != core->kundefined) 
					core->console << nm;
                else
                    core->console << "<local_" << i << ">";
				core->console << " = " << core->format(*ptr++) << "\n";
			}
		}
	}

	Atom DebugCLI::ease2Atom(const char* to, Atom baseline)
	{
		// first make a string out of the value
		Atom a = core->newStringLatin1(to)->atom();

		// using the type of baseline try to convert to into an appropriate Atom
		if (core->isNumber(baseline))
			return core->numberAtom(a);
		else if (core->isBoolean(baseline))
			return AvmCore::booleanAtom(a);
		
		return nullStringAtom;
	}

	void DebugCLI::set()
	{
		const char* what = nextToken();
		const char* equ = nextToken();
		const char* to = nextToken();
		if (!to || !equ || !what || *equ != '=')
		{
			core->console << " Bad format, should be:  'set {variable} = {value}' \n";
		}
		else
		{
			// look for the varable in our locals or args.
			Atom* ptr;
			int count, line; 
			SourceInfo* src;
			DebugFrame* frame = core->debugger()->frameAt(0);

			// source information
			frame->sourceLocation(src, line);
			if (!src)
			{
				core->console << "Unable to locate debug information for current source file, so no local or argument names known\n";
				return;
			}

			// method
			MethodInfo* info = functionFor(src, line);
			if (!info)
			{
				core->console << "Unable to find method debug information, so no local or argument names known\n";
				return;
			}

			frame->arguments(ptr, count);
			for(int i=0; i<count; i++)
			{
				Stringp arg = info->getArgName(i);
				if (arg->equalsLatin1(what)) 
				{
					// match!
					Atom a = ease2Atom(to, ptr[i]);
					if (a == undefinedAtom)
						core->console << " Type mismatch : current value is " << core->format(ptr[i]);
					else
						frame->setArgument(i, a);
					return;
				}
			}

			frame->locals(ptr, count);
			for(int i=0; i<count; i++)
			{
				Stringp local = info->getLocalName(i);
				if ( local->equalsLatin1(what)) 
				{
					// match!
					Atom a = ease2Atom(to, ptr[i]);
					if (a == undefinedAtom)
						core->console << " Type mismatch : current value is " << core->format(ptr[i]);
					else
						frame->setLocal(i, a);
					return;
				}
			}
		}
	}

	void DebugCLI::print(const char *name)
	{
		if (!name) {
			core->console << "Must specify a name.\n";
			return;
		}

		// todo deal with exceptions
		Multiname mname(
			core->getAnyPublicNamespace(),
			core->internStringLatin1(name)
		);

		#if 0
		// rick fixme
		Atom objAtom = env->findproperty(outerScope, scopes, extraScopes, &mname, false);
		Atom valueAtom = env->getproperty(objAtom, &mname);
		core->console << core->string(valueAtom) << '\n';
		#endif
	}

	bool DebugCLI::filterException(Exception *exception, bool /*willBeCaught*/)
	{
		// Filter exceptions when -d switch specified
		if (activeFlag) {
			core->console << "Exception has been thrown:\n"
						  << core->string(exception->atom)
						  << '\n';
			enterDebugger();
			return true;
		}
		return false;
	}

	void DebugCLI::info()
	{
		char *command = nextToken();
		int cmd = infoCommandFor(command);

		switch (cmd) {
		case -1:
			// ambiguous, we already printed error message
			break;
		case INFO_LOCALS_CMD:
			locals();
			break;
		case INFO_BREAK_CMD:
			showBreakpoints();
			break;
		case INFO_UNKNOWN_CMD:
			core->console << "Unknown command.\n";
			break;
		default:
			core->console << "Command not implemented.\n";
			break;
		}
	}
	
	void DebugCLI::enterDebugger()
	{	
		setCurrentSource( (core->callStack) ? (core->callStack->filename()) : 0 );

		for (;;) {
			printIP();
			
			core->console << "(asdb) ";
			Platform::GetInstance()->getUserInput(commandLine, kMaxCommandLine);

			commandLine[VMPI_strlen(commandLine)-1] = 0;
			
			if (!commandLine[0]) {
				VMPI_strcpy(commandLine, lastCommand);
			} else {
				VMPI_strcpy(lastCommand, commandLine);
			}
				
			currentToken = commandLine;
		
			char *command = nextToken();
			int cmd = commandFor(command);

			switch (cmd) {
			case -1:
				// ambiguous, we already printed error message
				break;
			case CMD_INFO:
				info();
				break;
			case CMD_BREAK:
				breakpoint(nextToken());
				break;
			case CMD_DELETE:
				deleteBreakpoint(nextToken());
				break;
			case CMD_LIST:
				list(nextToken());
				break;
			case CMD_UNKNOWN:
				core->console << "Unknown command.\n";
				break;
			case CMD_QUIT:
				Platform::GetInstance()->exit(0);
				break;
			case CMD_CONTINUE:
				return;
			case CMD_PRINT:
				print(nextToken());
				break;
			case CMD_NEXT:
				stepOver();
				return;
			case INFO_STACK_CMD:
				bt();
				break;
			case CMD_FINISH:
				stepOut();
				return;
			case CMD_STEP:
				stepInto();
				return;
			case CMD_SET:
				set();
				break;
			default:
				core->console << "Command not implemented.\n";
				break;
			}
		}
	}

	void DebugCLI::setCurrentSource(Stringp file)
	{
		if (!file)
			return;

		currentFile = file;

		if (currentSource) {
			delete [] currentSource;
			currentSource = NULL;
			currentSourceLen = 0;
		}
		
		// Open this file and suck it into memory
		StUTF8String currentFileUTF8(currentFile);
		FileInputStream f(currentFileUTF8.c_str());
		if (f.valid() && ((uint64_t)file->length() < UINT32_T_MAX)) { //cannot handle files > 4GB
			currentSourceLen = (uint32_t) f.available();
			currentSource = new char[currentSourceLen+1];
			f.read(currentSource, currentSourceLen);
			currentSource[currentSourceLen] = 0;

			// whip through converting \r\n to space \n
			for(int64_t i=0; i<currentSourceLen-1;i++) {
				if (currentSource[i] == '\r' && currentSource[i+1] == '\n')
					currentSource[i] = ' ';
			}
		} else if (warnMissingSource) {
			core->console << "Could not find '" << currentFile << "'.  Try running in the same directory as the .as file.\n";
            warnMissingSource = false;
		}
	}

	//
	// BreakAction
	//
		
	void BreakAction::print(PrintWriter& out)
	{
		out << id << " at "
			<< filename
			<< ":" << (linenum) << '\n';
	}
}
#endif