forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget.c
4086 lines (3317 loc) · 106 KB
/
target.c
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Select target systems and architectures at runtime for GDB.
Copyright (C) 1990-2015 Free Software Foundation, Inc.
Contributed by Cygnus Support.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "target.h"
#include "target-dcache.h"
#include "gdbcmd.h"
#include "symtab.h"
#include "inferior.h"
#include "infrun.h"
#include "bfd.h"
#include "symfile.h"
#include "objfiles.h"
#include "dcache.h"
#include <signal.h>
#include "regcache.h"
#include "gdbcore.h"
#include "target-descriptions.h"
#include "gdbthread.h"
#include "solib.h"
#include "exec.h"
#include "inline-frame.h"
#include "tracepoint.h"
#include "gdb/fileio.h"
#include "agent.h"
#include "auxv.h"
#include "target-debug.h"
static void target_info (char *, int);
static void generic_tls_error (void) ATTRIBUTE_NORETURN;
static void default_terminal_info (struct target_ops *, const char *, int);
static int default_watchpoint_addr_within_range (struct target_ops *,
CORE_ADDR, CORE_ADDR, int);
static int default_region_ok_for_hw_watchpoint (struct target_ops *,
CORE_ADDR, int);
static void default_rcmd (struct target_ops *, const char *, struct ui_file *);
static ptid_t default_get_ada_task_ptid (struct target_ops *self,
long lwp, long tid);
static int default_follow_fork (struct target_ops *self, int follow_child,
int detach_fork);
static void default_mourn_inferior (struct target_ops *self);
static int default_search_memory (struct target_ops *ops,
CORE_ADDR start_addr,
ULONGEST search_space_len,
const gdb_byte *pattern,
ULONGEST pattern_len,
CORE_ADDR *found_addrp);
static int default_verify_memory (struct target_ops *self,
const gdb_byte *data,
CORE_ADDR memaddr, ULONGEST size);
static struct address_space *default_thread_address_space
(struct target_ops *self, ptid_t ptid);
static void tcomplain (void) ATTRIBUTE_NORETURN;
static int return_zero (struct target_ops *);
static int return_zero_has_execution (struct target_ops *, ptid_t);
static void target_command (char *, int);
static struct target_ops *find_default_run_target (char *);
static struct gdbarch *default_thread_architecture (struct target_ops *ops,
ptid_t ptid);
static int dummy_find_memory_regions (struct target_ops *self,
find_memory_region_ftype ignore1,
void *ignore2);
static char *dummy_make_corefile_notes (struct target_ops *self,
bfd *ignore1, int *ignore2);
static char *default_pid_to_str (struct target_ops *ops, ptid_t ptid);
static enum exec_direction_kind default_execution_direction
(struct target_ops *self);
static struct target_ops debug_target;
#include "target-delegates.c"
static void init_dummy_target (void);
static void update_current_target (void);
/* Vector of existing target structures. */
typedef struct target_ops *target_ops_p;
DEF_VEC_P (target_ops_p);
static VEC (target_ops_p) *target_structs;
/* The initial current target, so that there is always a semi-valid
current target. */
static struct target_ops dummy_target;
/* Top of target stack. */
static struct target_ops *target_stack;
/* The target structure we are currently using to talk to a process
or file or whatever "inferior" we have. */
struct target_ops current_target;
/* Command list for target. */
static struct cmd_list_element *targetlist = NULL;
/* Nonzero if we should trust readonly sections from the
executable when reading memory. */
static int trust_readonly = 0;
/* Nonzero if we should show true memory content including
memory breakpoint inserted by gdb. */
static int show_memory_breakpoints = 0;
/* These globals control whether GDB attempts to perform these
operations; they are useful for targets that need to prevent
inadvertant disruption, such as in non-stop mode. */
int may_write_registers = 1;
int may_write_memory = 1;
int may_insert_breakpoints = 1;
int may_insert_tracepoints = 1;
int may_insert_fast_tracepoints = 1;
int may_stop = 1;
/* Non-zero if we want to see trace of target level stuff. */
static unsigned int targetdebug = 0;
static void
set_targetdebug (char *args, int from_tty, struct cmd_list_element *c)
{
update_current_target ();
}
static void
show_targetdebug (struct ui_file *file, int from_tty,
struct cmd_list_element *c, const char *value)
{
fprintf_filtered (file, _("Target debugging is %s.\n"), value);
}
static void setup_target_debug (void);
/* The user just typed 'target' without the name of a target. */
static void
target_command (char *arg, int from_tty)
{
fputs_filtered ("Argument required (target name). Try `help target'\n",
gdb_stdout);
}
/* Default target_has_* methods for process_stratum targets. */
int
default_child_has_all_memory (struct target_ops *ops)
{
/* If no inferior selected, then we can't read memory here. */
if (ptid_equal (inferior_ptid, null_ptid))
return 0;
return 1;
}
int
default_child_has_memory (struct target_ops *ops)
{
/* If no inferior selected, then we can't read memory here. */
if (ptid_equal (inferior_ptid, null_ptid))
return 0;
return 1;
}
int
default_child_has_stack (struct target_ops *ops)
{
/* If no inferior selected, there's no stack. */
if (ptid_equal (inferior_ptid, null_ptid))
return 0;
return 1;
}
int
default_child_has_registers (struct target_ops *ops)
{
/* Can't read registers from no inferior. */
if (ptid_equal (inferior_ptid, null_ptid))
return 0;
return 1;
}
int
default_child_has_execution (struct target_ops *ops, ptid_t the_ptid)
{
/* If there's no thread selected, then we can't make it run through
hoops. */
if (ptid_equal (the_ptid, null_ptid))
return 0;
return 1;
}
int
target_has_all_memory_1 (void)
{
struct target_ops *t;
for (t = current_target.beneath; t != NULL; t = t->beneath)
if (t->to_has_all_memory (t))
return 1;
return 0;
}
int
target_has_memory_1 (void)
{
struct target_ops *t;
for (t = current_target.beneath; t != NULL; t = t->beneath)
if (t->to_has_memory (t))
return 1;
return 0;
}
int
target_has_stack_1 (void)
{
struct target_ops *t;
for (t = current_target.beneath; t != NULL; t = t->beneath)
if (t->to_has_stack (t))
return 1;
return 0;
}
int
target_has_registers_1 (void)
{
struct target_ops *t;
for (t = current_target.beneath; t != NULL; t = t->beneath)
if (t->to_has_registers (t))
return 1;
return 0;
}
int
target_has_execution_1 (ptid_t the_ptid)
{
struct target_ops *t;
for (t = current_target.beneath; t != NULL; t = t->beneath)
if (t->to_has_execution (t, the_ptid))
return 1;
return 0;
}
int
target_has_execution_current (void)
{
return target_has_execution_1 (inferior_ptid);
}
/* Complete initialization of T. This ensures that various fields in
T are set, if needed by the target implementation. */
void
complete_target_initialization (struct target_ops *t)
{
/* Provide default values for all "must have" methods. */
if (t->to_has_all_memory == NULL)
t->to_has_all_memory = return_zero;
if (t->to_has_memory == NULL)
t->to_has_memory = return_zero;
if (t->to_has_stack == NULL)
t->to_has_stack = return_zero;
if (t->to_has_registers == NULL)
t->to_has_registers = return_zero;
if (t->to_has_execution == NULL)
t->to_has_execution = return_zero_has_execution;
/* These methods can be called on an unpushed target and so require
a default implementation if the target might plausibly be the
default run target. */
gdb_assert (t->to_can_run == NULL || (t->to_can_async_p != NULL
&& t->to_supports_non_stop != NULL));
install_delegators (t);
}
/* This is used to implement the various target commands. */
static void
open_target (char *args, int from_tty, struct cmd_list_element *command)
{
struct target_ops *ops = get_cmd_context (command);
if (targetdebug)
fprintf_unfiltered (gdb_stdlog, "-> %s->to_open (...)\n",
ops->to_shortname);
ops->to_open (args, from_tty);
if (targetdebug)
fprintf_unfiltered (gdb_stdlog, "<- %s->to_open (%s, %d)\n",
ops->to_shortname, args, from_tty);
}
/* Add possible target architecture T to the list and add a new
command 'target T->to_shortname'. Set COMPLETER as the command's
completer if not NULL. */
void
add_target_with_completer (struct target_ops *t,
completer_ftype *completer)
{
struct cmd_list_element *c;
complete_target_initialization (t);
VEC_safe_push (target_ops_p, target_structs, t);
if (targetlist == NULL)
add_prefix_cmd ("target", class_run, target_command, _("\
Connect to a target machine or process.\n\
The first argument is the type or protocol of the target machine.\n\
Remaining arguments are interpreted by the target protocol. For more\n\
information on the arguments for a particular protocol, type\n\
`help target ' followed by the protocol name."),
&targetlist, "target ", 0, &cmdlist);
c = add_cmd (t->to_shortname, no_class, NULL, t->to_doc, &targetlist);
set_cmd_sfunc (c, open_target);
set_cmd_context (c, t);
if (completer != NULL)
set_cmd_completer (c, completer);
}
/* Add a possible target architecture to the list. */
void
add_target (struct target_ops *t)
{
add_target_with_completer (t, NULL);
}
/* See target.h. */
void
add_deprecated_target_alias (struct target_ops *t, char *alias)
{
struct cmd_list_element *c;
char *alt;
/* If we use add_alias_cmd, here, we do not get the deprecated warning,
see PR cli/15104. */
c = add_cmd (alias, no_class, NULL, t->to_doc, &targetlist);
set_cmd_sfunc (c, open_target);
set_cmd_context (c, t);
alt = xstrprintf ("target %s", t->to_shortname);
deprecate_cmd (c, alt);
}
/* Stub functions */
void
target_kill (void)
{
current_target.to_kill (¤t_target);
}
void
target_load (const char *arg, int from_tty)
{
target_dcache_invalidate ();
(*current_target.to_load) (¤t_target, arg, from_tty);
}
/* Possible terminal states. */
enum terminal_state
{
/* The inferior's terminal settings are in effect. */
terminal_is_inferior = 0,
/* Some of our terminal settings are in effect, enough to get
proper output. */
terminal_is_ours_for_output = 1,
/* Our terminal settings are in effect, for output and input. */
terminal_is_ours = 2
};
static enum terminal_state terminal_state = terminal_is_ours;
/* See target.h. */
void
target_terminal_init (void)
{
(*current_target.to_terminal_init) (¤t_target);
terminal_state = terminal_is_ours;
}
/* See target.h. */
int
target_terminal_is_inferior (void)
{
return (terminal_state == terminal_is_inferior);
}
/* See target.h. */
void
target_terminal_inferior (void)
{
/* A background resume (``run&'') should leave GDB in control of the
terminal. Use target_can_async_p, not target_is_async_p, since at
this point the target is not async yet. However, if sync_execution
is not set, we know it will become async prior to resume. */
if (target_can_async_p () && !sync_execution)
return;
if (terminal_state == terminal_is_inferior)
return;
/* If GDB is resuming the inferior in the foreground, install
inferior's terminal modes. */
(*current_target.to_terminal_inferior) (¤t_target);
terminal_state = terminal_is_inferior;
}
/* See target.h. */
void
target_terminal_ours (void)
{
if (terminal_state == terminal_is_ours)
return;
(*current_target.to_terminal_ours) (¤t_target);
terminal_state = terminal_is_ours;
}
/* See target.h. */
void
target_terminal_ours_for_output (void)
{
if (terminal_state != terminal_is_inferior)
return;
(*current_target.to_terminal_ours_for_output) (¤t_target);
terminal_state = terminal_is_ours_for_output;
}
/* See target.h. */
int
target_supports_terminal_ours (void)
{
struct target_ops *t;
for (t = current_target.beneath; t != NULL; t = t->beneath)
{
if (t->to_terminal_ours != delegate_terminal_ours
&& t->to_terminal_ours != tdefault_terminal_ours)
return 1;
}
return 0;
}
/* Restore the terminal to its previous state (helper for
make_cleanup_restore_target_terminal). */
static void
cleanup_restore_target_terminal (void *arg)
{
enum terminal_state *previous_state = arg;
switch (*previous_state)
{
case terminal_is_ours:
target_terminal_ours ();
break;
case terminal_is_ours_for_output:
target_terminal_ours_for_output ();
break;
case terminal_is_inferior:
target_terminal_inferior ();
break;
}
}
/* See target.h. */
struct cleanup *
make_cleanup_restore_target_terminal (void)
{
enum terminal_state *ts = XNEW (enum terminal_state);
*ts = terminal_state;
return make_cleanup_dtor (cleanup_restore_target_terminal, ts, xfree);
}
static void
tcomplain (void)
{
error (_("You can't do that when your target is `%s'"),
current_target.to_shortname);
}
void
noprocess (void)
{
error (_("You can't do that without a process to debug."));
}
static void
default_terminal_info (struct target_ops *self, const char *args, int from_tty)
{
printf_unfiltered (_("No saved terminal information.\n"));
}
/* A default implementation for the to_get_ada_task_ptid target method.
This function builds the PTID by using both LWP and TID as part of
the PTID lwp and tid elements. The pid used is the pid of the
inferior_ptid. */
static ptid_t
default_get_ada_task_ptid (struct target_ops *self, long lwp, long tid)
{
return ptid_build (ptid_get_pid (inferior_ptid), lwp, tid);
}
static enum exec_direction_kind
default_execution_direction (struct target_ops *self)
{
if (!target_can_execute_reverse)
return EXEC_FORWARD;
else if (!target_can_async_p ())
return EXEC_FORWARD;
else
gdb_assert_not_reached ("\
to_execution_direction must be implemented for reverse async");
}
/* Go through the target stack from top to bottom, copying over zero
entries in current_target, then filling in still empty entries. In
effect, we are doing class inheritance through the pushed target
vectors.
NOTE: cagney/2003-10-17: The problem with this inheritance, as it
is currently implemented, is that it discards any knowledge of
which target an inherited method originally belonged to.
Consequently, new new target methods should instead explicitly and
locally search the target stack for the target that can handle the
request. */
static void
update_current_target (void)
{
struct target_ops *t;
/* First, reset current's contents. */
memset (¤t_target, 0, sizeof (current_target));
/* Install the delegators. */
install_delegators (¤t_target);
current_target.to_stratum = target_stack->to_stratum;
#define INHERIT(FIELD, TARGET) \
if (!current_target.FIELD) \
current_target.FIELD = (TARGET)->FIELD
/* Do not add any new INHERITs here. Instead, use the delegation
mechanism provided by make-target-delegates. */
for (t = target_stack; t; t = t->beneath)
{
INHERIT (to_shortname, t);
INHERIT (to_longname, t);
INHERIT (to_attach_no_wait, t);
INHERIT (to_have_steppable_watchpoint, t);
INHERIT (to_have_continuable_watchpoint, t);
INHERIT (to_has_thread_control, t);
}
#undef INHERIT
/* Finally, position the target-stack beneath the squashed
"current_target". That way code looking for a non-inherited
target method can quickly and simply find it. */
current_target.beneath = target_stack;
if (targetdebug)
setup_target_debug ();
}
/* Push a new target type into the stack of the existing target accessors,
possibly superseding some of the existing accessors.
Rather than allow an empty stack, we always have the dummy target at
the bottom stratum, so we can call the function vectors without
checking them. */
void
push_target (struct target_ops *t)
{
struct target_ops **cur;
/* Check magic number. If wrong, it probably means someone changed
the struct definition, but not all the places that initialize one. */
if (t->to_magic != OPS_MAGIC)
{
fprintf_unfiltered (gdb_stderr,
"Magic number of %s target struct wrong\n",
t->to_shortname);
internal_error (__FILE__, __LINE__,
_("failed internal consistency check"));
}
/* Find the proper stratum to install this target in. */
for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
{
if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
break;
}
/* If there's already targets at this stratum, remove them. */
/* FIXME: cagney/2003-10-15: I think this should be popping all
targets to CUR, and not just those at this stratum level. */
while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
{
/* There's already something at this stratum level. Close it,
and un-hook it from the stack. */
struct target_ops *tmp = (*cur);
(*cur) = (*cur)->beneath;
tmp->beneath = NULL;
target_close (tmp);
}
/* We have removed all targets in our stratum, now add the new one. */
t->beneath = (*cur);
(*cur) = t;
update_current_target ();
}
/* Remove a target_ops vector from the stack, wherever it may be.
Return how many times it was removed (0 or 1). */
int
unpush_target (struct target_ops *t)
{
struct target_ops **cur;
struct target_ops *tmp;
if (t->to_stratum == dummy_stratum)
internal_error (__FILE__, __LINE__,
_("Attempt to unpush the dummy target"));
/* Look for the specified target. Note that we assume that a target
can only occur once in the target stack. */
for (cur = &target_stack; (*cur) != NULL; cur = &(*cur)->beneath)
{
if ((*cur) == t)
break;
}
/* If we don't find target_ops, quit. Only open targets should be
closed. */
if ((*cur) == NULL)
return 0;
/* Unchain the target. */
tmp = (*cur);
(*cur) = (*cur)->beneath;
tmp->beneath = NULL;
update_current_target ();
/* Finally close the target. Note we do this after unchaining, so
any target method calls from within the target_close
implementation don't end up in T anymore. */
target_close (t);
return 1;
}
void
pop_all_targets_above (enum strata above_stratum)
{
while ((int) (current_target.to_stratum) > (int) above_stratum)
{
if (!unpush_target (target_stack))
{
fprintf_unfiltered (gdb_stderr,
"pop_all_targets couldn't find target %s\n",
target_stack->to_shortname);
internal_error (__FILE__, __LINE__,
_("failed internal consistency check"));
break;
}
}
}
void
pop_all_targets (void)
{
pop_all_targets_above (dummy_stratum);
}
/* Return 1 if T is now pushed in the target stack. Return 0 otherwise. */
int
target_is_pushed (struct target_ops *t)
{
struct target_ops *cur;
/* Check magic number. If wrong, it probably means someone changed
the struct definition, but not all the places that initialize one. */
if (t->to_magic != OPS_MAGIC)
{
fprintf_unfiltered (gdb_stderr,
"Magic number of %s target struct wrong\n",
t->to_shortname);
internal_error (__FILE__, __LINE__,
_("failed internal consistency check"));
}
for (cur = target_stack; cur != NULL; cur = cur->beneath)
if (cur == t)
return 1;
return 0;
}
/* Default implementation of to_get_thread_local_address. */
static void
generic_tls_error (void)
{
throw_error (TLS_GENERIC_ERROR,
_("Cannot find thread-local variables on this target"));
}
/* Using the objfile specified in OBJFILE, find the address for the
current thread's thread-local storage with offset OFFSET. */
CORE_ADDR
target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset)
{
volatile CORE_ADDR addr = 0;
struct target_ops *target = ¤t_target;
if (gdbarch_fetch_tls_load_module_address_p (target_gdbarch ()))
{
ptid_t ptid = inferior_ptid;
TRY
{
CORE_ADDR lm_addr;
/* Fetch the load module address for this objfile. */
lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch (),
objfile);
addr = target->to_get_thread_local_address (target, ptid,
lm_addr, offset);
}
/* If an error occurred, print TLS related messages here. Otherwise,
throw the error to some higher catcher. */
CATCH (ex, RETURN_MASK_ALL)
{
int objfile_is_library = (objfile->flags & OBJF_SHARED);
switch (ex.error)
{
case TLS_NO_LIBRARY_SUPPORT_ERROR:
error (_("Cannot find thread-local variables "
"in this thread library."));
break;
case TLS_LOAD_MODULE_NOT_FOUND_ERROR:
if (objfile_is_library)
error (_("Cannot find shared library `%s' in dynamic"
" linker's load module list"), objfile_name (objfile));
else
error (_("Cannot find executable file `%s' in dynamic"
" linker's load module list"), objfile_name (objfile));
break;
case TLS_NOT_ALLOCATED_YET_ERROR:
if (objfile_is_library)
error (_("The inferior has not yet allocated storage for"
" thread-local variables in\n"
"the shared library `%s'\n"
"for %s"),
objfile_name (objfile), target_pid_to_str (ptid));
else
error (_("The inferior has not yet allocated storage for"
" thread-local variables in\n"
"the executable `%s'\n"
"for %s"),
objfile_name (objfile), target_pid_to_str (ptid));
break;
case TLS_GENERIC_ERROR:
if (objfile_is_library)
error (_("Cannot find thread-local storage for %s, "
"shared library %s:\n%s"),
target_pid_to_str (ptid),
objfile_name (objfile), ex.message);
else
error (_("Cannot find thread-local storage for %s, "
"executable file %s:\n%s"),
target_pid_to_str (ptid),
objfile_name (objfile), ex.message);
break;
default:
throw_exception (ex);
break;
}
}
END_CATCH
}
/* It wouldn't be wrong here to try a gdbarch method, too; finding
TLS is an ABI-specific thing. But we don't do that yet. */
else
error (_("Cannot find thread-local variables on this target"));
return addr;
}
const char *
target_xfer_status_to_string (enum target_xfer_status status)
{
#define CASE(X) case X: return #X
switch (status)
{
CASE(TARGET_XFER_E_IO);
CASE(TARGET_XFER_UNAVAILABLE);
default:
return "<unknown>";
}
#undef CASE
};
#undef MIN
#define MIN(A, B) (((A) <= (B)) ? (A) : (B))
/* target_read_string -- read a null terminated string, up to LEN bytes,
from MEMADDR in target. Set *ERRNOP to the errno code, or 0 if successful.
Set *STRING to a pointer to malloc'd memory containing the data; the caller
is responsible for freeing it. Return the number of bytes successfully
read. */
int
target_read_string (CORE_ADDR memaddr, char **string, int len, int *errnop)
{
int tlen, offset, i;
gdb_byte buf[4];
int errcode = 0;
char *buffer;
int buffer_allocated;
char *bufptr;
unsigned int nbytes_read = 0;
gdb_assert (string);
/* Small for testing. */
buffer_allocated = 4;
buffer = xmalloc (buffer_allocated);
bufptr = buffer;
while (len > 0)
{
tlen = MIN (len, 4 - (memaddr & 3));
offset = memaddr & 3;
errcode = target_read_memory (memaddr & ~3, buf, sizeof buf);
if (errcode != 0)
{
/* The transfer request might have crossed the boundary to an
unallocated region of memory. Retry the transfer, requesting
a single byte. */
tlen = 1;
offset = 0;
errcode = target_read_memory (memaddr, buf, 1);
if (errcode != 0)
goto done;
}
if (bufptr - buffer + tlen > buffer_allocated)
{
unsigned int bytes;
bytes = bufptr - buffer;
buffer_allocated *= 2;
buffer = xrealloc (buffer, buffer_allocated);
bufptr = buffer + bytes;
}
for (i = 0; i < tlen; i++)
{
*bufptr++ = buf[i + offset];
if (buf[i + offset] == '\000')
{
nbytes_read += i + 1;
goto done;
}
}
memaddr += tlen;
len -= tlen;
nbytes_read += tlen;
}
done:
*string = buffer;
if (errnop != NULL)
*errnop = errcode;
return nbytes_read;
}
struct target_section_table *
target_get_section_table (struct target_ops *target)
{
return (*target->to_get_section_table) (target);
}
/* Find a section containing ADDR. */
struct target_section *
target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
{
struct target_section_table *table = target_get_section_table (target);
struct target_section *secp;
if (table == NULL)
return NULL;
for (secp = table->sections; secp < table->sections_end; secp++)
{
if (addr >= secp->addr && addr < secp->endaddr)