summaryrefslogtreecommitdiffstats
path: root/lldb/source/Plugins/Process/MacOSX-User/scripts/test-ProcessDebug.pl
blob: 96b3115c9125f4f0ca5501c235e7e85b196a7cf8 (plain)
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
#!/usr/bin/perl

use strict;
use Cwd 'abs_path';
our $home = $ENV{HOME} || die "ERROR: Couldn't deduce your home directory...\n";

our @inc_paths = (
	'./include',	
);

my $inc_paths_added = 0;
foreach my $inc_path (@inc_paths)
{
	if (-e $inc_path)
	{
		push (@INC, abs_path($inc_path));
		$inc_paths_added++;
	} 
}

if ($inc_paths_added == 0)
{
	die "Please compile the Release version of lldb\n";
}

require lldb;

# my $state = lldb::eStateAttaching;

use constant UINT32_MAX => 4294967295;

#----------------------------------------------------------------------
# Interactive Commands
#----------------------------------------------------------------------
our %commands = (
	break => {
		name => 'break',	# in case an alias is used to get to this command
		description => "Sets a breakpoint.",
		usage => ["break ADDR"],
		function => \&command_set_breakpoint,
		runs_target => 0,
	},
	delete => {
		name => 'delete',	# in case an alias is used to get to this command
		description => "Deletes one or more breakpoints by ID.\
If no breakpoint IDs are given all breakpoints will be deleted.\
If one or more IDs are given, only those breakpoints will be deleted.",
		usage => ["delete [ID1 ID2 ...]"],
		function => \&command_clear_breakpoint,
		runs_target => 0,
	},
	continue => {
		name => 'continue',	# in case an alias is used to get to this command
		description => "Continues target execution.",
		usage => ["continue [ADDR]"],
		function => \&command_continue,
		runs_target => 1
	},
	step => {
		name => 'step',	# in case an alias is used to get to this command
		description => "Single steps one instruction.",
		usage => ["step"],
		function => \&command_step,
		runs_target => 1
	},
	info => {
		name => 'info',	# in case an alias is used to get to this command
		description => "Gets info on a variety of things.",
		usage => ["info reg", "info thread", "info threads"],
		function => \&command_info,
		runs_target => 0
	},
	help => {
		name => 'help',	# in case an alias is used to get to this command
		description => "Displays a list of all commands, or help for a specific command.",
		usage => ["help", "help CMD"],
		function => \&command_help,
		runs_target => 0
	}
);

#----------------------------------------------------------------------
# Command aliases
#----------------------------------------------------------------------
our %aliases = (
	b => $commands{break},
	c => $commands{continue},
	s => $commands{step},
	d => $commands{delete},
	h => $commands{help}	
);

our $opt_g = 0;	# Enable verbose debug logging
our $opt_v = 0;	# Verbose mode
my $prev_command_href = undef;
my $stdio = '/dev/stdin';
my $launch = 0;
my @env = ();
my @break_ids;

#----------------------------------------------------------------------
# Given a command string, return the command hash reference for it, or
# undef if it doesn't exist.
#----------------------------------------------------------------------
sub get_command_hash_ref
{
	my $cmd = shift;
	my $cmd_href = undef;
	if (length($cmd) == 0)			{ $cmd_href = $prev_command_href;	}
	elsif (exists $aliases{$cmd})	{ $cmd_href = $aliases{$cmd};		} 
	elsif (exists $commands{$cmd})	{ $cmd_href = $commands{$cmd};		}
	defined $cmd_href and $prev_command_href = $cmd_href;
	return $cmd_href;
}

#----------------------------------------------------------------------
# Set a breakpoint
#----------------------------------------------------------------------
sub command_set_breakpoint
{
	my $pid = shift;
	my $tid = shift;
	$opt_g and print "command_set_breakpoint (pid = $pid, locations = @_)\n";
	foreach my $location (@_)
	{
		my $success = 0;
		my $address = hex($location);
		if ($address != 0)
		{
			my $break_id = lldb::PDBreakpointSet ($pid, $address, 1, 0);
			if ($break_id != $lldb::PD_INVALID_BREAK_ID)
			{
				printf("Breakpoint %i is set.\n", $break_id);
				push(@break_ids, $break_id);
				$success = 1;
			}
		}
		$success or print("error: failed to set breakpoint at $location.\n");
	}
	return 1;
}

