xref: /haiku/src/apps/debugger/user_interface/cli/commands/CliThreadCommand.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "CliThreadCommand.h"
8 
9 #include <stdio.h>
10 
11 #include <AutoLocker.h>
12 
13 #include "CliContext.h"
14 #include "Team.h"
15 
16 
17 CliThreadCommand::CliThreadCommand()
18 	:
19 	CliCommand("set or print the current thread",
20 		"%s [ <thread ID> ]\n"
21 		"Sets the current thread to <thread ID>, if supplied. Otherwise prints "
22 			"the\n"
23 		"current thread.")
24 {
25 }
26 
27 
28 void
29 CliThreadCommand::Execute(int argc, const char* const* argv,
30 	CliContext& context)
31 {
32 	if (argc > 2) {
33 		PrintUsage(argv[0]);
34 		return;
35 	}
36 
37 	if (argc < 2) {
38 		// no arguments -- print the current thread
39 		context.PrintCurrentThread();
40 		return;
41 	}
42 
43 	// parse the argument
44 	char* endPointer;
45 	long threadID = strtol(argv[1], &endPointer, 0);
46 	if (*endPointer != '\0' || threadID < 0) {
47 		printf("Error: Invalid parameter \"%s\"\n", argv[1]);
48 		return;
49 	}
50 
51 	// get the thread and change the current thread
52 	Team* team = context.GetTeam();
53 	AutoLocker<Team> teamLocker(team);
54 	if (Thread* thread = team->ThreadByID(threadID))
55 		context.SetCurrentThread(thread);
56 	else
57 		printf("Error: No thread with ID %ld\n", threadID);
58 }
59