#----------------------------------------------------------------------
# Clear a breakpoint
#----------------------------------------------------------------------
sub command_clear_breakpoint
{
	my $pid = shift;
	my $tid = shift;
	if (@_)
	{
		my $break_id;
		my @cleared_break_ids;
		my @new_break_ids;
		$opt_g and print "command_clear_breakpoint (pid = $pid, break_ids = @_)\n";
		foreach $break_id (@_)
		{
			if (lldb::PDBreakpointClear ($pid, $break_id))
			{
				printf("Breakpoint %i has been cleared.\n", $break_id);
				push (@cleared_break_ids, $break_id);
			}
			else
			{
				printf("error: failed to clear breakpoint %i.\n", $break_id);					
			}
		}
		
		foreach my $old_break_id (@break_ids)
		{
			my $found_break_id = 0;
			foreach $break_id (@cleared_break_ids)
			{
				if ($old_break_id == $break_id)
				{
					$found_break_id = 1;
				}
			}
			$found_break_id or push (@new_break_ids, $old_break_id);
		}
		@break_ids = @new_break_ids;
	}
	else
	{
		# Nothing specified, clear all breakpoints
		return command_clear_breakpoint($pid, $tid, @break_ids);
	}
	return 1;
}
#----------------------------------------------------------------------
# Continue program execution
#----------------------------------------------------------------------
sub command_continue
{
	my $pid = shift;
	my $tid = shift;
	$opt_g and print "command_continue (pid = $pid)\n";
	if ($pid != $lldb::PD_INVALID_PROCESS_ID)
	{
		$opt_v and printf("Resuming pid %d...\n", $pid);
		return lldb::PDProcessResume ($pid);
	}
	return 0;
}

sub command_step
{
	my $pid = shift;
	my $tid = shift;
	$opt_g and print "command_step (pid = $pid, tid = $tid)\n";
	if ($pid != $lldb::PD_INVALID_PROCESS_ID)
	{
		$opt_v and printf("Single stepping pid %d tid = %4.4x...\n", $pid, $tid);
		return lldb::PDThreadResume ($pid, $tid, 1);
	}
	return 0;
}

sub command_info
{
	my $pid = shift;
	my $tid = shift;
	$opt_g and print "command_step (pid = $pid, tid = $tid)\n";
	if ($pid != $lldb::PD_INVALID_PROCESS_ID)
	{
		if (@_)
		{
			my $info_cmd = shift;
			if ($info_cmd eq 'reg')
			{
				
			}
			elsif ($info_cmd eq 'thread')
			{
				# info on the current thread
				printf("thread 0x%4.4x %s\n", $tid, lldb::PDThreadGetInfo($pid, $tid));
			}
			elsif ($info_cmd eq 'threads')
			{
				my $num_threads = lldb::PDProcessGetNumThreads( $pid );
				for my $thread_num (1..$num_threads)
				{
					my $curr_tid = lldb::PDProcessGetThreadAtIndex ( $pid, $thread_num - 1 );
					printf("%c%u - thread 0x%4.4x %s\n", $curr_tid == $tid ? '*' : ' ', $thread_num, $curr_tid, lldb::PDThreadGetInfo($pid, $curr_tid));
				}
			}
		}
	}
	return 1;
}
#----------------------------------------------------------------------
# Get help on all commands, or a specific list of commands
#----------------------------------------------------------------------
sub command_help
{
	my $pid = shift;
	my $tid = shift;
	if (@_)
	{
		$opt_g and print "command_continue (pid = $pid, commands = @_)\n";
		foreach my $cmd (@_)
		{
			my $cmd_href = get_command_hash_ref($cmd);
			if ($cmd_href)
			{
				print '#', '-' x 72, "\n# $cmd_href->{name}\n", '#', '-' x 72, "\n";
				my $usage_aref = $cmd_href->{usage};
				if (@{$usage_aref})
				{
					print "  USAGE\n";
					foreach my $usage (@{$usage_aref}) {
						print "    $usage\n";
					}
					print "\n";
				}
				print "  DESCRIPTION\n    $cmd_href->{description}\n\n";
			}
			else
			{
				print "  invalid command: '$cmd'\n\n";
			}
		}
	}
	else
	{
		return command_help($pid, sort keys %commands);
	}
	return 1;
}


#lldb::PDLogSetLogMask ($lldb::PD_LOG_ALL);
#lldb::PDLogSetLogFile ('/dev/stdout');

print "running: ", join(' ', @ARGV), "\n";

my $pid = lldb::PDProcessLaunch ($ARGV[0], \@ARGV, \@env, "i386", '/dev/stdin', '/dev/stdout', '/dev/stderr', $launch, '', 0);
my $pid_state;
while ($pid)
{
	$opt_g and printf("PDProcessWaitForEvents (%d, 0x%4.4x, SET, 1)\n", $pid, $lldb::PD_ALL_EVENTS);
	my $events = lldb::PDProcessWaitForEvents ($pid, $lldb::PD_ALL_EVENTS, 1, 1);
	if ($events)
	{
		$opt_g and printf ("Got event: 0x%8.8x\n", $events);

		if ($events & $lldb::PD_EVENT_IMAGES_CHANGED)
		{
			$opt_g and printf("pid %d images changed...\n", $pid);
		}

		if ($events & $lldb::PD_EVENT_STDIO)
		{
			$opt_g and printf("pid %d has stdio...\n", $pid);
		}

		if ($events & $lldb::PD_EVENT_ASYNC_INTERRUPT)
		{
			$opt_g and printf("pid %d got async interrupt...\n", $pid);
		}

		if ($events & $lldb::PD_EVENT_RUNNING)
		{
			$pid_state = lldb::PDProcessGetState ($pid);
			$opt_v and printf( "pid %d state: %s.\n", $pid, lldb::PDStateAsString ($pid_state) );
		}
		
		if ($events & $lldb::PD_EVENT_STOPPED)
		{
			$pid_state = lldb::PDProcessGetState ($pid);
			$opt_v and printf( "pid %d state: %s.\n", $pid, lldb::PDStateAsString ($pid_state) );

			if ($pid_state == $lldb::eStateUnloaded ||
				$pid_state == $lldb::eStateAttaching ||
				$pid_state == $lldb::eStateLaunching )
			{
				
			}
		    elsif (	$pid_state == $lldb::eStateStopped )
			{
				my $tid = lldb::PDProcessGetCurrentThread ( $pid );
				my $pc = lldb::PDThreadGetRegisterHexValueByName($pid, $tid, $lldb::PD_REGISTER_SET_ALL, "eip", 0);
				$pc != 0 and printf("pc = 0x%8.8x ", $pc); 
				# my $sp = lldb::PDThreadGetRegisterHexValueByName($pid, $tid, $lldb::PD_REGISTER_SET_ALL, "esp", 0);
				# $sp != 0 and printf("sp = 0x%8.8x ", $sp);
				# my $fp = lldb::PDThreadGetRegisterHexValueByName($pid, $tid, $lldb::PD_REGISTER_SET_ALL, "ebp", 0);
				# $sp != 0 and printf("fp = 0x%8.8x ", $fp);
				# print "\n";
				my $done = 0;
				my $input;
				while (!$done)
				{
					print '(pdbg) '; 
					
					chomp($input = <STDIN>);
					my @argv = split(/\s+/, $input);
					my $cmd = @argv ? shift @argv : undef;
					my $cmd_href = get_command_hash_ref ($cmd);
					if ($cmd_href)
					{
						# Print the expanded alias if one was used
						if ($opt_v and $cmd_href->{name} ne $cmd)
						{
							print "$cmd_href->{name} @argv\n";
						}

						# Call the command's callback function to make things happen
						if ($cmd_href->{function}($pid, $tid, @argv))
						{							
							$done = $cmd_href->{runs_target};
						}
					}
					else
					{
						print "invalid command: '$cmd'\nType 'help' for a list of all commands.\nType 'help CMD' for help on a specific commmand.\n";
					}
				}
			}
		    elsif (	$pid_state == $lldb::eStateRunning ||
			 		$pid_state == $lldb::eStateStepping )
			{
				
			}
		    elsif (	$pid_state == $lldb::eStateCrashed ||
		    		$pid_state == $lldb::eStateDetached	||
		    		$pid_state == $lldb::eStateExited )
			{
				$pid = 0;				
			}
		    elsif ( $pid_state == $lldb::eStateSuspended )
			{
			}
			else
			{
			}
		}
		
		if ($pid)
		{
			$opt_g and printf("PDProcessResetEvents(%d, 0x%8.8x)\n", $pid, $events);
			lldb::PDProcessResetEvents($pid, $events);			
		}
	}	
}

if ($pid != $lldb::PD_INVALID_PROCESS_ID)
{
	lldb::PDProcessDetach ($pid);
}
OpenPOWER on IntegriCloud