Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
target_os_linux_generic.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, 2014, 2015, 2016, 2017 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "config.h"
20 
21 #include <assert.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <glib.h>
26 
27 #include "common.h"
28 #include "glib_wrapper.h"
29 #include "arch.h"
30 #include "arch_x86.h"
31 #include "arch_x86_64.h"
32 #include "binfile.h"
33 #include "target.h"
34 #include "target_event.h"
35 #include "target_os.h"
37 
42  struct value *taskv);
44  int force);
45 static int os_linux_updateregions(struct target *target,
46  struct addrspace *space);
47 static void os_linux_mm_free(struct os_linux_mm *olmm);
49  struct probe *trigger,struct probe *base);
51  struct probe *trigger,struct probe *base);
52 
53 /*
54  * We don't keep any local storage, so there is nothing to init or free!
55  * All of our storage is based on target_gkv_*().
56  */
57 
59  struct os_linux_state *lstate;
60  char *major = NULL,*minor = NULL,*patch = NULL;
61  REFCNT trefcnt;
62  unsigned int slen,i;
63  char pbuf[PATH_MAX];
64  char *k,*v;
65  FILE *cf;
66  char *tmp;
67  char *kdir = NULL;
68  int ksize;
69 
70  lstate = (struct os_linux_state *)calloc(1,sizeof(*lstate));
71  target->personality_state = lstate;
72 
73  lstate->kernel_filename = (char *) \
74  g_hash_table_lookup(target->config,"OS_KERNEL_FILENAME");
75  if (!lstate->kernel_filename) {
76  lstate->kernel_filename = (char *) \
77  g_hash_table_lookup(target->config,"MAIN_FILENAME");
78  }
79 
80  /*
81  * First, parse out its version. We look for the first number
82  * followed by a '.' and preceded by a '-'.
83  */
84  slen = strlen(lstate->kernel_filename);
85  for (i = 0; i < slen; ++i) {
86  if ((i > 0 && lstate->kernel_filename[i - 1] == '-')
87  && isdigit(lstate->kernel_filename[i])
88  && (i + 1) < slen && lstate->kernel_filename[i + 1] == '.') {
89  lstate->kernel_version = strdup(&lstate->kernel_filename[i]);
90  break;
91  }
92  }
93 
94  if (!lstate->kernel_version) {
95  verror("could not parse kernel version info for %s!\n",
96  lstate->kernel_filename);
97  goto errout;
98  }
99 
100  kdir = rindex(lstate->kernel_filename,'/');
101  if (!kdir)
102  kdir = strdup("./");
103  else {
104  ksize = kdir - lstate->kernel_filename + 1;
105  kdir = malloc(ksize + 1);
106  strncpy(kdir,lstate->kernel_filename,ksize);
107  kdir[ksize] = '\0';
108  }
109 
110  /*
111  * Figure out where the real ELF file is. We look in six
112  * places:
113  * /usr/lib/debug/lib/modules/<kernel_version>/vmlinux
114  * /usr/lib/debug/boot/vmlinux-<kernel_version>
115  * /boot/vmlinux-<kernel_version>
116  * /boot/vmlinux-syms-<kernel_version> (old A3 style)
117  * <kernel_filename_dir>/vmlinux-<kernel_version>
118  * <kernel_filename_dir>/vmlinux-syms-<kernel_version>
119  */
120  lstate->kernel_elf_filename = malloc(PATH_MAX);
121  lstate->kernel_elf_filename[0] = '\0';
122 
123  if (lstate->kernel_elf_filename[0] == '\0') {
124  snprintf(lstate->kernel_elf_filename,PATH_MAX,
125  "%s/usr/lib/debug/lib/modules/%s/vmlinux",
126  (target->spec->debugfile_root_prefix) \
127  ? target->spec->debugfile_root_prefix : "",
128  lstate->kernel_version);
129  if (access(lstate->kernel_elf_filename,R_OK))
130  lstate->kernel_elf_filename[0] = '\0';
131  }
132  if (lstate->kernel_elf_filename[0] == '\0') {
133  snprintf(lstate->kernel_elf_filename,PATH_MAX,
134  "%s/usr/lib/debug/boot/vmlinux-%s",
135  (target->spec->debugfile_root_prefix) \
136  ? target->spec->debugfile_root_prefix : "",
137  lstate->kernel_version);
138  if (access(lstate->kernel_elf_filename,R_OK))
139  lstate->kernel_elf_filename[0] = '\0';
140  }
141  if (lstate->kernel_elf_filename[0] == '\0') {
142  snprintf(lstate->kernel_elf_filename,PATH_MAX,
143  "%s/boot/vmlinux-%s",
144  (target->spec->debugfile_root_prefix) \
145  ? target->spec->debugfile_root_prefix : "",
146  lstate->kernel_version);
147  if (access(lstate->kernel_elf_filename,R_OK))
148  lstate->kernel_elf_filename[0] = '\0';
149  }
150  if (lstate->kernel_elf_filename[0] == '\0') {
151  snprintf(lstate->kernel_elf_filename,PATH_MAX,
152  "%s/boot/vmlinux-syms-%s",
153  (target->spec->debugfile_root_prefix) \
154  ? target->spec->debugfile_root_prefix : "",
155  lstate->kernel_version);
156  if (access(lstate->kernel_elf_filename,R_OK))
157  lstate->kernel_elf_filename[0] = '\0';
158  }
159  if (lstate->kernel_elf_filename[0] == '\0') {
160  snprintf(lstate->kernel_elf_filename,PATH_MAX,
161  "%s/vmlinux-%s",
162  kdir,lstate->kernel_version);
163  if (access(lstate->kernel_elf_filename,R_OK))
164  lstate->kernel_elf_filename[0] = '\0';
165  }
166  if (lstate->kernel_elf_filename[0] == '\0') {
167  snprintf(lstate->kernel_elf_filename,PATH_MAX,
168  "%s/vmlinux-syms-%s",
169  kdir,lstate->kernel_version);
170  if (access(lstate->kernel_elf_filename,R_OK))
171  lstate->kernel_elf_filename[0] = '\0';
172  }
173 
174  if (lstate->kernel_elf_filename[0] == '\0') {
175  verror("could not find vmlinux binary for %s!\n",
176  lstate->kernel_version);
177  goto errout;
178  }
179 
180  /*
181  * Replace the kernel file name with the real ELF one so that the
182  * target loaddebugfiles() op can load it trivially!
183  */
184  g_hash_table_insert(target->config,strdup("OS_KERNEL_FILENAME"),
185  strdup(lstate->kernel_elf_filename));
186  lstate->kernel_filename = strdup(lstate->kernel_elf_filename);
187 
188  /*
189  * Figure out where the System.map file is. We look in three
190  * places:
191  * /lib/modules/<kernel_version>/System.map
192  * /boot/System.map-<kernel_version>
193  * <kernel_filename_dir>/System.map-<kernel_version>
194  */
195  lstate->kernel_sysmap_filename = malloc(PATH_MAX);
196  lstate->kernel_sysmap_filename[0] = '\0';
197 
198  if (lstate->kernel_sysmap_filename[0] == '\0') {
199  snprintf(lstate->kernel_sysmap_filename,PATH_MAX,
200  "%s/lib/modules/%s/System.map",
201  (target->spec->debugfile_root_prefix) \
202  ? target->spec->debugfile_root_prefix : "",
203  lstate->kernel_version);
204  if (access(lstate->kernel_sysmap_filename,R_OK))
205  lstate->kernel_sysmap_filename[0] = '\0';
206  }
207  if (lstate->kernel_sysmap_filename[0] == '\0') {
208  snprintf(lstate->kernel_sysmap_filename,PATH_MAX,
209  "%s/boot/System.map-%s",
210  (target->spec->debugfile_root_prefix) \
211  ? target->spec->debugfile_root_prefix : "",
212  lstate->kernel_version);
213  if (access(lstate->kernel_sysmap_filename,R_OK))
214  lstate->kernel_sysmap_filename[0] = '\0';
215  }
216  if (lstate->kernel_sysmap_filename[0] == '\0') {
217  snprintf(lstate->kernel_sysmap_filename,PATH_MAX,
218  "%s/System.map-%s",
219  kdir,lstate->kernel_version);
220  if (access(lstate->kernel_sysmap_filename,R_OK))
221  lstate->kernel_sysmap_filename[0] = '\0';
222  }
223 
224  if (lstate->kernel_sysmap_filename[0] == '\0') {
225  verror("could not find System.map file for %s!\n",
226  lstate->kernel_version);
227  goto errout;
228  }
229  else {
230  g_hash_table_insert(target->config,strdup("OS_KERNEL_SYSMAP_FILE"),
231  strdup(lstate->kernel_sysmap_filename));
232  }
233 
234  /*
235  * If we found something, look for the kernel module dir. We look in
236  * two places:
237  *
238  * /usr/lib/debug/lib/modules/<kernel_version>
239  * /lib/modules/<kernel_version>
240  */
241  if (lstate->kernel_version) {
242  lstate->kernel_module_dir = malloc(PATH_MAX);
243  lstate->kernel_module_dir[0] = '\0';
244 
245  if (lstate->kernel_module_dir[0] == '\0') {
246  snprintf(lstate->kernel_module_dir,PATH_MAX,
247  "%s/usr/lib/debug/lib/modules/%s",
248  (target->spec->debugfile_root_prefix) \
249  ? target->spec->debugfile_root_prefix : "",
250  lstate->kernel_version);
251  if (access(lstate->kernel_module_dir,R_OK))
252  lstate->kernel_module_dir[0] = '\0';
253  }
254  if (lstate->kernel_module_dir[0] == '\0') {
255  snprintf(lstate->kernel_module_dir,PATH_MAX,
256  "%s/lib/modules/%s",
257  (target->spec->debugfile_root_prefix) \
258  ? target->spec->debugfile_root_prefix : "",
259  lstate->kernel_version);
260  if (access(lstate->kernel_module_dir,R_OK))
261  lstate->kernel_module_dir[0] = '\0';
262  }
263 
264  if (lstate->kernel_module_dir[0] == '\0') {
265  free(lstate->kernel_module_dir);
266  lstate->kernel_module_dir = NULL;
267  }
268  }
269 
270  if (sscanf(lstate->kernel_version,"%m[0-9].%m[0-9].%m[0-9]",
271  &major,&minor,&patch) == 3) {
272  g_hash_table_insert(target->config,strdup("__VERSION_MAJOR"),major);
273  g_hash_table_insert(target->config,strdup("__VERSION_MINOR"),minor);
274  g_hash_table_insert(target->config,strdup("__VERSION_PATCH"),patch);
275  }
276  else {
277  if (major)
278  free(major);
279  if (minor)
280  free(minor);
281  if (patch)
282  free(patch);
283  }
284 
285  /*
286  * Load the config file. We look in three places:
287  * <prefix>/lib/modules/<kernel_version>/config-<kernel_version>
288  * <prefix>/boot/config-<kernel_version>/
289  * /boot/config-<kernel_version>
290  * <kernel_filename_dir>/config-<kernel_version>
291  */
292  pbuf[0] = '\0';
293  if (pbuf[0] == '\0') {
294  snprintf(pbuf,sizeof(pbuf),"%s/lib/modules/%s/config-%s",
295  (target->spec->debugfile_root_prefix) \
296  ? target->spec->debugfile_root_prefix : "",
297  lstate->kernel_version,lstate->kernel_version);
298  if (access(pbuf,R_OK))
299  pbuf[0] = '\0';
300  }
301  if (pbuf[0] == '\0') {
302  snprintf(pbuf,sizeof(pbuf),"%s/boot/config-%s",
303  (target->spec->debugfile_root_prefix) \
304  ? target->spec->debugfile_root_prefix : "",
305  lstate->kernel_version);
306  if (access(pbuf,R_OK))
307  pbuf[0] = '\0';
308  }
309  if (pbuf[0] == '\0') {
310  snprintf(pbuf,sizeof(pbuf),"/boot/config-%s",
311  lstate->kernel_version);
312  if (access(pbuf,R_OK))
313  pbuf[0] = '\0';
314  }
315  if (pbuf[0] == '\0') {
316  snprintf(pbuf,sizeof(pbuf),"%s/config-%s",
317  kdir,lstate->kernel_version);
318  if (access(pbuf,R_OK))
319  pbuf[0] = '\0';
320  }
321 
322  if (pbuf[0] != '\0') {
323  cf = fopen(pbuf,"r");
324  if (!cf)
325  verror("fopen(%s): %s\n",pbuf,strerror(errno));
326  else {
327  /* scanf to look for normal lines. */
328  while (1) {
329  if (!fgets(pbuf,sizeof(pbuf),cf))
330  break;
331  if (*pbuf == '#')
332  continue;
333 
334  k = v = NULL;
335  if (sscanf(pbuf,"%m[^ \t=]=\"%ms\"",&k,&v) == 2) {
336  g_hash_table_insert(target->config,k,v);
337  continue;
338  }
339  else {
340  if (k)
341  free(k);
342  if (v)
343  free(v);
344  }
345 
346  k = v = NULL;
347  if (sscanf(pbuf,"%m[^ \t=]=%ms",&k,&v) == 2) {
348  g_hash_table_insert(target->config,k,v);
349  continue;
350  }
351  else {
352  if (k)
353  free(k);
354  if (v)
355  free(v);
356  }
357  }
358  fclose(cf);
359  }
360  }
361  else {
362  vwarn("could not read kernel config from %s; strange errors may result!\n",
363  pbuf);
364  }
365 
366  if (!lstate->kernel_elf_filename) {
367  verror("could not infer kernel ELF file (vmlinux) from %s; aborting!\n",
368  lstate->kernel_filename);
369  goto errout;
370  }
371 
372  /* Then grab stuff from the ELF binary itself. */
373  if (!target->binfile) {
374  target->binfile =
376  target->spec->debugfile_root_prefix,NULL);
377  if (!target->binfile) {
378  verror("binfile_open %s: %s\n",
379  lstate->kernel_elf_filename,strerror(errno));
380  goto errout;
381  }
382 
383  RHOLD(target->binfile,target);
384  /* Drop the self-ref that binfile_open held on our behalf. */
385  RPUT(target->binfile,binfile,target->binfile,trefcnt);
386 
388  "loaded ELF arch info for %s (wordsize=%d;endian=%s)\n",
389  lstate->kernel_elf_filename,target->binfile->arch->wordsize,
390  (target->binfile->arch->endian == ENDIAN_LITTLE ? "LSB" : "MSB"));
391  }
392 
393  lstate->task_struct_addr_to_thread =
394  g_hash_table_new(g_direct_hash,g_direct_equal);
395 
396  lstate->mm_addr_to_mm_cache =
397  g_hash_table_new(g_direct_hash,g_direct_equal);
398  lstate->processes =
399  g_hash_table_new(g_direct_hash,g_direct_equal);
400 
402 
403  if (kdir)
404  free(kdir);
405 
406  return 0;
407 
408  errout:
409  if (kdir)
410  free(kdir);
411  if (lstate->processes)
412  g_hash_table_destroy(lstate->processes);
413  if (lstate->mm_addr_to_mm_cache)
414  g_hash_table_destroy(lstate->mm_addr_to_mm_cache);
415  if (lstate->task_struct_addr_to_thread)
416  g_hash_table_destroy(lstate->task_struct_addr_to_thread);
417  if (lstate->kernel_version)
418  free(lstate->kernel_version);
419  if (lstate->kernel_filename)
420  free(lstate->kernel_filename);
421  if (lstate->kernel_elf_filename)
422  free(lstate->kernel_elf_filename);
423  if (lstate->kernel_sysmap_filename)
424  free(lstate->kernel_sysmap_filename);
425  if (lstate->kernel_module_dir)
426  free(lstate->kernel_module_dir);
427 
428  return -1;
429 }
430 
432  struct os_linux_state *lstate = \
433  (struct os_linux_state *)target->personality_state;
434  REFCNT trefcnt;
435  gpointer vp;
436  GHashTableIter iter;
437  struct target_process *process;
438  struct os_linux_mm *olmm;
439 
440  /*
441  * If we have loaded any processes, clear them.
442  */
443  g_hash_table_iter_init(&iter,lstate->processes);
444  while (g_hash_table_iter_next(&iter,NULL,&vp)) {
445  process = (struct target_process *)vp;
446  RPUT(process,target_process,target,trefcnt);
447  g_hash_table_iter_remove(&iter);
448  }
449 
450  /*
451  * If we have loaded any process addrspaces, clear them.
452  */
453  g_hash_table_iter_init(&iter,lstate->mm_addr_to_mm_cache);
454  while (g_hash_table_iter_next(&iter,NULL,&vp)) {
455  olmm = (struct os_linux_mm *)vp;
456  os_linux_mm_free(olmm);
457  g_hash_table_iter_remove(&iter);
458  }
459 
460  if (lstate->task_struct_addr_to_thread) {
461  g_hash_table_destroy(lstate->task_struct_addr_to_thread);
462  lstate->task_struct_addr_to_thread = NULL;
463  }
464 
465  /*
466  * If we have loaded any processes, clear them.
467  */
468 
469  if (lstate->init_task)
470  bsymbol_release(lstate->init_task);
471  if (lstate->task_struct_type)
473  if (lstate->thread_struct_type)
475  if (lstate->task_struct_type_ptr)
477  if (lstate->mm_struct_type)
479  if (lstate->pt_regs_type)
480  symbol_release(lstate->pt_regs_type);
481  if (lstate->thread_info_type)
482  RPUT(lstate->thread_info_type,symbol,target,trefcnt);
483  if (lstate->modules)
484  bsymbol_release(lstate->modules);
485  if (lstate->module_type)
486  bsymbol_release(lstate->module_type);
487 
488  if (lstate->kernel_version)
489  free(lstate->kernel_version);
490  if (lstate->kernel_elf_filename)
491  free(lstate->kernel_elf_filename);
492  if (lstate->kernel_sysmap_filename)
493  free(lstate->kernel_sysmap_filename);
494  if (lstate->kernel_module_dir)
495  free(lstate->kernel_module_dir);
496 
497  return 0;
498 }
499 
501  struct os_linux_state *lstate = \
502  (struct os_linux_state *)target->personality_state;
503  struct bsymbol *thread_info_type;
504  struct bsymbol *mm_struct_type;
505  struct lsymbol *tmpls;
506  struct bsymbol *tmpbs;
507  OFFSET offset;
508  char buf[128];
509  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
510 
511  /*
512  * Assume if we did this, we've done it all.
513  */
514  if (lstate->init_task)
515  return 0;
516 
517  /* XXX: enable linux decoders. */
518  if (target_decoder_lib_bind(target,
519  "os_linux_generic_decoder_lib",NULL) == 0) {
521  "autoinitialized the os_linux_generic decoders!\n");
522  }
523  else {
524  verror("failed to autoinitialize the os_linux_generic decoders!\n");
525  }
526 
527  /*
528  * Finally: initialize our state in the target's global thread!
529  */
531  calloc(1,sizeof(struct os_linux_thread_state));
532 
533  /*
534  * Try to load some debuginfo stuff so we can provide better
535  * functionality! We have to do this in target_attach because only
536  * at that point can we know that the debuginfo sources have been
537  * loaded.
538  */
539 
540  /*
541  * Find the kernel start address.
542  */
543 
544  /* Try .text first (and fake the delimiter!!!) */
545  if (lstate->kernel_start_addr == 0) {
546  tmpbs = target_lookup_sym(target,".text","|",NULL,
548  if (tmpbs) {
549  if (target_bsymbol_resolve_base(target,tlctxt,tmpbs,
550  &lstate->kernel_start_addr,NULL)) {
552  "could not resolve addr of .text;"
553  " trying startup_(32|64)!\n");
554  }
555  bsymbol_release(tmpbs);
556  tmpbs = NULL;
557  }
558  else {
560  "could not find symbol .text; trying startup_(32|64)!\n");
561  }
562  }
563 
564  /* If we didn't find .text, try startup_(32|64). */
565  if (lstate->kernel_start_addr == 0) {
566  if (target->arch->wordsize == 4) {
567  tmpbs = target_lookup_sym(target,"startup_32",NULL,NULL,
569  if (tmpbs) {
570  if (target_bsymbol_resolve_base(target,tlctxt,
571  tmpbs,&lstate->kernel_start_addr,
572  NULL)) {
574  "could not resolve addr of startup_32!\n");
575  }
576  bsymbol_release(tmpbs);
577  tmpbs = NULL;
578  }
579  else {
581  "could not find symbol startup_32!\n");
582  }
583  }
584  else {
585  tmpbs = target_lookup_sym(target,"startup_64",NULL,NULL,
587  if (tmpbs) {
588  if (target_bsymbol_resolve_base(target,tlctxt,
589  tmpbs,&lstate->kernel_start_addr,
590  NULL)) {
592  "could not resolve addr of startup_64!\n");
593  }
594  bsymbol_release(tmpbs);
595  tmpbs = NULL;
596  }
597  else {
599  "could not find symbol startup_64!\n");
600  }
601  }
602  }
603 
604  /* If we still didn't find it... */
605  if (lstate->kernel_start_addr == 0) {
606  vwarn("could not find addr of .text nor startup_(32|64);"
607  " using defaults!\n");
608 
609  if (target->arch->wordsize == 4)
610  lstate->kernel_start_addr = 0xC0000000;
611 #if __WORDSIZE == 64
612  else if (target->arch->wordsize == 8)
613  lstate->kernel_start_addr = 0xFFFFFFFF81000000ULL;
614 #endif
615  }
616 
617  snprintf(buf,sizeof(buf),"0x%"PRIxADDR,lstate->kernel_start_addr);
618  g_hash_table_insert(target->config,
619  strdup("OS_KERNEL_START_ADDR"),strdup(buf));
620 
621  vdebug(3,LA_TARGET,LF_OSLINUX,"kernel start addr is 0x%"PRIxREGVAL"\n",
622  lstate->kernel_start_addr);
623 
624  /*
625  * See if we have a hypercall_page...
626  */
627  lstate->hypercall_page = 0;
628  tmpbs = target_lookup_sym(target,"hypercall_page",NULL,NULL,
630  if (tmpbs) {
631  if (target_bsymbol_resolve_base(target,tlctxt,tmpbs,
632  &lstate->hypercall_page,NULL)) {
634  "could not resolve addr of hypercall_page!\n");
635  }
636  else {
637  snprintf(buf,sizeof(buf),"0x%"PRIxADDR,lstate->hypercall_page);
638  g_hash_table_insert(target->config,
639  strdup("OS_KERNEL_HYPERCALL_PAGE"),strdup(buf));
640  }
641  bsymbol_release(tmpbs);
642  tmpbs = NULL;
643  }
644  else {
646  "could not find symbol hypercall_page!\n");
647  }
648 
649  /*
650  * Find init_task.
651  */
652  lstate->init_task = target_lookup_sym(target,"init_task",NULL,NULL,
654  if (!lstate->init_task) {
655  vwarn("could not lookup init_task in debuginfo; no multithread support!\n");
656  /* This is not an error, so we don't return error -- it
657  * would upset target_open.
658  */
659  return 0;
660  }
661  if (target_bsymbol_resolve_base(target,tlctxt,
662  lstate->init_task,
663  &lstate->init_task_addr,NULL)) {
664  vwarn("could not resolve addr of init_task!\n");
665  }
666  else {
667  snprintf(buf,sizeof(buf),"0x%"PRIxADDR,lstate->init_task_addr);
668  g_hash_table_insert(target->config,
669  strdup("OS_KERNEL_INIT_TASK_ADDR"),strdup(buf));
670  }
671 
672  /*
673  * Save the 'struct task_struct' type. Hold a ref to it since it
674  * might be an autofollowed abstract origin type!
675  */
676  lstate->task_struct_type = \
678  RHOLD(lstate->task_struct_type,target);
679 
680  mm_struct_type = target_lookup_sym(target,"struct mm_struct",
681  NULL,NULL,SYMBOL_TYPE_FLAG_TYPE);
682  if (!mm_struct_type) {
683  vwarn("could not lookup 'struct mm_struct' in debuginfo;"
684  " userspace vm access might fail!\n");
685  /* This is not an error, so we don't return error -- it
686  * would upset target_open.
687  */
688  return 0;
689  }
691  RHOLD(lstate->mm_struct_type,target);
693 
694  /* We might also want to load tasks from pointers (i.e., the
695  * current task.
696  */
697  lstate->task_struct_type_ptr = \
699 
700  /*
701  * Find some offsets inside typeof(init_task).
702  */
703  offset = symbol_offsetof(lstate->task_struct_type,"tasks",NULL);
704  if (errno)
705  vwarn("could not resolve offset of task_struct.tasks!\n");
706  else {
707  snprintf(buf,sizeof(buf),"0x%"PRIxOFFSET,offset);
708  g_hash_table_insert(target->config,
709  strdup("OS_KERNEL_TASKS_OFFSET"),strdup(buf));
710  }
711  errno = 0;
712  offset = symbol_offsetof(lstate->task_struct_type,"pid",NULL);
713  if (errno)
714  vwarn("could not resolve offset of task_struct.pid!\n");
715  else {
716  snprintf(buf,sizeof(buf),"0x%"PRIxOFFSET,offset);
717  g_hash_table_insert(target->config,
718  strdup("OS_KERNEL_PID_OFFSET"),strdup(buf));
719  }
720  errno = 0;
721  offset = symbol_offsetof(lstate->task_struct_type,"mm",NULL);
722  if (errno)
723  vwarn("could not resolve offset of task_struct.mm!\n");
724  else {
725  snprintf(buf,sizeof(buf),"0x%"PRIxOFFSET,offset);
726  g_hash_table_insert(target->config,
727  strdup("OS_KERNEL_MM_OFFSET"),strdup(buf));
728  }
729  errno = 0;
730  offset = symbol_offsetof(lstate->mm_struct_type,"pgd",NULL);
731  if (errno)
732  vwarn("could not resolve offset of mm_struct.pgd!\n");
733  else {
734  snprintf(buf,sizeof(buf),"0x%"PRIxOFFSET,offset);
735  g_hash_table_insert(target->config,
736  strdup("OS_KERNEL_MM_PGD_OFFSET"),strdup(buf));
737  }
738 
739  /*
740  * For x86_64, current_thread_ptr depends on this value -- so just
741  * load it once and keep it around forever.
742  * target_xen_vm_util::current_thread_ptr refreshes it as needed.
743  */
744  if (target->arch->wordsize == 8) {
746  "attempting to find per-cpu kernel stack offset\n");
747 
748  if ((tmpbs = target_lookup_sym(target,"kernel_stack",NULL,NULL,
750  errno = 0;
751  lstate->kernel_stack_percpu_offset =
752  target_addressof_symbol(target,tlctxt,tmpbs,
753  LOAD_FLAG_NONE,NULL);
754  bsymbol_release(tmpbs);
755  if (errno) {
756  verror("could not load kernel_stack percpu offset;"
757  " cannot continue!\n");
758  return -1;
759  }
760  }
761  else if ((tmpbs = target_lookup_sym(target,"per_cpu__kernel_stack",NULL,
762  NULL,SYMBOL_TYPE_FLAG_VAR))) {
763  errno = 0;
764  lstate->kernel_stack_percpu_offset =
765  target_addressof_symbol(target,tlctxt,tmpbs,
766  LOAD_FLAG_NONE,NULL);
767  bsymbol_release(tmpbs);
768  if (errno) {
769  verror("could not load per_cpu__kernel_stack percpu offset;"
770  " cannot continue!\n");
771  return -1;
772  }
773  }
774  else if ((tmpbs = target_lookup_sym(target,"struct x8664_pda",NULL,NULL,
776  errno = 0;
777  lstate->kernel_stack_percpu_offset =
778  symbol_offsetof(bsymbol_get_symbol(tmpbs),"kernelstack",NULL);
779  if (errno) {
780  verror("could not get offsetof struct x8664_pda.kernelstack;"
781  " cannot continue!\n");
782  return -1;
783  }
784  }
785  else {
786  verror("could not find x86_64 kernel stack percpu var in debuginfo;"
787  " cannot continue!\n");
788  return -1;
789  }
790  }
791 
792  /*
793  * We have to figure out if we're on an interrupt stack or not, and
794  * on x86_64, that is in the PDA (%gs), modulo an offset.
795  */
796  if (target->arch->wordsize == 8) {
798  "attempting to find per-cpu irq_count offset\n");
799 
800  if ((tmpbs = target_lookup_sym(target,"per_cpu__irq_count",NULL,
801  NULL,SYMBOL_TYPE_FLAG_VAR))) {
802  errno = 0;
803  lstate->irq_count_percpu_offset =
804  target_addressof_symbol(target,tlctxt,tmpbs,
805  LOAD_FLAG_NONE,NULL);
806  bsymbol_release(tmpbs);
807  if (errno) {
808  vwarn("could not load per_cpu__irq_count percpu offset;"
809  " cannot continue!\n");
810  lstate->irq_count_percpu_offset = -1;
811  errno = 0;
812  }
813  }
814  else if ((tmpbs = target_lookup_sym(target,"per_cpu__preempt_count",NULL,
815  NULL,SYMBOL_TYPE_FLAG_VAR))) {
816  errno = 0;
817  lstate->irq_count_percpu_offset =
818  target_addressof_symbol(target,tlctxt,tmpbs,
819  LOAD_FLAG_NONE,NULL);
820  bsymbol_release(tmpbs);
821  if (errno) {
822  vwarn("could not load per_cpu__preempt_count percpu offset;"
823  " cannot continue!\n");
824  lstate->irq_count_percpu_offset = -1;
825  errno = 0;
826  }
827  }
828  else if ((tmpbs = target_lookup_sym(target,"struct x8664_pda",NULL,NULL,
830  errno = 0;
831  lstate->irq_count_percpu_offset =
832  symbol_offsetof(bsymbol_get_symbol(tmpbs),"irqcount",NULL);
833  if (errno) {
834  vwarn("could not get offsetof struct x8664_pda.irqcount;"
835  " cannot continue!\n");
836  lstate->irq_count_percpu_offset = -1;
837  errno = 0;
838  }
839  }
840  else {
841  vwarn("could not find x86_64 irq_count percpu var in debuginfo;"
842  " cannot continue!\n");
843  lstate->irq_count_percpu_offset = -1;
844  errno = 0;
845  }
846  }
847 
848  /* Fill in the init_task addr in the default thread. */
850  lstate->init_task_addr;
851 
852  /*
853  * Save the 'struct pt_regs' type.
854  */
855  tmpbs = target_lookup_sym(target,"struct pt_regs",NULL,NULL,
857  if (!tmpbs) {
858  vwarn("could not lookup 'struct pt_regs' in debuginfo;"
859  " no multithread support!\n");
860  /* This is not an error, so we don't return error -- it
861  * would upset target_open.
862  */
863  return 0;
864  }
865  lstate->pt_regs_type = bsymbol_get_symbol(tmpbs);
866  RHOLD(lstate->pt_regs_type,target);
867  bsymbol_release(tmpbs);
868 
869  /*
870  * Find out if pt_regs has ds/es (only i386 should have it; old i386
871  * has xds/xes; new i386 has ds/es).
872  */
873  if ((tmpls = symbol_lookup_sym(lstate->pt_regs_type,"ds",NULL))
874  || (tmpls = symbol_lookup_sym(lstate->pt_regs_type,"xds",NULL))) {
875  lsymbol_release(tmpls);
876  lstate->pt_regs_has_ds_es = 1;
877  }
878  else
879  lstate->pt_regs_has_ds_es = 0;
880 
881  /*
882  * Find out if pt_regs has fs/gs (only i386 should have it).
883  */
884  if ((tmpls = symbol_lookup_sym(lstate->pt_regs_type,"fs",NULL))) {
885  lsymbol_release(tmpls);
886  lstate->pt_regs_has_fs_gs = 1;
887  }
888  else
889  lstate->pt_regs_has_fs_gs = 0;
890 
891  /*
892  * Find the offset of the (r|e)ip member in pt_regs (we use this for
893  * faster loading/saving).
894  */
895  errno = 0;
896  lstate->pt_regs_ip_offset =
897  (int)symbol_offsetof(lstate->pt_regs_type,"ip",NULL);
898  if (errno) {
899  errno = 0;
900  lstate->pt_regs_ip_offset =
901  (int)symbol_offsetof(lstate->pt_regs_type,"eip",NULL);
902  if (errno) {
903  errno = 0;
904  lstate->pt_regs_ip_offset =
905  (int)symbol_offsetof(lstate->pt_regs_type,"rip",NULL);
906  if (errno) {
907  vwarn("could not find (r|e)ip in pt_regs; things will break!\n");
908  }
909  }
910  }
911 
912  /*
913  * Find out if task_struct has a thread_info member (older), or if
914  * it just has a void * stack (newer). As always, either way, the
915  * thread_info struct is at the "bottom" of the stack; the stack top
916  * is either a page or two up.
917  */
918  if ((tmpls = symbol_lookup_sym(lstate->task_struct_type,"thread_info",NULL))) {
919  lstate->task_struct_has_thread_info = 1;
920  lsymbol_release(tmpls);
921  }
922  else if ((tmpls = symbol_lookup_sym(lstate->task_struct_type,"stack",NULL))) {
923  lstate->task_struct_has_stack = 1;
924  lsymbol_release(tmpls);
925  }
926  else {
927  vwarn("could not find thread_info nor stack member in struct task_struct;"
928  " no multithread support!\n");
929  return 0;
930  }
931 
932  /*
933  * Find out the name of the uid/gid members.
934  */
935  if ((tmpls = symbol_lookup_sym(lstate->task_struct_type,"uid",NULL))) {
936  lstate->task_uid_member_name = "uid";
937  lstate->task_gid_member_name = "gid";
938  lsymbol_release(tmpls);
939  }
940  else if ((tmpls = symbol_lookup_sym(lstate->task_struct_type,
941  "cred.uid",NULL))) {
942  lstate->task_uid_member_name = "cred.uid";
943  lstate->task_gid_member_name = "cred.gid";
944  lsymbol_release(tmpls);
945  }
946  else {
947  vwarn("could not find uid/gid info in struct task_struct;"
948  " no uid/gid thread context support!\n");
949  }
950 
951  /*
952  * Save the 'struct thread_struct' type.
953  */
954  tmpbs = target_lookup_sym(target,"struct thread_struct",NULL,NULL,
956  if (!tmpbs) {
957  vwarn("could not lookup 'struct thread_struct' in debuginfo;"
958  " no multithread support!\n");
959  /* This is not an error, so we don't return error -- it
960  * would upset target_open.
961  */
962  return 0;
963  }
964  lstate->thread_struct_type = bsymbol_get_symbol(tmpbs);
965  RHOLD(lstate->thread_struct_type,target);
966  bsymbol_release(tmpbs);
967  /* Now figure out if the member is esp/sp. */
968  if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"esp0",NULL))) {
969  lstate->thread_sp_member_name = "esp";
970  lstate->thread_sp0_member_name = "esp0";
971  lsymbol_release(tmpls);
972  }
973  else if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"sp",NULL))) {
974  lstate->thread_sp_member_name = "sp";
975  lstate->thread_sp0_member_name = "sp0";
976  lsymbol_release(tmpls);
977  }
978  else if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"rsp0",NULL))) {
979  lstate->thread_sp_member_name = "rsp";
980  lstate->thread_sp0_member_name = "rsp0";
981  lsymbol_release(tmpls);
982  }
983  else {
984  vwarn("could not find 'struct thread_struct.(esp0|sp|rsp0)';"
985  " will cause problems!\n");
986  lstate->thread_sp_member_name = NULL;
987  lstate->thread_sp0_member_name = NULL;
988  }
989 
990  /* Now figure out if thread_struct has an eip/ip member. */
991  if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"eip",NULL))) {
992  lstate->thread_ip_member_name = "eip";
993  lsymbol_release(tmpls);
994  }
995  else if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"ip",NULL))) {
996  lstate->thread_ip_member_name = "ip";
997  lsymbol_release(tmpls);
998  }
999  else if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"rip",NULL))) {
1000  lstate->thread_ip_member_name = "rip";
1001  lsymbol_release(tmpls);
1002  }
1003  else {
1004  lstate->thread_ip_member_name = NULL;
1005  }
1006 
1007  /*
1008  * Find out if thread_struct has ds/es (x86_64).
1009  */
1010  if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"es",NULL))) {
1011  lsymbol_release(tmpls);
1012  lstate->thread_struct_has_ds_es = 1;
1013  }
1014  else
1015  lstate->thread_struct_has_ds_es = 0;
1016 
1017  /*
1018  * Find out if thread_struct has fs (x86_64 only -- it's on the
1019  * pt_regs stack for i386).
1020  *
1021  * Also, gs is always in the thread_struct, as far as I can tell.
1022  */
1023  if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"fs",NULL))) {
1024  lsymbol_release(tmpls);
1025  lstate->thread_struct_has_fs = 1;
1026  }
1027  else
1028  lstate->thread_struct_has_fs = 0;
1029 
1030  /*
1031  * Find out if thread_struct has debugreg, debugreg0, or perf_event.
1032  */
1033  if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"debugreg",
1034  NULL))) {
1035  lsymbol_release(tmpls);
1036  lstate->thread_struct_has_debugreg = 1;
1037  }
1038  else
1039  lstate->thread_struct_has_debugreg = 0;
1040  if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"debugreg0",
1041  NULL))) {
1042  lsymbol_release(tmpls);
1043  lstate->thread_struct_has_debugreg0 = 1;
1044  }
1045  else
1046  lstate->thread_struct_has_debugreg0 = 0;
1047  if ((tmpls = symbol_lookup_sym(lstate->thread_struct_type,"ptrace_bps",
1048  NULL))) {
1049  lsymbol_release(tmpls);
1050  lstate->thread_struct_has_perf_debugreg = 1;
1051  }
1052  else
1053  lstate->thread_struct_has_perf_debugreg = 0;
1054 
1055  /*
1056  * Load in thread_info struct type.
1057  */
1058  thread_info_type = target_lookup_sym(target,"struct thread_info",
1059  NULL,NULL,SYMBOL_TYPE_FLAG_TYPE);
1060  if (!thread_info_type) {
1061  vwarn("could not lookup 'struct thread_info' in debuginfo;"
1062  " no multithread support!\n");
1063  /* This is not an error, so we don't return error -- it
1064  * would upset target_open.
1065  */
1066  return 0;
1067  }
1068  lstate->thread_info_type = bsymbol_get_symbol(thread_info_type);
1069  RHOLD(lstate->thread_info_type,target);
1070  bsymbol_release(thread_info_type);
1071 
1072  /*
1073  * Find out if thread_info has preempt_count or saved_preempt_count.
1074  */
1075  if ((tmpls = symbol_lookup_sym(lstate->thread_info_type,"saved_preempt_count",
1076  NULL))) {
1077  lsymbol_release(tmpls);
1078  lstate->thread_info_preempt_count_name = "saved_preempt_count";
1079  }
1080  else
1081  lstate->thread_info_preempt_count_name = "preempt_count";
1082 
1083  if (!(lstate->module_type = target_lookup_sym(target,"struct module",
1084  NULL,NULL,
1086  vwarn("could not lookup 'struct module'; no module debuginfo support!\n");
1087  }
1088  else if (!(lstate->modules = target_lookup_sym(target,"modules",NULL,NULL,
1090  vwarn("could not lookup modules; not updating modules list!\n");
1091  return 0;
1092  }
1093 
1094  /*
1095  * Lookup symbols for active probing, here, regardless of
1096  * target->spec->active_probe_flags ; user may change active probing
1097  * settings later!
1098  */
1099  if (!(lstate->module_free_symbol =
1100  target_lookup_sym(target,"module_free",NULL,NULL,
1101  SYMBOL_TYPE_NONE))) {
1102  vwarn("could not lookup module_free; active memory updates"
1103  " cannot function!\n");
1104  }
1105  else if (!(lstate->module_free_mod_symbol =
1106  target_lookup_sym(target,"module_free.mod",NULL,NULL,
1107  SYMBOL_TYPE_NONE))) {
1109  lstate->module_free_symbol = NULL;
1110 
1111  vwarn("could not lookup module_free.mod; active memory updates"
1112  " cannot function!\n");
1113  }
1114  else {
1115  VLS(target,tlctxt,"MODULE_STATE_LIVE",LOAD_FLAG_NONE,
1116  &lstate->MODULE_STATE_LIVE,NULL,err_vmiload_meminfo);
1118  "MODULE_STATE_LIVE = %d\n",lstate->MODULE_STATE_LIVE);
1119  VLS(target,tlctxt,"MODULE_STATE_COMING",LOAD_FLAG_NONE,
1120  &lstate->MODULE_STATE_COMING,NULL,err_vmiload_meminfo);
1122  "MODULE_STATE_COMING = %d\n",lstate->MODULE_STATE_COMING);
1123  VLS(target,tlctxt,"MODULE_STATE_GOING",LOAD_FLAG_NONE,
1124  &lstate->MODULE_STATE_GOING,NULL,err_vmiload_meminfo);
1126  "MODULE_STATE_GOING = %d\n",lstate->MODULE_STATE_GOING);
1127 
1128  if (0) {
1129  err_vmiload_meminfo:
1131  lstate->module_free_symbol = NULL;
1133  lstate->module_free_mod_symbol = NULL;
1134 
1135  vwarn("could not lookup MODULE_STATE_* var; active memory updates"
1136  " cannot function!\n");
1137  }
1138  }
1139 
1140 #ifdef APF_TE_COPY_PROCESS
1141  if (!(lstate->thread_entry_f_symbol =
1142  target_lookup_sym(target,"copy_process",NULL,NULL,
1143  SYMBOL_TYPE_NONE))) {
1144  vwarn("could not lookup copy_process;"
1145  " active thread entry updates cannot function!\n");
1146  }
1147  /*
1148  if (!(lstate->thread_entry_v_symbol =
1149  target_lookup_sym(target,"copy_process.p",NULL,NULL,
1150  SYMBOL_TYPE_NONE))) {
1151  bsymbol_release(lstate->thread_entry_f_symbol);
1152  lstate->thread_entry_f_symbol = NULL;
1153 
1154  vwarn("could not lookup copy_process.p;"
1155  " active thread entry updates might not function!\n");
1156  }
1157  */
1158 #else
1159  if (!(lstate->thread_entry_f_symbol =
1160  target_lookup_sym(target,"wake_up_new_task",NULL,NULL,
1161  SYMBOL_TYPE_NONE))) {
1162  vwarn("could not lookup wake_up_new_task;"
1163  " active thread entry updates cannot function!\n");
1164  }
1165  else if (!(lstate->thread_entry_v_symbol =
1166  target_lookup_sym(target,"wake_up_new_task.p",NULL,NULL,
1167  SYMBOL_TYPE_NONE))) {
1169  lstate->thread_entry_f_symbol = NULL;
1170 
1171  vwarn("could not lookup wake_up_new_task.p;"
1172  " active thread entry updates might not function!\n");
1173  }
1174 #endif
1175 
1176  if (!(lstate->thread_exit_f_symbol =
1177  target_lookup_sym(target,"sched_exit",NULL,NULL,
1178  SYMBOL_TYPE_NONE))) {
1179  vwarn("could not lookup sched_exit; trying __unhash_process!\n");
1180 
1181  if (!(lstate->thread_exit_f_symbol =
1182  target_lookup_sym(target,"__unhash_process",NULL,NULL,
1183  SYMBOL_TYPE_NONE))) {
1184  vwarn("could not lookup __unhash_process;"
1185  " active thread exit updates cannot function!\n");
1186  }
1187  else if (!(lstate->thread_exit_v_symbol =
1188  target_lookup_sym(target,"__unhash_process.p",NULL,NULL,
1189  SYMBOL_TYPE_NONE))) {
1191  lstate->thread_exit_f_symbol = NULL;
1192  vwarn("could not lookup __unhash_process.p;"
1193  " active thread exit updates cannot function!\n");
1194  }
1195  }
1196  else if (!(lstate->thread_exit_v_symbol =
1197  target_lookup_sym(target,"sched_exit.p",NULL,NULL,
1198  SYMBOL_TYPE_NONE))) {
1200  lstate->thread_exit_f_symbol = NULL;
1201  vwarn("could not lookup sched_exit.p;"
1202  " active thread exit updates cannot function!\n");
1203  }
1204 
1205  return 0;
1206 }
1207 
1209  struct addrspace *space;
1210  int rc = 0;
1211  GList *t1;
1212  struct os_linux_state *lstate = \
1213  (struct os_linux_state *)target->personality_state;
1214  struct bsymbol *tmpbs;
1215  char buf[128];
1216  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
1217  struct value *v;
1218  struct os_linux_thread_state *ltstate;
1219 
1220  if (g_hash_table_lookup(target->config,
1221  "OS_EMULATE_USERSPACE_EXCEPTIONS"))
1223 
1224  /*
1225  * Find swapper_pg_dir.
1226  */
1227  tmpbs = target_lookup_sym(target,"swapper_pg_dir",NULL,NULL,
1229  if (tmpbs) {
1230  if (target_bsymbol_resolve_base(target,tlctxt,tmpbs,&lstate->pgd_addr,NULL)) {
1231  vwarn("could not resolve addr of swapper_pg_dir!\n");
1232  }
1233  else {
1234  snprintf(buf,sizeof(buf),"0x%"PRIxADDR,lstate->pgd_addr);
1235  g_hash_table_insert(target->config,
1236  strdup("OS_KERNEL_PGD_ADDR"),strdup(buf));
1237  }
1238  bsymbol_release(tmpbs);
1239  tmpbs = NULL;
1240  }
1241  else {
1242  tmpbs = target_lookup_sym(target,"init_mm.pgd",NULL,NULL,
1244  if (tmpbs) {
1245  v = target_load_symbol(target,tlctxt,tmpbs,LOAD_FLAG_NONE);
1246 
1247  if (v) {
1248  lstate->pgd_addr = v_addr(v);
1250  "kernel pgd = 0x%"PRIxADDR"\n",lstate->pgd_addr);
1251  value_free(v);
1252  snprintf(buf,sizeof(buf),"0x%"PRIxADDR,lstate->pgd_addr);
1253  g_hash_table_insert(target->config,
1254  strdup("OS_KERNEL_PGD_ADDR"),strdup(buf));
1255  }
1256  else {
1257  lstate->pgd_addr = 0xffffffff81c0d000;
1258  }
1259 
1260  bsymbol_release(tmpbs);
1261  }
1262  }
1263 
1264  if (!tmpbs)
1265  vwarn("could not find 'swapper_pg_dir' nor 'init_mm.pgd';"
1266  " kernel v2p may fail!\n");
1267 
1268  /*
1269  * Propagate the pgd to the current thread! We loaded the current
1270  * thread in target_open(), but we haven't filled in pgd yet,
1271  * *potentially*, if the thread was a kernel thread! If it was a
1272  * user thread, it got filled in already.
1273  */
1274  if (target->current_thread && target->current_thread->personality_state) {
1275  ltstate = (struct os_linux_thread_state *) \
1277  if (ltstate->pgd == 0)
1278  ltstate->pgd = lstate->pgd_addr;
1279  }
1280 
1281  /*
1282  * If this is an x86_64 system, find the magic location in
1283  * __schedule() or schedule() where the new %rsp is swapped in
1284  * during context switch. See other comments when we load threads
1285  * below.
1286  */
1287  if (target->arch->wordsize == 8) {
1288  ADDR end = 0;
1289  lstate->schedule_addr = 0;
1290  lstate->schedule_swap_new_rsp_addr = 0;
1291  tmpbs = target_lookup_sym(target,"__schedule",NULL,NULL,
1293  if (tmpbs) {
1294  if (target_symbol_resolve_bounds(target,tlctxt,
1295  bsymbol_get_symbol(tmpbs),
1296  &lstate->schedule_addr,&end,
1297  NULL,NULL,NULL)) {
1299  "could not resolve bounds of __schedule!\n");
1300  }
1301  bsymbol_release(tmpbs);
1302  tmpbs = NULL;
1303  }
1304  if (lstate->schedule_addr == 0) {
1305  tmpbs = target_lookup_sym(target,"schedule",NULL,NULL,
1307  if (tmpbs) {
1308  if (target_symbol_resolve_bounds(target,tlctxt,
1309  bsymbol_get_symbol(tmpbs),
1310  &lstate->schedule_addr,
1311  &end,NULL,NULL,NULL)) {
1313  "could not resolve bounds of schedule!\n");
1314  }
1315  bsymbol_release(tmpbs);
1316  tmpbs = NULL;
1317  }
1318  }
1319  if (lstate->schedule_addr && end) {
1320  int len = end - lstate->schedule_addr;
1321  int caller_should_free = 0;
1322  unsigned char *cbuf;
1323 
1324  cbuf = target_load_code(target,lstate->schedule_addr,len,0,0,
1325  &caller_should_free);
1326  if (!cbuf) {
1328  "could not load code of __schedule/schedule();"
1329  " will not be able to load IP for sleeping threads!\n");
1330  }
1331  else {
1332  /*
1333  * Look for the 48 8b a6 instruction sub-part, which
1334  * moves something at x(%rsi) to %rsp . Fortunately
1335  * this always the way this instruction is!
1336  */
1337  int i = 0;
1338  while (i < len) {
1339  if (cbuf[i] == 0x48
1340  && (i + 1) < len && cbuf[i + 1] == 0x8b
1341  && (i + 2) < len && cbuf[i + 2] == 0xa6)
1342  break;
1343  ++i;
1344  }
1345  if (i < len) {
1346  lstate->schedule_swap_new_rsp_addr =
1347  lstate->schedule_addr + i;
1348 
1350  "found x86_64 schedule swap %%rsp instruction"
1351  " at 0x%"PRIxADDR"!\n",
1352  lstate->schedule_swap_new_rsp_addr);
1353  }
1354  }
1355  if (caller_should_free) {
1356  free(cbuf);
1357  }
1358  }
1359  }
1360 
1361  /*
1362  * If this is an x86_64 system, find ret_from_fork -- it is used for
1363  * the IP for newly created threads that are sleeping and haven't
1364  * been run yet.
1365  */
1366  if (target->arch->wordsize == 8) {
1367  tmpbs = target_lookup_sym(target,"ret_from_fork",NULL,NULL,
1369  if (tmpbs) {
1370  if (target_bsymbol_resolve_base(target,tlctxt,tmpbs,
1371  &lstate->ret_from_fork_addr,NULL)) {
1372  vwarn("could not resolve addr of ret_from_fork;"
1373  " will not be able to load IP for newly forked"
1374  " sleeping threads!\n");
1375  }
1376  bsymbol_release(tmpbs);
1377  tmpbs = NULL;
1378  }
1379  }
1380 
1381  /*
1382  * If this hypervisor just forwards userspace debug exceptions into
1383  * the guest, try to snag them so we can pass them to the overlay
1384  * handler! We assume that Linux's do_int3 and do_debug handlers
1385  * will never get called for kernel code; if they do, we may have
1386  * more work to do here.
1387  */
1388  if (target->writeable && lstate->hypervisor_ignores_userspace_exceptions) {
1389  ADDR start = 0;
1390  struct target_location_ctxt *tlctxt = NULL;
1391 
1392  tmpbs = target_lookup_sym(target,"do_int3",NULL,NULL,
1394  if (!tmpbs) {
1395  verror("could not lookup do_int3\n");
1396  goto uperr;
1397  }
1398 
1400 
1401  if (target_lsymbol_resolve_bounds(target,tlctxt,tmpbs->lsymbol,0,
1402  &start,NULL,NULL,NULL,NULL)) {
1403  verror("could not resolve base addr for symbol %s!\n",
1404  bsymbol_get_name(tmpbs));
1405  goto uperr;
1406  }
1407 
1408  lstate->int3_probe =
1409  probe_create(target,TID_GLOBAL,NULL,"do_int3__bp_emulate",
1410  os_linux_int3_handler,NULL,NULL,1,1);
1411  if (!lstate->int3_probe) {
1412  verror("could not probe do_int3\n");
1413  goto uperr;
1414  }
1415 
1416  if (!probe_register_addr(lstate->int3_probe,start,PROBEPOINT_BREAK,
1417  PROBEPOINT_SW,0,0,tmpbs)) {
1418  verror("could not probe addr 0x%"PRIxADDR" for do_int3\n",start);
1419  goto uperr;
1420  }
1421 
1422  bsymbol_release(tmpbs);
1423  tmpbs = NULL;
1424  target_location_ctxt_free(tlctxt);
1425  tlctxt = NULL;
1426 
1427  tmpbs = target_lookup_sym(target,"do_debug",NULL,NULL,
1429  if (!tmpbs) {
1430  verror("could not lookup do_debug\n");
1431  goto uperr;
1432  }
1433 
1435 
1436  if (target_lsymbol_resolve_bounds(target,tlctxt,tmpbs->lsymbol,0,
1437  &start,NULL,NULL,NULL,NULL)) {
1438  verror("could not resolve base addr for symbol %s!\n",
1439  bsymbol_get_name(tmpbs));
1440  goto uperr;
1441  }
1442 
1443  lstate->debug_probe =
1444  probe_create(target,TID_GLOBAL,NULL,"do_debug__bp_emulate",
1445  os_linux_debug_handler,NULL,NULL,1,1);
1446  if (!lstate->debug_probe) {
1447  verror("could not probe do_debug\n");
1448  goto uperr;
1449  }
1450 
1452  PROBEPOINT_SW,0,0,tmpbs)) {
1453  verror("could not probe addr 0x%"PRIxADDR" for do_debug\n",start);
1454  goto uperr;
1455  }
1456 
1457  bsymbol_release(tmpbs);
1458  tmpbs = NULL;
1459  target_location_ctxt_free(tlctxt);
1460  tlctxt = NULL;
1461 
1462 
1463 
1464  goto updone;
1465 
1466  uperr:
1467  verror("cannot handle userspace exceptions via do_int3/do_debug!\n");
1468  if (tmpbs)
1469  bsymbol_release(tmpbs);
1470  if (tlctxt)
1471  target_location_ctxt_free(tlctxt);
1472  if (lstate->int3_probe) {
1473  probe_free(lstate->int3_probe,1);
1474  lstate->int3_probe = NULL;
1475  }
1476  if (lstate->debug_probe) {
1477  probe_free(lstate->debug_probe,1);
1478  lstate->debug_probe = NULL;
1479  }
1480  updone:
1481  ;
1482  }
1483  else if (lstate->hypervisor_ignores_userspace_exceptions) {
1484  vwarn("target %s not writeable; cannot snag hypervisor-ignored"
1485  " userspace exceptions via emulation!\n",target->name);
1486  }
1487 
1488  v_g_list_foreach(target->spaces,t1,space) {
1489  rc |= os_linux_updateregions(target,space);
1490  }
1491 
1492  return rc;
1493 }
1494 
1496  struct probe *trigger,struct probe *base) {
1497  struct os_linux_state *lstate;
1498  struct os_linux_thread_state *ltstate;
1499  struct target *target;
1500  struct target_thread *tthread;
1501  struct target *overlay;
1502  REGVAL ip,eflags,realip;
1503  tid_t overlay_leader_tid;
1504  struct target_memmod *pmmod;
1505  int rc;
1506  ADDR paddr;
1507  target_status_t tstat;
1508 
1509  target = probe->target;
1510  tthread = target_load_thread(target,tid,0);
1511  lstate = (struct os_linux_state *)target->personality_state;
1512  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
1513 
1514  /*
1515  * Ugh, this is complicated. We have to handle physical address
1516  * breakpoints, because that's what the OS Process driver installs
1517  * (it does this because breakpoints in shared code pages are really
1518  * physical breakpoints; so we have to recognize when they are hit
1519  * in other processes, and "emulate" them
1520  */
1521 
1522  /*
1523  * Ok, we need to load IP and EFLAGS in THREAD_CTXT_USER, then push
1524  * a breakpoint notification to the overlay if EF_TF is not set in
1525  * EFLAGS; else push a singlestep notification.
1526  */
1527  if (target->arch->wordsize == 8) {
1529  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
1531  }
1532  else {
1534  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
1535  REG_X86_EFLAGS);
1536  }
1537 
1539  "tid %d at IP 0x%"PRIxADDR"; eflags=0x%"PRIxREGVAL"\n",
1540  tid,ip,eflags);
1541 
1542  /*
1543  * Find the overlay target; if we can't find one, it is a legit
1544  * exception in a process we're not attached to, or a bug!
1545  */
1546  overlay = target_lookup_overlay(target,tid);
1547  /* If we didn't find one, try to find its leader as an overlay. */
1548  if (!overlay) {
1549  overlay_leader_tid =
1550  target_os_thread_get_leader(target,tthread->tid);
1551  overlay = target_lookup_overlay(target,overlay_leader_tid);
1552  if (overlay) {
1554  "found yet-unknown thread %d with"
1555  " overlay leader %d; will notify!\n",
1556  tthread->tid,overlay_leader_tid);
1557  }
1558  else {
1560  "could not find overlay target for tid %d!\n",tid);
1561  }
1562  }
1563 
1564  /*
1565  * If there is an overlay, forward the exception up to it for handling.
1566  */
1567  if (overlay) {
1569  "user-mode breakpoint in overlay tid %"PRIiTID
1570  " (tgid %"PRIiTID") at 0x%"PRIxADDR"; eflags 0x%"PRIxREGVAL";"
1571  " passing to overlay!\n",
1572  tid,overlay->base_tid,ip,eflags);
1573  tstat = target_notify_overlay(overlay,EXCEPTION_BREAKPOINT,tid,ip,NULL);
1574  if (tstat == TSTATUS_ERROR)
1575  goto out_err_again;
1576  }
1577  else {
1578  /*
1579  * Else, find the physical mmod associated with the breaking IP, and
1580  * emulate it if there is one!
1581  */
1582  pmmod = NULL;
1583  realip = ip - target->arch->breakpoint_instrs_len;
1584  rc = target_addr_v2p(target,TID_GLOBAL,realip,&paddr);
1585  if (!rc)
1586  pmmod = target_memmod_lookup(target,TID_GLOBAL,paddr,1);
1587  if (!rc && pmmod) {
1588  /*
1589  * Emulate it!
1590  */
1592  "emulating debug memmod at bp for tid %"PRIiTID
1593  " at paddr 0x%"PRIxADDR" (vaddr 0x%"PRIxADDR")\n",
1594  tid,pmmod->addr,realip);
1595 
1596  if (target_os_emulate_bp_handler(target,tid,THREAD_CTXT_USER,pmmod)) {
1597  verror("could not emulate debug memmod for"
1598  " tid %"PRIiTID" at paddr 0x%"PRIxADDR"\n",
1599  tid,pmmod->addr);
1600  goto out_err_again;
1601  }
1602  }
1603  else {
1604  verror("user-mode debug event (not single step) at 0x%"PRIxADDR";"
1605  " eflags 0x%"PRIxREGVAL"; skipping handling!\n",
1606  realip,eflags);
1607  goto out_err_again;
1608  }
1609  }
1610 
1611  /*
1612  * Ok, we succeeded in handling this via the overlay target, or by
1613  * emulating the pmmod. Now we have to abort the interrupt handler!
1614  */
1615  struct action *action = action_return(0);
1616  if (!action) {
1617  verror("could not create return action!\n");
1618  }
1619  else if (action_sched(base,action,ACTION_ONESHOT,NULL,NULL)) {
1620  verror("could not schedule return action!\n");
1621  action_release(action);
1622  }
1623  else {
1624  vdebug(5,LA_TARGET,LF_OSLINUX,"scheduled return action\n");
1625  action_release(action);
1626  }
1627 
1628  out_bp_again:
1629  return RESULT_SUCCESS;
1630  out_err_again:
1631  return RESULT_SUCCESS;
1632 }
1633 
1635  struct probe *trigger,struct probe *base) {
1636  struct os_linux_state *lstate;
1637  struct os_linux_thread_state *ltstate;
1638  struct target *target;
1639  struct target_thread *tthread;
1640  struct target *overlay;
1641  REGVAL ip,eflags,realip;
1642  tid_t overlay_leader_tid;
1643  struct target_memmod *pmmod;
1644  int rc;
1645  ADDR paddr;
1646  target_status_t tstat;
1647 
1648  target = probe->target;
1649  tthread = target_load_thread(target,tid,0);
1650  lstate = (struct os_linux_state *)target->personality_state;
1651  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
1652 
1653  /*
1654  * Ok, we need to load IP and EFLAGS in THREAD_CTXT_USER, then push
1655  * a singlestep notification to the overlay.
1656  */
1657  if (target->arch->wordsize == 8) {
1659  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
1661  }
1662  else {
1664  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
1665  REG_X86_EFLAGS);
1666  }
1667 
1669  "tid %d at IP 0x%"PRIxADDR"; eflags=0x%"PRIxREGVAL"\n",
1670  tid,ip,eflags);
1671 
1672  /*
1673  * Find the overlay target; if we can't find one, it is a legit
1674  * exception in a process we're not attached to, or a bug!
1675  */
1676  overlay = target_lookup_overlay(target,tid);
1677  /* If we didn't find one, try to find its leader as an overlay. */
1678  if (!overlay) {
1679  overlay_leader_tid =
1680  target_os_thread_get_leader(target,tthread->tid);
1681  overlay = target_lookup_overlay(target,overlay_leader_tid);
1682  if (overlay) {
1684  "found yet-unknown thread %d with"
1685  " overlay leader %d; will notify!\n",
1686  tthread->tid,overlay_leader_tid);
1687  }
1688  else {
1690  "could not find overlay target for tid %d!\n",tid);
1691  }
1692  }
1693 
1694  /*
1695  * If there is an overlay, forward the exception up to it for handling.
1696  */
1697  if (overlay) {
1699  "user-mode breakpoint in overlay tid %"PRIiTID
1700  " (tgid %"PRIiTID") at 0x%"PRIxADDR"; eflags 0x%"PRIxREGVAL";"
1701  " passing to overlay!\n",
1702  tid,overlay->base_tid,ip,eflags);
1703  tstat = target_notify_overlay(overlay,EXCEPTION_SINGLESTEP,tid,ip,NULL);
1704  if (tstat == TSTATUS_ERROR)
1705  goto out_err_again;
1706  }
1707  else if (tthread->emulating_debug_mmod) {
1708  pmmod = tthread->emulating_debug_mmod;
1709 
1710  /*
1711  * Else, emulate the stepping mmod.
1712  */
1714  "emulating debug memmod at ss for tid %"PRIiTID
1715  " at paddr 0x%"PRIxADDR" (vaddr 0x%"PRIxADDR")\n",
1716  tid,pmmod->addr,ip);
1717 
1718  if (target_os_emulate_ss_handler(target,tid,THREAD_CTXT_USER,pmmod)) {
1719  verror("could not emulate debug memmod for"
1720  " tid %"PRIiTID" at paddr 0x%"PRIxADDR"\n",
1721  tid,pmmod->addr);
1722  goto out_err_again;
1723  }
1724  }
1725  else {
1726  verror("user-mode debug event (not single step) at 0x%"PRIxADDR";"
1727  " eflags 0x%"PRIxREGVAL"; skipping handling!\n",
1728  ip,eflags);
1729  goto out_err_again;
1730  }
1731 
1732  /*
1733  * Ok, we succeeded in handling this via the overlay target, or by
1734  * emulating the pmmod. Now we have to abort the interrupt handler!
1735  */
1736  struct action *action = action_return(0);
1737  if (!action) {
1738  verror("could not create return action!\n");
1739  }
1740  else if (action_sched(base,action,ACTION_ONESHOT,NULL,NULL)) {
1741  verror("could not schedule return action!\n");
1742  action_release(action);
1743  }
1744  else {
1745  vdebug(5,LA_TARGET,LF_OSLINUX,"scheduled return action\n");
1746  action_release(action);
1747  }
1748 
1749  out_bp_again:
1750  return RESULT_SUCCESS;
1751  out_err_again:
1752  return RESULT_SUCCESS;
1753 }
1754 
1756  struct os_linux_state *lstate;
1757  struct addrspace *space;
1758  GList *t1,*t2;
1759  struct target_thread *tthread;
1760  struct target_event *event;
1761  GHashTableIter iter;
1762  gpointer vp;
1763  struct os_linux_mm *olmm;
1764 
1765  lstate = (struct os_linux_state *)target->personality_state;
1766 
1767  /*
1768  * If not active probing memory, we kind of want to update our
1769  * addrspaces aggressively (by checking the module list) so that
1770  * if a user lookups a module symbol, we already have it.
1771  *
1772  * Active probing memory for the Xen target is a big win.
1773  */
1774  if (!(target->ap_flags & APF_OS_MEMORY)) {
1775  v_g_list_foreach(target->spaces,t1,space) {
1776  os_linux_updateregions(target,space);
1777  }
1778  }
1779 
1780  /*
1781  * If we are tracking thread exits, we have to nuke
1782  * "exiting" threads. See comments near
1783  * os_linux_active_thread_exit_handler .
1784  */
1785  if (target->ap_flags & APF_OS_THREAD_EXIT) {
1786  t1 = g_hash_table_get_values(target->threads);
1787  v_g_list_foreach(t1,t2,tthread) {
1788  if (!tthread->exiting)
1789  continue;
1790 
1791  if (tthread == target->current_thread) {
1793  "active-probed exiting thread %"PRIiTID" (%s)"
1794  " is still running; not deleting yet!\n",
1795  tthread->tid,tthread->name);
1796  }
1797  else {
1799  "active-probed exiting thread %"PRIiTID" (%s)"
1800  " can be deleted; doing it\n",
1801  tthread->tid,tthread->name);
1802  event = target_create_event(target,tthread,
1804  tthread);
1805  target_broadcast_event(target,event);
1806  target_detach_thread(target,tthread);
1807  }
1808  }
1809  g_list_free(t1);
1810  }
1811 
1812  /*
1813  * If we have loaded any process addrspaces, and if any of their
1814  * addrspaces are stale, clear stale spaces (they are stale if no
1815  * one is using them -- if their refcnt is 0, no
1816  * target_process->space holds them).
1817  */
1818  g_hash_table_iter_init(&iter,lstate->mm_addr_to_mm_cache);
1819  while (g_hash_table_iter_next(&iter,NULL,&vp)) {
1820  olmm = (struct os_linux_mm *)vp;
1821 
1822  if (olmm->space->refcnt == 1) {
1823  os_linux_mm_free(olmm);
1824  g_hash_table_iter_remove(&iter);
1825  }
1826  }
1827 
1828  return 0;
1829 }
1830 
1832  return TARGET_OS_TYPE_LINUX;
1833 }
1834 
1835 /*
1836  * Lookup version by reading either 'system_utsname.release' OR
1837  * 'init_uts_ns.release' (if namespace support). We don't care if we
1838  * read current's namespace uts struct or just init's uts namespace,
1839  * because we want the base OS's release. I don't even know if the
1840  * kernel allows a process's UTS namespace to override the release info :).
1841  */
1842 uint64_t os_linux_version(struct target *target) {
1843  uint64_t retval = 0;
1844  char *vstring;
1845  struct bsymbol *bs;
1846  struct value *v;
1847  char *endptr;
1848  char *next;
1849  unsigned long int vnum;
1850  uint64_t *sretval;
1851  struct target_location_ctxt *tlctxt;
1852 
1853  /* Check cache. */
1854  if ((vstring = (char *)target_gkv_lookup(target,"os_linux_version_string"))
1855  && (sretval = (uint64_t *)target_gkv_lookup(target,"os_linux_version")))
1856  return *sretval;
1857 
1858  /* Otherwise load it. */
1859  if (!(bs = target_lookup_sym(target,"system_utsname.release",NULL,NULL,
1861  && !(bs = target_lookup_sym(target,"init_uts_ns.name.release",NULL,NULL,
1863  verror("could not find system_utsname.release"
1864  " nor init_uts_ns.name.release!\n");
1865  return 0;
1866  }
1867  else {
1869  v = target_load_symbol(target,tlctxt,bs,LOAD_FLAG_NONE);
1870  if (!v) {
1871  verror("could not load %s!\n",
1873  target_location_ctxt_free(tlctxt);
1874  return 0;
1875  }
1876  target_location_ctxt_free(tlctxt);
1877  }
1878 
1879  bsymbol_release(bs);
1880 
1881  /* NULL out the last byte of the .release char array just to be safe */
1882  v->buf[v->bufsiz - 1] = '\0';
1883 
1884  /* Release is always, always <major>.<minor>.<patchlevel> */
1885  next = v->buf;
1886  endptr = NULL;
1887  vnum = strtoul(next,&endptr,10);
1888  if (endptr == next) {
1889  verror("could not determine major release number from '%s'!\n",v->buf);
1890  retval = 0;
1891  goto out;
1892  }
1893  retval |= vnum << 16;
1894 
1895  next = endptr + 1;
1896  endptr = NULL;
1897  vnum = strtoul(next,&endptr,10);
1898  if (endptr == next) {
1899  verror("could not determine minor release number from '%s'!\n",v->buf);
1900  retval = 0;
1901  goto out;
1902  }
1903  retval |= vnum << 8;
1904 
1905  next = endptr + 1;
1906  endptr = NULL;
1907  vnum = strtoul(next,&endptr,10);
1908  if (endptr == next) {
1909  verror("could not determine patchlevel release number from '%s'!\n",
1910  v->buf);
1911  retval = 0;
1912  goto out;
1913  }
1914  retval |= vnum;
1915 
1917  "version number %"PRIu64" (0x%"PRIx64") from '%s'",
1918  retval,v->buf);
1919 
1920  /* Cache it. */
1921  target_gkv_insert(target,"os_linux_version_string",strdup(v->buf),
1922  target_gkv_dtor_free);
1923  sretval = malloc(sizeof(*sretval));
1924  *(uint64_t *)sretval = retval;
1925  target_gkv_insert(target,"os_linux_version",sretval,
1926  target_gkv_dtor_free);
1927 
1928  out:
1929  bsymbol_release(bs);
1930  value_free(v);
1931 
1932  return retval;
1933 }
1934 
1935 int os_linux_version_cmp(struct target *target,uint64_t vers) {
1936  uint64_t ourvers;
1937 
1938  ourvers = target_os_version(target);
1939 
1940  if (ourvers == vers)
1941  return 0;
1942  else if (ourvers < vers)
1943  return -1;
1944  else
1945  return 1;
1946 }
1947 
1949  target_os_version(target);
1950  return (char *)target_gkv_lookup(target,"os_linux_version");
1951 }
1952 
1954  struct target_thread *tthread,ADDR *pgdp) {
1955  struct os_linux_thread_state *ltstate =
1956  (struct os_linux_thread_state *)tthread->personality_state;
1957 
1958  return target_addr_v2p(target,TID_GLOBAL,ltstate->pgd,pgdp);
1959 }
1960 
1962  struct target_thread *tthread) {
1963  struct os_linux_thread_state *ltstate =
1964  (struct os_linux_thread_state *)tthread->personality_state;
1965 
1966  if (ltstate->mm_addr)
1967  return 1;
1968  else
1969  return 0;
1970 }
1971 
1973  struct target_thread *tthread) {
1974  struct target_thread *leader;
1975  struct os_linux_state *lstate =
1976  (struct os_linux_state *)target->personality_state;
1977  struct os_linux_thread_state *ltstate =
1978  (struct os_linux_thread_state *)tthread->personality_state;
1979  struct array_list *tlist;
1980  struct os_linux_thread_state *tmp_ltstate;
1981  struct value *value;
1982  int i;
1983 
1984  /* The only reason this should be NULL is if it's a kernel thread. */
1985  if (!ltstate->task_struct) {
1987  "thread %d did not have a task struct value!\n",tthread->tid);
1988  return NULL;
1989  }
1990 
1991  /* If we are the group leader, return ourself. */
1992  if (value_addr(ltstate->task_struct) == ltstate->group_leader)
1993  return tthread;
1994 
1995  /*
1996  * Otherwise, see if our group_leader is already loaded, and return
1997  * it if it is.
1998  */
1999  tlist = target_list_threads(target);
2000  array_list_foreach(tlist,i,leader) {
2001  tmp_ltstate = (struct os_linux_thread_state *)leader->personality_state;
2002  if (tmp_ltstate->task_struct
2003  && value_addr(tmp_ltstate->task_struct) == ltstate->group_leader) {
2004  array_list_free(tlist);
2005  return leader;
2006  }
2007  }
2008  array_list_free(tlist);
2009  leader = NULL;
2010 
2011  /* Otherwise, load it. */
2013  "trying to load tid %d's group_leader at 0x%"PRIxADDR"\n",
2014  tthread->tid,ltstate->group_leader);
2015 
2016  value = target_load_type(target,lstate->task_struct_type,
2017  ltstate->group_leader,LOAD_FLAG_NONE);
2018  if (!value) {
2019  vwarn("could not load tid %d's group_leader at 0x%"PRIxADDR"; BUG?!\n",
2020  tthread->tid,ltstate->group_leader);
2021  return NULL;
2022  }
2023 
2024  if (!(leader = os_linux_load_thread_from_value(target,value))) {
2025  verror("could not load thread from task value at 0x%"PRIxADDR"; BUG?\n",
2026  ltstate->group_leader);
2027  value_free(value);
2028  return NULL;
2029  }
2030 
2032  "new group leader loaded (%"PRIiTID",%s)\n",
2033  leader->tid,leader->name);
2034 
2035  return leader;
2036 }
2037 
2039  int signo,void *data) {
2040  struct os_linux_thread_state *ltstate =
2041  (struct os_linux_thread_state *)tthread->personality_state;
2042  struct value *value;
2043  struct value *signal_v;
2044  struct value *signal_pending_v;
2045  struct value *signal_pending_signal_v;
2046  struct value *v;
2047  uint32_t sigmask = 1UL << (signo - 1);
2048  const char *signame;
2049  int rc;
2050 
2051  /* The only reason this should be NULL is if it's a kernel thread. */
2052  if (!ltstate->task_struct) {
2054  "thread %d did not have a task struct value!\n",tthread->tid);
2055  return -1;
2056  }
2057 
2058  value = ltstate->task_struct;
2059 
2060  signame = target_os_signal_to_name(target,signo);
2061 
2063  "sending %s (%d) to %d %s\n",
2064  signame,signo,tthread->tid,tthread->name);
2065 
2066  /* Load task_struct.signal (which is a struct signal_struct *) */
2067  signal_v = target_load_value_member(target,NULL,value,"signal",NULL,
2069  /* Load task_struct.signal->shared_pending */
2070  signal_pending_v =
2071  target_load_value_member(target,NULL,signal_v,"shared_pending",
2072  NULL,LOAD_FLAG_NONE);
2073  /* Load task-struct.signal->shared_pending.signal, which is
2074  * technically a struct containing an array, but we know what parts
2075  * of it to update, so we do it "raw".
2076  */
2077  signal_pending_signal_v =
2078  target_load_value_member(target,NULL,signal_pending_v,"signal",
2079  NULL,LOAD_FLAG_NONE);
2080  /* Set a pending SIGSTOP in the pending sigset. */
2081  if (value_update_zero(signal_pending_signal_v,(char *)&sigmask,
2082  sizeof(uint32_t))) {
2083  verror("could not setup pending signal %s (%d) to %d %s!\n",
2084  signame,signo,tthread->tid,tthread->name);
2085  value_free(signal_v);
2086  return -1;
2087  }
2088  rc = target_store_value(target,signal_pending_signal_v);
2089  value_free(signal_pending_signal_v);
2090  value_free(signal_pending_v);
2091  if (rc) {
2092  verror("could not store signal pending value; aborting!\n");
2093  value_free(signal_v);
2094  return -1;
2095  }
2096 
2097  /* Now, set some junk in the signal struct. We really should do
2098  * these three things for each thread in the process, but for now,
2099  * assume single-threaded processes!
2100  */
2101  /*
2102  * NB: don't set SIGNAL_GROUP_EXIT, after all. It interferes with
2103  * SIGSTOP delivery, and does not seem necessary for KILL. Although
2104  * maybe that's because I haven't tried to kill a multithreaded
2105  * program...
2106  */
2107  /*
2108 #define LOCAL_SIGNAL_GROUP_EXIT 0x00000008
2109  v = target_load_value_member(target,NULL,signal_v,"flags",NULL,
2110  LOAD_FLAG_NONE);
2111  value_update_u32(v,LOCAL_SIGNAL_GROUP_EXIT);
2112  target_store_value(target,v);
2113  value_free(v);
2114 
2115  v = target_load_value_member(target,NULL,signal_v,"group_exit_code",
2116  NULL,LOAD_FLAG_NONE);
2117  value_update_i32(v,signo);
2118  target_store_value(target,v);
2119  value_free(v);
2120  */
2121 
2122  v = target_load_value_member(target,NULL,signal_v,"group_stop_count",
2123  NULL,LOAD_FLAG_NONE);
2124  value_update_i32(v,0);
2125  rc = target_store_value(target,v);
2126  value_free(v);
2127  if (rc) {
2128  verror("could not store group stop count!\n");
2129  value_free(signal_v);
2130  return -1;
2131  }
2132 
2133  value_free(signal_v);
2134 
2135  signal_pending_signal_v =
2136  target_load_value_member(target,NULL,value,"pending.signal",NULL,
2138  if (value_update_zero(signal_pending_signal_v,(char *)&sigmask,
2139  sizeof(uint32_t))) {
2140  verror("could not setup pending signal %d!\n",signo);
2141  return -1;
2142  }
2143  rc = target_store_value(target,signal_pending_signal_v);
2144  value_free(signal_pending_signal_v);
2145 
2146  if (rc) {
2147  verror("could not store pending signal!\n");
2148  return -1;
2149  }
2150 
2151 #define LOCAL_TIF_SIGPENDING (1UL << 2)
2152 
2153  /*
2154  * Finally, set SIGPENDING in the task_struct's thread_info struct.
2155  *
2156  * NB: task_struct->thread_info is already loaded in ltstate -- so
2157  * just use it and let it get updated in thread flush at
2158  * target_resume!
2159  */
2161  OBJSDIRTY(tthread);
2162 
2163  return 0;
2164 }
2165 
2167  char *name;
2168  int signo;
2169 };
2170 
2171 static struct os_linux_signame sigmap[] = {
2172  { "SIGHUP",SIGHUP },
2173  { "SIGINT",SIGINT },
2174  { "SIGQUIT",SIGQUIT },
2175  { "SIGILL",SIGILL },
2176  { "SIGTRAP",SIGTRAP },
2177  { "SIGABRT",SIGABRT },
2178  { "SIGIOT",SIGIOT },
2179  { "SIGBUS",SIGBUS },
2180  { "SIGFPE",SIGFPE },
2181  { "SIGKILL",SIGKILL },
2182  { "SIGUSR1",SIGUSR1 },
2183  { "SIGSEGV",SIGSEGV },
2184  { "SIGUSR2",SIGUSR2 },
2185  { "SIGPIPE",SIGPIPE },
2186  { "SIGALRM",SIGALRM },
2187  { "SIGTERM",SIGTERM },
2188  { "SIGSTKFLT",SIGSTKFLT },
2189  { "SIGCLD",SIGCLD },
2190  { "SIGCHLD",SIGCHLD },
2191  { "SIGCONT",SIGCONT },
2192  { "SIGSTOP",SIGSTOP },
2193  { "SIGTSTP",SIGTSTP },
2194  { "SIGTTIN",SIGTTIN },
2195  { "SIGTTOU",SIGTTOU },
2196  { "SIGURG",SIGURG },
2197  { "SIGXCPU",SIGXCPU },
2198  { "SIGXFSZ",SIGXFSZ },
2199  { "SIGVTALRM",SIGVTALRM },
2200  { "SIGPROF",SIGPROF },
2201  { "SIGWINCH",SIGWINCH },
2202  { "SIGPOLL",SIGPOLL },
2203  { "SIGIO",SIGIO },
2204  { "SIGPWR",SIGPWR },
2205  { "SIGSYS",SIGSYS },
2206  { NULL,-1 },
2207 };
2208 
2209 const char *os_linux_signal_to_name(struct target *target,int signo) {
2210  unsigned int i;
2211 
2212  for (i = 0; i < sizeof(sigmap) / sizeof(struct os_linux_signame); ++i) {
2213  if (!sigmap[i].name)
2214  continue;
2215  if (sigmap[i].signo == signo)
2216  return sigmap[i].name;
2217  }
2218  return NULL;
2219 }
2220 
2221 int os_linux_signal_from_name(struct target *target,const char *name) {
2222  unsigned int i;
2223  unsigned int len;
2224  char *argcopy = NULL;
2225 
2226  if (isdigit(*name))
2227  return atoi(name);
2228 
2229  argcopy = strdup(name);
2230  len = strlen(argcopy);
2231  for (i = 0; i < len; ++i)
2232  argcopy[i] = toupper(argcopy[i]);
2233 
2234  for (i = 0; i < sizeof(sigmap) / sizeof(struct os_linux_signame); ++i) {
2235  if (!sigmap[i].name)
2236  continue;
2237  if (strcmp(sigmap[i].name,argcopy) == 0) {
2238  free(argcopy);
2239  return sigmap[i].signo;
2240  }
2241  }
2242 
2243  free(argcopy);
2244 
2245  /* Prepend SIG and try again. */
2246  len = strlen(name) + 3 + 1;
2247  argcopy = (char *)malloc(len);
2248  snprintf(argcopy,len,"SIG%s",name);
2249  for (i = 0; i < len; ++i)
2250  argcopy[i] = toupper(argcopy[i]);
2251  for (i = 0; i < sizeof(sigmap) / sizeof(struct os_linux_signame); ++i) {
2252  if (!sigmap[i].name)
2253  continue;
2254  if (strcmp(sigmap[i].name,argcopy) == 0) {
2255  free(argcopy);
2256  return sigmap[i].signo;
2257  }
2258  }
2259 
2260  free(argcopy);
2261  return -1;
2262 }
2263 
2264 static void __os_linux_syscalls_by_num_dtor(struct target *target,
2265  char *key,void *value) {
2266  GHashTableIter iter;
2267  gpointer vp;
2268  struct target_os_syscall *sc;
2269  GHashTable *syscalls_by_num;
2270 
2271  syscalls_by_num = (GHashTable *)value;
2272 
2273  if (syscalls_by_num) {
2274  g_hash_table_iter_init(&iter,syscalls_by_num);
2275  while (g_hash_table_iter_next(&iter,NULL,&vp)) {
2276  sc = (struct target_os_syscall *)vp;
2277 
2278  if (sc->bsymbol)
2279  bsymbol_release(sc->bsymbol);
2280  free(sc);
2281  }
2282 
2283  g_hash_table_destroy(syscalls_by_num);
2284  }
2285 }
2286 
2287 static void __os_linux_syscalls_by_X_dtor(struct target *target,
2288  char *key,void *value) {
2289  GHashTable *syscalls_by_X;
2290 
2291  syscalls_by_X = (GHashTable *)value;
2292 
2293  if (syscalls_by_X)
2294  g_hash_table_destroy(syscalls_by_X);
2295 }
2296 
2297 int os_linux_syscall_table_load(struct target *target) {
2298  struct bsymbol *bs = NULL;
2299  struct value *v = NULL;
2300  char *current;
2301  struct target_os_syscall *sc;
2302  GHashTable *syscalls_by_num;
2303  GHashTable *syscalls_by_addr;
2304  GHashTable *syscalls_by_name;
2305  char *name;
2306  char *wrapped_name;
2307  struct target_location_ctxt *tlctxt;
2308 
2309  if (target_gkv_lookup(target,"os_linux_syscalls_by_num"))
2310  return 0;
2311 
2312  /*
2313  * Lookup and load the value of sys_call_table. For x86[_64], the
2314  * values are the addresses of the system call functions. We look
2315  * up those addrs in debuginfo and fill in the tables.
2316  */
2317  bs = target_lookup_sym(target,"sys_call_table",NULL,NULL,
2319  if (!bs) {
2320  verror("could not lookup symbol sys_call_table!\n");
2321  return -1;
2322  }
2323 
2325  v = target_load_symbol(target,tlctxt,bs,LOAD_FLAG_NONE);
2326  if (!v) {
2327  verror("could not load sys_call_table!\n");
2328  target_location_ctxt_free(tlctxt);
2329  bsymbol_release(bs);
2330  bs = NULL;
2331  return -1;
2332  }
2333  target_location_ctxt_free(tlctxt);
2334 
2335  syscalls_by_num = g_hash_table_new(g_direct_hash,g_direct_equal);
2336  syscalls_by_addr = g_hash_table_new(g_direct_hash,g_direct_equal);
2337  syscalls_by_name = g_hash_table_new(g_str_hash,g_str_equal);
2338 
2339  /* Insert them all, but just set one with a
2340  * target_os_syscall_table_unload() wrapped as a gkv dtor to unload
2341  * them all.
2342  */
2343  target_gkv_insert(target,"os_linux_syscalls_by_num",syscalls_by_num,
2344  __os_linux_syscalls_by_num_dtor);
2345  target_gkv_insert(target,"os_linux_syscalls_by_name",syscalls_by_name,
2346  __os_linux_syscalls_by_X_dtor);
2347  target_gkv_insert(target,"os_linux_syscalls_by_addr",syscalls_by_addr,
2348  __os_linux_syscalls_by_X_dtor);
2349 
2350  /*
2351  * Go through the "array" according to target wordsize and lookup
2352  * the addrs.
2353  */
2354  current = v->buf;
2355  while ((current - v->buf) < v->bufsiz) {
2356  sc = calloc(1,sizeof(*sc));
2357  sc->num = (current - v->buf) / target->arch->wordsize;
2358  memcpy(&sc->addr,current,target->arch->wordsize);
2359 
2360  sc->bsymbol = target_lookup_sym_addr(target,sc->addr);
2361  if(sc->bsymbol) {
2362  name = bsymbol_get_name(sc->bsymbol);
2363  if (strncmp("stub_",name,strlen("stub_")) == 0) {
2364  wrapped_name = malloc(strlen(name));
2365  sprintf(wrapped_name,"sys_%s",name + strlen("stub_"));
2366  sc->wrapped_bsymbol =
2367  target_lookup_sym(target,wrapped_name,
2368  NULL,NULL,SYMBOL_TYPE_FLAG_FUNC);
2369  free(wrapped_name);
2370  }
2373  }
2374 
2375  g_hash_table_insert(syscalls_by_num,
2376  (gpointer)(uintptr_t)sc->num,sc);
2377  if (sc->addr)
2378  g_hash_table_insert(syscalls_by_addr,
2379  (gpointer)(uintptr_t)sc->addr,sc);
2380  if (sc->bsymbol && bsymbol_get_name(sc->bsymbol))
2381  g_hash_table_insert(syscalls_by_name,
2382  (gpointer)bsymbol_get_name(sc->bsymbol),sc);
2383 
2384  if (sc->bsymbol && bsymbol_get_name(sc->bsymbol))
2385  vdebug(9,LA_TARGET,LF_OSLINUX,"syscall '%s' num %d addr 0x%"PRIxADDR"\n",
2386  bsymbol_get_name(sc->bsymbol),sc->num,sc->addr);
2387  else
2388  vdebug(9,LA_TARGET,LF_OSLINUX,"syscall '' num %d addr 0x%"PRIxADDR"\n",
2389  sc->num,sc->addr);
2390 
2391  current += target->arch->wordsize;
2392  }
2393 
2394  value_free(v);
2395  bsymbol_release(bs);
2396 
2397  return 0;
2398 }
2399 
2400 int os_linux_syscall_table_unload(struct target *target) {
2401  GHashTableIter iter;
2402  gpointer vp;
2403  struct target_os_syscall *sc;
2404  GHashTable *syscalls_by_num;
2405  GHashTable *syscalls_by_addr;
2406  GHashTable *syscalls_by_name;
2407 
2408  syscalls_by_num = (GHashTable *) \
2409  target_gkv_lookup(target,"os_linux_syscalls_by_num");
2410  syscalls_by_name = (GHashTable *) \
2411  target_gkv_lookup(target,"os_linux_syscalls_by_name");
2412  syscalls_by_addr = (GHashTable *) \
2413  target_gkv_lookup(target,"os_linux_syscalls_by_addr");
2414 
2415  if (syscalls_by_name) {
2416  g_hash_table_destroy(syscalls_by_name);
2417  target_gkv_remove(target,"os_linux_syscalls_by_num");
2418  }
2419  if (syscalls_by_addr) {
2420  g_hash_table_destroy(syscalls_by_addr);
2421  target_gkv_remove(target,"os_linux_syscalls_by_addr");
2422  }
2423  if (syscalls_by_num) {
2424  g_hash_table_iter_init(&iter,syscalls_by_num);
2425  while (g_hash_table_iter_next(&iter,NULL,&vp)) {
2426  sc = (struct target_os_syscall *)vp;
2427 
2428  if (sc->bsymbol)
2429  bsymbol_release(sc->bsymbol);
2430  if (sc->args)
2431  g_slist_free(sc->args);
2432  free(sc);
2433  }
2434 
2435  g_hash_table_destroy(syscalls_by_num);
2436  target_gkv_remove(target,"os_linux_syscalls_by_num");
2437  }
2438 
2439  return 0;
2440 }
2441 
2442 GHashTable *os_linux_syscall_table_get(struct target *target) {
2443  return (GHashTable *)target_gkv_lookup(target,"os_linux_syscalls_by_num");
2444 }
2445 
2446 struct target_os_syscall *os_linux_syscall_lookup_name(struct target *target,
2447  char *name) {
2448  GHashTable *syscalls;
2449 
2450  if (!(syscalls = (GHashTable *) \
2451  target_gkv_lookup(target,"os_linux_syscalls_by_name"))) {
2452  if (target_os_syscall_table_load(target))
2453  return NULL;
2454  else
2455  syscalls = (GHashTable *) \
2456  target_gkv_lookup(target,"os_linux_syscalls_by_name");
2457  }
2458 
2459  return (struct target_os_syscall *)g_hash_table_lookup(syscalls,name);
2460 }
2461 
2462 struct target_os_syscall *os_linux_syscall_lookup_num(struct target *target,
2463  int num) {
2464  GHashTable *syscalls;
2465 
2466  if (!(syscalls = (GHashTable *) \
2467  target_gkv_lookup(target,"os_linux_syscalls_by_num"))) {
2468  if (target_os_syscall_table_load(target))
2469  return NULL;
2470  else
2471  syscalls = (GHashTable *) \
2472  target_gkv_lookup(target,"os_linux_syscalls_by_num");
2473  }
2474 
2475  return (struct target_os_syscall *) \
2476  g_hash_table_lookup(syscalls,(gpointer)(uintptr_t)num);
2477 }
2478 
2479 struct target_os_syscall *os_linux_syscall_lookup_addr(struct target *target,
2480  ADDR addr) {
2481  GHashTable *syscalls;
2482 
2483  if (!(syscalls = (GHashTable *) \
2484  target_gkv_lookup(target,"os_linux_syscalls_by_addr"))) {
2485  if (target_os_syscall_table_load(target))
2486  return NULL;
2487  else
2488  syscalls = (GHashTable *) \
2489  target_gkv_lookup(target,"os_linux_syscalls_by_addr");
2490  }
2491 
2492  return (struct target_os_syscall *) \
2493  g_hash_table_lookup(syscalls,(gpointer)(uintptr_t)addr);
2494 }
2495 
2496 
2497 
2502 
2503  /*
2504  * We support a couple kinds of syscall probing. Which is used is
2505  * up to the user. By passing NULL to linux_syscall_probe for the
2506  * @name param, they install probes on the syscall entry/exit path.
2507  * By passing a specific name to that function, they place a probe
2508  * on the entry of that specific syscall, as well as on the return
2509  * path.
2510  *
2511  * Hm, it is easier to probe specific syscalls by just probing them
2512  * internally (i.e., entry instr, and any RET/IRET instrs). Also a
2513  * faster option than probing on the generic syscall ret path. I
2514  * think we can have three options for now:
2515  *
2516  * 1) probe a single syscall only
2517  * 2) probe a single syscall, but probe the generic return path
2518  * 3) probe the generic entry/exit paths
2519  */
2522 
2523  /* Currently in-flight syscall per-thread. */
2525  /* Last completed syscall per-thread. */
2527 };
2528 
2529 static int _os_linux_syscall_probe_init(struct target *target) {
2530  struct bsymbol *system_call_bsymbol = NULL;
2531  ADDR system_call_base_addr = 0;
2532  ADDR end = 0;
2533  struct array_list *system_call_ret_idata_list = NULL;
2534  int caller_should_free = 0;
2535  ADDR *ap;
2536  unsigned char *cbuf = NULL;
2537  struct target_location_ctxt *tlctxt = NULL;
2538 
2539  /* Check cache to see if we've loaded symbol and code info. */
2540  if (target_gkv_lookup(target,"os_linux_system_call_bsymbol"))
2541  return 0;
2542 
2543  /*
2544  * Do some setup for probing syscall returns. We disasm
2545  * system_call, placing probes on IRET and SYSRET; so we need the
2546  * offsets of any of those instructions within system_call.
2547  */
2548 
2549  system_call_bsymbol = target_lookup_sym(target,"system_call",NULL,NULL,
2551  if (!system_call_bsymbol) {
2552  verror("could not lookup system_call;"
2553  " smart syscall probing will fail!\n");
2554  goto errout;
2555  }
2557  system_call_bsymbol);
2558  if (target_lsymbol_resolve_bounds(target,tlctxt,
2559  system_call_bsymbol->lsymbol,0,
2560  &system_call_base_addr,&end,NULL,
2561  NULL,NULL)) {
2562  verror("could not resolve base addr of system_call;"
2563  " smart syscall probing will fail!\n");
2564  goto errout;
2565  }
2566  target_location_ctxt_free(tlctxt);
2567  tlctxt = NULL;
2568  if (!(cbuf = target_load_code(target,system_call_base_addr,
2569  end - system_call_base_addr,
2570  0,0,&caller_should_free))) {
2571  verror("could not load code of system_call;"
2572  " smart syscall probing will fail!\n");
2573  goto errout;
2574  }
2577  cbuf,end - system_call_base_addr,
2578  &system_call_ret_idata_list,
2579  system_call_base_addr,1)) {
2580  verror("could not disassemble system_call in range"
2581  " 0x%"PRIxADDR"-0x%"PRIxADDR";"
2582  " smart syscall probing will fail!\n",
2583  system_call_base_addr,end);
2584  goto errout;
2585  }
2586  if (!system_call_ret_idata_list
2587  || array_list_len(system_call_ret_idata_list) <= 0) {
2588  verror("no IRETs or SYSRETs in system_call;"
2589  " smart syscall probing will fail!\n");
2590  goto errout;
2591  }
2592 
2593  /* Cache. */
2594  target_gkv_insert(target,"os_linux_system_call_bsymbol",system_call_bsymbol,
2595  target_gkv_dtor_bsymbol);
2596  ap = calloc(1,sizeof(*ap));
2597  memcpy(ap,&system_call_base_addr,sizeof(*ap));
2598  target_gkv_insert(target,"os_linux_system_call_base_addr",ap,
2599  target_gkv_dtor_free);
2600  target_gkv_insert(target,"os_linux_system_call_ret_idata_list",
2601  system_call_ret_idata_list,
2602  target_gkv_dtor_alist_deep_free);
2603 
2604  return 0;
2605 
2606  errout:
2607  if (tlctxt)
2608  target_location_ctxt_free(tlctxt);
2609  if (system_call_ret_idata_list)
2610  array_list_deep_free(system_call_ret_idata_list);
2611  if (caller_should_free)
2612  free(cbuf);
2613  if (system_call_bsymbol)
2614  bsymbol_release(system_call_bsymbol);
2615 
2616  return -1;
2617 }
2618 
2619 #define SYSCALL_GLOBAL_ENTRY_PROBE "os_linux_syscall_entry_probe"
2620 #define SYSCALL_GLOBAL_RET_PROBE "os_linux_syscall_ret_probe"
2621 
2622 static int __global_entry_uncache(struct probe *probe) {
2623  /* Steal it cause it is getting autofreed if this is getting called. */
2625  return 0;
2626 }
2627 static struct probe_ops __global_entry_probe_ops = {
2628  .fini = __global_entry_uncache,
2629 };
2630 
2631 static int __syscall_entry_uncache(struct probe *probe) {
2632  /* Steal it cause it is getting autofreed if this is getting called. */
2633  target_gkv_steal(probe->target,probe->name);
2634  return 0;
2635 }
2636 static struct probe_ops __syscall_entry_probe_ops = {
2637  .fini = __syscall_entry_uncache,
2638 };
2639 
2640 static int __global_ret_uncache(struct probe *probe) {
2641  /* Steal it cause it is getting autofreed if this is getting called. */
2643  return 0;
2644 }
2645 static struct probe_ops __global_ret_probe_ops = {
2646  .fini = __global_ret_uncache,
2647 };
2648 
2649 /* "overload" probe_do_sink_pre_handlers . */
2650 static result_t __syscall_entry_handler(struct probe *probe,tid_t tid,
2651  void *handler_data,
2652  struct probe *trigger,
2653  struct probe *base) {
2654  struct target *target;
2655  struct target_os_syscall *syscall;
2656  struct target_os_syscall_state *scs;
2657  GSList *args;
2658  struct array_list *argvals;
2659  GSList *gsltmp;
2660  struct symbol *argsym;
2661  struct symbol *symbol;
2662  struct value *v;
2663  char *name;
2664  struct target_location_ctxt *tlctxt;
2665 
2666  target = probe->target;
2667 
2668  syscall = (struct target_os_syscall *)handler_data;
2669 
2670  scs = target_os_syscall_record_entry(target,tid,syscall);
2671  if (!scs) {
2672  verror("could not record syscall entry in tid %"PRIiTID"!\n",tid);
2673  return RESULT_SUCCESS;
2674  }
2675 
2676  /*
2677  * The only values we try to autoderef are char * bufs; we need
2678  * system-specific info to know if/when it's safe to deref the
2679  * others. We don't know generically whether a param is in/out.
2680  */
2681  argvals = array_list_create(6);
2682  symbol = bsymbol_get_symbol(probe->bsymbol);
2684  if (args) {
2685  /*
2686  * Load each argument if it hasn't already been loaded.
2687  */
2689  probe->thread->tid,
2690  probe->bsymbol);
2691  v_g_slist_foreach(args,gsltmp,argsym) {
2692  name = symbol_get_name(argsym);
2693  v = target_load_symbol_member(probe->target,tlctxt,probe->bsymbol,
2694  name,NULL,LOAD_FLAG_AUTO_DEREF);
2695  array_list_append(argvals,v);
2696  }
2697  target_location_ctxt_free(tlctxt);
2698  }
2699 
2700  target_os_syscall_record_argv(target,tid,NULL,argvals);
2701 
2702  /* There, now call the probe's sink pre_handlers! */
2703  return probe_do_sink_pre_handlers(probe,tid,handler_data,trigger,base);
2704 }
2705 
2706 /* "overload" probe_do_sink_pre_handlers . */
2707 static result_t __global_entry_handler(struct probe *probe,tid_t tid,
2708  void *handler_data,
2709  struct probe *trigger,
2710  struct probe *base) {
2711  struct target *target;
2712  struct target_os_syscall *syscall;
2713  struct target_os_syscall_state *scs;
2714  REGVAL scnum;
2715  struct array_list *regvals;
2716  struct array_list *argvals;
2717  GSList *gsltmp;
2718  struct symbol *argsym;
2719  struct symbol *datatype;
2720  int j;
2721  /*
2722  * On x86_64 Linux, syscall args are in %rdi, %rsi, %rdx, %r10, %r8, %r9.
2723  */
2724  static REG regs_x86_64[6] = { 5,4,1,10,8,9 };
2725  /*
2726  * On i386 Linux, syscall args are in %ebx, %ecx, %edx, %esi, %edi, %ebp.
2727  */
2728  static REG regs_i386[6] = { 3,1,2,6,7,5 };
2729  static REG *regs;
2730  struct value *v;
2731 
2732  target = probe->target;
2733 
2734  scnum = target_read_creg(target,tid,CREG_RET);
2735  if (!(syscall = target_os_syscall_lookup_num(target,(int)scnum))) {
2736  vwarn("could not find syscall for eax %d in tid %"PRIiTID"; ignoring!\n",
2737  (int)scnum,tid);
2738  return RESULT_SUCCESS;
2739  }
2740 
2741  scs = target_os_syscall_record_entry(target,tid,syscall);
2742  if (!scs) {
2743  verror("could not record syscall entry in tid %"PRIiTID"!\n",tid);
2744  return RESULT_SUCCESS;
2745  }
2746 
2747  /*
2748  * Read args by type, according to the i386/x86_64 calling
2749  * convention. Never more than 6 args; just read all the regs.
2750  */
2751  if (target->arch->wordsize == 8)
2752  regs = regs_x86_64;
2753  else
2754  regs = regs_i386;
2755 
2756  regvals = array_list_create(6);
2757  for (j = 0; j < 6; ++j) {
2758  array_list_append(regvals,
2759  (void *)(uintptr_t)target_read_reg(target,tid,regs[j]));
2760  }
2761 
2762  /*
2763  * The only values we try to autoderef are char * bufs; we need
2764  * system-specific info to know if/when it's safe to deref the
2765  * others. We don't know generically whether a param is in/out.
2766  */
2767  if (syscall->args) {
2768  argvals = array_list_create(6);
2769  v_g_slist_foreach(syscall->args,gsltmp,argsym) {
2770  datatype = symbol_get_datatype(argsym);
2771  if (!datatype) {
2772  array_list_append(argvals,NULL);
2773  continue;
2774  }
2775  else if (j < 6) {
2776  v = target_load_type_regval(target,datatype,tid,regs[j],
2777  (REGVAL)array_list_item(regvals,j),
2779  array_list_append(argvals,v);
2780  }
2781  else {
2782  array_list_append(argvals,NULL);
2783  continue;
2784  }
2785  }
2786  }
2787  else
2788  argvals = NULL;
2789 
2790  target_os_syscall_record_argv(target,tid,regvals,argvals);
2791 
2792  /* There, now call the probe's sink pre_handlers! */
2793  return probe_do_sink_pre_handlers(probe,tid,handler_data,trigger,base);
2794 }
2795 
2796 /*
2797  * Call the sink probes' post_handlers -- but we do it BEFORE the return
2798  * to userspace -- i.e., before the IRET/SYSRET.
2799  */
2800 static result_t __syscall_ret_handler(struct probe *probe,tid_t tid,
2801  void *handler_data,
2802  struct probe *trigger,
2803  struct probe *base) {
2804  struct target *target;
2805  struct target_os_syscall_state *scs;
2806 
2807  target = probe->target;
2808 
2809  scs = target_os_syscall_probe_last(target,tid);
2810  if (!scs) {
2812  "could not find a current syscall tid %"PRIiTID"; ignoring!\n",
2813  tid);
2814  return RESULT_SUCCESS;
2815  }
2816  else if (scs->returned) {
2818  "current syscall for tid %"PRIiTID" already returned; ignoring!\n",
2819  tid);
2820  return RESULT_SUCCESS;
2821  }
2822 
2824  target_read_creg(target,tid,CREG_RET));
2825 
2826  /* There, now call the probe's sink POST_handlers! */
2827  return probe_do_sink_post_handlers(probe,tid,handler_data,trigger,base);
2828 }
2829 
2830 static struct probe *
2831 os_linux_syscall_probe_init_syscall_entry(struct target *target,
2832  struct target_os_syscall *syscall) {
2833  struct probe *probe;
2834  char namebuf[128];
2835  struct bsymbol *bs;
2836 
2837  if (syscall->isstub && syscall->wrapped_bsymbol)
2838  bs = syscall->wrapped_bsymbol;
2839  else
2840  bs = syscall->bsymbol;
2841 
2842  snprintf(namebuf,sizeof(namebuf),"os_linux_%s_probe",
2843  bsymbol_get_name(bs));
2844 
2845  if ((probe = (struct probe *)target_gkv_lookup(target,namebuf)))
2846  return probe;
2847 
2848  if (_os_linux_syscall_probe_init(target))
2849  return NULL;
2850 
2851  probe = probe_create(target,TID_GLOBAL,&__syscall_entry_probe_ops,
2852  namebuf,__syscall_entry_handler,NULL,syscall,1,1);
2853 
2854  if (!probe_register_symbol(probe,bs,PROBEPOINT_SW,0,0)) {
2855  verror("could not register %s!\n",namebuf);
2856  probe_free(probe,0);
2857  return NULL;
2858  }
2859 
2860  /* Cache it. */
2861  target_gkv_insert(target,namebuf,probe,target_gkv_dtor_probe);
2862 
2863  return probe;
2864 }
2865 
2866 static struct probe *
2867 os_linux_syscall_probe_init_global_entry(struct target *target) {
2868  struct bsymbol *system_call_bsymbol;
2869  ADDR *system_call_base_addr = NULL;
2870  struct probe *probe;
2871 
2872  if ((probe = (struct probe *) \
2874  return probe;
2875 
2876  if (_os_linux_syscall_probe_init(target))
2877  return NULL;
2878 
2879  system_call_base_addr = (ADDR *) \
2880  target_gkv_lookup(target,"os_linux_system_call_base_addr");
2881  system_call_bsymbol = (struct bsymbol *) \
2882  target_gkv_lookup(target,"os_linux_system_call_bsymbol");
2883 
2884  /*
2885  * Place probes on all entries. For some Linux kernels, this might
2886  * be system_call, AND ia32_sysenter_target (for i386).
2887  *
2888  * XXX: do ia32_sysenter_target eventually!
2889  */
2890 
2891  probe = probe_create(target,TID_GLOBAL,&__global_entry_probe_ops,
2892  "system_call",
2893  __global_entry_handler,NULL,NULL,1,1);
2894 
2895  if (!probe_register_addr(probe,*system_call_base_addr,
2897  system_call_bsymbol)) {
2898  verror("could not register system_call entry probe at 0x%"PRIxADDR"!\n",
2899  *system_call_base_addr);
2900  probe_free(probe,0);
2901  return NULL;
2902  }
2903 
2904  /* Cache it. */
2906  target_gkv_dtor_probe);
2907 
2908  return probe;
2909 }
2910 
2911 static struct probe *
2912 os_linux_syscall_probe_init_global_ret(struct target *target) {
2913  ADDR *system_call_base_addr;
2914  struct array_list *system_call_ret_idata_list;
2915  struct probe *rprobe;
2916  struct probe *probe;
2917  int name_len;
2918  char *name;
2919  struct cf_inst_data *idata;
2920  int i;
2921 
2922  if ((probe = (struct probe *) \
2924  return probe;
2925 
2926  if (_os_linux_syscall_probe_init(target))
2927  return NULL;
2928 
2929  if (!(system_call_ret_idata_list = (struct array_list *) \
2930  target_gkv_lookup(target,"os_linux_system_call_ret_idata_list"))) {
2931  verror("BUG: could not find system_call RET info!\n");
2932  return NULL;
2933  }
2934 
2935  system_call_base_addr = (ADDR *) \
2936  target_gkv_lookup(target,"os_linux_system_call_base_addr");
2937 
2938  /*
2939  * Place probes on all IRET/SYSRET/SYSEXIT in system_call, and
2940  * register our probe to listen to them.
2941  */
2942 
2943  probe = probe_create(target,TID_GLOBAL,&__global_ret_probe_ops,
2944  "system_call_ret",
2945  NULL,__syscall_ret_handler,NULL,1,1);
2946 
2947  /*
2948  * Create probes one by one on the IRET/SYSRETs and register
2949  * the metaprobe on them.
2950  */
2951  name_len = sizeof("system_call_SYSRET_+") + 12;
2952  name = malloc(name_len);
2953 
2954  array_list_foreach(system_call_ret_idata_list,i,idata) {
2955  snprintf(name,name_len,
2956  "system_call_%s_+0x%"PRIxOFFSET,
2957  (idata->type == INST_SYSRET) ? "SYSRET" : "IRET",
2958  idata->offset);
2959 
2960  /* We call any sink probes' POST handlers from our pre_handler. */
2961  rprobe = probe_create(target,TID_GLOBAL,NULL,name,
2962  probe_do_sink_post_handlers,NULL,NULL,1,1);
2963 
2964  if (!probe_register_addr(rprobe,*system_call_base_addr + idata->offset,
2965  PROBEPOINT_BREAK,PROBEPOINT_SW,0,0,NULL)) {
2966  verror("could not register probe %s at 0x%"PRIxADDR"!\n",
2967  rprobe->name,*system_call_base_addr + idata->offset);
2968  probe_free(rprobe,1);
2969  rprobe = NULL;
2970  goto errout;
2971  }
2972 
2973  if (!probe_register_source(probe,rprobe)) {
2974  verror("could not register probe %s on source %s!\n",
2975  probe->name,rprobe->name);
2976  probe_free(rprobe,1);
2977  rprobe = NULL;
2978  goto errout;
2979  }
2980  }
2981 
2982  /* Cache it. */
2984  target_gkv_dtor_probe);
2985  free(name);
2986  return probe;
2987 
2988  errout:
2989  probe_free(probe,1);
2990  free(name);
2991  return NULL;
2992 }
2993 
2994 /*
2995  * Syscall probe type.
2996  *
2997  * These per-syscall probes are exposed to the probe value API.
2998  *
2999  * The global probe is not, for now, because it doesn't have a fixed
3000  * bsymbol backing it; it changes. Also, since we're not technically
3001  * *in* the bsymbol (syscall) when the pre/post handlers are called
3002  * (we're in the global sysenter/sysret path), faking it could result in
3003  * some oddities!
3004  */
3005 static const char *_os_linux_syscall_probe_gettype(struct probe *probe) {
3006  return "os_linux_syscall_probe";
3007 }
3008 
3010  .gettype = _os_linux_syscall_probe_gettype,
3011 
3012  .summarize = target_os_syscall_probe_summarize,
3013  .summarize_tid = target_os_syscall_probe_summarize_tid,
3014 
3015  .get_value_table = probe_value_get_table_function_ee,
3016  .get_raw_value_table = probe_value_get_raw_table_function_ee,
3017  .get_last_value_table = probe_value_get_last_table_function_ee,
3018  .get_last_raw_value_table = probe_value_get_last_raw_table_function_ee,
3019  .get_value = probe_value_get_function_ee,
3020  .get_raw_value = probe_value_get_raw_function_ee,
3021  .get_last_value = probe_value_get_last_function_ee,
3022  .get_last_raw_value = probe_value_get_last_raw_function_ee,
3023  .values_notify_phase = probe_value_notify_phase_function_ee,
3024  .values_free = probe_values_free_stacked,
3025 };
3026 
3027 struct probe *os_linux_syscall_probe(struct target *target,tid_t tid,
3028  struct target_os_syscall *syscall,
3031  void *handler_data) {
3032  struct probe *probe, *eprobe, *rprobe;
3033  char namebuf[128];
3034 
3035  snprintf(namebuf,sizeof(namebuf),"os_linux_syscall_probe_%s",
3036  bsymbol_get_name(syscall->bsymbol));
3037 
3038  probe = probe_create(target,tid,&os_linux_syscall_ret_probe_ops,
3039  namebuf,pre_handler,post_handler,handler_data,0,1);
3040 
3041  eprobe = os_linux_syscall_probe_init_syscall_entry(target,syscall);
3042  if (!eprobe) {
3043  verror("could not setup syscall entry probe!\n");
3044  probe_free(probe,1);
3045  return NULL;
3046  }
3047  probe_register_source(probe,eprobe);
3048  rprobe = os_linux_syscall_probe_init_global_ret(target);
3049  if (!rprobe) {
3050  verror("could not setup global system_call ret probes!\n");
3051  probe_free(probe,1);
3052  return NULL;
3053  }
3054  probe_register_source(probe,rprobe);
3055 
3056  return probe;
3057 }
3058 
3059 /*
3060  * Global syscall probe type.
3061  */
3062 static const char *_os_linux_global_syscall_probe_gettype(struct probe *probe) {
3063  return "os_linux_global_syscall_probe";
3064 }
3065 
3067  .gettype = _os_linux_global_syscall_probe_gettype,
3068 
3069  .summarize = target_os_syscall_probe_summarize,
3070  .summarize_tid = target_os_syscall_probe_summarize_tid,
3071 };
3072 
3073 struct probe *os_linux_syscall_probe_all(struct target *target,tid_t tid,
3076  void *handler_data) {
3077  struct probe *probe, *eprobe, *rprobe;
3078 
3079  probe = probe_create(target,tid,&os_linux_global_syscall_ret_probe_ops,
3080  "os_linux_global_syscall_probe",
3081  pre_handler,post_handler,handler_data,0,1);
3082 
3083  eprobe = os_linux_syscall_probe_init_global_entry(target);
3084  if (!eprobe) {
3085  verror("could not setup global system_call entry probes!\n");
3086  probe_free(probe,1);
3087  return NULL;
3088  }
3089  probe_register_source(probe,eprobe);
3090  rprobe = os_linux_syscall_probe_init_global_ret(target);
3091  if (!rprobe) {
3092  verror("could not setup global system_call ret probes!\n");
3093  probe_free(probe,1);
3094  return NULL;
3095  }
3096  probe_register_source(probe,rprobe);
3097 
3098  return probe;
3099 }
3100 
3101 num_t os_linux_get_preempt_count(struct target *target) {
3102  struct target_thread *tthread;
3103  struct os_linux_thread_state *ltstate;
3104 
3105  tthread = target->current_thread;
3106  if (!tthread) {
3107  verror("no current thread!\n");
3108  errno = EINVAL;
3109  return 0;
3110  }
3111  else if (!OBJVALID(tthread)) {
3112  verror("current thread not valid; forgot to load it?\n");
3113  errno = EINVAL;
3114  return 0;
3115  }
3116 
3117  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
3118  if (!ltstate) {
3119  verror("no personality info for thread %d!\n",tthread->tid);
3120  errno = EINVAL;
3121  return 0;
3122  }
3123 
3124  return ltstate->thread_info_preempt_count;
3125 }
3126 
3127 static int os_linux_current_gs(struct target *target,REGVAL *gs) {
3128  struct os_linux_state *lstate =
3129  (struct os_linux_state *)target->personality_state;
3130  REGVAL rgb,gbk,gb,gbu;
3131 
3132  rgb = gbk = gb = gbu = 0;
3133 
3134  vdebug(9,LA_TARGET,LF_OSLINUX,"reading current %%gs\n");
3135 
3136  /*
3137  * Try to read Xen's special kernel gs base.
3138  */
3140 
3141  if (!rgb) {
3142  /*
3143  * If that doesn't work, read the vanilla one.
3144  */
3145  rgb = gb = target_read_reg(target,TID_GLOBAL,REG_X86_64_GS_BASE);
3146 
3147  /*
3148  * Finally, if that doesn't work, try Xen's special user gs base;
3149  * if that *does* work, check ipval and sanitize -- we need
3150  * a kernel gs base.
3151  */
3152  if (!rgb) {
3154  if (rgb) {
3155  REGVAL ipval,spval;
3156 
3157  ipval = target_read_reg(target,TID_GLOBAL,target->ipregno);
3158  spval = target_read_reg(target,TID_GLOBAL,target->spregno);
3159 
3160  if (!(ipval < lstate->kernel_start_addr
3161  || spval < lstate->kernel_start_addr)) {
3163  "doctoring gs to 0 because ip/sp is usermode\n");
3164  rgb = gb = 0;
3165  }
3166  }
3167  }
3168  }
3169 
3170  vdebug(9,LA_TARGET,LF_OSLINUX,"current %%gs = 0x%"PRIxREGVAL"\n",rgb);
3171 
3172  if (gs)
3173  *gs = rgb;
3174 
3175  if (!rgb) {
3176  vwarn("invalid gs_base_kernel=0x%"PRIxADDR"/gs_base_user=0x%"PRIxADDR
3177  "/gs_base=0x%"PRIxADDR"; cannot get percpu data and current thread!\n",
3178  gbk,gbu,gb);
3179  }
3180 
3181  return 0;
3182 }
3183 
3184 /*
3185  * For i386/x86:
3186  *
3187  * The bottom of each kernel stack has the thread_info struct; the first
3188  * pointer in the thread info struct is to the task_struct associated
3189  * with the thread_info (i.e., thread_info->task). So, if we want
3190  * thread_info, just load the value at current_thread_ptr; if we want
3191  * the current task_struct, load the first pointer at
3192  * current_thread_ptr, and deref it to load the current task_struct's
3193  * value.
3194  *
3195  * For x86_64:
3196  *
3197  * kernel_stacks are always per_cpu unsigned long "pointers", even if
3198  * there is only one CPU. The value of
3199  * lstate->kernel_stack_percpu_offset is an offset from the kernel's
3200  * %gs. So we have to grab the saved %gs (which Xen places in
3201  * target->global_thread->personality_state->context.gs_base_kernel), then apply the
3202  * offset, then we have our pointer.
3203  */
3204 ADDR os_linux_current_thread_ptr(struct target *target,REGVAL kernel_esp) {
3205  struct os_linux_state *lstate =
3206  (struct os_linux_state *)target->personality_state;
3207  REGVAL sp;
3208  ADDR kernel_stack_addr;
3209  ADDR gs_base = 0;
3210  ADDR ipval;
3211 
3212  errno = 0;
3213 
3214  if (target->arch->wordsize == 4) {
3215  if (kernel_esp)
3216  sp = kernel_esp;
3217  else {
3218  errno = 0;
3220  target->spregno);
3221  if (errno) {
3222  verror("could not read ESP!\n");
3223  return 0;
3224  }
3225  }
3226 
3227  vdebug(8,LA_TARGET,LF_OSLINUX,"current->thread_info at 0x%"PRIxADDR"\n",
3228  sp & ~(THREAD_SIZE - 1));
3229 
3230  return (sp & ~(THREAD_SIZE - 1));
3231  }
3232  else {
3233 #ifndef __x86_64__
3234  /*
3235  * This is impossible; a 64-bit guest on a 32-bit host. We just
3236  * ifdef the 64-bit stuff away in case the host is 32-bit.
3237  */
3238 #else
3239  ipval = target_read_reg(target,TID_GLOBAL,target->ipregno);
3240  sp = target_read_reg(target,TID_GLOBAL,target->spregno);
3241 
3242  os_linux_current_gs(target,&gs_base);
3243 
3244  if (!gs_base) {
3245  if (ipval >= lstate->kernel_start_addr) {
3246  kernel_stack_addr = sp & ~(THREAD_SIZE - 1);
3247 
3249  "assuming current->thread_info at 0x%"PRIxADDR"\n",
3250  kernel_stack_addr);
3251 
3252  return kernel_stack_addr;
3253  }
3254  else {
3255  verror("%%gs is 0x0; VM not in kernel (ip 0x%"PRIxADDR");"
3256  " cannot infer current thread!\n",
3257  ipval);
3258  errno = EINVAL;
3259  return 0;
3260  }
3261  }
3262  else if (!target_read_addr(target,
3263  gs_base + lstate->kernel_stack_percpu_offset,
3264  target->arch->wordsize,
3265  (unsigned char *)&kernel_stack_addr)) {
3266  verror("could not read %%gs:kernel_stack"
3267  " (0x%"PRIxADDR":%"PRIiOFFSET"); cannot continue!\n",
3268  (ADDR)gs_base,lstate->kernel_stack_percpu_offset);
3269  if (!errno)
3270  errno = EFAULT;
3271  return 0;
3272  }
3273 
3274  vdebug(8,LA_TARGET,LF_OSLINUX,"current->thread_info at 0x%"PRIxADDR"\n",
3275  kernel_stack_addr + KERNEL_STACK_OFFSET - THREAD_SIZE);
3276 
3277  /* XXX: somehow errno is getting set incorrectly on this path. */
3278  errno = 0;
3279 
3280  return kernel_stack_addr + KERNEL_STACK_OFFSET - THREAD_SIZE;
3281 #endif
3282  }
3283 }
3284 
3285 struct symbol *os_linux_get_task_struct_type(struct target *target) {
3286  struct os_linux_state *lstate;
3287 
3288  lstate = (struct os_linux_state *)target->personality_state;
3289  if (!lstate || !lstate->task_struct_type) {
3290  verror("target does not seem to be loaded!\n");
3291  return NULL;
3292  }
3293 
3294  RHOLD(lstate->task_struct_type,lstate->task_struct_type);
3295 
3296  return lstate->task_struct_type;
3297 }
3298 
3299 struct symbol *os_linux_get_task_struct_type_ptr(struct target *target) {
3300  struct os_linux_state *lstate;
3301 
3302  lstate = (struct os_linux_state *)target->personality_state;
3303  if (!lstate || !lstate->task_struct_type_ptr) {
3304  verror("target does not seem to be loaded!\n");
3305  return NULL;
3306  }
3307 
3309 
3310  return lstate->task_struct_type_ptr;
3311 }
3312 
3313 /*
3314 struct symbol *os_linux_get_thread_info_type(struct target *target) {
3315  struct os_linux_state *lstate;
3316 
3317  lstate = (struct os_linux_state *)target->personality_state;
3318  if (!lstate || !lstate->thread_info_type) {
3319  verror("target does not seem to be loaded!\n");
3320  return NULL;
3321  }
3322 
3323  RHOLD(lstate->thread_info_type,lstate->thread_info_type);
3324 
3325  return lstate->thread_info_type;
3326 }
3327 */
3328 
3329 /*
3330 struct value *os_linux_load_current_task_as_type(struct target *target,
3331  struct symbol *datatype,
3332  REGVAL kernel_esp) {
3333  struct value *value;
3334  ADDR tptr;
3335 
3336  errno = 0;
3337  tptr = os_linux_current_thread_ptr(target,kernel_esp);
3338  if (errno)
3339  return NULL;
3340 
3341  value = target_load_type(target,datatype,tptr,LOAD_FLAG_AUTO_DEREF);
3342 
3343  return value;
3344 }
3345 
3346 struct value *os_linux_load_current_task(struct target *target,
3347  REGVAL kernel_esp) {
3348  struct value *value;
3349  ADDR itptr;
3350  struct symbol *itptr_type;
3351 
3352  itptr_type = os_linux_get_task_struct_type_ptr(target);
3353  if (!itptr_type) {
3354  verror("could not find type for struct task_struct!\n");
3355  return NULL;
3356  }
3357 
3358  errno = 0;
3359  itptr = os_linux_current_thread_ptr(target,kernel_esp);
3360  if (errno)
3361  return NULL;
3362 
3363  value = target_load_type(target,itptr_type,itptr,
3364  LOAD_FLAG_AUTO_DEREF);
3365 
3366  symbol_release(itptr_type);
3367 
3368  return value;
3369 }
3370 */
3371 
3372 struct value *os_linux_load_current_thread_as_type(struct target *target,
3373  struct symbol *datatype,
3374  REGVAL kernel_esp) {
3375  struct value *value;
3376  ADDR tptr;
3377 
3378  errno = 0;
3379  tptr = os_linux_current_thread_ptr(target,kernel_esp);
3380  if (errno) {
3381  verror("could not get current_thread_ptr!\n");
3382  return NULL;
3383  }
3384 
3385  value = target_load_type(target,datatype,tptr,LOAD_FLAG_NONE);
3386 
3387  return value;
3388 }
3389 
3390 int os_linux_get_task_pid(struct target *target,struct value *task) {
3391  struct value *value;
3392  int pid;
3393  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
3394 
3395  if (!task)
3396  return -1;
3397 
3398  value = target_load_value_member(target,tlctxt,
3399  task,"pid",NULL,LOAD_FLAG_NONE);
3400  if (!value) {
3401  verror("could not load 'pid' of task!\n");
3402  return -2;
3403  }
3404  pid = v_i32(value);
3405 
3406  value_free(value);
3407 
3408  return pid;
3409 }
3410 
3413  struct value *match;
3414 };
3415 
3416 static int match_pid(struct target *target,struct value *value,void *data) {
3417  struct match_pid_data *mpd = (struct match_pid_data *)data;
3418  struct value *mv = NULL;
3419  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
3420 
3421  mv = target_load_value_member(target,tlctxt,value,"pid",NULL,
3422  LOAD_FLAG_NONE);
3423  if (!mv) {
3424  vwarn("could not load pid from task; skipping!\n");
3425  return 0;
3426  }
3427  else if (mpd->tid != v_i32(mv)) {
3428  value_free(mv);
3429  return 0;
3430  }
3431  else {
3432  mpd->match = value;
3433  value_free(mv);
3434  return -1;
3435  }
3436 }
3437 
3438 struct value *os_linux_get_task(struct target *target,tid_t tid) {
3439  struct os_linux_state *lstate =
3440  (struct os_linux_state *)target->personality_state;
3441  struct match_pid_data mpd;
3442 
3443  mpd.tid = tid;
3444  mpd.match = NULL;
3445  os_linux_list_for_each_struct(target,lstate->init_task,"tasks",0,
3446  match_pid,&mpd);
3447 
3448  if (!mpd.match) {
3449  vwarn("no task matching %"PRIiTID"\n",tid);
3450 
3451  return NULL;
3452  }
3453 
3454  return mpd.match;
3455 }
3456 
3457 /*
3458  * d_flags entries -- from include/linux/dcache.h
3459  */
3460 #define DCACHE_AUTOFS_PENDING 0x0001
3461 #define DCACHE_NFSFS_RENAMED 0x0002
3462 #define DCACHE_DISCONNECTED 0x0004
3463 #define DCACHE_REFERENCED 0x0008
3464 #define DCACHE_UNHASHED 0x0010
3465 #define DCACHE_INOTIFY_PARENT_WATCHED 0x0020
3466 
3467 /*
3468  * This function fills in @buf from the end! @return is a ptr to
3469  * somewhere inside @buf, consequently.
3470  */
3471 char *os_linux_d_path(struct target *target,
3472  struct value *dentry,struct value *vfsmnt,
3473  struct value *root_dentry,struct value *root_vfsmnt,
3474  char *buf,int buflen) {
3475  ADDR dentry_addr;
3476  ADDR vfsmnt_addr;
3477  ADDR root_dentry_addr;
3478  ADDR root_vfsmnt_addr;
3479  unum_t dentry_flags;
3480  ADDR parent_addr;
3481  ADDR mnt_root_addr;
3482  struct value *vfsmnt_mnt_parent;
3483  ADDR vfsmnt_mnt_parent_addr;
3484  struct value *orig_dentry = dentry;
3485  struct value *orig_vfsmnt = vfsmnt;
3486  struct value *ph;
3487  char *retval;
3488  char *end;
3489  uint32_t namelen;
3490  ADDR nameaddr;
3491  char *namebuf;
3492  struct value *smnamevalue;
3493  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
3494 
3495  assert(buf != NULL);
3496 
3497  dentry_addr = v_addr(dentry);
3498  vfsmnt_addr = v_addr(vfsmnt);
3499  root_dentry_addr = v_addr(root_dentry);
3500  root_vfsmnt_addr = v_addr(root_vfsmnt);
3501 
3502  /*
3503  * Basically from fs/dcache.c:__d_path, except VMI-ified.
3504  */
3505  end = buf + buflen;
3506  *--end = '\0';
3507  buflen--;
3508  VLV(target,tlctxt,dentry,"d_parent",LOAD_FLAG_NONE,
3509  &parent_addr,NULL,err_vmiload);
3510  VLV(target,tlctxt,dentry,"d_flags",LOAD_FLAG_NONE,
3511  &dentry_flags,NULL,err_vmiload);
3512  if (dentry_addr != parent_addr && dentry_flags & DCACHE_UNHASHED) {
3513  buflen -= 10;
3514  end -= 10;
3515  if (buflen < 0)
3516  goto err_toolong;
3517  memcpy(end, " (deleted)", 10);
3518  }
3519 
3520  if (buflen < 1)
3521  goto err_toolong;
3522  /* Get '/' right */
3523  retval = end - 1;
3524  *retval = '/';
3525 
3526  while (1) {
3527  if (dentry_addr == root_dentry_addr && vfsmnt_addr == root_vfsmnt_addr)
3528  break;
3529  VLV(target,tlctxt,vfsmnt,"mnt_root",LOAD_FLAG_NONE,
3530  &mnt_root_addr,NULL,err_vmiload);
3531  if (dentry_addr == mnt_root_addr || dentry_addr == parent_addr) {
3532  vfsmnt_mnt_parent = NULL;
3533  VL(target,tlctxt,vfsmnt,"mnt_parent",
3534  LOAD_FLAG_AUTO_DEREF,&vfsmnt_mnt_parent,err_vmiload);
3535  vfsmnt_mnt_parent_addr = v_addr(vfsmnt_mnt_parent);
3536 
3537  /* Global root? */
3538  if (vfsmnt_mnt_parent_addr == vfsmnt_addr) {
3539  value_free(vfsmnt_mnt_parent);
3540  vfsmnt_mnt_parent = NULL;
3541  goto global_root;
3542  }
3543  if (dentry != orig_dentry) {
3544  value_free(dentry);
3545  dentry = NULL;
3546  }
3547  VL(target,tlctxt,vfsmnt,"mnt_mountpoint",
3548  LOAD_FLAG_AUTO_DEREF,&dentry,err_vmiload);
3549  dentry_addr = v_addr(dentry);
3550  if (vfsmnt != orig_vfsmnt) {
3551  value_free(vfsmnt);
3552  }
3553  vfsmnt = vfsmnt_mnt_parent;
3554  vfsmnt_addr = v_addr(vfsmnt);
3555  vfsmnt_mnt_parent = NULL;
3556  continue;
3557  }
3558  namelen = 0;
3559  VLV(target,tlctxt,dentry,"d_name.len",LOAD_FLAG_NONE,
3560  &namelen,NULL,err_vmiload);
3561 
3562  /*
3563  * Newer linux keeps a "small dentry name" cache inside the
3564  * dentry itself; so, if namelen == 0, check dentry.d_iname
3565  * instead of dentry.d_name.name .
3566  */
3567  smnamevalue = target_load_value_member(target,tlctxt,
3568  dentry,"d_iname",
3569  NULL,LOAD_FLAG_NONE);
3570 
3571  VLV(target,tlctxt,dentry,"d_name.name",LOAD_FLAG_NONE,
3572  &nameaddr,NULL,err_vmiload);
3573 
3574  if (!nameaddr || (smnamevalue && nameaddr == value_addr(smnamevalue))) {
3575  if (!smnamevalue) {
3576  verror("dentry.d_name.name invalid!!\n");
3577  goto err_vmiload;
3578  }
3579 
3580  namelen = strnlen(smnamevalue->buf,smnamevalue->bufsiz);
3581 
3582  buflen -= namelen + 1;
3583  if (buflen < 0)
3584  goto err_toolong;
3585  end -= namelen;
3586 
3587  memcpy(end,smnamevalue->buf,namelen);
3588  value_free(smnamevalue);
3589  smnamevalue = NULL;
3590  namebuf = NULL;
3591  *--end = '/';
3592  retval = end;
3593  }
3594  else if (namelen > 0) {
3595  buflen -= namelen + 1;
3596  if (buflen < 0)
3597  goto err_toolong;
3598  end -= namelen;
3599 
3600  namebuf = NULL;
3601  VLA(target,nameaddr,LOAD_FLAG_NONE,&namebuf,namelen,NULL,err_vmiload);
3602  memcpy(end,namebuf,namelen);
3603  free(namebuf);
3604  namebuf = NULL;
3605  *--end = '/';
3606  retval = end;
3607  }
3608  else {
3609  verror("dentry.d_name.len (%"PRIu32") was invalid!\n",namelen);
3610  goto err_vmiload;
3611  }
3612 
3613  ph = dentry;
3614  VL(target,tlctxt,ph,"d_parent",LOAD_FLAG_AUTO_DEREF,
3615  &dentry,err_vmiload);
3616  if (ph != orig_dentry) {
3617  value_free(ph);
3618  }
3619  dentry_addr = v_addr(dentry);
3620  }
3621 
3622  goto out;
3623 
3624  global_root:
3625  namelen = 0;
3626  VLV(target,tlctxt,dentry,"d_name.len",LOAD_FLAG_NONE,
3627  &namelen,NULL,err_vmiload);
3628 
3629  smnamevalue = target_load_value_member(target,tlctxt,
3630  dentry,"d_iname",NULL,LOAD_FLAG_NONE);
3631 
3632  if (!nameaddr || (smnamevalue && nameaddr == value_addr(smnamevalue))) {
3633  if (!smnamevalue) {
3634  verror("global_root dentry.d_name.name invalid!!\n");
3635  goto err_vmiload;
3636  }
3637 
3638  namelen = strnlen(smnamevalue->buf,smnamevalue->bufsiz);
3639 
3640  buflen -= namelen;
3641  if (buflen < 0)
3642  goto err_toolong;
3643  retval -= namelen - 1; /* hit the slash */
3644 
3645  memcpy(retval,smnamevalue->buf,namelen);
3646  value_free(smnamevalue);
3647  smnamevalue = NULL;
3648  namebuf = NULL;
3649  }
3650  else if (namelen > 0) {
3651  buflen -= namelen;
3652  if (buflen < 0)
3653  goto err_toolong;
3654  retval -= namelen - 1; /* hit the slash */
3655  namebuf = NULL;
3656  VLA(target,nameaddr,LOAD_FLAG_NONE,&namebuf,namelen,NULL,err_vmiload);
3657  memcpy(retval,namebuf,namelen);
3658  free(namebuf);
3659  namebuf = NULL;
3660  }
3661  else {
3662  verror("dentry.d_name.len (%"PRIu32") was invalid!\n",namelen);
3663  goto err_vmiload;
3664  }
3665 
3666  goto out;
3667 
3668  err_toolong:
3669  err_vmiload:
3670 
3671  retval = NULL;
3672 
3673  out:
3674  /* Free intermediate dentry/vfsmnt values left, if any. */
3675  if (dentry && dentry != orig_dentry) {
3676  value_free(dentry);
3677  dentry = NULL;
3678  }
3679  if (vfsmnt && vfsmnt != orig_vfsmnt) {
3680  value_free(vfsmnt);
3681  vfsmnt = NULL;
3682  }
3683 
3684  return retval;
3685 }
3686 
3687 char *os_linux_file_get_path(struct target *target,struct value *task,
3688  struct value *file,char *ibuf,int buflen) {
3689  struct value *dentry = NULL;
3690  struct value *vfsmnt = NULL;
3691  struct value *root_dentry = NULL;
3692  struct value *root_vfsmnt = NULL;
3693  char buf[PATH_MAX];
3694  char *bufptr;
3695  int len;
3696  char *retval;
3697  struct lsymbol *tmpls;
3698  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
3699 
3700  /*
3701  * See if we're into the newer struct file::f_path stuff, or if we
3702  * still have the older struct file::{f_dentry,f_vfsmnt}.
3703  */
3704  if (!(tmpls = symbol_lookup_sym(file->type,"f_path",NULL))) {
3705  VL(target,tlctxt,file,"f_vfsmnt",LOAD_FLAG_AUTO_DEREF,
3706  &vfsmnt,err_vmiload);
3707  VL(target,tlctxt,file,"f_dentry",LOAD_FLAG_AUTO_DEREF,
3708  &dentry,err_vmiload);
3709  VL(target,tlctxt,task,"fs.rootmnt",LOAD_FLAG_AUTO_DEREF,
3710  &root_vfsmnt,err_vmiload);
3711  VL(target,tlctxt,task,"fs.root",LOAD_FLAG_AUTO_DEREF,
3712  &root_dentry,err_vmiload);
3713  }
3714  else {
3715  lsymbol_release(tmpls);
3716  VL(target,tlctxt,file,"f_path.mnt",LOAD_FLAG_AUTO_DEREF,
3717  &vfsmnt,err_vmiload);
3718  VL(target,tlctxt,file,"f_path.dentry",LOAD_FLAG_AUTO_DEREF,
3719  &dentry,err_vmiload);
3720  VL(target,tlctxt,task,"fs.root.mnt",LOAD_FLAG_AUTO_DEREF,
3721  &root_vfsmnt,err_vmiload);
3722  VL(target,tlctxt,task,"fs.root.dentry",LOAD_FLAG_AUTO_DEREF,
3723  &root_dentry,err_vmiload);
3724  }
3725 
3726  bufptr = os_linux_d_path(target,dentry,vfsmnt,root_dentry,root_vfsmnt,
3727  buf,PATH_MAX);
3728  if (!bufptr)
3729  goto err;
3730 
3731  if (!ibuf) {
3732  ibuf = malloc(PATH_MAX);
3733  buflen = PATH_MAX;
3734  }
3735 
3736  len = sizeof(buf) - (bufptr - buf);
3737  memcpy(ibuf,bufptr,(len < buflen) ? len : buflen);
3738 
3739  retval = ibuf;
3740  goto out;
3741 
3742  err:
3743  err_vmiload:
3744  retval = NULL;
3745  goto out;
3746 
3747  out:
3748  if (dentry)
3749  value_free(dentry);
3750  if (vfsmnt)
3751  value_free(vfsmnt);
3752  if (root_dentry)
3753  value_free(root_dentry);
3754  if (root_vfsmnt)
3755  value_free(root_vfsmnt);
3756 
3757  return retval;
3758 }
3759 
3760 /*
3761  * Since linux linked lists are formed by chaining C structs together
3762  * using struct members as the list next/prev pointers, we provide a
3763  * generic function to traverse them.
3764  *
3765  * Basically, we calculate the offset of the list head member name in
3766  * the type, and then, starting with the struct at the @head - offset,
3767  * we load that value. We then continue looping by loading the next
3768  * struct specified in the list head member's next field.
3769  */
3770 int os_linux_list_for_each_struct(struct target *t,struct bsymbol *bsymbol,
3771  char *list_head_member_name,int nofree,
3772  os_linux_list_iterator_t iterator,void *data) {
3773  struct symbol *symbol;
3774  struct symbol *type;
3775  OFFSET list_head_member_offset;
3776  ADDR head;
3777  ADDR next_head;
3778  ADDR current_struct_addr;
3779  struct value *value = NULL;
3780  int i = 0;
3781  int retval = -1;
3782  int rc;
3783  struct target_location_ctxt *tlctxt = target_global_tlctxt(t);
3784 
3785  symbol = bsymbol_get_symbol(bsymbol);
3786  type = symbol_get_datatype(symbol);
3787  if (!type) {
3788  verror("no type for bsymbol %s!\n",bsymbol_get_name(bsymbol));
3789  goto out;
3790  }
3791 
3792  errno = 0;
3793  list_head_member_offset =
3794  symbol_offsetof(type,list_head_member_name,NULL);
3795  if (errno) {
3796  verror("could not get offset for %s in symbol %s!\n",
3797  list_head_member_name,symbol_get_name(type));
3798  ERRORDUMPSYMBOL_NL(symbol);
3799  goto out;
3800  }
3801 
3802  /* We just blindly use TID_GLOBAL because init_task is the symbol
3803  * they are supposed to pass, and resolving that is not going to
3804  * depend on any registers, so it doesn't matter which thread we
3805  * use.
3806  */
3807  current_struct_addr = head = \
3808  target_addressof_symbol(t,tlctxt,bsymbol,LOAD_FLAG_NONE,
3809  NULL);
3810  if (errno) {
3811  verror("could not get the address of bsymbol %s!\n",
3812  bsymbol_get_name(bsymbol));
3813  goto out;
3814  }
3815  /* The real head is plus the member offset. */
3816  head += list_head_member_offset;
3817 
3818  /* Now, start loading the struct values one by one, starting with
3819  * the symbol arg itself.
3820  */
3821  while (1) {
3822  value = target_load_type(t,type,current_struct_addr,LOAD_FLAG_NONE);
3823  if (!value) {
3824  verror("could not load value in list position %d, aborting!\n",i);
3825  goto out;
3826  }
3827 
3828  rc = iterator(t,value,data);
3829  if (rc == 1)
3830  break;
3831  else if (rc == -1) {
3832  nofree = 1;
3833  break;
3834  }
3835 
3836  next_head = *((ADDR *)(value->buf + list_head_member_offset));
3837 
3838  if (!nofree) {
3839  value_free(value);
3840  value = NULL;
3841  }
3842 
3843  if (next_head == head)
3844  break;
3845 
3846  current_struct_addr = next_head - list_head_member_offset;
3847  ++i;
3848  }
3849 
3850  retval = 0;
3851 
3852  out:
3853  if (!nofree && value)
3854  value_free(value);
3855 
3856  return retval;
3857 }
3858 
3859 /*
3860  * Since linux linked lists are formed by chaining C structs together
3861  * using struct members as the list next/prev pointers, we provide a
3862  * generic function to traverse them.
3863  *
3864  * This function is different from the one above in the sense that the
3865  * input to this one is a struct type, plus a list_head variable
3866  * (instead of having a struct instance that is the "head" of the
3867  * list). So, for the task list, whose head is the init_task
3868  * task_struct, the above function is more appropriate. For the modules
3869  * list_head, which might have nothing in it, this function is more
3870  * appropriate.
3871  *
3872  * Basically, we calculate the offset of the list head member name in
3873  * the type, and then, starting with the struct at the @head - offset,
3874  * we load that value. We then continue looping by loading the next
3875  * struct specified in the list head member's next field.
3876  */
3877 int os_linux_list_for_each_entry(struct target *t,struct bsymbol *btype,
3878  struct bsymbol *list_head,
3879  char *list_head_member_name,int nofree,
3880  os_linux_list_iterator_t iterator,void *data) {
3881  struct symbol *type;
3882  OFFSET list_head_member_offset;
3883  ADDR head;
3884  ADDR next_head;
3885  ADDR current_struct_addr;
3886  struct value *value = NULL;
3887  struct value *value_next;
3888  int i = 0;
3889  int retval = -1;
3890  int rc;
3891  struct target_location_ctxt *tlctxt = target_global_tlctxt(t);
3892 
3893  type = bsymbol_get_symbol(btype);
3894 
3895  errno = 0;
3896  list_head_member_offset =
3897  symbol_offsetof(type,list_head_member_name,NULL);
3898  if (errno) {
3899  verror("could not get offset for %s in symbol %s!\n",
3900  list_head_member_name,symbol_get_name(type));
3901  ERRORDUMPSYMBOL_NL(type);
3902  goto out;
3903  }
3904 
3905  /*
3906  * We just blindly use TID_GLOBAL because init_task is the symbol
3907  * they are supposed to pass, and resolving that is not going to
3908  * depend on any registers, so it doesn't matter which thread we
3909  * use.
3910  */
3911 
3912  value = target_load_symbol(t,tlctxt,list_head,LOAD_FLAG_NONE);
3913  if (!value) {
3914  verror("could not load list_head for symbol %s!\n",bsymbol_get_name(list_head));
3915  goto out;
3916  }
3917  head = value_addr(value);
3918  value_next = target_load_value_member(t,tlctxt,value,"next",
3919  NULL,LOAD_FLAG_NONE);
3920  next_head = *((ADDR *)value->buf);
3921 
3922  value_free(value_next);
3923  value_free(value);
3924  value = NULL;
3925 
3926  /*
3927  * Now, start loading the struct values one by one, starting with next_head.
3928  */
3929  while (next_head != head) {
3930  /* The real head is plus the member offset. */
3931  current_struct_addr = next_head - list_head_member_offset;
3932 
3933  value = target_load_type(t,type,current_struct_addr,LOAD_FLAG_NONE);
3934  if (!value) {
3935  verror("could not load value in list position %d, aborting!\n",i);
3936  goto out;
3937  }
3938 
3939  rc = iterator(t,value,data);
3940  if (rc == 1)
3941  break;
3942  else if (rc == -1) {
3943  nofree = 1;
3944  break;
3945  }
3946 
3947  next_head = *((ADDR *)(value->buf + list_head_member_offset));
3948 
3949  if (!nofree) {
3950  value_free(value);
3951  value = NULL;
3952  }
3953 
3954  if (next_head == head)
3955  break;
3956 
3957  current_struct_addr = next_head - list_head_member_offset;
3958  ++i;
3959  }
3960 
3961  retval = 0;
3962 
3963  out:
3964  if (!nofree && value)
3965  value_free(value);
3966 
3967  return retval;
3968 }
3969 
3970 static int __value_get_append_tid(struct target *target,struct value *value,
3971  void *data) {
3972  struct array_list *list = (struct array_list *)data;
3973  struct value *v;
3974 
3976  value,"pid",NULL,LOAD_FLAG_NONE);
3977  if (!v) {
3978  verror("could not load pid in task; BUG?\n");
3979  /* errno should be set for us. */
3980  return -1;
3981  }
3982  array_list_append(list,(void *)(uintptr_t)v_i32(v));
3983  value_free(v);
3984 
3985  return 0;
3986 }
3987 
3988 struct array_list *os_linux_list_available_tids(struct target *target) {
3989  struct array_list *retval;
3990  struct os_linux_state *lstate =
3991  (struct os_linux_state *)target->personality_state;
3992 
3993  /*
3994  * If we are tracking threads, we don't have scan the list!
3995  */
3996  if (target->ap_flags & APF_OS_THREAD_ENTRY
3997  && target->ap_flags & APF_OS_THREAD_EXIT) {
3999  "active probing thread entry/exit, so just reloading cache!\n");
4000  return target_list_tids(target);
4001  }
4002 
4003  /* Try to be smart about the size of the list we create. */
4004  if (lstate->last_thread_count)
4005  retval = array_list_create((lstate->last_thread_count + 16) & ~15);
4006  else
4007  retval = array_list_create(64);
4008 
4009  if (os_linux_list_for_each_struct(target,lstate->init_task,"tasks",0,
4010  __value_get_append_tid,retval)) {
4011  verror("could not load all tids in task list (did %d tasks)\n",
4012  array_list_len(retval));
4013  array_list_free(retval);
4014  return NULL;
4015  }
4016 
4017  lstate->last_thread_count = array_list_len(retval);
4018 
4019  vdebug(5,LA_TARGET,LF_OSLINUX | LF_THREAD,"%d current threads\n",
4020  lstate->last_thread_count);
4021 
4022  return retval;
4023 }
4024 
4025 static int __value_load_thread(struct target *target,struct value *value,
4026  void *data) {
4027  struct target_thread *tthread;
4028  int *load_counter = (int *)data;
4029 
4030  if (!(tthread = os_linux_load_thread_from_value(target,value))) {
4031  verror("could not load thread from task value; BUG?\n");
4032  value_free(value);
4033  return -1;
4034  }
4035 
4037  char buf[512];
4038  target_thread_snprintf(target,tthread->tid,buf,sizeof(buf),
4039  1,NULL,NULL);
4041  "loaded tid(%d) (%s)\n",tthread->tid,tthread->name,buf);
4042  }
4043 
4044  if (load_counter)
4045  ++*load_counter;
4046 
4047  return 0;
4048 }
4049 
4050 int os_linux_load_available_threads(struct target *target,int force) {
4051  int rc = 0;
4052  struct os_linux_state *xstate =
4053  (struct os_linux_state *)target->personality_state;
4054  int i = 0;
4055  struct array_list *cthreads;
4056  struct target_thread *tthread;
4057  struct target_event *event;
4058 
4059  /*
4060  * If we are tracking threads, we don't have scan the list!
4061  */
4062  if (target->ap_flags & APF_OS_THREAD_ENTRY
4063  && target->ap_flags & APF_OS_THREAD_EXIT) {
4065  "active probing thread entry/exit, so just reloading cache!\n");
4066  return target_load_all_threads(target,force);
4067  }
4068 
4069  if (os_linux_list_for_each_struct(target,xstate->init_task,"tasks",1,
4070  __value_load_thread,&i)) {
4071  verror("could not load all threads in task list (did %d tasks)\n",i);
4072  rc = -1;
4073  }
4074  /*
4075  * If there are still any invalid threads, they are no longer live
4076  * -- so delete them!
4077  */
4078  else {
4079  cthreads = target_list_threads(target);
4080  for (i = 0; i < array_list_len(cthreads); ++i) {
4081  tthread = (struct target_thread *)array_list_item(cthreads,i);
4082  if (!OBJVALID(tthread)) {
4084  "evicting invalid thread %"PRIiTID"; no longer exists\n",
4085  tthread->tid);
4086  event = target_create_event(target,tthread,
4087  T_EVENT_OS_THREAD_EXITED,tthread);
4088  target_broadcast_event(target,event);
4089  target_detach_thread(target,tthread);
4090  event->thread = NULL;
4091  }
4092  }
4093  array_list_free(cthreads);
4094  }
4095 
4096  return rc;
4097 }
4098 
4099 struct target_thread *os_linux_load_thread(struct target *target,
4100  tid_t tid,int force) {
4101  struct target_thread *tthread = NULL;
4102  struct os_linux_thread_state *ltstate;
4103  int taskv_loaded;
4104  struct value *taskv = NULL;
4105  struct target_event *event;
4106 
4107  /*
4108  * We need to find the task on the kernel's task list that matches
4109  * @tid. If no match, but we had a thread with a matching @tid in
4110  * our cache, we need to nuke that thread. If there is a match, but
4111  * its core data is different than what's in the cache, we have to
4112  * nuke the old task from the cache and build a new one. If the
4113  * match matches, just reload its volatile data and context.
4114  */
4115 
4116  if (target_status(target) != TSTATUS_PAUSED) {
4117  verror("target not paused; cannot load thread id %d!\n",tid);
4118  errno = EBUSY;
4119  return NULL;
4120  }
4121 
4122  tthread = target_lookup_thread(target,tid);
4123 
4124  /*
4125  * If we didn't find a cached thread, or we're not live-tracking
4126  * thread exit, check for stale thread! If we have a cached thread,
4127  * and we are tracking EXITs, we don't need to walk the task list.
4128  */
4129  if (!tthread
4130  || !(target->ap_flags & APF_OS_THREAD_EXIT)) {
4131  taskv = os_linux_get_task(target,tid);
4132  taskv_loaded = 1;
4133 
4134  if (!taskv) {
4135  vwarn("no task matching %"PRIiTID"\n",tid);
4136 
4137  if (tthread) {
4139  "evicting old thread %"PRIiTID"; no longer exists!\n",tid);
4140  event = target_create_event(target,tthread,
4141  T_EVENT_OS_THREAD_EXITED,tthread);
4142  target_broadcast_event(target,event);
4143  target_detach_thread(target,tthread);
4144  event->thread = NULL;
4145  }
4146 
4147  return NULL;
4148  }
4149  }
4150  else {
4151  taskv_loaded = 0;
4152  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
4153 
4154  if (!value_refresh(ltstate->task_struct,1)) {
4155  verror("could not refresh cached struct; aborting to manual update!\n");
4156 
4157  taskv = os_linux_get_task(target,tid);
4158  taskv_loaded = 1;
4159 
4160  if (!taskv) {
4161  vwarn("no task matching %"PRIiTID"\n",tid);
4162 
4163  if (tthread) {
4165  "evicting old thread %"PRIiTID"; no longer exists!\n",
4166  tid);
4167  event = target_create_event(target,tthread,
4168  T_EVENT_OS_THREAD_EXITED,tthread);
4169  target_broadcast_event(target,event);
4170  target_detach_thread(target,tthread);
4171  event->thread = NULL;
4172  }
4173 
4174  return NULL;
4175  }
4176  }
4177  }
4178 
4179  if (!(tthread = os_linux_load_thread_from_value(target,taskv)))
4180  goto errout;
4181 
4182  return tthread;
4183 
4184  errout:
4185  if (taskv_loaded && taskv)
4186  value_free(taskv);
4187 
4188  return NULL;
4189 }
4190 
4191 static struct target_thread *
4192 __os_linux_load_current_thread_from_userspace(struct target *target,int force) {
4193  GHashTableIter iter;
4194  gpointer vp;
4195  struct target_thread *tthread = NULL;
4196  struct os_linux_thread_state *ltstate;
4197  uint64_t cr3;
4198  REGVAL ipval;
4199 
4200 #if __WORDSIZE == 64
4201  /*
4202  * libxc claims that for x86_64, pagetable is in CR1.
4203  */
4204  if (__WORDSIZE == 64 || target->arch->wordsize == 8)
4205  cr3 = (uint64_t)target_read_reg(target,TID_GLOBAL,REG_X86_64_CR1);
4206  else
4207  cr3 = (uint64_t)target_read_reg(target,TID_GLOBAL,REG_X86_CR3);
4208 #endif
4209 
4210  ipval = target_read_reg(target,TID_GLOBAL,target->ipregno);
4211 
4213  "ip 0x%"PRIxADDR"; cr3/pgd = 0x%"PRIx64"\n",ipval,cr3);
4214 
4215  /*
4216  * First, we scan our current cache; if we find a cr3 hit, we're
4217  * money. Otherwise, we have load all tasks (well, at least until
4218  * we find what we need).
4219  */
4220  g_hash_table_iter_init(&iter,target->threads);
4221  while (g_hash_table_iter_next(&iter,NULL,(gpointer *)&vp)) {
4222  tthread = (struct target_thread *)vp;
4223  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
4224 
4225  if (ltstate->pgd == cr3)
4226  break;
4227  else {
4228  tthread = NULL;
4229  ltstate = NULL;
4230  }
4231  }
4232 
4233  if (!tthread) {
4235  "could not find task match for cr3 0x%"PRIx64";"
4236  " loading all tasks!\n",cr3);
4237 
4238  /*
4239  * We really should just use a reverse init_task list traversal
4240  * here. The task is most likely to be nearer the end.
4241  */
4242  if (target_load_available_threads(target,force)) {
4243  verror("could not load all threads to match on cr3!\n");
4244  return NULL;
4245  }
4246 
4247  /* Search again. */
4248  g_hash_table_iter_init(&iter,target->threads);
4249  while (g_hash_table_iter_next(&iter,NULL,(gpointer *)&vp)) {
4250  tthread = (struct target_thread *)vp;
4251  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
4252 
4253  if (ltstate->pgd == cr3)
4254  break;
4255  else {
4257  "thread %"PRIiTID" with pgd 0x%"PRIx64" did not match!\n",
4258  tthread->tid,ltstate->pgd);
4259  tthread = NULL;
4260  ltstate = NULL;
4261  }
4262  }
4263 
4264  if (!tthread) {
4265  verror("could not find task match for cr3 0x%"PRIx64
4266  " after loading all tasks!\n",cr3);
4267  errno = ESRCH;
4268  return NULL;
4269  }
4270  }
4271  else {
4272  /* Reload its value. */
4273  if (!OBJVALID(tthread)) {
4274  tthread = os_linux_load_thread_from_value(target,
4275  ltstate->task_struct);
4276  if (!tthread) {
4277  verror("could not load cached thread %"PRIiTID" from value!",
4278  tthread->tid);
4279  return NULL;
4280  }
4281  }
4282  }
4283 
4285  "ip 0x%"PRIxADDR"; cr3/pgd = 0x%"PRIx64" --> thread %"PRIiTID"\n",
4286  ipval,cr3,tthread->tid);
4287 
4288  return tthread;
4289 }
4290 
4291 struct target_thread *os_linux_load_current_thread(struct target *target,
4292  int force) {
4293  struct os_linux_state *lstate = (struct os_linux_state *)target->personality_state;
4294  tid_t tid = 0;
4295  struct value *threadinfov = NULL;
4296  int preempt_count;
4297  unum_t tiflags;
4298  struct value *taskv = NULL;
4299  tid_t tgid;
4300  unum_t task_flags = 0;
4301  ADDR group_leader;
4302  struct target_thread *tthread = NULL;
4303  struct os_linux_thread_state *ltstate = NULL;
4304  struct os_linux_thread_state *gtstate;
4305  struct value *v = NULL;
4306  REGVAL ipval,spval;
4307  ADDR mm_addr = 0;
4308  uint64_t pgd = 0;
4309  REGVAL kernel_esp = 0;
4310  char *comm = NULL;
4311  struct target_thread *ptthread;
4312  tid_t ptid = -1;
4313  int uid = -1;
4314  int gid = -1;
4315  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
4316  thread_ctxt_t ctidctxt;
4317  struct target_event *event;
4318  int in_hypercall = 0;
4319  ADDR ptregs_stack_addr = 0;
4320 
4321  /*
4322  * Load EIP for later user-mode check.
4323  */
4324  errno = 0;
4325  ipval = target_read_reg(target,TID_GLOBAL,target->ipregno);
4326  if (errno) {
4327  vwarn("could not read EIP for user-mode check; continuing anyway.\n");
4328  errno = 0;
4329  }
4330 
4331  /*
4332  * Load SP. Sometimes (on x86_64) we might be on one of many
4333  * stacks. With i386 code, there's only one stack per task, and
4334  * interrupts and syscalls are all handled on that stack (I think).
4335  * But on x86_64, there are all kinds of stacks. There's the task's
4336  * kernel stack; there's the several interrupt stacks. %sp is
4337  * always the "current" stack, but even though the current thread is
4338  * a userspace process, it might not be on its current stack... like
4339  * if the userspace process caused a debug exception! In that case,
4340  * the interrupt handling code swaps us over to one of the per-cpu,
4341  * per-exception stacks very early on in the interrupt. Our latest
4342  * userspace state is always on that stack.
4343  */
4344  spval = target_read_reg(target,TID_GLOBAL,target->spregno);
4345  if (errno) {
4346  vwarn("could not read SP; continuing anyway.\n");
4347  errno = 0;
4348  }
4349 
4350  //if (ipval < lstate->kernel_start_addr)
4351  // ctidctxt = THREAD_CTXT_USER;
4352  //else
4353  // ctidctxt = THREAD_CTXT_KERNEL;
4354 
4355  ctidctxt = target->global_thread->tidctxt;
4356 
4357  /*
4358  * We used to not load the current thread in this case, but now we
4359  * do. This is sort of misleading, because the thread is not
4360  * exactly in kernel context. BUT, the important thing to realize
4361  * is that this target does not only provide service for in-kernel
4362  * operations. The difference between this target and the
4363  * xen-process target is that the xen-process target has *extra*
4364  * functionality for inspecting the guts of a process, using its
4365  * symbols; this target can manipulate a process's CPU state, and it
4366  * can write virtual memory that is paged in; but that's it.
4367  *
4368  * So -- this target has to realize that its CPU state currently
4369  * corresponds to user state. That means tricks like using the
4370  * current esp to find the current thread, and the task, do not
4371  * work.
4372  *
4373  * I'm not 100% happy about this; you have to check which context a
4374  * thread is in before you access its stack, etc. But it's good
4375  * enough for now.
4376  *
4377  * Once we have loaded the global thread above, in this case, we
4378  * call a different function. We actually try to infer which task
4379  * is running by checking cr3; we compare it to our existing, cached
4380  * tasks, and try to load the cached thread that matches. If there
4381  * is no match, we have no choice but to load all the threads again
4382  * so we find the match.
4383  *
4384  * Anyway, we do that in another function.
4385  */
4386  if (ctidctxt == THREAD_CTXT_USER) {
4387  /*
4388  vdebug(9,LA_TARGET,LF_OSLINUX,
4389  "at user-mode EIP 0x%"PRIxADDR"; not loading current thread;"
4390  " returning global thread.\n",
4391  ipval);
4392  return __os_linux_load_current_thread_from_userspace(target,force);
4393  */
4394 
4396  target->spregno);
4398  "at user-mode EIP 0x%"PRIxADDR"; trying to load current kernel"
4399  " thread with kernel_sp 0x%"PRIxADDR"\n",
4400  ipval,kernel_esp);
4401  }
4402  else
4403  /*
4404  * There are different ways to find the current thread on Linux;
4405  * setting this to 0 handles it elsewhere!
4406  */
4407  kernel_esp = 0;
4408 
4409  /*
4410  * If we're a PV guest Linux, and we're on the hypercall page, we
4411  * might not be able to load the current task; in this case (and
4412  * if we're not in interrupt context), use init_task!
4413  */
4414  if (lstate->hypercall_page != 0
4415  && (ipval & lstate->hypercall_page) == lstate->hypercall_page) {
4416  vdebug(5,LA_TARGET,LF_XV,"on hypercall page; might load init_task!\n");
4417 
4418  in_hypercall = 1;
4419  }
4420 
4421  /* We need to load in the current task_struct, AND if it's already
4422  * in target->threads, CHECK if it really matches one of our cached
4423  * threads. If not, and there is an old thread in the cache, nuke
4424  * that one and build a new one -- TIDs can of course be reused in
4425  * Linux.
4426  */
4427 
4428  /*
4429  * But first, we need to see if we're handling a hard or soft IRQ
4430  * (and are ksoftirqd (?) -- but how do we check *which* kind of
4431  * soft IRQ we are??).
4432  *
4433  * If we are, we just set out TID to TID_GLOBAL, and load state
4434  * from Xen.
4435  *
4436  * If we are not, then, we can be safe to check the kernel's
4437  * task_struct to see which thread we are. But wait, since the
4438  * kernel runs all softirqs in interrupt context, the current task
4439  * is really pretty irrelevant (unless it's ksoftirqd; we could
4440  * check and make a note of that...).
4441  *
4442  * So, we always set TID to TID_GLOBAL, and load state from Xen.
4443  */
4444 
4445  threadinfov = os_linux_load_current_thread_as_type(target,
4446  lstate->thread_info_type,
4447  kernel_esp);
4448  if (!threadinfov) {
4449  verror("could not load current thread info! cannot get current TID!\n");
4450  /* errno should be set for us. */
4451  goto errout;
4452  }
4453 
4454  v = target_load_value_member(target,tlctxt,
4455  threadinfov,lstate->thread_info_preempt_count_name,NULL,
4456  LOAD_FLAG_NONE);
4457  if (!v) {
4458  verror("could not load thread_info->preempt_count (to check IRQ status)!\n");
4459  /* errno should be set for us. */
4460  goto errout;
4461  }
4462  preempt_count = v_num(v);
4463  value_free(v);
4464  v = NULL;
4465 
4466  if (SOFTIRQ_COUNT(preempt_count) || HARDIRQ_COUNT(preempt_count)) {
4467  vdebug(3,LA_TARGET,LF_OSLINUX,"in interrupt context (hardirq=%d,softirq=%d)\n",
4468  HARDIRQ_COUNT(preempt_count),SOFTIRQ_COUNT(preempt_count));
4469  tid = TID_GLOBAL;
4470  tgid = TID_GLOBAL;
4471  taskv = NULL;
4472 
4474  "loading global thread cause in hard/soft irq (0x%"PRIx64")\n",
4475  preempt_count);
4476 
4477  pgd = lstate->pgd_addr;
4478  }
4479  else {
4480  /* Now, load the current task_struct. */
4481  taskv = target_load_value_member(target,tlctxt,
4482  threadinfov,"task",NULL,
4484 
4485  if (!taskv && in_hypercall && lstate->init_task) {
4486  /*
4487  * Just load init_task.
4488  */
4489  taskv = target_load_symbol(target,tlctxt,lstate->init_task,
4491  }
4492 
4493  if (!taskv) {
4494  verror("could not load current task! cannot get current TID!\n");
4495  /* errno should be set for us. */
4496  goto errout;
4497  }
4498 
4499  v = target_load_value_member(target,tlctxt,
4500  taskv,"pid",NULL,LOAD_FLAG_NONE);
4501  if (!v) {
4502  verror("could not load pid in current task; BUG?\n");
4503  /* errno should be set for us. */
4504  goto errout;
4505  }
4506  tid = v_i32(v);
4507  value_free(v);
4508  v = NULL;
4509 
4510  v = target_load_value_member(target,tlctxt,
4511  taskv,"parent",NULL,LOAD_FLAG_NONE);
4512  if (!v) {
4513  verror("could not load parent in task value; BUG?\n");
4514  /* errno should be set for us. */
4515  goto errout;
4516  }
4517  else if (v_addr(v) != value_addr(taskv)) {
4518  ptthread = (struct target_thread *) \
4519  g_hash_table_lookup(lstate->task_struct_addr_to_thread,
4520  (gpointer)v_addr(v));
4521  if (!ptthread) {
4522  /* Gotta load it. */
4523  value_free(v);
4524  v = target_load_value_member(target,tlctxt,
4525  taskv,"parent",NULL,
4527  if (!v) {
4528  verror("could not load parent value from task;"
4529  " ptid will be invalid!\n");
4530  }
4531  else {
4532  ptthread = os_linux_load_thread_from_value(target,v);
4533  if (!ptthread) {
4534  verror("could not load parent thread from value;"
4535  " ptid will be invalid!\n");
4536  }
4537  else {
4539  "loaded tid %"PRIiTID" parent %"PRIiTID"\n",
4540  tid,ptthread->tid);
4541  /* Don't free it! */
4542  v = NULL;
4543  }
4544  }
4545  }
4546  else
4548  "tid %"PRIiTID" parent %"PRIiTID" already loaded\n",
4549  tid,ptthread->tid);
4550 
4551  if (ptthread)
4552  ptid = ptthread->tid;
4553  }
4554  else if (tid != 0) {
4555  vwarn("tid %"PRIiTID" ->parent is itself!\n",tid);
4556  }
4557  if (v) {
4558  value_free(v);
4559  v = NULL;
4560  }
4561 
4562  v = target_load_value_member(target,tlctxt,
4563  taskv,lstate->task_uid_member_name,
4564  NULL,LOAD_FLAG_NONE);
4565  if (!v) {
4566  verror("could not load %s in task value; BUG?\n",
4567  lstate->task_uid_member_name);
4568  uid = -1;
4569  }
4570  else {
4571  uid = v_i32(v);
4572  value_free(v);
4573  v = NULL;
4574  }
4575 
4576  v = target_load_value_member(target,tlctxt,
4577  taskv,lstate->task_gid_member_name,
4578  NULL,LOAD_FLAG_NONE);
4579  if (!v) {
4580  verror("could not load %s in task value; BUG?\n",
4581  lstate->task_gid_member_name);
4582  gid = -1;
4583  }
4584  else {
4585  gid = v_i32(v);
4586  value_free(v);
4587  v = NULL;
4588  }
4589 
4590  v = target_load_value_member(target,tlctxt,
4591  taskv,"comm",NULL,LOAD_FLAG_NONE);
4592  if (!v) {
4593  verror("could not load comm in current task; BUG?\n");
4594  /* errno should be set for us. */
4595  goto errout;
4596  }
4597  comm = strndup(v->buf,v->bufsiz);
4598  value_free(v);
4599  v = NULL;
4600 
4601  vdebug(5,LA_TARGET,LF_OSLINUX,"loading thread %"PRIiTID"\n",tid);
4602 
4603  v = target_load_value_member(target,tlctxt,
4604  taskv,"tgid",NULL,LOAD_FLAG_NONE);
4605  if (!v) {
4606  verror("could not load tgid in current task; BUG?\n");
4607  /* errno should be set for us. */
4608  goto errout;
4609  }
4610  tgid = (tid_t)v_num(v);
4611  value_free(v);
4612  v = NULL;
4613 
4614  v = target_load_value_member(target,tlctxt,
4615  taskv,"flags",NULL,LOAD_FLAG_NONE);
4616  if (!v) {
4617  verror("could not load flags in task %"PRIiTID" current task; BUG?\n",
4618  tid);
4619  /* errno should be set for us. */
4620  goto errout;
4621  }
4622  task_flags = v_unum(v);
4623  value_free(v);
4624  v = NULL;
4625 
4626  v = target_load_value_member(target,tlctxt,
4627  taskv,"group_leader",NULL,LOAD_FLAG_NONE);
4628  if (!v) {
4629  verror("could not load group_leader in task %"PRIiTID" current task; BUG?\n",
4630  tid);
4631  /* errno should be set for us. */
4632  goto errout;
4633  }
4634  group_leader = v_addr(v);
4635  value_free(v);
4636  v = NULL;
4637 
4638  v = target_load_value_member(target,tlctxt,
4639  taskv,"mm",NULL,LOAD_FLAG_NONE);
4640  if (!v) {
4641  verror("could not see if thread %"PRIiTID" was kernel or user\n",tid);
4642  goto errout;
4643  }
4644  mm_addr = v_addr(v);
4645  value_free(v);
4646  v = NULL;
4647 
4648  if (mm_addr) {
4649  v = target_load_value_member(target,tlctxt,
4650  taskv,"mm.pgd",NULL,LOAD_FLAG_NONE);
4651  if (!v) {
4652  verror("could not load thread %"PRIiTID" pgd (for cr3 tracking)\n",
4653  tid);
4654  goto errout;
4655  }
4656  /* Load a unum, so we get the right number of bytes read. */
4657  pgd = (uint64_t)v_unum(v);
4658  value_free(v);
4659  v = NULL;
4660 
4661  /* If pgd was NULL, try task_struct.active_mm.pgd */
4662  if (pgd == 0) {
4663  v = target_load_value_member(target,tlctxt,
4664  taskv,"active_mm.pgd",NULL,
4665  LOAD_FLAG_NONE);
4666  if (!v) {
4667  vwarn("could not load thread %"PRIiTID" (active_mm) pgd (for cr3 tracking)\n",
4668  tid);
4669  goto errout;
4670  }
4671  /* Load a unum, so we get the right number of bytes read. */
4672  pgd = (uint64_t)v_unum(v);
4673  value_free(v);
4674  v = NULL;
4675  }
4676  }
4677  else
4678  pgd = lstate->pgd_addr;
4679  }
4680 
4681  v = target_load_value_member(target,tlctxt,
4682  threadinfov,"flags",NULL,LOAD_FLAG_NONE);
4683  if (!v) {
4684  verror("could not load thread_info->flags in current thread; BUG?\n");
4685  /* errno should be set for us. */
4686  goto errout;
4687  }
4688  tiflags = v_unum(v);
4689  value_free(v);
4690  v = NULL;
4691 
4692  /*
4693  * Now, update the current thread with the value info we just
4694  * loaded. If it's not the global thread (irq context), we check
4695  * our cache, and create/delete as needed.
4696  */
4697 
4698  /* Check the cache: */
4699  if ((tthread = (struct target_thread *) \
4700  g_hash_table_lookup(target->threads,(gpointer)(uintptr_t)tid))) {
4701  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
4702  /*
4703  * Check if this is a cached entry for an old task. Except
4704  * don't check if the thread is TID_GLOBAL, since we must
4705  * always leave that meta-thread in the cache; it doesn't
4706  * represent a real system thread.
4707  */
4708  if (tid != TID_GLOBAL
4709  && (tthread->tgid != tgid
4710  || (taskv && ltstate->task_struct_addr != value_addr(taskv)))) {
4712  "deleting non-matching cached old thread %"PRIiTID
4713  " (thread %p, tpc %p)\n",
4714  tid,tthread,tthread->tpc);
4715  event = target_create_event(target,tthread,
4716  T_EVENT_OS_THREAD_EXITED,tthread);
4717  target_broadcast_event(target,event);
4718  target_detach_thread(target,tthread);
4719  event->thread = NULL;
4720 
4721  ltstate = NULL;
4722  tthread = NULL;
4723  }
4724  else {
4726  "found matching cached thread %"PRIiTID" (thread %p, tpc %p)\n",
4727  tid,tthread,tthread->tpc);
4728  }
4729  }
4730 
4731  if (!tthread) {
4732  /* Build a new one. */
4733  ltstate = (struct os_linux_thread_state *)calloc(1,sizeof(*ltstate));
4734  tthread = target_create_thread(target,tid,NULL,ltstate);
4735  g_hash_table_insert(lstate->task_struct_addr_to_thread,
4736  (gpointer)value_addr(taskv),tthread);
4737 
4738  event = target_create_event(target,tthread,
4739  T_EVENT_OS_THREAD_CREATED,tthread);
4740  target_broadcast_event(target,event);
4741 
4743  "built new thread %"PRIiTID" (thread %p, tpc %p)\n",
4744  tid,tthread,tthread->tpc);
4745  }
4746 
4747  /*
4748  * Just conform the current thread to the global thread's context.
4749  */
4750  tthread->tidctxt = ctidctxt;
4751 
4752  /*
4753  * If this is a user-level thread that is in the kernel, pull our
4754  * user level-saved regs off the stack and put them in alt_context.
4755  */
4756  if (mm_addr && tthread->tidctxt == THREAD_CTXT_KERNEL) {
4757  ADDR stack_top;
4758  ADDR gs_base;
4759  ADDR old_rsp = 0;
4760  int irq_count = 0;
4761  int altstack = 0;
4762 
4763  if (target->arch->wordsize == 8) {
4764  /*
4765  * Check if we're on an irq stack.
4766  */
4767  os_linux_current_gs(target,&gs_base);
4768 
4769  if (!gs_base) {
4770  vwarn("%%gs is 0x0; cannot check if on irqstack!\n");
4771  }
4772  else if (lstate->irq_count_percpu_offset > 0) {
4773  if (!target_read_addr(target,
4774  gs_base + lstate->irq_count_percpu_offset,
4775  sizeof(int),
4776  (unsigned char *)&irq_count)) {
4777  vwarn("could not read %%gs:irq_count"
4778  " (0x%"PRIxADDR":%"PRIiOFFSET");"
4779  "; assuming not on irqstack!\n",
4780  (ADDR)gs_base,lstate->irq_count_percpu_offset);
4781  }
4782 
4783  vdebug(9,LA_TARGET,LF_OSLINUX,"irq_count = %d\n",irq_count);
4784  }
4785 
4786  if (spval < (value_addr(threadinfov) + THREAD_SIZE)
4787  && spval >= value_addr(threadinfov))
4788  altstack = 0;
4789  else
4790  altstack = 1;
4791 
4792  if (altstack)
4793  /*
4794  * On x86_64, there are multiple IRQ stacks for
4795  * different IRQs -- and they are per-processor.
4796  *
4797  * What really sucks is that most of the stacks are 4K,
4798  * but the debug stacks (currently) are 8K!
4799  *
4800  * So what we do here is, if we're not on the real task
4801  * stack, assume that we're not very deep into (one of)
4802  * the other stack yet -- that it's on the first page.
4803  * Then our userspace regs are on the "top" (base) of
4804  * the page, minus ptregs size!
4805  */
4806  stack_top = (spval & ~(ADDR)(PAGE_SIZE - 1)) + PAGE_SIZE;
4807  else
4808  stack_top = value_addr(threadinfov) + THREAD_SIZE;
4809 
4810 
4811  ptregs_stack_addr =
4812  stack_top - 0 - symbol_get_bytesize(lstate->pt_regs_type);
4813  }
4814  else {
4815  stack_top = value_addr(threadinfov) + THREAD_SIZE;
4816  ptregs_stack_addr =
4817  stack_top - 8 - symbol_get_bytesize(lstate->pt_regs_type);
4818  }
4819 
4821  "loading userspace regs from kernel stack for user tid %d"
4822  " currently in kernel (altstack = %d)!\n",
4823  tid,altstack);
4824 
4825  v = target_load_addr_real(target,ptregs_stack_addr,
4828  if (!v) {
4829  verror("could not load stack register save frame task %"PRIiTID"!\n",
4830  tid);
4831  goto errout;
4832  }
4833 
4834  if (vdebug_is_on(13,LA_TARGET,LF_OSLINUX)) {
4835  char *pp;
4836  vdebug(8,LA_TARGET,LF_OSLINUX," current stack:\n");
4837  pp = v->buf + v->bufsiz - target->arch->wordsize;
4838  while (pp >= v->buf) {
4839  if (target->arch->wordsize == 8) {
4841  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
4842  value_addr(v) + (pp - v->buf),*(uint64_t *)pp);
4843  }
4844  else {
4846  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
4847  value_addr(v) + (pp - v->buf),*(uint32_t *)pp);
4848  }
4849  pp -= target->arch->wordsize;
4850  }
4851  vdebug(13,LA_TARGET,LF_OSLINUX,"\n");
4852  }
4853 
4854  /* Copy the first range. */
4855  if (target->arch->wordsize == 8) {
4857  REG_X86_64_R15,((uint64_t *)v->buf)[0]);
4859  REG_X86_64_R14,((uint64_t *)v->buf)[1]);
4861  REG_X86_64_R13,((uint64_t *)v->buf)[2]);
4863  REG_X86_64_R12,((uint64_t *)v->buf)[3]);
4865  REG_X86_64_RBP,((uint64_t *)v->buf)[4]);
4867  REG_X86_64_RBX,((uint64_t *)v->buf)[5]);
4869  REG_X86_64_R11,((uint64_t *)v->buf)[6]);
4871  REG_X86_64_R10,((uint64_t *)v->buf)[7]);
4873  REG_X86_64_R9,((uint64_t *)v->buf)[8]);
4875  REG_X86_64_R8,((uint64_t *)v->buf)[9]);
4877  REG_X86_64_RAX,((uint64_t *)v->buf)[10]);
4879  REG_X86_64_RCX,((uint64_t *)v->buf)[11]);
4881  REG_X86_64_RDX,((uint64_t *)v->buf)[12]);
4883  REG_X86_64_RSI,((uint64_t *)v->buf)[13]);
4885  REG_X86_64_RDI,((uint64_t *)v->buf)[14]);
4886  //memcpy(&ltstate->alt_context.user_regs,v->buf,8 * 15);
4887  }
4888  else {
4890  REG_X86_EBX,((uint32_t *)v->buf)[0]);
4892  REG_X86_ECX,((uint32_t *)v->buf)[1]);
4894  REG_X86_EDX,((uint32_t *)v->buf)[2]);
4896  REG_X86_ESI,((uint32_t *)v->buf)[3]);
4898  REG_X86_EDI,((uint32_t *)v->buf)[4]);
4900  REG_X86_EBP,((uint32_t *)v->buf)[5]);
4902  REG_X86_EAX,((uint32_t *)v->buf)[6]);
4903  //memcpy(&ltstate->alt_context.user_regs,v->buf,4 * 7);
4904  }
4905 
4906  /* Copy the second range. */
4912  ADDR ssp;
4913  int ip_offset = lstate->pt_regs_ip_offset;
4914  if (__WORDSIZE == 64 || target->arch->wordsize == 8) {
4915  uint64_t rv;
4916  rv = ((uint64_t *)(v->buf + ip_offset))[0];
4918  REG_X86_64_RIP,rv);
4919  rv = ((uint64_t *)(v->buf + ip_offset))[1];
4921  REG_X86_64_CS,rv);
4922  rv = ((uint64_t *)(v->buf + ip_offset))[2];
4924  REG_X86_64_RFLAGS,rv);
4925  ssp = rv = ((uint64_t *)(v->buf + ip_offset))[3];
4927  REG_X86_64_RSP,rv);
4928  rv = ((uint64_t *)(v->buf + ip_offset))[4];
4930  REG_X86_64_SS,rv);
4931  }
4932  else {
4933  uint32_t rv;
4934  rv = ((uint32_t *)(v->buf + ip_offset))[0];
4936  REG_X86_EIP,rv);
4937  rv = ((uint32_t *)(v->buf + ip_offset))[1];
4939  REG_X86_CS,rv);
4940  rv = ((uint32_t *)(v->buf + ip_offset))[2];
4942  REG_X86_EFLAGS,rv);
4943  ssp = rv = ((uint32_t *)(v->buf + ip_offset))[3];
4945  REG_X86_ESP,rv);
4946  rv = ((uint32_t *)(v->buf + ip_offset))[4];
4948  REG_X86_SS,rv);
4949  }
4950 
4951  /*
4952  * ds, es, fs, gs are all special; see other comments.
4953  */
4954  if (target->arch->type == ARCH_X86
4955  && !lstate->thread_struct_has_ds_es && lstate->pt_regs_has_ds_es) {
4956  uint32_t rv;
4957  /* XXX: this works because we know the location of (x)ds/es;
4958  * it's only on i386/x86; and because Xen pads its
4959  * cpu_user_regs structs from u16s to ulongs for segment
4960  * registers. :)
4961  */
4962  rv = *(uint32_t *)(v->buf + 7 * target->arch->wordsize);
4964  REG_X86_DS,rv);
4965  rv = *(uint32_t *)(v->buf + 8 * target->arch->wordsize);
4967  REG_X86_ES,rv);
4968  }
4969  if (target->arch->type == ARCH_X86
4970  && !lstate->thread_struct_has_fs && lstate->pt_regs_has_fs_gs) {
4971  uint32_t rv;
4972  /* XXX: this is only true on newer x86 stuff; x86_64 and old
4973  * i386 stuff did not save it on the stack.
4974  */
4975  rv = *(uint32_t *)(v->buf + 9 * target->arch->wordsize);
4977  REG_X86_FS,rv);
4978  }
4979 
4980  if (!target_read_addr(target,gs_base + 0xbf00,
4981  target->arch->wordsize,
4982  (unsigned char *)&old_rsp)) {
4983  verror("could not read %%gs:old_rsp (%%gs+0xbf00)"
4984  " (0x%"PRIxADDR":%"PRIxOFFSET"); cannot continue!\n",
4985  gs_base,0xbf00UL);
4986  if (!errno)
4987  errno = EFAULT;
4988  return 0;
4989  }
4990 
4992  "stacked rsp 0x%"PRIxADDR", old_rsp 0x%"PRIxADDR"\n",
4993  ssp,old_rsp);
4994 
4995  if (target->arch->wordsize == 8 || __WORDSIZE == 64)
4997  REG_X86_64_RSP,old_rsp);
4998  else
5000  REG_X86_ESP,old_rsp);
5001 
5002  /*
5003 #if __WORDSIZE == 64
5004  int r_offset = offsetof(struct vcpu_guest_context,user_regs.r12);
5005  vdebug(5,LA_TARGET,LF_OSLINUX,
5006  "stacked r12 0x%"PRIxADDR", adjusting\n",
5007  *(ADDR *)(((char *)&ltstate->alt_context) + r_offset));
5008  *(ADDR *)(((char *)&ltstate->alt_context) + r_offset) += 8;
5009 #endif
5010  */
5011 
5012  value_free(v);
5013  v = NULL;
5014  }
5015 
5016  if (taskv) {
5017  if (ltstate->task_struct) {
5018  vwarn("stale task_struct for thread %"PRIiTID"!\n",tid);
5019  value_free(ltstate->task_struct);
5020  ltstate->task_struct = NULL;
5021  }
5022  ltstate->task_struct_addr = value_addr(taskv);
5023  ltstate->task_struct = taskv;
5024  ltstate->task_flags = task_flags;
5025  ltstate->group_leader = group_leader;
5026  }
5027 
5028  /*
5029  * Check for stale cached values. These should not be here, but... !
5030  */
5031  if (ltstate->thread_struct) {
5032  vwarn("stale thread_struct for thread %"PRIiTID"!\n",tid);
5033  value_free(ltstate->thread_struct);
5034  ltstate->thread_struct = NULL;
5035  }
5036  if (ltstate->thread_info) {
5037  vwarn("stale thread_info for thread %"PRIiTID"!\n",tid);
5038  value_free(ltstate->thread_info);
5039  ltstate->thread_info = NULL;
5040  }
5041 
5042  ltstate->thread_info = threadinfov;
5043  ltstate->thread_info_flags = tiflags;
5044  ltstate->thread_info_preempt_count = preempt_count;
5045 
5046  /*
5047  * We don't bother loading this, because it's our "current" thread
5048  * -- all the state in the thread_struct is directly accessible from
5049  * hardware.
5050  */
5051  ltstate->thread_struct = NULL;
5052  /* NB: ptregs might have been loaded; save addr. */
5054  ltstate->mm_addr = mm_addr;
5055  ltstate->pgd = pgd;
5056  /*
5057  * If the current thread is not the global thread, fill in a little
5058  * bit more info for the global thread.
5059  */
5060  if (tthread != target->global_thread) {
5061  gtstate = (struct os_linux_thread_state *) \
5063 
5064  /*
5065  * Don't copy in any of the other per-xen thread state; we want
5066  * to force users to load and operate on real threads for any
5067  * other information. The global thread only has thread_info in
5068  * interrupt context.
5069  */
5070  gtstate->task_struct_addr = 0;
5071  gtstate->task_struct = NULL;
5072  gtstate->task_flags = 0;
5073  gtstate->group_leader = 0;
5074  gtstate->thread_struct = NULL;
5075  /* Still don't save ptregs for global thread */
5076  gtstate->ptregs_stack_addr = 0;
5077  gtstate->mm_addr = 0;
5078  gtstate->pgd = 0;
5079 
5080  /* BUT, do copy in thread_info. */
5081  if (gtstate->thread_info) {
5082  vwarn("stale thread_info for global thread %"PRIiTID"!\n",TID_GLOBAL);
5083  value_free(gtstate->thread_info);
5084  gtstate->thread_info = NULL;
5085  }
5086  gtstate->thread_info = value_clone(threadinfov);
5087  gtstate->thread_info_flags = tiflags;
5088  gtstate->thread_info_preempt_count = preempt_count;
5089 
5090  if (mm_addr)
5092  else
5094 
5095  if (tthread->name)
5096  free(tthread->name);
5097  tthread->name = comm;
5098  tthread->ptid = ptid;
5099  tthread->tgid = tgid;
5100  tthread->uid = uid;
5101  tthread->gid = gid;
5102  comm = NULL;
5103  }
5104 
5105  if (v)
5106  value_free(v);
5107  if (comm)
5108  free(comm);
5109 
5110  return tthread;
5111 
5112  errout:
5113  if (v)
5114  value_free(v);
5115  if (comm)
5116  free(comm);
5117  if (threadinfov)
5118  value_free(threadinfov);
5119  if (taskv)
5120  value_free(taskv);
5121  if (ltstate) {
5122  ltstate->thread_info = NULL;
5123  ltstate->thread_struct = NULL;
5124  ltstate->task_struct = NULL;
5125  }
5126 
5127  /* XXX: should we really set this here? */
5128  target->current_thread = target->global_thread;
5129 
5130  vwarn("error loading current thread; trying to use default thread\n");
5131  errno = 0;
5132 
5133  return target->global_thread;
5134 }
5135 
5136 void os_linux_free_thread_state(struct target *target,void *state) {
5137  struct os_linux_state *lstate =
5138  (struct os_linux_state *)target->personality_state;
5139  struct os_linux_thread_state *ltstate =
5140  (struct os_linux_thread_state *)state;
5141 
5142  /*
5143  * XXX: this stinks, but it's the only time we have to remove a
5144  * thread from our hash cache of task_struct_addrs_to_thread .
5145  */
5146  if (lstate->task_struct_addr_to_thread)
5147  g_hash_table_remove(lstate->task_struct_addr_to_thread,
5148  (gpointer)ltstate->task_struct_addr);
5149 
5150  if (ltstate->thread_struct) {
5151  value_free(ltstate->thread_struct);
5152  ltstate->thread_struct = NULL;
5153  }
5154  if (ltstate->thread_info) {
5155  value_free(ltstate->thread_info);
5156  ltstate->thread_info = NULL;
5157  }
5158  if (ltstate->task_struct) {
5159  value_free(ltstate->task_struct);
5160  ltstate->task_struct = NULL;
5161  }
5162 
5163  free(state);
5164 }
5165 
5166 struct target_thread *os_linux_load_thread_from_value(struct target *target,
5167  struct value *taskv) {
5168  struct os_linux_state *lstate =
5169  (struct os_linux_state *)target->personality_state;
5170  struct target_thread *tthread;
5171  struct target_thread *ptthread;
5172  struct os_linux_thread_state *ltstate = NULL;
5173  tid_t tid;
5174  tid_t ptid = -1;
5175  tid_t tgid = 0;
5176  unum_t task_flags = 0;
5177  ADDR group_leader;
5178  struct value *threadinfov = NULL;
5179  unum_t tiflags = 0;
5180  num_t preempt_count = 0;
5181  struct value *threadv = NULL;
5182  struct value *v = NULL;
5183  int iskernel = 0;
5184  ADDR stack_top;
5185  char *comm = NULL;
5186  ADDR stack_member_addr;
5187  int i;
5188  int uid;
5189  int gid;
5190  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
5191  thread_ctxt_t ptregs_tidctxt;
5192  struct target_event *event;
5193 
5194  vdebug(5,LA_TARGET,LF_OSLINUX,"loading\n");
5195 
5196  v = target_load_value_member(target,tlctxt,
5197  taskv,"pid",NULL,LOAD_FLAG_NONE);
5198  if (!v) {
5199  verror("could not load pid in task value; BUG?\n");
5200  /* errno should be set for us. */
5201  goto errout;
5202  }
5203  tid = v_i32(v);
5204  value_free(v);
5205  v = NULL;
5206 
5207  v = target_load_value_member(target,tlctxt,
5208  taskv,"parent",NULL,LOAD_FLAG_NONE);
5209  if (!v) {
5210  verror("could not load parent in task value; BUG?\n");
5211  /* errno should be set for us. */
5212  goto errout;
5213  }
5214  else if (v_addr(v) != value_addr(taskv)) {
5215  ptthread = (struct target_thread *) \
5216  g_hash_table_lookup(lstate->task_struct_addr_to_thread,
5217  (gpointer)v_addr(v));
5218  if (!ptthread) {
5219  /* Gotta load it. */
5220  value_free(v);
5221  v = target_load_value_member(target,tlctxt,
5222  taskv,"parent",NULL,
5224  if (!v) {
5225  verror("could not load parent value from task;"
5226  " ptid will be invalid!\n");
5227  }
5228  else {
5229  ptthread = os_linux_load_thread_from_value(target,v);
5230  if (!ptthread) {
5231  verror("could not load parent thread from value;"
5232  " ptid will be invalid!\n");
5233  }
5234  else {
5236  "loaded tid %"PRIiTID" parent %"PRIiTID"\n",
5237  tid,ptthread->tid);
5238  /* Don't free it! */
5239  v = NULL;
5240  }
5241  }
5242  }
5243  else
5245  "tid %"PRIiTID" parent %"PRIiTID" already loaded\n",
5246  tid,ptthread->tid);
5247 
5248  if (ptthread)
5249  ptid = ptthread->tid;
5250  }
5251  else if (tid != 0) {
5252  /* The parent of 0 is 0, so that is ok. */
5253  vwarn("tid %"PRIiTID" ->parent is itself!\n",tid);
5254  }
5255  if (v) {
5256  value_free(v);
5257  v = NULL;
5258  }
5259 
5260  v = target_load_value_member(target,tlctxt,
5261  taskv,lstate->task_uid_member_name,
5262  NULL,LOAD_FLAG_NONE);
5263  if (!v) {
5264  verror("could not load %s in task value; BUG?\n",
5265  lstate->task_uid_member_name);
5266  uid = -1;
5267  }
5268  else {
5269  uid = v_i32(v);
5270  value_free(v);
5271  v = NULL;
5272  }
5273 
5274  v = target_load_value_member(target,tlctxt,
5275  taskv,lstate->task_gid_member_name,
5276  NULL,LOAD_FLAG_NONE);
5277  if (!v) {
5278  verror("could not load %s in task value; BUG?\n",
5279  lstate->task_gid_member_name);
5280  gid = -1;
5281  }
5282  else {
5283  gid = v_i32(v);
5284  value_free(v);
5285  v = NULL;
5286  }
5287 
5288  v = target_load_value_member(target,tlctxt,
5289  taskv,"comm",NULL,LOAD_FLAG_NONE);
5290  if (!v) {
5291  verror("could not load comm in task value; BUG?\n");
5292  /* errno should be set for us. */
5293  goto errout;
5294  }
5295  comm = strndup(v->buf,v->bufsiz);
5296  value_free(v);
5297  v = NULL;
5298 
5299  v = target_load_value_member(target,tlctxt,
5300  taskv,"tgid",NULL,LOAD_FLAG_NONE);
5301  if (!v) {
5302  verror("could not load tgid in task %"PRIiTID"; BUG?\n",tid);
5303  /* errno should be set for us. */
5304  goto errout;
5305  }
5306  tgid = (tid_t)v_num(v);
5307  value_free(v);
5308  v = NULL;
5309 
5310  v = target_load_value_member(target,tlctxt,
5311  taskv,"flags",NULL,LOAD_FLAG_NONE);
5312  if (!v) {
5313  verror("could not load flags in task %"PRIiTID"; BUG?\n",tid);
5314  /* errno should be set for us. */
5315  goto errout;
5316  }
5317  task_flags = v_unum(v);
5318  value_free(v);
5319  v = NULL;
5320 
5321  v = target_load_value_member(target,tlctxt,
5322  taskv,"group_leader",NULL,LOAD_FLAG_NONE);
5323  if (!v) {
5324  verror("could not load group_leader in task %"PRIiTID"; BUG?\n",tid);
5325  /* errno should be set for us. */
5326  goto errout;
5327  }
5328  group_leader = v_addr(v);
5329  value_free(v);
5330  v = NULL;
5331 
5332  /*
5333  * Before loading anything else, check the cache.
5334  */
5335  tthread = target_lookup_thread(target,tid);
5336  if (tthread) {
5337  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
5338 
5339  /* Check if this is a cached entry for an old task */
5340  if (tthread->tgid != tgid
5341  || ltstate->task_struct_addr != value_addr(taskv)) {
5342  event = target_create_event(target,tthread,
5343  T_EVENT_OS_THREAD_EXITED,tthread);
5344  target_broadcast_event(target,event);
5345  event->thread = NULL;
5346  target_detach_thread(target,tthread);
5347 
5348  ltstate = NULL;
5349  tthread = NULL;
5350  }
5351  }
5352 
5353  if (!tthread) {
5354  /* Build a new one. */
5355  ltstate = (struct os_linux_thread_state *)calloc(1,sizeof(*ltstate));
5356 
5357  /* XXX: how to init backend's state??? */
5358  tthread = target_create_thread(target,tid,NULL,ltstate);
5359  g_hash_table_insert(lstate->task_struct_addr_to_thread,
5360  (gpointer)value_addr(taskv),tthread);
5361 
5362  event = target_create_event(target,tthread,
5363  T_EVENT_OS_THREAD_CREATED,tthread);
5364  target_broadcast_event(target,event);
5365  }
5366  else {
5367  /*
5368  * If this is the current thread, we cannot load it from its
5369  * task_struct value (especially its register state!). The
5370  * current_thread should have been loaded by now, so if it has
5371  * been loaded, don't reload from the thread stack (because the
5372  * thread stack does not have saved registers because the thread
5373  * is running!).
5374  *
5375  * XXX: to support SMP, we would have to check the task_struct's
5376  * running status, and only load from CPU in those cases.
5377  */
5378  if (tthread == target->current_thread
5379  && OBJVALID(target->current_thread)) {
5381  "not loading running, valid current thread %"PRIiTID" from"
5382  " task_struct 0x%"PRIxADDR"; loaded from CPU of course\n",
5383  tid,value_addr(taskv));
5384  return target->current_thread;
5385  }
5386  }
5387 
5388  if (lstate->task_struct_has_thread_info) {
5389  threadinfov = target_load_value_member(target,tlctxt,
5390  taskv,"thread_info",NULL,
5392  if (!threadinfov) {
5393  verror("could not load thread_info in task %"PRIiTID"; BUG?\n",tid);
5394  /* errno should be set for us. */
5395  goto errout;
5396  }
5397  }
5398  else if (lstate->task_struct_has_stack) {
5399  v = target_load_value_member(target,tlctxt,
5400  taskv,"stack",NULL,LOAD_FLAG_NONE);
5401  if (!v) {
5402  verror("could not load stack (thread_info) in task %"PRIiTID";"
5403  " BUG?\n",tid);
5404  /* errno should be set for us. */
5405  goto errout;
5406  }
5407  stack_member_addr = v_addr(v);
5408  value_free(v);
5409  v = NULL;
5410 
5411  threadinfov = target_load_type(target,lstate->thread_info_type,
5412  stack_member_addr,LOAD_FLAG_NONE);
5413  if (!threadinfov) {
5414  verror("could not load stack (thread_info) in task %"PRIiTID";"
5415  " BUG?\n",tid);
5416  goto errout;
5417  }
5418  }
5419  else {
5420  verror("cannot load thread_info/stack; no thread support!\n");
5421  goto errout;
5422  }
5423 
5424  v = target_load_value_member(target,tlctxt,
5425  threadinfov,"flags",NULL,LOAD_FLAG_NONE);
5426  if (!v) {
5427  verror("could not load thread_info.flags in task %"PRIiTID"; BUG?\n",tid);
5428  /* errno should be set for us. */
5429  goto errout;
5430  }
5431  tiflags = v_unum(v);
5432  value_free(v);
5433  v = NULL;
5434 
5435  v = target_load_value_member(target,tlctxt,
5436  threadinfov,lstate->thread_info_preempt_count_name,NULL,LOAD_FLAG_NONE);
5437  if (!v) {
5438  verror("could not load thread_info.preempt_count in task %"PRIiTID";"
5439  " BUG?\n",tid);
5440  /* errno should be set for us. */
5441  goto errout;
5442  }
5443  preempt_count = v_num(v);
5444  value_free(v);
5445  v = NULL;
5446 
5447  ltstate->task_struct_addr = value_addr(taskv);
5448  ltstate->task_struct = taskv;
5449  tthread->ptid = ptid;
5450  tthread->uid = uid;
5451  tthread->gid = gid;
5452  tthread->tgid = tgid;
5453  ltstate->task_flags = task_flags;
5454  ltstate->group_leader = group_leader;
5455  ltstate->thread_info = threadinfov;
5456  ltstate->thread_info_flags = tiflags;
5457  ltstate->thread_info_preempt_count = preempt_count;
5458 
5459  /*
5460  * If we have the thread, we can load as much of the stuff in the
5461  * vcpu_guest_context struct as the kernel contains!
5462  */
5463 
5464  v = target_load_value_member(target,tlctxt,
5465  taskv,"mm",NULL,LOAD_FLAG_NONE);
5466  if (!v) {
5467  verror("could not see if thread %"PRIiTID" was kernel or user\n",tid);
5468  goto errout;
5469  }
5470  ltstate->mm_addr = v_addr(v);
5471  if (ltstate->mm_addr == 0)
5472  iskernel = 1;
5473  value_free(v);
5474  v = NULL;
5475 
5476  if (ltstate->mm_addr) {
5477  v = target_load_value_member(target,tlctxt,
5478  taskv,"mm.pgd",NULL,LOAD_FLAG_NONE);
5479  if (!v) {
5480  verror("could not load thread %"PRIiTID" (mm) pgd (for cr3 tracking)\n",
5481  tid);
5482  goto errout;
5483  }
5484  /* Load a unum, so we get the right number of bytes read. */
5485  ltstate->pgd = (uint64_t)v_unum(v);
5486  value_free(v);
5487  v = NULL;
5488 
5489  /* If pgd was NULL, try task_struct.active_mm.pgd */
5490  if (ltstate->pgd == 0) {
5491  v = target_load_value_member(target,tlctxt,
5492  taskv,"active_mm.pgd",NULL,
5493  LOAD_FLAG_NONE);
5494  if (!v) {
5495  vwarn("could not load thread %"PRIiTID" (active_mm) pgd (for cr3 tracking)\n",
5496  tid);
5497  goto errout;
5498  }
5499  /* Load a unum, so we get the right number of bytes read. */
5500  ltstate->pgd = (uint64_t)v_unum(v);
5501  value_free(v);
5502  v = NULL;
5503  }
5504  }
5505  else
5506  ltstate->pgd = lstate->pgd_addr;
5507 
5508  /*
5509  * In our world, a Linux thread will not be executing an interrupt
5510  * top/bottom half (ISR or softirq) if it is not running (i.e.,
5511  * ISRs/softirqs are not preemptible). So, if the sleeping thread
5512  * is in kernel context, the task's state (registers) are on the
5513  * stack in the right place, unless we are in a section of the code
5514  * that is setting up the stack or tearing it down (i.e., preempted
5515  * during syscall init or something -- unless this is not possible
5516  * if the kernel disables interrupts in those critical
5517  * sections...). BUT, even if this is true, the current machine
5518  * state will have been pushed on the stack to handle the interrupt
5519  * (and perhaps the following context switch, if there is one after
5520  * servicing the ISR/softirq(s)).
5521  *
5522  * Thus, we don't have to check what the thread is executing.
5523  *
5524  * It doesn't matter whether the thread is a kernel thread or not.
5525  */
5526  if (iskernel) {
5529 
5530  tthread->tidctxt = THREAD_CTXT_KERNEL;
5531  ptregs_tidctxt = THREAD_CTXT_KERNEL;
5532  //thi_tidctxt = THREAD_CTXT_KERNEL;
5533  }
5534  else {
5537 
5538  tthread->tidctxt = THREAD_CTXT_KERNEL;
5539  ptregs_tidctxt = THREAD_CTXT_USER;
5540  //thi_tidctxt = THREAD_CTXT_USER;
5541  }
5542 
5543  /* We are entirely loading this thread, not the backend, so nuke it. */
5544  target_regcache_zero(target,tthread,THREAD_CTXT_KERNEL);
5545  target_regcache_zero(target,tthread,THREAD_CTXT_USER);
5546 
5547  if (tthread->name)
5548  free(tthread->name);
5549  tthread->name = comm;
5550  comm = NULL;
5551 
5552  /*
5553  * Load the stored registers from the kernel stack; except fs/gs and
5554  * the debug regs are in the task_struct->thread thread_struct
5555  * struct.
5556  */
5557  threadv = target_load_value_member(target,tlctxt,
5558  taskv,"thread",NULL,LOAD_FLAG_NONE);
5559  if (!threadv) {
5560  verror("could not load thread_struct for task %"PRIiTID"!\n",tid);
5561  goto errout;
5562  }
5563 
5564  ltstate->thread_struct = threadv;
5565 
5566  v = target_load_value_member(target,tlctxt,
5567  threadv,lstate->thread_sp_member_name,
5568  NULL,LOAD_FLAG_NONE);
5569  if (!v) {
5570  verror("could not load thread.%s for task %"PRIiTID"!\n",
5571  lstate->thread_sp_member_name,tid);
5572  goto errout;
5573  }
5574  ltstate->esp = v_addr(v);
5575  value_free(v);
5576  v = NULL;
5577 
5578  /* The stack base is also the value of the task_struct->thread_info ptr. */
5579  ltstate->stack_base = value_addr(threadinfov);
5580  stack_top = ltstate->stack_base + THREAD_SIZE;
5581 
5582  /* See include/asm-i386/processor.h . And since it doesn't explain
5583  * why it is subtracting 8, it's because fs/gs are not pushed on the
5584  * stack, so the ptrace regs struct doesn't really match with what's
5585  * on the stack ;).
5586  */
5587  if (iskernel && preempt_count) {
5588  if (target->arch->wordsize == 8) {
5589  ltstate->ptregs_stack_addr =
5590  stack_top - 0 - symbol_get_bytesize(lstate->pt_regs_type);
5591  }
5592  else {
5593  ltstate->ptregs_stack_addr =
5594  stack_top - 8 - symbol_get_bytesize(lstate->pt_regs_type);
5595  }
5596  //ltstate->ptregs_stack_addr = ltstate->esp - 8 - 15 * 4;
5597  // + 8 - 7 * 4; // - 8 - 15 * 4;
5598  }
5599  /*
5600  * Registers are not saved if it's a sleeping, non-preempted kernel
5601  * thread. All that was saved is the esp and eip, and fs/gs, in the
5602  * thread struct. Registers are only saved on kernel interrupt, or
5603  * mode switch from user to kernel mode. The best we could do is
5604  * look into schedule()'s frame and look at its saved registers so
5605  * that we could see what schedule's caller will have -- and then
5606  * look and see what the caller saved. Anything else is trashed.
5607  */
5608  else if (iskernel) {
5609  ltstate->ptregs_stack_addr = 0;
5610  /*
5611  v = target_load_addr_real(target,esp0,LOAD_FLAG_NONE,32*4);
5612  int i;
5613  for (i = 0; i < 32; ++i) {
5614  vwarn("%d esp[%d] = 0x%x\n",tid,i,((int *)v->buf)[i]);
5615  }
5616  value_free(v);
5617  v = NULL;
5618  */
5619  }
5620  else if (target->arch->wordsize == 8) {
5621  ltstate->ptregs_stack_addr =
5622  stack_top - 0 - symbol_get_bytesize(lstate->pt_regs_type);
5623  }
5624  else {
5625  ltstate->ptregs_stack_addr =
5626  stack_top - 8 - symbol_get_bytesize(lstate->pt_regs_type);
5627  }
5628 
5630  "esp=%"PRIxADDR",stack_base=%"PRIxADDR",stack_top=%"PRIxADDR
5631  ",ptregs_stack_addr=%"PRIxADDR"\n",
5632  ltstate->esp,stack_top,ltstate->stack_base,ltstate->ptregs_stack_addr);
5633 
5634  v = target_load_value_member(target,tlctxt,
5635  threadv,lstate->thread_sp0_member_name,
5636  NULL,LOAD_FLAG_NONE);
5637  if (!v)
5638  vwarn("could not load thread.%s for task %"PRIiTID"!\n",
5639  lstate->thread_sp0_member_name,tid);
5640  ltstate->esp0 = v_addr(v);
5641  value_free(v);
5642  v = NULL;
5643 
5644  /*
5645  * On x86_64, %ip is not tracked -- there's no point anyway --
5646  * either it's in the scheduler at context switch, or it's at
5647  * ret_from_fork -- no point loading. But still, it's on the kernel
5648  * stack at *(thread.sp - 8). That's how we load it.
5649  */
5650  if (lstate->thread_ip_member_name) {
5651  v = target_load_value_member(target,tlctxt,
5652  threadv,lstate->thread_ip_member_name,
5653  NULL,LOAD_FLAG_NONE);
5654  if (!v)
5655  vwarn("could not load thread.%s for task %"PRIiTID"!\n",
5656  lstate->thread_ip_member_name,tid);
5657  else {
5658  ltstate->eip = v_addr(v);
5659  value_free(v);
5660  v = NULL;
5661  }
5662  }
5663  else {
5664  /*
5665  * Check thread_info->flags & TIF_FORK; if that is set, we're
5666  * returning at ret_from_fork . Otherwise, we're at the point
5667  * in the scheduler where the new rsp is about to be changed.
5668  * Exactly where that is is somewhat debatable. To make CFA
5669  * make the most sense, we want the old rsp (which is the one in
5670  * the thread struct, for the thread that is sleeping in the
5671  * scheduler) to actually be the one still in %rsp. That means
5672  * we are stopped right at the instruction that moves the new
5673  * rsp into %rsp. On x86_64, the instruction prefix that
5674  * accomplishes that is 48 8b a6 (from kernels from 2.6.18 to
5675  * 3.8.x), and that instruction is followed by the call to
5676  * __switch_to . So we're lucky -- and we look for this prefix
5677  * in __schedule() (contains the %rsp swap in modern kernels) or
5678  * in schedule() if __schedule() does not exist (like in
5679  * 2.6.18). We do this above in postloadinit().
5680  */
5681 
5682  if (tiflags & 0x40000) {
5683  if (!lstate->ret_from_fork_addr)
5684  ltstate->eip = 0;
5685  else
5686  ltstate->eip = lstate->ret_from_fork_addr;
5687  }
5688  else {
5689  if (!lstate->schedule_swap_new_rsp_addr)
5690  ltstate->eip = 0;
5691  else
5692  ltstate->eip = lstate->schedule_swap_new_rsp_addr;
5693  }
5694  }
5695 
5696  /*
5697  * For old i386 stuff, fs/gs are in the thread data structure.
5698  * For newer x86 stuff, only gs is saved in thread_struct; fs is on
5699  * the stack.
5700  *
5701  * For x86_64, ds/es are saved in thread_struct; some threads have
5702  * 64-bit fs/gs bases in thread_struct; the fs/gs segment selectors
5703  * are saved in fsindex/gsindex. Not sure how to expose fs/gs in
5704  * this model... for now we ignore fsindex/gsindex.
5705  */
5706  REG reg;
5707  if (target->arch->type == ARCH_X86)
5708  reg = REG_X86_FS;
5709  else
5710  reg = REG_X86_64_FS;
5711  if (lstate->thread_struct_has_fs) {
5712  v = target_load_value_member(target,tlctxt,
5713  threadv,"fs",NULL,LOAD_FLAG_NONE);
5714  if (!v) {
5715  vwarn("could not load thread.fs for task %"PRIiTID"!\n",tid);
5716  goto errout;
5717  }
5718  else {
5719  ltstate->fs = v_u16(v);
5720  value_free(v);
5721  v = NULL;
5722  }
5723  }
5724  else {
5725  /* Load this from pt_regs below if we can. */
5726  ltstate->fs = 0;
5727  }
5728  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5729  reg,ltstate->fs);
5730 
5731  /* Everybody always has gs. */
5732  if (target->arch->type == ARCH_X86)
5733  reg = REG_X86_GS;
5734  else
5735  reg = REG_X86_64_GS;
5736  v = target_load_value_member(target,tlctxt,
5737  threadv,"gs",NULL,LOAD_FLAG_NONE);
5738  if (!v) {
5739  verror("could not load thread.gs for task %"PRIiTID"!\n",tid);
5740  goto errout;
5741  }
5742  else {
5743  ltstate->gs = v_u16(v);
5744  value_free(v);
5745  v = NULL;
5746  }
5747  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5748  reg,ltstate->gs);
5749 
5750  if (lstate->thread_struct_has_ds_es) {
5751  v = target_load_value_member(target,tlctxt,
5752  threadv,"ds",NULL,LOAD_FLAG_NONE);
5753  if (!v) {
5754  vwarn("could not load thread.ds for task %"PRIiTID"!\n",tid);
5755  goto errout;
5756  }
5757  else {
5758  if (target->arch->type == ARCH_X86_64)
5759  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5760  REG_X86_64_DS,v_u64(v));
5761  else
5762  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5763  REG_X86_DS,v_u32(v));
5764  value_free(v);
5765  v = NULL;
5766  }
5767 
5768  v = target_load_value_member(target,tlctxt,
5769  threadv,"es",NULL,LOAD_FLAG_NONE);
5770  if (!v) {
5771  vwarn("could not load thread.es for task %"PRIiTID"!\n",tid);
5772  goto errout;
5773  }
5774  else {
5775  if (target->arch->type == ARCH_X86_64)
5776  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5777  REG_X86_64_ES,v_u64(v));
5778  else
5779  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5780  REG_X86_ES,v_u32(v));
5781  value_free(v);
5782  v = NULL;
5783  }
5784  }
5785  else {
5786  /* Load this from pt_regs below if we can. */
5787  if (target->arch->type == ARCH_X86_64) {
5788  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5789  REG_X86_64_DS,0);
5790  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5791  REG_X86_64_ES,0);
5792  }
5793  else {
5794  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5795  REG_X86_DS,0);
5796  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5797  REG_X86_ES,0);
5798  }
5799  }
5800 
5801  if (ltstate->ptregs_stack_addr) {
5802  /*
5803  * The xen cpu_user_regs struct, and pt_regs struct, tend to
5804  * align (no pun intended) reasonably well. On i386, we can
5805  * copy pt_regs::(e)bx--(e)ax directly to cpu_user_regs::ebx--eax;
5806  * then copy pt_regs::(e)ip--(x)ss to cpu_user_regs::eip--ss.
5807  * For x86_64, the same is basically true; the ranges are
5808  * r15--(r)di, and (r)ip--ss.
5809  *
5810  * (The (X) prefixes are because the Linux kernel x86 and x86_64
5811  * struct pt_regs member names have changed over the years; but
5812  * by just doing this brutal copying, we can ignore all that --
5813  * which is faster from a value-loading perspective.)
5814  *
5815  * (This all works because Xen's cpu_user_regs has been
5816  * carefully mapped to x86 and x86_64 pt_regs structs, in the
5817  * specific register ranges listed (the Xen structs have some
5818  * other things in the middle and es/ds/fs/gs regs at the end,
5819  * so it's not a complete alignment.)
5820  */
5821  v = target_load_addr_real(target,ltstate->ptregs_stack_addr,
5824  if (!v) {
5825  verror("could not load stack register save frame task %"PRIiTID"!\n",
5826  tid);
5827  goto errout;
5828  }
5829 
5830  if (vdebug_is_on(13,LA_TARGET,LF_OSLINUX)) {
5831  char *pp;
5832  vdebug(8,LA_TARGET,LF_OSLINUX," current stack:\n");
5833  pp = v->buf + v->bufsiz - target->arch->wordsize;
5834  while (pp >= v->buf) {
5835  if (target->arch->wordsize == 8) {
5837  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
5838  value_addr(v) + (pp - v->buf),*(uint64_t *)pp);
5839  }
5840  else {
5842  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
5843  value_addr(v) + (pp - v->buf),*(uint32_t *)pp);
5844  }
5845  pp -= target->arch->wordsize;
5846  }
5847  vdebug(13,LA_TARGET,LF_OSLINUX,"\n");
5848  }
5849 
5850  /* Copy the first range. */
5851  if (target->arch->wordsize == 8) {
5852  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5853  REG_X86_64_R15,((uint64_t *)v->buf)[0]);
5854  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5855  REG_X86_64_R14,((uint64_t *)v->buf)[1]);
5856  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5857  REG_X86_64_R13,((uint64_t *)v->buf)[2]);
5858  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5859  REG_X86_64_R12,((uint64_t *)v->buf)[3]);
5860  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5861  REG_X86_64_RBP,((uint64_t *)v->buf)[4]);
5862  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5863  REG_X86_64_RBX,((uint64_t *)v->buf)[5]);
5864  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5865  REG_X86_64_R11,((uint64_t *)v->buf)[6]);
5866  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5867  REG_X86_64_R10,((uint64_t *)v->buf)[7]);
5868  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5869  REG_X86_64_R9,((uint64_t *)v->buf)[8]);
5870  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5871  REG_X86_64_R8,((uint64_t *)v->buf)[9]);
5872  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5873  REG_X86_64_RAX,((uint64_t *)v->buf)[10]);
5874  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5875  REG_X86_64_RCX,((uint64_t *)v->buf)[11]);
5876  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5877  REG_X86_64_RDX,((uint64_t *)v->buf)[12]);
5878  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5879  REG_X86_64_RSI,((uint64_t *)v->buf)[13]);
5880  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5881  REG_X86_64_RDI,((uint64_t *)v->buf)[14]);
5882  //memcpy(&lltstate->alt_context.user_regs,v->buf,8 * 15);
5883  }
5884  else {
5885  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5886  REG_X86_EBX,((uint32_t *)v->buf)[0]);
5887  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5888  REG_X86_ECX,((uint32_t *)v->buf)[1]);
5889  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5890  REG_X86_EDX,((uint32_t *)v->buf)[2]);
5891  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5892  REG_X86_ESI,((uint32_t *)v->buf)[3]);
5893  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5894  REG_X86_EDI,((uint32_t *)v->buf)[4]);
5895  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5896  REG_X86_EBP,((uint32_t *)v->buf)[5]);
5897  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5898  REG_X86_EAX,((uint32_t *)v->buf)[6]);
5899  //memcpy(&ltstate->alt_context.user_regs,v->buf,4 * 7);
5900  }
5901 
5902  /* Copy the second range. */
5908  //ADDR ssp;
5909  int ip_offset = lstate->pt_regs_ip_offset;
5910  if (__WORDSIZE == 64 || target->arch->wordsize == 8) {
5911  uint64_t rv;
5912  rv = ((uint64_t *)(v->buf + ip_offset))[0];
5913  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5914  REG_X86_64_RIP,rv);
5915  rv = ((uint64_t *)(v->buf + ip_offset))[1];
5916  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5917  REG_X86_64_CS,rv);
5918  rv = ((uint64_t *)(v->buf + ip_offset))[2];
5919  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5920  REG_X86_64_RFLAGS,rv);
5921  rv = ((uint64_t *)(v->buf + ip_offset))[3];
5922  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5923  REG_X86_64_RSP,rv);
5924  rv = ((uint64_t *)(v->buf + ip_offset))[4];
5925  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5926  REG_X86_64_SS,rv);
5927  }
5928  else {
5929  uint32_t rv;
5930  rv = ((uint32_t *)(v->buf + ip_offset))[0];
5931  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5932  REG_X86_EIP,rv);
5933  rv = ((uint32_t *)(v->buf + ip_offset))[1];
5934  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5935  REG_X86_CS,rv);
5936  rv = ((uint32_t *)(v->buf + ip_offset))[2];
5937  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5938  REG_X86_EFLAGS,rv);
5939  rv = ((uint32_t *)(v->buf + ip_offset))[3];
5940  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5941  REG_X86_ESP,rv);
5942  rv = ((uint32_t *)(v->buf + ip_offset))[4];
5943  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5944  REG_X86_SS,rv);
5945  }
5946 
5947  /*
5948  * ds, es, fs, gs are all special; see other comments.
5949  */
5950 
5951  if (target->arch->type == ARCH_X86
5952  && !lstate->thread_struct_has_ds_es && lstate->pt_regs_has_ds_es) {
5953  uint32_t rv;
5954  /* XXX: this works because we know the location of (x)ds/es;
5955  * it's only on i386/x86; and because Xen pads its
5956  * cpu_user_regs structs from u16s to ulongs for segment
5957  * registers. :)
5958  */
5959  rv = *(uint32_t *)(v->buf + 7 * target->arch->wordsize);
5960  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5961  REG_X86_DS,rv);
5962  rv = *(uint32_t *)(v->buf + 8 * target->arch->wordsize);
5963  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5964  REG_X86_ES,rv);
5965  }
5966  if (target->arch->type == ARCH_X86
5967  && !lstate->thread_struct_has_fs && lstate->pt_regs_has_fs_gs) {
5968  uint32_t rv;
5969  /* XXX: this is only true on newer x86 stuff; x86_64 and old
5970  * i386 stuff did not save it on the stack.
5971  */
5972  rv = *(uint32_t *)(v->buf + 9 * target->arch->wordsize);
5973  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
5974  REG_X86_FS,rv);
5975  }
5976 
5977  value_free(v);
5978  v = NULL;
5979  }
5980 
5981  if (!ltstate->ptregs_stack_addr
5982  || (!iskernel && ptregs_tidctxt == THREAD_CTXT_USER)) {
5983  thread_ctxt_t otidctxt;
5984 
5985  if (!ltstate->ptregs_stack_addr)
5986  otidctxt = ptregs_tidctxt;
5987  else
5988  otidctxt = THREAD_CTXT_KERNEL;
5989 
5990  /*
5991  * Either we could not load pt_regs due to lack of type info; or
5992  * this thread was just context-switched out, not interrupted
5993  * nor preempted, so we can't get its GP registers. Get what we
5994  * can...
5995  */
5996 
5997  if (target->arch->type == ARCH_X86) {
5998  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
5999  REG_X86_EIP,ltstate->eip);
6000  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6001  REG_X86_ESP,ltstate->esp);
6002  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6003  REG_X86_FS,ltstate->fs);
6004  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6005  REG_X86_GS,ltstate->gs);
6006  }
6007  else {
6008  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6009  REG_X86_64_RIP,ltstate->eip);
6010  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6011  REG_X86_64_RSP,ltstate->esp);
6012  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6013  REG_X86_64_FS,ltstate->fs);
6014  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6015  REG_X86_64_GS,ltstate->gs);
6016  }
6017 
6018  /* eflags and ebp are on the stack. */
6019  v = target_load_addr_real(target,ltstate->esp,LOAD_FLAG_NONE,
6020  target->arch->wordsize * 2);
6021 
6022  if (vdebug_is_on(13,LA_TARGET,LF_OSLINUX)) {
6023  char *pp;
6024  vdebug(8,LA_TARGET,LF_OSLINUX," current stack (otidctxt):\n");
6025  pp = v->buf + v->bufsiz - target->arch->wordsize;
6026  while (pp >= v->buf) {
6027  if (target->arch->wordsize == 8) {
6029  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6030  value_addr(v) + (pp - v->buf),*(uint64_t *)pp);
6031  }
6032  else {
6034  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6035  value_addr(v) + (pp - v->buf),*(uint32_t *)pp);
6036  }
6037  pp -= target->arch->wordsize;
6038  }
6039  vdebug(13,LA_TARGET,LF_OSLINUX,"\n");
6040  }
6041 
6042  if (target->arch->wordsize == 8) {
6043  ltstate->eflags = ((uint64_t *)v->buf)[1];
6044  ltstate->ebp = ((uint64_t *)v->buf)[0];
6045  }
6046  else {
6047  ltstate->eflags = ((uint32_t *)v->buf)[1];
6048  ltstate->ebp = ((uint32_t *)v->buf)[0];
6049  }
6050  value_free(v);
6051  v = NULL;
6052 
6053  if (target->arch->type == ARCH_X86) {
6054  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6055  REG_X86_EFLAGS,ltstate->eflags);
6056  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6057  REG_X86_EBP,ltstate->ebp);
6058  }
6059  else {
6060  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6061  REG_X86_64_RFLAGS,ltstate->eflags);
6062  target_regcache_init_reg_tidctxt(target,tthread,otidctxt,
6063  REG_X86_64_RBP,ltstate->ebp);
6064  }
6065  }
6066 
6067  /*
6068  * Load the current debug registers from the thread.
6069  */
6070  if (lstate->thread_struct_has_debugreg) {
6071  v = target_load_value_member(target,tlctxt,
6072  threadv,"debugreg",NULL,
6074  if (!v) {
6075  verror("could not load thread->debugreg for task %"PRIiTID"\n",tid);
6076  goto errout;
6077  }
6078  if (target->arch->type == ARCH_X86_64) {
6079  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6081  ((uint64_t *)v->buf)[0]);
6082  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6084  ((uint64_t *)v->buf)[1]);
6085  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6087  ((uint64_t *)v->buf)[2]);
6088  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6090  ((uint64_t *)v->buf)[3]);
6091  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6093  ((uint64_t *)v->buf)[6]);
6094  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6096  ((uint64_t *)v->buf)[7]);
6097  }
6098  else {
6099  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6100  REG_X86_DR0,
6101  ((uint32_t *)v->buf)[0]);
6102  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6103  REG_X86_DR1,
6104  ((uint32_t *)v->buf)[1]);
6105  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6106  REG_X86_DR2,
6107  ((uint32_t *)v->buf)[2]);
6108  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6109  REG_X86_DR3,
6110  ((uint32_t *)v->buf)[3]);
6111  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6112  REG_X86_DR6,
6113  ((uint32_t *)v->buf)[6]);
6114  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6115  REG_X86_DR7,
6116  ((uint32_t *)v->buf)[7]);
6117  }
6118  value_free(v);
6119  v = NULL;
6120  }
6121  else if (lstate->thread_struct_has_debugreg0) {
6122  /*
6123  * This is old x86_64 style.
6124  */
6125  static const char *dregmembers[8] = {
6126  "debugreg0","debugreg1","debugreg2","debugreg3",
6127  NULL,NULL,
6128  "debugreg6","debugreg7"
6129  };
6130 
6131  for (i = 0; i < 8; ++i) {
6132  if (!dregmembers[i])
6133  continue;
6134 
6135  v = target_load_value_member(target,tlctxt,
6136  threadv,dregmembers[i],NULL,
6138  if (!v) {
6139  verror("could not load thread->%s for task %"PRIiTID"\n",
6140  dregmembers[i],tid);
6141  goto errout;
6142  }
6143  if (target->arch->type == ARCH_X86_64)
6144  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6145  REG_X86_64_DR0 + i,
6146  *(uint64_t *)v->buf);
6147  else
6148  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6149  REG_X86_DR0 + i,
6150  *(uint32_t *)v->buf);
6151  value_free(v);
6152  v = NULL;
6153  }
6154  }
6155  else if (lstate->thread_struct_has_perf_debugreg) {
6156  /*
6157  * XXX: still need to load perf_events 0-3.
6158  */
6159 
6160  v = target_load_value_member(target,tlctxt,
6161  threadv,"debugreg6",NULL,
6163  if (!v) {
6164  verror("could not load thread->debugreg6 for task %"PRIiTID"\n",tid);
6165  goto errout;
6166  }
6167  if (target->arch->type == ARCH_X86_64)
6168  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6169  REG_X86_64_DR6,*(uint64_t *)v->buf);
6170  else
6171  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6172  REG_X86_DR6,*(uint32_t *)v->buf);
6173  value_free(v);
6174  v = NULL;
6175 
6176  v = target_load_value_member(target,tlctxt,
6177  threadv,"ptrace_dr7",NULL,
6179  if (!v) {
6180  verror("could not load thread->ptrace_dr7 for task %"PRIiTID"\n",tid);
6181  goto errout;
6182  }
6183  if (target->arch->type == ARCH_X86_64)
6184  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6185  REG_X86_64_DR7,*(uint64_t *)v->buf);
6186  else
6187  target_regcache_init_reg_tidctxt(target,tthread,ptregs_tidctxt,
6188  REG_X86_DR7,*(uint32_t *)v->buf);
6189  value_free(v);
6190  v = NULL;
6191 
6192  }
6193  else {
6194  vwarn("could not load debugreg for tid %d; no debuginfo!\n",tid);
6195  }
6196 
6197  if (v)
6198  value_free(v);
6199  if (comm)
6200  free(comm);
6201 
6202  OBJSVALID(tthread);
6203 
6204  return tthread;
6205 
6206  errout:
6207  if (v)
6208  value_free(v);
6209  if (comm)
6210  free(comm);
6211  if (threadinfov)
6212  value_free(threadinfov);
6213  if (threadv)
6214  value_free(threadv);
6215  if (ltstate) {
6216  ltstate->thread_info = NULL;
6217  ltstate->thread_struct = NULL;
6218  ltstate->task_struct = NULL;
6219  }
6220 
6221  return NULL;
6222 }
6223 
6224 int os_linux_flush_current_thread(struct target *target) {
6225  struct os_linux_state *lstate;
6226  struct target_thread *tthread;
6227  struct os_linux_thread_state *ltstate;
6228  struct value *v;
6229  tid_t tid;
6230  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
6231 
6232  if (!target->current_thread) {
6233  verror("current thread not loaded!\n");
6234  errno = EINVAL;
6235  return -1;
6236  }
6237 
6238  lstate = (struct os_linux_state *)target->personality_state;
6239  tthread = target->current_thread;
6240  tid = tthread->tid;
6241  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
6242 
6243  vdebug(5,LA_TARGET,LF_OSLINUX,"tid %d thid %"PRIiTID"\n",target->id,tid);
6244 
6245  if (!OBJVALID(tthread) || !OBJDIRTY(tthread)) {
6247  "tid %d thid %"PRIiTID" not valid (%d) or not dirty (%d)\n",
6248  target->id,tid,OBJVALID(tthread),OBJDIRTY(tthread));
6249  return 0;
6250  }
6251 
6253  "EIP is 0x%"PRIxREGVAL" before flush (tid %d thid %"PRIiTID")\n",
6254  target_read_reg(target,TID_GLOBAL,target->ipregno),
6255  target->id,tid);
6256 
6257  if (!target->writeable) {
6258  verror("target %s not writeable!\n",target->name);
6259  errno = EROFS;
6260  return -1;
6261  }
6262 
6263  /*
6264  * Flush PCB state -- task_flags, thread_info_flags.
6265  *
6266  * NOTE: might not be able to flush this if the current thread is
6267  * the global thread...
6268  */
6269  if (ltstate->thread_info) {
6270  v = target_load_value_member(target,tlctxt,
6271  ltstate->thread_info,"flags",NULL,
6272  LOAD_FLAG_NONE);
6274  target_store_value(target,v);
6275  value_free(v);
6276  }
6277 
6278  /* Can only flush this if we weren't in interrupt context. */
6279  if (ltstate->task_struct) {
6280  v = target_load_value_member(target,tlctxt,
6281  ltstate->task_struct,"flags",NULL,
6282  LOAD_FLAG_NONE);
6283  value_update_unum(v,ltstate->task_flags);
6284  target_store_value(target,v);
6285  value_free(v);
6286  }
6287 
6288  /*
6289  * Ok, if this is a userspace thread that is in the kernel, if the
6290  * userspace regs on the stack were dirty, we need to replace them!
6291  */
6292  if (ltstate->mm_addr && tthread->tidctxt == THREAD_CTXT_KERNEL
6293  && ltstate->ptregs_stack_addr
6294  && target_regcache_isdirty(target,tthread,THREAD_CTXT_USER)) {
6295  ADDR ptregs_stack_addr = ltstate->ptregs_stack_addr;
6296 
6298  "writing dirty userspace regs from kernel stack for user tid %d"
6299  " currently in kernel to ptregs_stack_addr 0x%"PRIxADDR"!\n",
6300  tid,ptregs_stack_addr);
6301 
6302  v = target_load_addr_real(target,ptregs_stack_addr,
6305  if (!v) {
6306  verror("could not load stack register save frame task %"PRIiTID"!\n",
6307  tid);
6308  goto errout;
6309  }
6310 
6311  if (vdebug_is_on(13,LA_TARGET,LF_OSLINUX)) {
6312  char *pp;
6313  vdebug(8,LA_TARGET,LF_OSLINUX," current stack:\n");
6314  pp = v->buf + v->bufsiz - target->arch->wordsize;
6315  while (pp >= v->buf) {
6316  if (target->arch->wordsize == 8) {
6318  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6319  value_addr(v) + (pp - v->buf),*(uint64_t *)pp);
6320  }
6321  else {
6323  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6324  value_addr(v) + (pp - v->buf),*(uint32_t *)pp);
6325  }
6326  pp -= target->arch->wordsize;
6327  }
6328  vdebug(13,LA_TARGET,LF_OSLINUX,"\n");
6329  }
6330 
6331  /* Copy the first range. */
6332  if (target->arch->type == ARCH_X86_64) {
6334  REG_X86_64_R15,&(((uint64_t *)v->buf)[0]));
6336  REG_X86_64_R14,&(((uint64_t *)v->buf)[1]));
6338  REG_X86_64_R13,&(((uint64_t *)v->buf)[2]));
6340  REG_X86_64_R12,&(((uint64_t *)v->buf)[3]));
6342  REG_X86_64_RBP,&(((uint64_t *)v->buf)[4]));
6344  REG_X86_64_RBX,&(((uint64_t *)v->buf)[5]));
6346  REG_X86_64_R11,&(((uint64_t *)v->buf)[6]));
6348  REG_X86_64_R10,&(((uint64_t *)v->buf)[7]));
6350  REG_X86_64_R9,&(((uint64_t *)v->buf)[8]));
6352  REG_X86_64_R8,&(((uint64_t *)v->buf)[9]));
6354  REG_X86_64_RAX,&(((uint64_t *)v->buf)[10]));
6356  REG_X86_64_RCX,&(((uint64_t *)v->buf)[11]));
6358  REG_X86_64_RDX,&(((uint64_t *)v->buf)[12]));
6360  REG_X86_64_RSI,&(((uint64_t *)v->buf)[13]));
6362  REG_X86_64_RDI,&(((uint64_t *)v->buf)[14]));
6363  }
6364  else {
6365  REGVAL rv;
6367  REG_X86_EBX,&rv) == 1)
6368  ((uint32_t *)v->buf)[0] = (uint32_t)rv;
6370  REG_X86_ECX,&rv) == 1)
6371  ((uint32_t *)v->buf)[1] = (uint32_t)rv;
6373  REG_X86_EDX,&rv) == 1)
6374  ((uint32_t *)v->buf)[2] = (uint32_t)rv;
6376  REG_X86_ESI,&rv) == 1)
6377  ((uint32_t *)v->buf)[3] = (uint32_t)rv;
6379  REG_X86_EDI,&rv) == 1)
6380  ((uint32_t *)v->buf)[4] = (uint32_t)rv;
6382  REG_X86_EBP,&rv) == 1)
6383  ((uint32_t *)v->buf)[5] = (uint32_t)rv;
6385  REG_X86_EAX,&rv) == 1)
6386  ((uint32_t *)v->buf)[6] = (uint32_t)rv;
6387  }
6388  /*
6389  if (target->arch->wordsize == 8)
6390  memcpy(v->buf,&ltstate->context.user_regs,8 * 15);
6391  else
6392  memcpy(v->buf,&ltstate->context.user_regs,4 * 7);
6393  */
6394 
6395  /* Copy the second range. */
6401  //ADDR ssp;
6402  int ip_offset = lstate->pt_regs_ip_offset;
6403  if (__WORDSIZE == 64 || target->arch->wordsize == 8) {
6406  ((uint64_t *)(v->buf + ip_offset)) + 0);
6408  REG_X86_64_CS,
6409  ((uint64_t *)(v->buf + ip_offset)) + 1);
6412  ((uint64_t *)(v->buf + ip_offset)) + 2);
6415  ((uint64_t *)(v->buf + ip_offset)) + 3);
6417  REG_X86_64_SS,
6418  ((uint64_t *)(v->buf + ip_offset)) + 4);
6419  }
6420  else {
6421  REGVAL rv;
6423  REG_X86_EIP,&rv) == 1)
6424  ((uint32_t *)(v->buf + ip_offset))[0] = (uint32_t)rv;
6426  REG_X86_CS,&rv) == 1)
6427  ((uint32_t *)(v->buf + ip_offset))[1] = (uint32_t)rv;
6429  REG_X86_EFLAGS,&rv) == 1)
6430  ((uint32_t *)(v->buf + ip_offset))[2] = (uint32_t)rv;
6432  REG_X86_ESP,&rv) == 1)
6433  ((uint32_t *)(v->buf + ip_offset))[3] = (uint32_t)rv;
6435  REG_X86_SS,&rv) == 1)
6436  ((uint32_t *)(v->buf + ip_offset))[4] = (uint32_t)rv;
6437  }
6438 
6439  /*
6440  * ds, es, fs, gs are all special; see other comments.
6441  */
6442  if (target->arch->type == ARCH_X86
6443  && !lstate->thread_struct_has_ds_es && lstate->pt_regs_has_ds_es) {
6444  REGVAL rv;
6445  /* XXX: this works because we know the location of (x)ds/es;
6446  * it's only on i386/x86; and because Xen pads its
6447  * cpu_user_regs structs from u16s to ulongs for segment
6448  * registers. :)
6449  */
6451  REG_X86_DS,&rv) == 1)
6452  *(uint32_t *)(v->buf + 7 * target->arch->wordsize) = (uint32_t)rv;
6454  REG_X86_ES,&rv) == 1)
6455  *(uint32_t *)(v->buf + 8 * target->arch->wordsize) = (uint32_t)rv;
6456  }
6457  if (target->arch->type == ARCH_X86
6458  && !lstate->thread_struct_has_fs && lstate->pt_regs_has_fs_gs) {
6459  REGVAL rv;
6460  /* XXX: this is only true on newer x86 stuff; x86_64 and old
6461  * i386 stuff did not save it on the stack.
6462  */
6464  REG_X86_FS,&rv) == 1)
6465  *(uint32_t *)(v->buf + 9 * target->arch->wordsize) = (uint32_t)rv;
6466  }
6467 
6468  if (vdebug_is_on(13,LA_TARGET,LF_OSLINUX)) {
6469  char *pp;
6470  vdebug(8,LA_TARGET,LF_OSLINUX," new stack (about to write):\n");
6471  pp = v->buf + v->bufsiz - target->arch->wordsize;
6472  while (pp >= v->buf) {
6473  if (target->arch->wordsize == 8) {
6475  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6476  value_addr(v) + (pp - v->buf),*(uint64_t *)pp);
6477  }
6478  else {
6480  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6481  value_addr(v) + (pp - v->buf),*(uint32_t *)pp);
6482  }
6483  pp -= target->arch->wordsize;
6484  }
6485  vdebug(13,LA_TARGET,LF_OSLINUX,"\n");
6486  }
6487 
6488  target_store_value(target,v);
6489 
6490  value_free(v);
6491  v = NULL;
6492  }
6493 
6494  /* Mark cached copy as clean. */
6495  OBJSCLEAN(tthread);
6496 
6497  return 0;
6498 
6499  errout:
6500  return -1;
6501 }
6502 
6503 int os_linux_flush_thread(struct target *target,tid_t tid) {
6504  struct os_linux_state *lstate =
6505  (struct os_linux_state *)target->personality_state;
6506  struct target_thread *tthread;
6507  struct os_linux_thread_state *ltstate = NULL;
6508  struct value *v;
6509  int i;
6510  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
6511  REG reg;
6512  REGVAL regval;
6513 
6514  /*
6515  * Try to lookup thread @tid.
6516  */
6517  if ((tthread = target_lookup_thread(target,tid)))
6518  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
6519 
6520  if (tthread == target->current_thread)
6521  return os_linux_flush_current_thread(target);
6522 
6523  if (!tthread) {
6524  verror("cannot flush unknown thread %"PRIiTID"; you forgot to load?\n",
6525  tid);
6526  errno = EINVAL;
6527  return -1;
6528  }
6529 
6530  if (!OBJVALID(tthread) || !OBJDIRTY(tthread)) {
6532  "target %d tid %"PRIiTID" not valid (%d) or not dirty (%d)\n",
6533  target->id,tthread->tid,OBJVALID(tthread),OBJDIRTY(tthread));
6534  return 0;
6535  }
6536 
6537  if (!target->writeable) {
6538  verror("target %s not writeable!\n",target->name);
6539  errno = EROFS;
6540  return -1;
6541  }
6542 
6543  /*
6544  * Ok, we can finally flush this thread's state to memory.
6545  */
6546 
6547  /*
6548  * Flush (fake) Xen machine context loaded from stack, back to
6549  * stack.
6550  *
6551  * NB: this is a duplicate of the loading procedure! See all the
6552  * comments there.
6553  */
6554 
6555  /*
6556  * Changing the IP for a non-current thread is probably a very, very
6557  * bad idea -- but it could be done on x86_32 -- but keep reading
6558  * for why it can't be done on x86_64.
6559  */
6560  if (lstate->thread_ip_member_name) {
6561  v = target_load_value_member(target,tlctxt,
6562  ltstate->thread_struct,
6563  lstate->thread_ip_member_name,
6564  NULL,LOAD_FLAG_NONE);
6565  if (!v)
6566  vwarn("could not store thread.%s for task %"PRIiTID"!\n",
6567  lstate->thread_ip_member_name,tid);
6568  else {
6569  value_update(v,(const char *)&ltstate->eip,v->bufsiz);
6570  target_store_value(target,v);
6571  value_free(v);
6572  v = NULL;
6573  }
6574  }
6575  else {
6576  /*
6577  * NB: we cannot do anything about this! In this case, x86_64
6578  * context switch does not save the IP. Either the thread
6579  * called schedule(), and its stack was swapped with another new
6580  * thread that will then run -- or it is a new thread that
6581  * starts execution at ret_from_fork . We cannot change the IP
6582  * for a sleeping x86_64 thread without deep, black magic.
6583  * Deeper than whatever else is already in here. It doesn't
6584  * even make sense to do this. The best we could do is catch
6585  * the rsp swap, and then change IP. But that will corrupt the
6586  * world and panic the kernel shortly, because the rest of the
6587  * context switch won't happen correctly. I guess then the best
6588  * we could do is catch the return from __schedule() or
6589  * schedule() (which depends on kernel version, etc), and change
6590  * it then.
6591  */
6592  }
6593 
6594  /*
6595  * GS is always in the thread data structure:
6596  */
6597  if (target->arch->type == ARCH_X86_64)
6598  reg = REG_X86_64_GS;
6599  else
6600  reg = REG_X86_GS;
6601  regval = 0;
6602  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6603  reg,&regval) == 1) {
6604  v = target_load_value_member(target,tlctxt,
6605  ltstate->thread_struct,"gs",NULL,
6606  LOAD_FLAG_NONE);
6607  value_update_unum(v,regval);
6608  target_store_value(target,v);
6609  value_free(v);
6610  v = NULL;
6611  }
6612 
6613  if (target->arch->type == ARCH_X86_64)
6614  reg = REG_X86_64_GS;
6615  else
6616  reg = REG_X86_GS;
6617  regval = 0;
6618  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6619  reg,&regval) == 1) {
6620  if (lstate->thread_struct_has_fs) {
6621  v = target_load_value_member(target,tlctxt,
6622  ltstate->thread_struct,"fs",NULL,
6623  LOAD_FLAG_NONE);
6624  if (!v) {
6625  vwarn("could not store thread.fs for task %"PRIiTID"!\n",tid);
6626  goto errout;
6627  }
6628  else {
6629  value_update(v,(const char *)&regval,v->bufsiz);
6630  target_store_value(target,v);
6631  value_free(v);
6632  v = NULL;
6633  }
6634  }
6635  else {
6636  /* Load this to pt_regs below if we can. */
6637  }
6638  }
6639 
6640  if (lstate->thread_struct_has_ds_es) {
6641  if (target->arch->type == ARCH_X86_64)
6642  reg = REG_X86_64_DS;
6643  else
6644  reg = REG_X86_DS;
6645  regval = 0;
6646  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6647  reg,&regval) == 1) {
6648  v = target_load_value_member(target,tlctxt,
6649  ltstate->thread_struct,"ds",NULL,
6650  LOAD_FLAG_NONE);
6651  if (!v) {
6652  vwarn("could not store thread.ds for task %"PRIiTID"!\n",tid);
6653  goto errout;
6654  }
6655  else {
6656  value_update(v,(const char *)&regval,v->bufsiz);
6657  target_store_value(target,v);
6658  value_free(v);
6659  v = NULL;
6660  }
6661  }
6662 
6663  if (target->arch->type == ARCH_X86_64)
6664  reg = REG_X86_64_ES;
6665  else
6666  reg = REG_X86_ES;
6667  regval = 0;
6668  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6669  reg,&regval) == 1) {
6670  v = target_load_value_member(target,tlctxt,
6671  ltstate->thread_struct,"es",NULL,
6672  LOAD_FLAG_NONE);
6673  if (!v) {
6674  vwarn("could not store thread.es for task %"PRIiTID"!\n",tid);
6675  goto errout;
6676  }
6677  else {
6678  value_update(v,(const char *)&regval,v->bufsiz);
6679  target_store_value(target,v);
6680  value_free(v);
6681  v = NULL;
6682  }
6683  }
6684  else {
6685  /* Load this to pt_regs below if we can. */
6686  }
6687  }
6688 
6689  if (ltstate->ptregs_stack_addr) {
6690  v = target_load_addr_real(target,ltstate->ptregs_stack_addr,
6693  if (!v) {
6694  verror("could not store stack register save frame task %"PRIiTID"!\n",
6695  tid);
6696  goto errout;
6697  }
6698 
6699  if (vdebug_is_on(13,LA_TARGET,LF_OSLINUX)) {
6700  char *pp;
6701  vdebug(8,LA_TARGET,LF_OSLINUX," current stack:\n");
6702  pp = v->buf + v->bufsiz - target->arch->wordsize;
6703  while (pp >= v->buf) {
6704  if (target->arch->wordsize == 8) {
6706  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6707  value_addr(v) + (pp - v->buf),*(uint64_t *)pp);
6708  }
6709  else {
6711  " 0x%"PRIxADDR" == %"PRIxADDR"\n",
6712  value_addr(v) + (pp - v->buf),*(uint32_t *)pp);
6713  }
6714  pp -= target->arch->wordsize;
6715  }
6716  vdebug(13,LA_TARGET,LF_OSLINUX,"\n");
6717  }
6718 
6719  /* Copy the first range. */
6720  if (target->arch->type == ARCH_X86_64) {
6721  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6722  REG_X86_64_R15,&(((uint64_t *)v->buf)[0]));
6723  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6724  REG_X86_64_R14,&(((uint64_t *)v->buf)[1]));
6725  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6726  REG_X86_64_R13,&(((uint64_t *)v->buf)[2]));
6727  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6728  REG_X86_64_R12,&(((uint64_t *)v->buf)[3]));
6729  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6730  REG_X86_64_RBP,&(((uint64_t *)v->buf)[4]));
6731  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6732  REG_X86_64_RBX,&(((uint64_t *)v->buf)[5]));
6733  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6734  REG_X86_64_R11,&(((uint64_t *)v->buf)[6]));
6735  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6736  REG_X86_64_R10,&(((uint64_t *)v->buf)[7]));
6737  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6738  REG_X86_64_R9,&(((uint64_t *)v->buf)[8]));
6739  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6740  REG_X86_64_R8,&(((uint64_t *)v->buf)[9]));
6741  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6742  REG_X86_64_RAX,&(((uint64_t *)v->buf)[10]));
6743  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6744  REG_X86_64_RCX,&(((uint64_t *)v->buf)[11]));
6745  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6746  REG_X86_64_RDX,&(((uint64_t *)v->buf)[12]));
6747  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6748  REG_X86_64_RSI,&(((uint64_t *)v->buf)[13]));
6749  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6750  REG_X86_64_RDI,&(((uint64_t *)v->buf)[14]));
6751  }
6752  else {
6753  REGVAL rv;
6754  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6755  REG_X86_EBX,&rv) == 1)
6756  ((uint32_t *)v->buf)[0] = (uint32_t)rv;
6757  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6758  REG_X86_ECX,&rv) == 1)
6759  ((uint32_t *)v->buf)[1] = (uint32_t)rv;
6760  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6761  REG_X86_EDX,&rv) == 1)
6762  ((uint32_t *)v->buf)[2] = (uint32_t)rv;
6763  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6764  REG_X86_ESI,&rv) == 1)
6765  ((uint32_t *)v->buf)[3] = (uint32_t)rv;
6766  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6767  REG_X86_EDI,&rv) == 1)
6768  ((uint32_t *)v->buf)[4] = (uint32_t)rv;
6769  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6770  REG_X86_EBP,&rv) == 1)
6771  ((uint32_t *)v->buf)[5] = (uint32_t)rv;
6772  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6773  REG_X86_EAX,&rv) == 1)
6774  ((uint32_t *)v->buf)[6] = (uint32_t)rv;
6775  }
6776  /*
6777  if (target->arch->wordsize == 8)
6778  memcpy(v->buf,&ltstate->context.user_regs,8 * 15);
6779  else
6780  memcpy(v->buf,&ltstate->context.user_regs,4 * 7);
6781  */
6782 
6783  /* Copy the second range. */
6789  //ADDR ssp;
6790  int ip_offset = lstate->pt_regs_ip_offset;
6791  if (__WORDSIZE == 64 || target->arch->wordsize == 8) {
6792  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6794  ((uint64_t *)(v->buf + ip_offset)) + 0);
6795  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6796  REG_X86_64_CS,
6797  ((uint64_t *)(v->buf + ip_offset)) + 1);
6798  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6800  ((uint64_t *)(v->buf + ip_offset)) + 2);
6801  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6803  ((uint64_t *)(v->buf + ip_offset)) + 3);
6804  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6805  REG_X86_64_SS,
6806  ((uint64_t *)(v->buf + ip_offset)) + 4);
6807  }
6808  else {
6809  REGVAL rv;
6810  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6811  REG_X86_EIP,&rv) == 1)
6812  ((uint32_t *)(v->buf + ip_offset))[0] = (uint32_t)rv;
6813  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6814  REG_X86_CS,&rv) == 1)
6815  ((uint32_t *)(v->buf + ip_offset))[1] = (uint32_t)rv;
6816  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6817  REG_X86_EFLAGS,&rv) == 1)
6818  ((uint32_t *)(v->buf + ip_offset))[2] = (uint32_t)rv;
6819  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6820  REG_X86_ESP,&rv) == 1)
6821  ((uint32_t *)(v->buf + ip_offset))[3] = (uint32_t)rv;
6822  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6823  REG_X86_SS,&rv) == 1)
6824  ((uint32_t *)(v->buf + ip_offset))[4] = (uint32_t)rv;
6825  }
6826 
6827  /*
6828  * ds, es, fs, gs are all special; see other comments.
6829  */
6830  if (target->arch->type == ARCH_X86
6831  && !lstate->thread_struct_has_ds_es && lstate->pt_regs_has_ds_es) {
6832  REGVAL rv;
6833  /* XXX: this works because we know the location of (x)ds/es;
6834  * it's only on i386/x86; and because Xen pads its
6835  * cpu_user_regs structs from u16s to ulongs for segment
6836  * registers. :)
6837  */
6838  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6839  REG_X86_DS,&rv) == 1)
6840  *(uint32_t *)(v->buf + 7 * target->arch->wordsize) = (uint32_t)rv;
6841  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6842  REG_X86_ES,&rv) == 1)
6843  *(uint32_t *)(v->buf + 8 * target->arch->wordsize) = (uint32_t)rv;
6844  }
6845  if (target->arch->type == ARCH_X86
6846  && !lstate->thread_struct_has_fs && lstate->pt_regs_has_fs_gs) {
6847  REGVAL rv;
6848  /* XXX: this is only true on newer x86 stuff; x86_64 and old
6849  * i386 stuff did not save it on the stack.
6850  */
6851  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6852  REG_X86_FS,&rv) == 1)
6853  *(uint32_t *)(v->buf + 9 * target->arch->wordsize) = (uint32_t)rv;
6854  }
6855 
6856  target_store_value(target,v);
6857 
6858  value_free(v);
6859  v = NULL;
6860  }
6861  else {
6862  /*
6863  * Either we could not load pt_regs due to lack of type info; or
6864  * this thread was just context-switched out, not interrupted
6865  * nor preempted, so we can't get its GP registers. Get what we
6866  * can...
6867  */
6868 
6877  /* eflags and ebp are on the stack. */
6878  v = target_load_addr_real(target,ltstate->esp,LOAD_FLAG_NONE,
6879  2 * target->arch->wordsize);
6880  if (target->arch->wordsize == 8) {
6881  ((uint64_t *)v->buf)[1] = ltstate->eflags;
6882  ((uint64_t *)v->buf)[0] = ltstate->ebp;
6883  }
6884  else {
6885  ((uint32_t *)v->buf)[1] = ltstate->eflags;
6886  ((uint32_t *)v->buf)[0] = ltstate->ebp;
6887  }
6888 
6889  target_store_value(target,v);
6890 
6891  value_free(v);
6892  v = NULL;
6893  }
6894 
6895 
6896 
6897  if (lstate->thread_struct_has_debugreg) {
6898  v = target_load_value_member(target,tlctxt,
6899  ltstate->thread_struct,"debugreg",
6900  NULL,LOAD_FLAG_AUTO_DEREF);
6901  if (!v) {
6902  verror("could not store thread->debugreg for task %"PRIiTID"\n",tid);
6903  goto errout;
6904  }
6905  if (target->arch->type == ARCH_X86_64) {
6906  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6908  ((uint64_t *)v->buf) + 0);
6909  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6911  ((uint64_t *)v->buf) + 1);
6912  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6914  ((uint64_t *)v->buf) + 2);
6915  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6917  ((uint64_t *)v->buf) + 3);
6918  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6920  ((uint64_t *)v->buf) + 6);
6921  target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6923  ((uint64_t *)v->buf) + 7);
6924  }
6925  else {
6926  REGVAL rv;
6927  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6928  REG_X86_DR0,&rv) == 1)
6929  ((uint32_t *)v->buf)[0] = rv;
6930  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6931  REG_X86_DR1,&rv) == 1)
6932  ((uint32_t *)v->buf)[1] = rv;
6933  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6934  REG_X86_DR2,&rv) == 1)
6935  ((uint32_t *)v->buf)[2] = rv;
6936  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6937  REG_X86_DR3,&rv) == 1)
6938  ((uint32_t *)v->buf)[3] = rv;
6939  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6940  REG_X86_DR6,&rv) == 1)
6941  ((uint32_t *)v->buf)[6] = rv;
6942  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6943  REG_X86_DR7,&rv) == 1)
6944  ((uint32_t *)v->buf)[7] = rv;
6945  }
6946 
6947  target_store_value(target,v);
6948 
6949  value_free(v);
6950  v = NULL;
6951  }
6952  else if (lstate->thread_struct_has_debugreg0) {
6953  /*
6954  * This is old x86_64 style.
6955  */
6956  static const char *dregmembers[8] = {
6957  "debugreg0","debugreg1","debugreg2","debugreg3",
6958  NULL,NULL,
6959  "debugreg6","debugreg7"
6960  };
6961 
6962  REG reg;
6963  REGVAL rv;
6964  if (target->arch->type == ARCH_X86_64)
6965  reg = REG_X86_64_DR0;
6966  else
6967  reg = REG_X86_DR0;
6968  for (i = 0; i < 8; ++i) {
6969  if (!dregmembers[i])
6970  continue;
6971 
6972  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
6973  reg + i,&rv) < 1)
6974  continue;
6975 
6976  v = target_load_value_member(target,tlctxt,
6977  ltstate->thread_struct,
6978  dregmembers[i],NULL,
6980  if (!v) {
6981  verror("could not store thread->%s for task %"PRIiTID"\n",
6982  dregmembers[i],tid);
6983  goto errout;
6984  }
6985  if (target->arch->type == ARCH_X86_64)
6986  *(uint64_t *)v->buf = rv;
6987  else
6988  *(uint32_t *)v->buf = (uint32_t)rv;
6989 
6990  target_store_value(target,v);
6991 
6992  value_free(v);
6993  v = NULL;
6994  }
6995  }
6996  else if (lstate->thread_struct_has_perf_debugreg) {
6997  /*
6998  * XXX: still need to store perf_events 0-3.
6999  */
7000  REG reg;
7001  REGVAL rv;
7002 
7003  if (target->arch->type == ARCH_X86_64)
7004  reg = REG_X86_64_DR6;
7005  else
7006  reg = REG_X86_DR6;
7007  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
7008  reg,&rv) == 1) {
7009  v = target_load_value_member(target,tlctxt,
7010  ltstate->thread_struct,"debugreg6",
7011  NULL,LOAD_FLAG_AUTO_DEREF);
7012  if (!v) {
7013  verror("could not store thread->debugreg6 for task %"PRIiTID"\n",
7014  tid);
7015  goto errout;
7016  }
7017  if (target->arch->type == ARCH_X86_64)
7018  *(uint64_t *)v->buf = rv;
7019  else
7020  *(uint32_t *)v->buf = rv;
7021 
7022  target_store_value(target,v);
7023 
7024  value_free(v);
7025  v = NULL;
7026  }
7027 
7028  if (target->arch->type == ARCH_X86_64)
7029  reg = REG_X86_64_DR7;
7030  else
7031  reg = REG_X86_DR7;
7032  if (target_regcache_readreg_ifdirty(target,tthread,tthread->tidctxt,
7033  reg,&rv) == 1) {
7034  v = target_load_value_member(target,tlctxt,
7035  ltstate->thread_struct,"ptrace_dr7",
7036  NULL,LOAD_FLAG_AUTO_DEREF);
7037  if (!v) {
7038  verror("could not store thread->ptrace_dr7 for task %"PRIiTID"\n",
7039  tid);
7040  goto errout;
7041  }
7042  if (target->arch->type == ARCH_X86_64)
7043  *(uint64_t *)v->buf = rv;
7044  else
7045  *(uint32_t *)v->buf = (uint32_t)rv;
7046 
7047  target_store_value(target,v);
7048 
7049  value_free(v);
7050  v = NULL;
7051  }
7052  }
7053  else {
7054  vwarn("could not store debugreg for tid %d; no debuginfo!\n",tid);
7055  }
7056 
7057  /*
7058  * Flush PCB state -- task_flags, thread_info_flags.
7059  */
7060  v = target_load_value_member(target,tlctxt,
7061  ltstate->thread_info,"flags",NULL,
7062  LOAD_FLAG_NONE);
7064  target_store_value(target,v);
7065  value_free(v);
7066 
7067  /* Can only flush this if we weren't in interrupt context. */
7068  if (ltstate->task_struct) {
7069  v = target_load_value_member(target,tlctxt,
7070  ltstate->task_struct,"flags",NULL,
7071  LOAD_FLAG_NONE);
7072  value_update_unum(v,ltstate->task_flags);
7073  target_store_value(target,v);
7074  value_free(v);
7075  }
7076 
7077  OBJSCLEAN(tthread);
7078 
7079  return 0;
7080 
7081  errout:
7082  return -1;
7083 
7084 }
7085 
7086 int os_linux_invalidate_thread(struct target *target,
7087  struct target_thread *tthread) {
7088  struct os_linux_thread_state *ltstate;
7089 
7090  vdebug(5,LA_TARGET,LF_OSLINUX,"target %d thid %d\n",target->id,tthread->tid);
7091 
7092  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
7093 
7094  if (ltstate) {
7095  if (ltstate->thread_struct) {
7096  value_free(ltstate->thread_struct);
7097  ltstate->thread_struct = NULL;
7098  }
7099  if (ltstate->thread_info) {
7100  value_free(ltstate->thread_info);
7101  ltstate->thread_info = NULL;
7102  }
7103  if (ltstate->task_struct) {
7104  value_free(ltstate->task_struct);
7105  ltstate->task_struct = NULL;
7106  }
7107  }
7108 
7109  OBJSINVALID(tthread);
7110 
7111  return 0;
7112 }
7113 
7114 int os_linux_thread_snprintf(struct target *target,struct target_thread *tthread,
7115  char *buf,int bufsiz,
7116  int detail,char *sep,char *kvsep) {
7117  struct os_linux_thread_state *ltstate;
7118  int rc = 0;
7119  int nrc;
7120  thread_ctxt_t othertidctxt;
7121 
7122  if (detail < 0)
7123  return 0;
7124 
7125  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
7126 
7127  if (detail >= 0) {
7128  rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
7129  (rc >= bufsiz) ? 0 : bufsiz - rc,
7130  "%stask_flags%s0x%"PRIxNUM "%s"
7131  "thread_info_flags%s0x%"PRIxNUM "%s"
7132  "preempt_count%s0x%"PRIiNUM "%s"
7133  "task%s0x%"PRIxADDR "%s"
7134  "stack_base%s0x%"PRIxADDR "%s"
7135  "pgd%s0x%"PRIx64 "%s" "mm%s0x%"PRIxADDR,
7136  sep,
7137  kvsep,ltstate->task_flags,sep,
7138  kvsep,ltstate->thread_info_flags,sep,
7139  kvsep,ltstate->thread_info_preempt_count,sep,
7140  kvsep,ltstate->task_struct ? value_addr(ltstate->task_struct) : 0x0UL,sep,
7141  kvsep,ltstate->stack_base,sep,
7142  kvsep,ltstate->pgd,sep,kvsep,ltstate->mm_addr);
7143  }
7144 
7145  /*
7146  * If this thread was not the current thread, assume we loaded all
7147  * its contexts' states from memory -- so print both contexts.
7148  * Otherwise, print
7149  */
7150  if (tthread != target->current_thread && tthread != target->global_thread) {
7151  nrc = target_regcache_snprintf(target,tthread,tthread->tidctxt,
7152  (rc >= bufsiz) ? NULL : buf + rc,
7153  (rc >= bufsiz) ? 0 : bufsiz - rc,
7154  detail,sep,kvsep,0);
7155  if (nrc < 0)
7156  return nrc;
7157  else
7158  rc += nrc;
7159  }
7160 
7161  /*
7162  * We always loaded the non-current context from memory as much as
7163  * possible, so print that here.
7164  */
7165  if (tthread->tidctxt == THREAD_CTXT_KERNEL)
7166  othertidctxt = THREAD_CTXT_USER;
7167  else
7168  othertidctxt = THREAD_CTXT_KERNEL;
7169  nrc = target_regcache_snprintf(target,tthread,othertidctxt,
7170  (rc >= bufsiz) ? NULL : buf + rc,
7171  (rc >= bufsiz) ? 0 : bufsiz - rc,
7172  detail,sep,kvsep,0);
7173  if (nrc < 0)
7174  return nrc;
7175  else
7176  rc += nrc;
7177 
7178  return rc;
7179 }
7180 
7185 #if 0
7186 #if __WORDSIZE == 64
7187 #define RF "lx"
7188 #define RIF "lu"
7189 #define DRF "lx"
7190 #else
7191 #define RF "x"
7192 #define RIF "u"
7193 #define DRF "lx"
7194 #endif
7195 
7196 int os_linux_thread_snprintf(struct target *target,struct target_thread *tthread,
7197  char *buf,int bufsiz,
7198  int detail,char *sep,char *kvsep) {
7199  struct os_linux_thread_state *ltstate;
7200  int rc = 0;
7201  int nrc;
7202 
7203  if (detail < 0)
7204  return 0;
7205 
7206  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
7207 
7208  if (detail >= 0) {
7209  rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
7210  (rc >= bufsiz) ? 0 :bufsiz - rc,
7211  "task_flags%s0x%"PRIxNUM "%s"
7212  "thread_info_flags%s0x%"PRIxNUM "%s"
7213  "preempt_count%s0x%"PRIiNUM "%s"
7214  "task%s0x%"PRIxADDR "%s"
7215  "stack_base%s0x%"PRIxADDR "%s"
7216  "pgd%s0x%"PRIx64 "%s" "mm%s0x%"PRIxADDR,
7217  kvsep,ltstate->task_flags,sep,
7218  kvsep,ltstate->thread_info_flags,sep,
7219  kvsep,ltstate->thread_info_preempt_count,sep,
7220  kvsep,ltstate->task_struct ? value_addr(ltstate->task_struct) : 0x0UL,sep,
7221  kvsep,ltstate->stack_base,sep,
7222  kvsep,ltstate->pgd,sep,kvsep,ltstate->mm_addr);
7223  }
7224 
7225  if (detail >= 1)
7226  rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
7227  (rc >= bufsiz) ? 0 :bufsiz - rc,
7228  "%s" "ip%s%"RF "%s" "bp%s%"RF "%s" "sp%s%"RF "%s"
7229  "flags%s%"RF "%s" "ax%s%"RF "%s" "bx%s%"RF "%s"
7230  "cx%s%"RF "%s" "dx%s%"RF "%s" "di%s%"RF "%s"
7231  "si%s%"RF "%s" "cs%s%"RIF "%s" "ss%s%"RIF "%s"
7232  "ds%s%"RIF "%s" "es%s%"RIF "%s"
7233  "fs%s%"RF "%s" "gs%s%"RF,
7234 #if __WORDSIZE == 64
7235  sep,kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RIP),sep,
7236  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RBP),sep,
7237  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RSP),sep,
7238  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RFLAGS),sep,
7239  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RAX),sep,
7240  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RBX),sep,
7241  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RCX),sep,
7242  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RDX),sep,
7243  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RDI),sep,
7244  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_RSI),sep,
7245  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_CS),sep,
7246  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_SS),sep,
7247  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_DS),sep,
7248  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_ES),sep,
7249  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_FS),sep,
7250  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_64_GS)
7251 #else
7252  sep,kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_EIP),sep,
7253  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_EBP),sep,
7254  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_ESP),sep,
7255  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_EFLAGS),sep,
7256  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_EAX),sep,
7257  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_EBX),sep,
7258  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_ECX),sep,
7259  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_EDX),sep,
7260  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_EDI),sep,
7261  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_ESI),sep,
7262  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_CS),sep,
7263  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_SS),sep,
7264  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_DS),sep,
7265  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_ES),sep,
7266  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_FS),sep,
7267  kvsep,target_regcache_readreg_tidctxt(target,tthread->tid,tthread->tidctxt,REG_X86_GS)
7268 #endif
7269  );
7270  if (detail >= 2) {
7271  REGVAL drs[8];
7272  if (target->arch->type == ARCH_X86_64) {
7273  drs[0] = target_regcache_readreg_tidctxt(target,tthread->tid,
7274  tthread->tidctxt,
7275  REG_X86_64_DR0);
7276  drs[1] = target_regcache_readreg_tidctxt(target,tthread->tid,
7277  tthread->tidctxt,
7278  REG_X86_64_DR1);
7279  drs[2] = target_regcache_readreg_tidctxt(target,tthread->tid,
7280  tthread->tidctxt,
7281  REG_X86_64_DR2);
7282  drs[3] = target_regcache_readreg_tidctxt(target,tthread->tid,
7283  tthread->tidctxt,
7284  REG_X86_64_DR3);
7285  drs[6] = target_regcache_readreg_tidctxt(target,tthread->tid,
7286  tthread->tidctxt,
7287  REG_X86_64_DR6);
7288  drs[7] = target_regcache_readreg_tidctxt(target,tthread->tid,
7289  tthread->tidctxt,
7290  REG_X86_64_DR7);
7291  }
7292  else {
7293  drs[0] = target_regcache_readreg_tidctxt(target,tthread->tid,
7294  tthread->tidctxt,
7295  REG_X86_DR0);
7296  drs[1] = target_regcache_readreg_tidctxt(target,tthread->tid,
7297  tthread->tidctxt,
7298  REG_X86_DR1);
7299  drs[2] = target_regcache_readreg_tidctxt(target,tthread->tid,
7300  tthread->tidctxt,
7301  REG_X86_DR2);
7302  drs[3] = target_regcache_readreg_tidctxt(target,tthread->tid,
7303  tthread->tidctxt,
7304  REG_X86_DR3);
7305  drs[6] = target_regcache_readreg_tidctxt(target,tthread->tid,
7306  tthread->tidctxt,
7307  REG_X86_DR6);
7308  drs[7] = target_regcache_readreg_tidctxt(target,tthread->tid,
7309  tthread->tidctxt,
7310  REG_X86_DR7);
7311  }
7312 
7313  rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
7314  (rc >= bufsiz) ? 0 :bufsiz - rc,
7315  "%s" "dr0%s%"DRF "%s" "dr1%s%"DRF
7316  "%s" "dr2%s%"DRF "%s" "dr3%s%"DRF
7317  "%s" "dr6%s%"DRF "%s" "dr7%s%"DRF,
7318  sep,kvsep,drs[0],sep,kvsep,drs[1],
7319  sep,kvsep,drs[1],sep,kvsep,drs[2],
7320  sep,kvsep,drs[6],sep,kvsep,drs[7]);
7321  }
7322 
7323  return rc;
7324 }
7325 #endif /* 0 */
7326 
7332  struct addrspace *space;
7334  GHashTable *moddep;
7335  GHashTable *config;
7336 };
7337 
7338 static int __update_module(struct target *target,struct value *value,void *data) {
7339  struct value *mod_name = NULL;
7340  struct value *vt = NULL;
7341  ADDR mod_core_addr;
7342  ADDR mod_init_addr;
7343  unum_t mod_core_size;
7344  unum_t mod_init_size;
7345  struct __update_module_data *ud = (struct __update_module_data *)data;
7346  GList *t1;
7347  struct memregion *tregion = NULL;
7348  struct memrange *range;
7349  char *modfilename = NULL;
7350  int retval;
7351  struct binfile_instance *bfi = NULL;
7352  struct debugfile *debugfile = NULL;
7353  struct addrspace *space = ud->space;
7354  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
7355  struct target_event *event;
7356 
7357  if (!ud) {
7358  errno = EINVAL;
7359  return -1;
7360  }
7361 
7362  mod_name = target_load_value_member(target,tlctxt,
7363  value,"name",NULL,LOAD_FLAG_NONE);
7364  if (!mod_name) {
7365  verror("could not load name for module!\n");
7366  goto errout;
7367  }
7368 
7369  vt = target_load_value_member(target,tlctxt,
7370  value,"module_core",NULL,LOAD_FLAG_NONE);
7371  if (!vt) {
7372  verror("could not load module_core addr!\n");
7373  goto errout;
7374  }
7375  mod_core_addr = v_addr(vt);
7376  value_free(vt);
7377 
7378  vt = target_load_value_member(target,tlctxt,
7379  value,"module_init",NULL,LOAD_FLAG_NONE);
7380  if (!vt) {
7381  verror("could not load module_init addr!\n");
7382  goto errout;
7383  }
7384  mod_init_addr = v_addr(vt);
7385  value_free(vt);
7386 
7387  vt = target_load_value_member(target,tlctxt,
7388  value,"core_size",NULL,LOAD_FLAG_NONE);
7389  if (!vt) {
7390  verror("could not load module core_size!\n");
7391  goto errout;
7392  }
7393  mod_core_size = v_unum(vt);
7394  value_free(vt);
7395 
7396  vt = target_load_value_member(target,tlctxt,
7397  value,"init_size",NULL,LOAD_FLAG_NONE);
7398  if (!vt) {
7399  verror("could not load module init_size!\n");
7400  goto errout;
7401  }
7402  mod_init_size = v_unum(vt);
7403  value_free(vt);
7404 
7406  "module %s (core=0x%"PRIxADDR"(%u),init=0x%"PRIxADDR"(%u))\n",
7407  v_string(mod_name),mod_core_addr,(unsigned)mod_core_size,
7408  mod_init_addr,(unsigned)mod_init_size);
7409 
7410  if (!ud->moddep) {
7412  "no moddep info for %s; cannot load binfile info!\n",
7413  v_string(mod_name));
7414  }
7415  else {
7416  modfilename = g_hash_table_lookup(ud->moddep,v_string(mod_name));
7417  if (!modfilename) {
7419  "no moddep info for %s; cannot load binfile info!\n",
7420  v_string(mod_name));
7421  }
7422  }
7423 
7424  v_g_list_foreach(space->regions,t1,tregion) {
7425  if (strcmp(tregion->name,
7426  (modfilename) ? modfilename : v_string(mod_name)) == 0) {
7427  if (tregion->base_load_addr == mod_core_addr)
7428  break;
7429  }
7430  tregion = NULL;
7431  }
7432 
7433  if (!tregion) {
7434  /*
7435  * Create a new one! Anything could have happened.
7436  */
7437  tregion = memregion_create(ud->space,REGION_TYPE_LIB,
7438  (modfilename) ? strdup(modfilename) \
7439  : strdup(v_string(mod_name)));
7440 
7441  OBJSNEW(tregion);
7442 
7443  /*
7444  * Create a new range for the region.
7445  */
7446  range = memrange_create(tregion,mod_core_addr,
7447  mod_core_addr + mod_core_size,0,0);
7448  OBJSNEW(range);
7449  if (!range) {
7450  verror("could not create range for module addr 0x%"PRIxADDR"!\n",
7451  mod_core_addr);
7452  retval = -1;
7453  goto errout;
7454  }
7455 
7456  if (modfilename) {
7457  /*
7458  * Load its debuginfo.
7459  */
7460  bfi = binfile_infer_instance(tregion->name,
7461  target->spec->debugfile_root_prefix,
7462  mod_core_addr,target->config);
7463  if (!bfi) {
7464  verror("could not infer instance for module %s!\n",tregion->name);
7465  retval = -1;
7466  goto errout;
7467  }
7468 
7469  debugfile =
7471  if (!debugfile) {
7472  retval = -1;
7473  goto errout;
7474  }
7475 
7476  if (target_associate_debugfile(target,tregion,debugfile)) {
7477  retval = -1;
7478  goto errout;
7479  }
7480 
7481  tregion->binfile = debugfile->binfile;
7482  RHOLD(debugfile->binfile,tregion);
7483 
7484  /* The debugfile, etc, hold it now; we don't care. */
7486  bfi = NULL;
7487  }
7488 
7489  event = target_create_event(target,NULL,T_EVENT_OS_REGION_NEW,tregion);
7490  target_broadcast_event(target,event);
7491  event = target_create_event(target,NULL,T_EVENT_OS_RANGE_NEW,range);
7492  target_broadcast_event(target,event);
7493  }
7494 
7495  ud->region = tregion;
7496  retval = 0;
7497 
7498  if (mod_name)
7499  value_free(mod_name);
7500 
7501  return retval;
7502 
7503  errout:
7504  if (mod_name)
7505  value_free(mod_name);
7506  if (bfi)
7508  if (tregion)
7509  addrspace_detach_region(space,tregion);
7510 
7511  return retval;
7512 }
7513 
7514 static int os_linux_reload_modules_dep(struct target *target) {
7515  struct os_linux_state *lstate = \
7516  (struct os_linux_state *)target->personality_state;
7517  GHashTable *moddep = NULL;
7518  FILE *moddep_file;
7519  char moddep_path[PATH_MAX];
7520  char buf[PATH_MAX * 2];
7521  char *colon;
7522  char *slash;
7523  char *newline;
7524  char *extension;
7525  char *modname;
7526  char *modfilename;
7527  struct stat statbuf;
7528  time_t moddep_mtime;
7529 
7530  snprintf(moddep_path,PATH_MAX,"%s/modules.dep",lstate->kernel_module_dir);
7531 
7532  /*
7533  * Stat it and see if our cached copy (if there is one) is still
7534  * valid.
7535  */
7536  if (stat(moddep_path,&statbuf)) {
7538  "stat(%s): %s; aborting!\n",moddep_path,strerror(errno));
7539  return -1;
7540  }
7541  moddep_mtime = statbuf.st_mtime;
7542 
7543  if (lstate->moddep && lstate->last_moddep_mtime == moddep_mtime) {
7545  "cached moddep is valid\n");
7546  return 0;
7547  }
7548 
7549  /*
7550  * Read the current modules.dep file and build up the name->file map.
7551  */
7552  if (!(moddep_file = fopen(moddep_path,"r"))) {
7553  verror("fopen(%s): %s\n",moddep_path,strerror(errno));
7554  return -1;
7555  }
7556 
7557  moddep = g_hash_table_new_full(g_str_hash,g_str_equal,free,free);
7558 
7559  while (fgets(buf,sizeof(buf),moddep_file)) {
7560  newline = index(buf,'\n');
7561 
7562  /*
7563  * Find lines starting with "<module_filename>:", and split them
7564  * into a map of <module_name> -> <module_filename>
7565  */
7566  if (!(colon = index(buf,':')))
7567  goto drain;
7568 
7569  *colon = '\0';
7570  if (!(slash = rindex(buf,'/')))
7571  goto drain;
7572 
7573  /* Check relative and abs paths. */
7574  modfilename = strdup(buf);
7575  if (stat(modfilename,&statbuf)) {
7577  "could not find modules.dep file %s; trying abs path\n",
7578  modfilename);
7579  free(modfilename);
7580 
7581  modfilename = calloc(1,sizeof(char)*(strlen(lstate->kernel_module_dir)
7582  +1+strlen(buf)+1));
7583  sprintf(modfilename,"%s/%s",lstate->kernel_module_dir,buf);
7584  if (stat(modfilename,&statbuf)) {
7586  "could not find modules.dep file %s at all!\n",
7587  modfilename);
7588  free(modfilename);
7589  modfilename = NULL;
7590  goto drain;
7591  }
7592  }
7593  /* If we have one, insert it. */
7594  if (modfilename) {
7595  modname = strdup(slash+1);
7596  if ((extension = rindex(modname,'.')))
7597  *extension = '\0';
7598 
7599  g_hash_table_insert(moddep,modname,modfilename);
7600 
7602  "modules.dep: %s -> %s\n",modname,modfilename);
7603  }
7604 
7605  /*
7606  * Drain until we get a newline.
7607  */
7608  drain:
7609  if (!newline) {
7610  while (fgets(buf,sizeof(buf),moddep_file)) {
7611  if (index(buf,'\n'))
7612  break;
7613  }
7614  }
7615  }
7616  fclose(moddep_file);
7617 
7618  /*
7619  * Update our cache.
7620  */
7621  if (lstate->moddep)
7622  g_hash_table_destroy(lstate->moddep);
7623  lstate->moddep = moddep;
7624  lstate->last_moddep_mtime = moddep_mtime;
7625 
7627  "updated modules.dep cache (%d entries from %s)\n",
7628  g_hash_table_size(lstate->moddep),moddep_path);
7629 
7630  return 0;
7631 }
7632 
7633 /*
7634  * Only called if active memory probing is disabled, or if it fails.
7635  */
7636 static int os_linux_updateregions(struct target *target,
7637  struct addrspace *space) {
7638  struct os_linux_state *lstate = \
7639  (struct os_linux_state *)target->personality_state;
7640  struct __update_module_data ud;
7641  struct memregion *region;
7642  struct memrange *range;
7643  struct target_event *event;
7644  GList *t1,*t2,*t3,*t4;
7645 
7646  vdebug(5,LA_TARGET,LF_OSLINUX,"target %d\n",target->id);
7647 
7648  /*
7649  * We never update the main kernel region. Instead, we update the
7650  * module subregions as needed.
7651  *
7652  * XXX: in this first version, we don't worry about module init
7653  * sections that can be removed after the kernel initializes the
7654  * module.
7655  */
7656 
7657  if (!lstate->module_type || !lstate->modules || !lstate->kernel_module_dir) {
7658  /*
7659  * Don't return an error; would upset target_open.
7660  */
7661  return 0;
7662  }
7663 
7664  if (os_linux_reload_modules_dep(target))
7666  "failed to reload modules.dep; trying to continue!\n");
7667 
7668  ud.space = space;
7669  ud.moddep = lstate->moddep;
7670 
7671  /*
7672  * Clear out the current modules region bits.
7673  *
7674  * We don't bother checking ranges. No need.
7675  */
7676  v_g_list_foreach(space->regions,t1,region) {
7677  if (region->type == REGION_TYPE_LIB) {
7678  OBJSDEAD(region,memregion);
7679  }
7680  }
7681 
7682  /*
7683  * Handle the modules via callback via iterator.
7684  */
7685  os_linux_list_for_each_entry(target,lstate->module_type,lstate->modules,
7686  "list",0,__update_module,&ud);
7687 
7688  /*
7689  * Now, for all the regions, check if they were newly added
7690  * or still exist; if none of those, then they vanished
7691  * and we have to purge them.
7692  */
7693 
7694  v_g_list_foreach_safe(space->regions,t1,t2,region) {
7695  /* Skip anything not a kernel module. */
7696  if (region->type != REGION_TYPE_LIB)
7697  continue;
7698 
7699  if (!OBJLIVE(region) && !OBJNEW(region)) {
7700  v_g_list_foreach_safe(region->ranges,t3,t4,range) {
7702  "removing stale range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
7703  range->start,range->end,range->offset);
7704 
7705  OBJSDEL(range);
7706 
7707  event = target_create_event(target,NULL,
7708  T_EVENT_OS_RANGE_DEL,range);
7709  target_broadcast_event(target,event);
7710  event->priv = NULL;
7711 
7712  memregion_detach_range(region,range);
7713  }
7714 
7716  "removing stale region (%s:0x%"PRIxADDR":%s:%s)\n",
7717  region->space->name,region->space->tag,
7718  region->name,REGION_TYPE(region->type));
7719 
7720  OBJSDEL(region);
7721 
7722  event = target_create_event(target,NULL,
7723  T_EVENT_OS_REGION_DEL,region);
7724  target_broadcast_event(target,event);
7725  event->priv = NULL;
7726 
7727  addrspace_detach_region(space,region);
7728  }
7729  else if (OBJNEW(region)) {
7730  v_g_list_foreach_safe(region->ranges,t3,t4,range) {
7732  "new range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
7733  range->start,range->end,range->offset);
7734 
7735  event = target_create_event(target,NULL,
7736  T_EVENT_OS_RANGE_NEW,range);
7737  target_broadcast_event(target,event);
7738  }
7739 
7740  event = target_create_event(target,NULL,
7741  T_EVENT_OS_REGION_NEW,region);
7742  target_broadcast_event(target,event);
7743  }
7744  }
7745 
7746  return 0;
7747 }
7748 
7750  void *handler_data,
7751  struct probe *trigger,
7752  struct probe *base) {
7753  struct target *target;
7754  struct os_linux_state *lstate;
7755  struct value *mod = NULL;
7756  struct addrspace *space;
7757  int state = -1;
7758  struct __update_module_data ud;
7759  char *modfilename;
7760  GList *t1,*t2;
7761  struct memregion *region;
7762  struct memrange *range;
7763  char *name;
7764  struct value *name_value = NULL;
7765  struct target_location_ctxt *tlctxt;
7766  struct target_event *event;
7767 
7768  target = probe->target;
7769  tlctxt = target_global_tlctxt(target);
7770  lstate = (struct os_linux_state *)target->personality_state;
7771 
7772  /* Only one space... */
7773  space = (struct addrspace *)g_list_nth_data(target->spaces,0);
7774 
7775  /*
7776  * For kernels that have do_init_module(), we can simply place a
7777  * probe on the entry of that function. If we cannot read the first
7778  * arg, the module is already on the list, so just scan the list
7779  * manually.
7780  *
7781  * For older kernels, it is not as good. We cannot catch the
7782  * incoming module once it is guaranteed to be on the list UNLESS we
7783  * use return probes on one of a few functions (__link_module would
7784  * work), but that requires disasm, which we want to avoid.
7785  *
7786  * So -- the only strategy that works for all kernels is to place a
7787  * probe on module_free(), and look at both the addr being freed,
7788  * and the module->state field. If ->state is MODULE_STATE_LIVE,
7789  * we know the module is new. If mod->state is MODULE_STATE_GOING,
7790  * we know the module is being removed (or failed to initialize).
7791  * If mod->state is MODULE_STATE_COMING, we know the module failed
7792  * to initialize.
7793  *
7794  * This way, if we fail to load module_free's mod arg -- we can just
7795  * rescan the list. By the time this function is called, whether
7796  * the module is coming or going, the module is either on the list
7797  * or off it.
7798  *
7799  * NB: module_free is called multiple times per module (for the
7800  * init_text section, and the core_text section). So, we just
7801  * handle those cases and ignore them.
7802  *
7803  * NB: we *could* utilize this to track the presence/absence of
7804  * the init_text section too; but we don't for now.
7805  */
7806 
7807  /*
7808  * Load mod.
7809  */
7810  mod = target_load_symbol(target,tlctxt,
7812  if (!mod) {
7813  /*
7814  * Again, the module is either on the list if it's coming; or
7815  * off the list if it's going. By the time module_free is
7816  * called, its state is known based on whether it is on the list
7817  * or off the list.
7818  *
7819  * So, it is safe to manually update regions.
7820  */
7821  vwarn("could not load mod in module_free; manually updating"
7822  " modules!\n");
7823  goto manual_update;
7824  }
7825 
7826  /*
7827  * Load mod->state, so we know what is happening.
7828  */
7829  VLV(target,tlctxt,mod,"state",LOAD_FLAG_NONE,&state,NULL,
7830  err_vmiload_state);
7831 
7832  /*
7833  * Update modules.dep, just in case; don't worry if it fails, just
7834  * do our best.
7835  */
7836  os_linux_reload_modules_dep(target);
7837 
7838  if (state == lstate->MODULE_STATE_LIVE) {
7839  ud.space = space;
7840  ud.moddep = lstate->moddep;
7841 
7842  __update_module(target,mod,&ud);
7843  region = ud.region;
7844 
7845  v_g_list_foreach(region->ranges,t1,range) {
7847  "new range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
7848  range->start,range->end,range->offset);
7849 
7850  event = target_create_event(target,NULL,
7851  T_EVENT_OS_RANGE_NEW,range);
7852  target_broadcast_event(target,event);
7853  }
7854 
7855  event = target_create_event(target,NULL,
7856  T_EVENT_OS_REGION_NEW,region);
7857  target_broadcast_event(target,event);
7858  }
7859  else if (state == lstate->MODULE_STATE_COMING
7860  || state == lstate->MODULE_STATE_GOING) {
7861  /*
7862  * Look up and destroy it if it's one of our regions.
7863  */
7864  VLV(target,tlctxt,mod,"name",LOAD_FLAG_AUTO_STRING,
7865  NULL,&name_value,err_vmiload_name);
7866  name = v_string(name_value);
7867 
7868  modfilename = g_hash_table_lookup(lstate->moddep,name);
7869  if (!modfilename) {
7870  verror("could not find modfilename for module '%s'; aborting to"
7871  " manual update!\n",
7872  name);
7873  goto manual_update;
7874  }
7875 
7876  v_g_list_foreach(space->regions,t1,region) {
7877  if (strcmp(region->name,modfilename) == 0)
7878  break;
7879  region = NULL;
7880  }
7881 
7882  if (region) {
7883  v_g_list_foreach_safe(region->ranges,t1,t2,range) {
7885  "removing stale range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
7886  range->start,range->end,range->offset);
7887 
7888  event = target_create_event(target,NULL,
7889  T_EVENT_OS_RANGE_DEL,range);
7890  target_broadcast_event(target,event);
7891  event->priv = NULL;
7892 
7893  memregion_detach_range(region,range);
7894  }
7895 
7897  "removing stale region (%s:0x%"PRIxADDR":%s:%s)\n",
7898  region->space->name,region->space->tag,
7899  region->name,REGION_TYPE(region->type));
7900 
7901  event = target_create_event(target,NULL,
7902  T_EVENT_OS_REGION_DEL,region);
7903  target_broadcast_event(target,event);
7904  event->priv = NULL;
7905 
7906  addrspace_detach_region(space,region);
7907  }
7908  else {
7910  "ignoring untracked departing module '%s'\n",name);
7911  }
7912  }
7913  else {
7914  verror("unexpected module state %d; reverting to manual update!\n",state);
7915  goto manual_update;
7916  }
7917 
7918  if (name_value)
7919  value_free(name_value);
7920  if (mod)
7921  value_free(mod);
7922 
7923  return RESULT_SUCCESS;
7924 
7925  err_vmiload_state:
7926  verror("could not load mod->state; aborting to manual update!\n");
7927  goto manual_update;
7928 
7929  err_vmiload_name:
7930  verror("could not load mod->name for departing module; aborting to manual"
7931  " update!\n");
7932  goto manual_update;
7933 
7934  manual_update:
7935  if (name_value)
7936  value_free(name_value);
7937  if (mod)
7938  value_free(mod);
7939 
7940  if (os_linux_updateregions(target,space)) {
7941  verror("manual module update failed; regions may be wrong!\n");
7942  return RESULT_SUCCESS;
7943  }
7944 
7945  return RESULT_SUCCESS;
7946 }
7947 
7948 /*
7949  * NB: this was hard!
7950  *
7951  * It is very, very hard to find an available function to place a probe
7952  * on, that is not either optimized out of existence (well, it may
7953  * exist, but either the instances are not available or the parameters
7954  * have no locations at the inlined site).
7955  *
7956  * And we really want to try hard to only probe *after* (but very near
7957  * to) the place where the task is placed on the tasks list; this way,
7958  * if we fail to read the new task out of target memory, we can abort to
7959  * scanning the task list. I guess that doesn't matter so much though.
7960  * Anyway, the list add happens in copy_process, so that is our target.
7961  *
7962  * I worked very hard to avoid requiring disasm support (so that I could
7963  * register probes on the RETs from copy_process); but ultimately I
7964  * could not manage it. proc_fork_connector() is the only
7965  * copy_process()-specific hook just before the success RET in
7966  * copy_process. We could also have caught the increment of total_forks
7967  * via watchpoint, but I want to avoid wasting a watchpoint if I can!
7968  *
7969  * I tried to catch proc_fork_connector() inside copy_process() (but of
7970  * course, silly me, that is only really defined if CONFIG_CONNECTOR;
7971  * otherwise it's just declared) because it is the last "hook" in
7972  * copy_process; by that time, we know that the new task is on the tasks
7973  * list.
7974  *
7975  * Another option might have been to catch the new task just after the
7976  * invocations of copy_process() in fork_idle() or do_fork(); but this
7977  * would basically require us to use debuginfo variable availability
7978  * (when does the `task' local var in those functions become
7979  * available!), and then place a probe on that exact location. But, of
7980  * course that doesn't work; you have no idea what code is where, or the
7981  * debuginfo might be wrong/incomplete.
7982  *
7983  * So -- the best options all involve requiring disasm support. Once we
7984  * have this, the easiest thing to do is catch the RETs in copy_process;
7985  * if the local var 'p' is !IS_ERR(), we know we have a new task. If we
7986  * fail to load memory, or something goes wrong, we can fall back to
7987  * manually walking the task list.
7988  */
7989 
7990 #define _LINUX_MAX_ERRNO 4095
7991 #define _LINUX_IS_ERR(x) unlikely((x) >= (REGVAL)-_LINUX_MAX_ERRNO)
7992 
7994  void *handler_data,
7995  struct probe *trigger,
7996  struct probe *base) {
7997  struct target *target = probe->target;
7998  struct os_linux_state *lstate =
7999  (struct os_linux_state *)target->personality_state;
8000  struct target_thread *tthread;
8001  REGVAL ax;
8002  struct value *value;
8003  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
8004 
8005 #ifdef APF_TE_COPY_PROCESS
8006  /*
8007  * Load task from retval in %ax
8008  */
8009  errno = 0;
8010  ax = target_read_creg(target,tid,CREG_RET);
8011  if (errno) {
8012  verror("could not read %%ax to get copy_process retval!\n");
8013  return RESULT_SUCCESS;
8014  }
8015 
8016  if (_LINUX_IS_ERR(ax)) {
8017  vwarnopt(5,LA_TARGET,LF_OSLINUX,"copy_process failed internally!\n");
8018  return RESULT_SUCCESS;
8019  }
8020 
8021  vdebug(5,LA_TARGET,LF_OSLINUX,"copy_process returned 0x%"PRIxADDR"\n",ax);
8022 
8023  value = target_load_type(target,lstate->task_struct_type,ax,LOAD_FLAG_NONE);
8024  if (!value) {
8025  /*
8026  * This target does not require thread entry tracking; so ignore
8027  * it. We just load threads as they appear; it's stale threads
8028  * we really prefer to avoid -- or for overlay targets, we need
8029  * to know when a overlay thread disappears.
8030  */
8031  vwarn("could not load retval in %s; ignoring new thread!\n",
8033  return RESULT_SUCCESS;
8034  }
8035 #else
8036  value = target_load_symbol(target,tlctxt,lstate->thread_entry_v_symbol,
8038  if (!value) {
8039  /*
8040  * We need avoid stale threads for overlay targets; we need to
8041  * know when a overlay thread disappears.
8042  */
8043  vwarn("could not load %s in %s; ignoring new thread!\n",
8046  return RESULT_SUCCESS;
8047  }
8048 #endif
8049 
8050  if (!(tthread = os_linux_load_thread_from_value(target,value))) {
8051  verror("could not load thread from task value; BUG?\n");
8052  value_free(value);
8053  return RESULT_SUCCESS;
8054  }
8055 
8057  "new task %"PRIiTID" (%s)\n",tthread->tid,tthread->name);
8058 
8059  return RESULT_SUCCESS;
8060 }
8061 
8062 /*
8063  * NB: this was hard!
8064  *
8065  * It is very, very hard to find an available function to place a probe
8066  * on, that is not either optimized out of existence (well, it may
8067  * exist, but either the instances are not available or the parameters
8068  * have no locations at the inlined site). __unhash_process, the
8069  * function that takes the pid off the list, is the ideal place, but
8070  * that doesn't work. release_task also does not work, but that is
8071  * because it contains a loop that reaps zombie group leaders (if they
8072  * exist) -- so we would miss some zombies. Thus we are left with
8073  * sched_exit, which is (unfortunately) well after the process is off
8074  * the task list. BUT -- every exiting task hits it. Another
8075  * alternative (of course) is to probe inside of schedule(), but that is
8076  * tricky because it adds tons of unnecessary overhead,
8077  *
8078  * Of course, then the problem becomes that in release_task, the task IS
8079  * exiting, but it is still running on its kernel stack (at least when
8080  * called from the normal do_exit() exit path; this means that although
8081  * we want to delete our thread, we cannot delete it because it may
8082  * "reappear".
8083  *
8084  * One strategy, for kernels that do NOT support CONFIG_PREEMPT, is to
8085  * mark the thread "exiting", and when the next debug exception hits,
8086  * clear out any such threads if the exception is not for one of them!
8087  * Why does this work? An exiting task will simply not be preempted
8088  * until it calls schedule().
8089  *
8090  * For kernels that do support CONFIG_PREEMPT, the only "safe" places to
8091  * *know* that a task is exiting, but preemption has been disabled, is
8092  * the call to preempt_disable() inside do_exit(). And we can't catch
8093  * that -- it is really just a write to a field in the task struct,
8094  * followed by an MFENCE instruction (on x86). So, we really have to
8095  * catch the call to schedule() inside do_exit() -- or the call to
8096  * release_task() inside wait_task_zombie(). This makes sure we catch
8097  * all the paths leading to release_task().
8098  *
8099  * NB: therefore, this version does not support CONFIG_PREEMPT very well
8100  * -- it could be racy. We need support for placing probes on function
8101  * invocations; this requires disasm support.
8102  */
8104  void *handler_data,
8105  struct probe *trigger,
8106  struct probe *base) {
8107  struct target *target = probe->target;
8108  struct os_linux_state *lstate =
8109  (struct os_linux_state *)target->personality_state;
8110  struct target_thread *tthread;
8111  struct value *value;
8112  struct target_location_ctxt *tlctxt = target_global_tlctxt(target);
8113 
8114  /*
8115  * Load task.
8116  */
8117  value = target_load_symbol(target,tlctxt,
8119 
8120  if (!value) {
8121  /*
8122  * We need avoid stale threads for overlay targets; we need to
8123  * know when a overlay thread disappears.
8124  */
8125  vwarn("could not load %s in %s; ignoring new thread!\n",
8128  return RESULT_SUCCESS;
8129  }
8130 
8131  if (!(tthread = os_linux_load_thread_from_value(target,value))) {
8132  verror("could not load thread from task value; BUG?\n");
8133  value_free(value);
8134  return RESULT_SUCCESS;
8135  }
8136 
8137  tthread->exiting = 1;
8138 
8140  "exiting task %"PRIiTID" (%s)\n",tthread->tid,tthread->name);
8141 
8142  return RESULT_SUCCESS;
8143 }
8144 
8145 static void os_linux_mm_free(struct os_linux_mm *olmm) {
8146  struct os_linux_vma *olvma,*olvma_next;
8147  REFCNT trefcnt;
8148 
8149  if (olmm->space) {
8150  RPUT(olmm->space,addrspace,target,trefcnt);
8151  olmm->space = NULL;
8152  }
8153 
8154  olvma = olmm->vma_cache;
8155  olvma_next = (olvma) ? olvma->next : NULL;
8156  while (olvma) {
8157  value_free(olvma->vma);
8158  free(olvma);
8159  olvma = olvma_next;
8160  olvma_next = olvma ? olvma->next : NULL;
8161  }
8162  olmm->vma_cache = NULL;
8163 
8164  if (olmm->mm) {
8165  value_free(olmm->mm);
8166  olmm->mm = NULL;
8167  }
8168  olmm->vma_len = 0;
8169 
8170  return;
8171 }
8172 
8173 /*
8174  * This actively updates target_os_process structs's @space member.
8175  *
8176  * Its performance is (much) better if its values are mmap'd. Why?
8177  * Because we cache vm_area_struct values for each region in the task's
8178  * mmap, and we can compare the member values in the mmap'd value direct
8179  * with the region-cached values without copying. If we have to load
8180  * the new
8181  */
8182 static struct addrspace *os_linux_space_load(struct target *target,
8183  struct target_thread *tthread) {
8184  struct os_linux_state *lstate;
8185  struct os_linux_thread_state *xtstate;
8186  tid_t tid;
8187  char buf[PATH_MAX];
8188  struct memregion *region;
8189  struct memrange *range;
8190  region_type_t rtype;
8191  struct value *vma;
8192  struct value *vma_prev;
8193  struct os_linux_mm *olmm = NULL;
8194  struct os_linux_vma *new_vma,*cached_vma,*cached_vma_prev;
8195  struct os_linux_vma *tmp_cached_vma,*tmp_cached_vma_d;
8196  ADDR mm_addr;
8197  ADDR vma_addr;
8198  ADDR vma_next_addr;
8199  ADDR start,end,offset;
8200  unum_t prot_flags;
8201  ADDR file_addr;
8202  char *prev_vma_member_name;
8203  struct value *file_value;
8204  int found;
8205  int created = 0;
8206  struct target_location_ctxt *tlctxt;
8207  struct target_event *event;
8208  char nbuf[32];
8209  ADDR vm_mm_addr = 0;
8210 
8211  lstate = (struct os_linux_state *)target->personality_state;
8212  xtstate = \
8213  (struct os_linux_thread_state *)tthread->personality_state;
8214  tid = tthread->tid;
8215 
8216  vdebug(5,LA_TARGET,LF_OSP,"tid %d scanning\n",tid);
8217 
8218  tlctxt = target_global_tlctxt(target);
8219 
8220  /*
8221  * So, what do we have to do? target_load_thread on the base target
8222  * will get us the task_struct; but from there we have to check
8223  * everything: first task->mm, then task->mm->mmap, then all the
8224  * task->mm->mmap->vm_next pointers that form the list of
8225  * vm_area_structs that compose the task's mmap. Basically, we keep
8226  * a list of cached vm_area_struct values that mirrors the kernel's
8227  * list; for each vm_area_struct, we cache its value and its last
8228  * vm_next addr, and a non-VMI pointer to the next cached vma.
8229  *
8230  * (This code is written to use the value_refresh() API to quickly
8231  * see if a value has changed. We don't care about the old value.)
8232  *
8233  * The kernel maintains a sorted list, so figuring out which ranges
8234  * need updating is (somewhat) easier. We simply run through the
8235  * kernel's list, comparing it to our list. If the vaddr of the
8236  * i-th vma *does* match the cached vma's vaddr, we just check to
8237  * see if the value has changed. If it has, we updated accordingly;
8238  * otherwise we proceed to the i+1-th entry. If it *does not*
8239  * match, either our cached entry has been deleted; a new entry has
8240  * been inserted in the kernel; or both. If it does not match, we
8241  * scan down our cached list until the i-th vma start addr >= our
8242  * i+j-th cached vma start addr, and delete all the cached entries
8243  * until that point. At that point, if it does not match the i+j-th
8244  * cached vma addr, we insert it as a new mmap entry. That's it! :)
8245  *
8246  * One other note. Each vm_area_struct gets its own memrange; we
8247  * combine memranges into memregions based on the files that back
8248  * them. Only ranges with the same filename get combined into
8249  * memregions.
8250  */
8251 
8252  /* Grab the base task's mm address to see if it changed. */
8253  /* Wait, already have this in xtstate->mm_addr; it should be up-to-date! */
8254  //VLV(target,tlctxt,xtstate->task_struct,"mm",LOAD_FLAG_NONE,
8255  // &mm_addr,NULL,err_vmiload);
8256 
8257  if (xtstate->mm_addr == 0 && xtstate->last_mm_addr) {
8258  /*
8259  * This should be impossible! A task's mm should not go away.
8260  */
8261  verror("tid %d's task->mm became NULL; impossible -- BUG!!\n",tid);
8262 
8263  return NULL;
8264  }
8265 
8266  /* This is the thread's mm_addr; because we assume that the tthread
8267  * param is loaded per this current pause/exception.
8268  */
8269  mm_addr = xtstate->mm_addr;
8270 
8271  /*
8272  * Ok, either we have a new mm, or one we already cached.
8273  */
8274 
8275  olmm = (struct os_linux_mm *) \
8276  g_hash_table_lookup(lstate->mm_addr_to_mm_cache,
8277  (gpointer)(uintptr_t)mm_addr);
8278  if (!olmm) {
8279  created = 1;
8280  olmm = (struct os_linux_mm *)calloc(1,sizeof(*olmm));
8281  snprintf(nbuf,sizeof(nbuf),"os_linux_process(%d)",tid);
8282  olmm->space = addrspace_create(NULL,nbuf,mm_addr);
8283  RHOLD(olmm->space,target);
8284  g_hash_table_insert(lstate->mm_addr_to_mm_cache,
8285  (gpointer)(uintptr_t)mm_addr,olmm);
8286  }
8287 
8288  /*
8289  * Reload the mm struct first, and re-cache its members. Null
8290  * everything out to try to minimize inconsistent state.
8291  */
8292  olmm->mm_start_brk = 0;
8293  olmm->mm_brk = 0;
8294  olmm->mm_start_stack = 0;
8295 
8296  if (olmm->mm) {
8297  /*
8298  * NB: when value_refresh is implemented, we maybe want to use that
8299  * to reload so we can try not to; for now, just do it manually.
8300  */
8301  value_free(olmm->mm);
8302  olmm->mm = NULL;
8303  }
8304  VL(target,tlctxt,xtstate->task_struct,"mm",
8305  LOAD_FLAG_AUTO_DEREF,&olmm->mm,err_vmiload);
8306  //xtstate->mm_addr = value_addr(olmm->mm);
8307  VLV(target,tlctxt,olmm->mm,"start_brk",LOAD_FLAG_NONE,
8308  &olmm->mm_start_brk,NULL,err_vmiload);
8309  VLV(target,tlctxt,olmm->mm,"brk",LOAD_FLAG_NONE,
8310  &olmm->mm_brk,NULL,err_vmiload);
8311  VLV(target,tlctxt,olmm->mm,"start_stack",LOAD_FLAG_NONE,
8312  &olmm->mm_start_stack,NULL,err_vmiload);
8313 
8314  /*
8315  * We used to only free and update the mm_struct value for the first
8316  * two cases, I think -- but in reality we should always reload it.
8317  * Plus, if the base target has mmap support, the overhead is not so
8318  * bad... anyway, this stuff is just debugging now.
8319  */
8320  if (xtstate->last_mm_addr == 0) {
8321  vdebug(5,LA_TARGET,LF_OSP,"tid %d analyzing mmaps anew.\n",tid);
8322  }
8323  else if (mm_addr && mm_addr != xtstate->last_mm_addr) {
8324  vwarn("tid %d's task->mm changed (0x%"PRIxADDR" to 0x%"PRIxADDR");"
8325  " checking cached VMAs like normal!\n",
8326  tid,xtstate->last_mm_addr,mm_addr);
8327  }
8328  else {
8329  vdebug(5,LA_TARGET,LF_OSP,"tid %d checking cached VMAs.\n",tid);
8330  }
8331  xtstate->last_mm_addr = mm_addr;
8332 
8333  /*
8334  * Now that we have loaded or re-cached the mm_struct, we
8335  * need to loop through its mmaps, and add/delete/modify as
8336  * necessary.
8337  */
8338 
8339  /* Now we have a valid task->mm; load the first vm_area_struct pointer. */
8340  VLV(target,tlctxt,olmm->mm,"mmap",LOAD_FLAG_NONE,
8341  &vma_addr,NULL,err_vmiload);
8342  cached_vma = olmm->vma_cache;
8343  cached_vma_prev = NULL;
8344 
8345  /* First time through, the value we load the vm_area_struct value
8346  * from is the mm_struct; after that, it is the previous
8347  * vm_area_struct. The macros in the loop hide this.
8348  */
8349  vma_prev = olmm->mm;
8350  prev_vma_member_name = "mmap";
8351 
8352  VL(target,tlctxt,vma_prev,prev_vma_member_name,
8353  LOAD_FLAG_AUTO_DEREF,&vma,err_vmiload);
8354  VLV(target,tlctxt,vma,"vm_start",LOAD_FLAG_NONE,
8355  &start,NULL,err_vmiload);
8356  value_free(vma);
8357 
8358 #warning "generate MOD events!"
8359 
8360  /* If we have either a vma_addr to process, or a cached_vma, keep going. */
8361  while (vma_addr || cached_vma) {
8362  if (vma_addr && !cached_vma) {
8363  /*
8364  * New entry; load it and add/cache it.
8365  *
8366  * NB: do_new_unmatched comes from lower in the loop, where
8367  * we
8368  */
8369  do_new_unmatched:
8370 
8371  VL(target,tlctxt,vma_prev,prev_vma_member_name,
8372  LOAD_FLAG_AUTO_DEREF,&vma,err_vmiload);
8373  new_vma = calloc(1,sizeof(*new_vma));
8374  new_vma->vma = vma;
8375 
8376  /* Load the vma's start,end,offset,prot_flags,file,next addr. */
8377  VLV(target,tlctxt,vma,"vm_start",LOAD_FLAG_NONE,
8378  &start,NULL,err_vmiload);
8379  VLV(target,tlctxt,vma,"vm_end",LOAD_FLAG_NONE,
8380  &end,NULL,err_vmiload);
8381  VLV(target,tlctxt,vma,"vm_flags",LOAD_FLAG_NONE,
8382  &prot_flags,NULL,err_vmiload);
8383  VLV(target,tlctxt,vma,"vm_pgoff",LOAD_FLAG_NONE,
8384  &offset,NULL,err_vmiload);
8385  VLV(target,tlctxt,vma,"vm_file",LOAD_FLAG_NONE,
8386  &file_addr,NULL,err_vmiload);
8387  VLV(target,tlctxt,vma,"vm_next",LOAD_FLAG_NONE,
8388  &vma_next_addr,NULL,err_vmiload);
8389  VLV(target,tlctxt,vma,"vm_mm",LOAD_FLAG_NONE,
8390  &vm_mm_addr,NULL,err_vmiload);
8391 
8392  /* Figure out the region type. */
8393  rtype = REGION_TYPE_ANON;
8394  region = NULL;
8395  buf[0] = '\0';
8396 
8397  /* If it has a file, load the path! */
8398  if (file_addr != 0) {
8399  file_value = NULL;
8400  VL(target,tlctxt,vma,"vm_file",LOAD_FLAG_AUTO_DEREF,
8401  &file_value,err_vmiload);
8402  if (!os_linux_file_get_path(target,xtstate->task_struct,file_value,
8403  buf,sizeof(buf))) {
8404  vwarn("could not get filepath for struct file for new range;"
8405  " continuing! (file 0x%"PRIxADDR")\n",file_addr);
8406  file_addr = 0;
8407  }
8408  else {
8409  /* Find the region this is in, if any. */
8410  region = addrspace_find_region(olmm->space,buf);
8411  }
8412  }
8413  else {
8414  if (addrspace_find_range_real(olmm->space,start - 1,&region,NULL)
8415  && region->name && *region->name != '\0') {
8417  "found contiguous region (%s) for next anon region at start 0x%"PRIxADDR"\n",
8418  region->name,start);
8419  }
8420  else
8421  region = NULL;
8422  }
8423 
8424  /* Create the region if we didn't find one. */
8425  if (!region) {
8426  if (!file_addr) {
8427  if (start <= olmm->mm_start_brk
8428  && end >= olmm->mm_brk)
8429  rtype = REGION_TYPE_HEAP;
8430  else if (start <= olmm->mm_start_stack
8431  && end >= olmm->mm_start_stack)
8432  rtype = REGION_TYPE_STACK;
8433  else if (vm_mm_addr == 0)
8434  rtype = REGION_TYPE_VDSO;
8435  /* else, stick with our REGION_TYPE_ANON default. */
8436  }
8437  else {
8438  /*
8439  * Anything with a filename starts out as a lib; if
8440  * we can't load it, it might become anon; if we can
8441  * load it and it has a main(), we'll convert it to
8442  * MAIN later.
8443  */
8444  rtype = REGION_TYPE_LIB;
8445  }
8446 
8447  region = memregion_create(olmm->space,rtype,
8448  (buf[0] == '\0') ? NULL : buf);
8449  if (!region)
8450  goto err;
8451 
8453  "created memregion(%s:0x%"PRIxADDR":%s:%s)\n",
8454  region->space->name,region->space->tag,region->name,
8455  REGION_TYPE(region->type));
8456 
8457  event = target_create_event(target,tthread,
8459  region);
8460  target_broadcast_event(target,event);
8461  }
8462 
8463  /* Create the range. */
8464  if (!(range = memrange_create(region,start,end,offset,prot_flags)))
8465  goto err;
8466  new_vma->range = range;
8467 
8469  "created memrange(%s:%s:0x%"PRIxADDR",0x%"PRIxADDR","
8470  "%"PRIiOFFSET",%u)\n",
8471  range->region->name,REGION_TYPE(range->region->type),
8472  range->start,range->end,range->offset,range->prot_flags);
8473 
8474  event = target_create_event(target,tthread,
8476  target_broadcast_event(target,event);
8477 
8478  /*
8479  * Update list/metadata:
8480  *
8481  * Either make it the sole entry on list, or add it at tail.
8482  * Either way, there is still no cached_vma to process; it's
8483  * just that our previous one points to the new tail of the
8484  * list for the next iteration.
8485  */
8486  if (!olmm->vma_cache)
8487  olmm->vma_cache = new_vma;
8488  else if (cached_vma_prev) {
8489  cached_vma_prev->next = new_vma;
8490  cached_vma_prev->next_vma_addr = vma_addr;
8491  }
8492  else {
8493  /* Add it as the first entry on the list. */
8494  new_vma->next = olmm->vma_cache;
8495  new_vma->next_vma_addr = olmm->vma_cache ? value_addr(olmm->vma_cache->vma) : 0;
8496  olmm->vma_cache = new_vma;
8497  }
8498  ++olmm->vma_len;
8499  cached_vma_prev = new_vma;
8500 
8501  new_vma->next = cached_vma;
8502 
8503  vma_addr = vma_next_addr;
8504  vma_prev = vma;
8505 
8506  /* After the first iteration, it's always this. */
8507  prev_vma_member_name = "vm_next";
8508 
8509  continue;
8510  }
8511  else if (!vma_addr && cached_vma) {
8512  /*
8513  * We don't have any more vm_area_structs from the kernel,
8514  * so any cached entries are stale at this point.
8515  */
8516  tmp_cached_vma_d = cached_vma;
8517 
8518  /*
8519  * Update list/metadata:
8520  */
8521  if (cached_vma_prev) {
8522  cached_vma_prev->next = cached_vma->next;
8523  if (cached_vma->next && cached_vma->next->vma)
8524  cached_vma_prev->next_vma_addr =
8525  value_addr(cached_vma->next->vma);
8526  else
8527  cached_vma_prev->next_vma_addr = 0;
8528  }
8529  else {
8530  olmm->vma_cache = cached_vma->next;
8531  if (cached_vma->next && cached_vma->next->vma)
8532  olmm->vma_cache->next_vma_addr =
8533  value_addr(cached_vma->next->vma);
8534  else
8535  cached_vma_prev->next_vma_addr = 0;
8536  }
8537 
8538  cached_vma = cached_vma->next;
8539  --olmm->vma_len;
8540 
8542  "removing stale memrange(%s:%s:0x%"PRIxADDR",0x%"PRIxADDR","
8543  "%"PRIiOFFSET",%u)\n",
8544  tmp_cached_vma_d->range->region->name,
8545  REGION_TYPE(tmp_cached_vma_d->range->region->type),
8546  tmp_cached_vma_d->range->start,tmp_cached_vma_d->range->end,
8547  tmp_cached_vma_d->range->offset,
8548  tmp_cached_vma_d->range->prot_flags);
8549 
8550  /* delete range; delete empty regions when they empty. */
8551  region = tmp_cached_vma_d->range->region;
8552  event = target_create_event(target,tthread,
8554  tmp_cached_vma_d->range);
8555  target_broadcast_event(target,event);
8556 
8557  memregion_detach_range(region,tmp_cached_vma_d->range);
8558 
8559  if (!region->ranges) {
8561  "removing empty memregion(%s:0x%"PRIxADDR":%s:%s)\n",
8562  region->space->name,region->space->tag,region->name,
8563  REGION_TYPE(region->type));
8564 
8565  event = target_create_event(target,tthread,
8567  region);
8568  target_broadcast_event(target,event);
8569 
8570  addrspace_detach_region(region->space,region);
8571  }
8572 
8573  /* delete cached value stuff */
8574  value_free(tmp_cached_vma_d->vma);
8575  free(tmp_cached_vma_d);
8576  tmp_cached_vma_d = NULL;
8577 
8578  continue;
8579  }
8580  /*
8581  * Need to compare vma_addr with our cached_vma's addr; and...
8582  *
8583  * If the vaddr of the i-th vma *does* match the cached
8584  * vma's vaddr, we just check to see if the value has
8585  * changed. If it has, we updated accordingly; otherwise we
8586  * proceed to the i+1-th entry. If it *does not* match,
8587  * either our cached entry has been deleted; a new entry has
8588  * been inserted in the kernel; or both. If it does not
8589  * match, we scan down our cached list until the i-th vma
8590  * start addr >= our i+j-th cached vma start addr, and
8591  * delete all the cached entries until that point. At that
8592  * point, if it does not match the i+j-th cached vma addr,
8593  * we insert it as a new mmap entry.
8594  */
8595  else if (vma_addr == value_addr(cached_vma->vma)) {
8596  /*
8597  * Refresh the value; update the range.
8598  */
8600  "tid %d refreshing vm_area_struct at 0x%"PRIxADDR"\n",
8601  vma_addr);
8602  value_refresh(cached_vma->vma,0);
8603 
8604  /* Load the vma's start,end,prot_flags. */
8605  VLV(target,tlctxt,cached_vma->vma,"vm_start",
8606  LOAD_FLAG_NONE,&start,NULL,err_vmiload);
8607  VLV(target,tlctxt,cached_vma->vma,"vm_end",
8608  LOAD_FLAG_NONE,&end,NULL,err_vmiload);
8609  VLV(target,tlctxt,cached_vma->vma,"vm_flags",
8610  LOAD_FLAG_NONE,&prot_flags,NULL,err_vmiload);
8611  VLV(target,tlctxt,cached_vma->vma,"vm_pgoff",
8612  LOAD_FLAG_NONE,&offset,NULL,err_vmiload);
8613  VLV(target,tlctxt,cached_vma->vma,"vm_next",
8614  LOAD_FLAG_NONE,&vma_next_addr,NULL,err_vmiload);
8615 
8616  if (cached_vma->range->end == end
8617  && cached_vma->range->offset == offset
8618  && cached_vma->range->prot_flags == (unsigned int)prot_flags) {
8619  OBJSLIVE(cached_vma->range,memrange);
8620 
8622  "no change to memrange(%s:%s:0x%"PRIxADDR",0x%"PRIxADDR","
8623  "%"PRIiOFFSET",%u)\n",
8624  cached_vma->range->region->name,
8625  REGION_TYPE(cached_vma->range->region->type),
8626  cached_vma->range->start,cached_vma->range->end,
8627  cached_vma->range->offset,cached_vma->range->prot_flags);
8628  }
8629  else {
8630  cached_vma->range->end = end;
8631  cached_vma->range->offset = offset;
8632  cached_vma->range->prot_flags = prot_flags;
8633 
8634  OBJSMOD(cached_vma->range);
8635 
8636  if (start < cached_vma->range->region->base_load_addr)
8637  cached_vma->range->region->base_load_addr = start;
8638 
8640  "update to memrange(%s:%s:0x%"PRIxADDR",0x%"PRIxADDR","
8641  "%"PRIiOFFSET",%u)\n",
8642  cached_vma->range->region->name,
8643  REGION_TYPE(cached_vma->range->region->type),
8644  cached_vma->range->start,cached_vma->range->end,
8645  cached_vma->range->offset,cached_vma->range->prot_flags);
8646 
8647  event = target_create_event(target,tthread,
8649  cached_vma->range);
8650  target_broadcast_event(target,event);
8651  }
8652 
8653  /*
8654  * Update list/metadata for next iteration:
8655  */
8656  cached_vma_prev = cached_vma;
8657  cached_vma = cached_vma->next;
8658 
8659  vma_addr = vma_next_addr;
8660  vma_prev = cached_vma_prev->vma;
8661 
8662  /* After the first iteration, it's always this. */
8663  prev_vma_member_name = "vm_next";
8664 
8665  continue;
8666  }
8667  else {
8668  /*
8669  * Load the next one enough to get its start addr, so we can
8670  * do the comparison. The load is not wasted; we goto
8671  * (ugh, ugh, ugh) wherever we need after loading it.
8672  */
8673 
8674  /*
8675  * Since we haven't loaded the vm_area_struct corresponding
8676  * to vma_addr yet, the best we can do is look through the
8677  * rest of our cached list, and see if we get a match on
8678  * vma_addr and value_addr(tmp_cached_vma->vma). If we do,
8679  * *then* feel safe enough to delete the intervening
8680  * entries. If we do not -- we can only add a new entry,
8681  * then continue to process the rest of our list -- so goto
8682  * the top of the loop where we add new entries -- ugh!!!
8683  */
8684  tmp_cached_vma = cached_vma;
8685 
8686  found = 0;
8687  while (tmp_cached_vma) {
8688  if (vma_addr == value_addr(tmp_cached_vma->vma)) {
8689  found = 1;
8690  break;
8691  }
8692  tmp_cached_vma = tmp_cached_vma->next;
8693  }
8694 
8695  if (!found) {
8696  /* XXX: teleport! */
8697  goto do_new_unmatched;
8698  }
8699 
8700  /* Otherwise, proceed to delete the intermediate ones. */
8701 
8702  tmp_cached_vma = cached_vma;
8703 
8704  while (tmp_cached_vma && vma_addr != value_addr(tmp_cached_vma->vma)) {
8705  /*
8706  * Update list/metadata:
8707  */
8708  if (cached_vma_prev) {
8709  cached_vma_prev->next = tmp_cached_vma->next;
8710  if (tmp_cached_vma->next && tmp_cached_vma->next->vma)
8711  cached_vma_prev->next_vma_addr =
8712  value_addr(tmp_cached_vma->next->vma);
8713  else
8714  cached_vma_prev->next_vma_addr = 0;
8715  }
8716  else {
8717  olmm->vma_cache = tmp_cached_vma->next;
8718  if (tmp_cached_vma->next && tmp_cached_vma->next->vma)
8719  olmm->vma_cache->next_vma_addr =
8720  value_addr(tmp_cached_vma->next->vma);
8721  else
8722  cached_vma_prev->next_vma_addr = 0;
8723  }
8724 
8725  tmp_cached_vma_d = tmp_cached_vma;
8726 
8727  tmp_cached_vma = tmp_cached_vma->next;
8728  --olmm->vma_len;
8729 
8731  "removing stale memrange(%s:%s:0x%"PRIxADDR",0x%"PRIxADDR","
8732  "%"PRIiOFFSET",%u)\n",
8733  tmp_cached_vma_d->range->region->name,
8734  REGION_TYPE(tmp_cached_vma_d->range->region->type),
8735  tmp_cached_vma_d->range->start,
8736  tmp_cached_vma_d->range->end,
8737  tmp_cached_vma_d->range->offset,
8738  tmp_cached_vma_d->range->prot_flags);
8739 
8740  /* delete range; delete empty regions when they empty. */
8741  region = tmp_cached_vma_d->range->region;
8742 
8743  event = target_create_event(target,tthread,
8745  tmp_cached_vma_d->range);
8746  target_broadcast_event(target,event);
8747 
8748  memregion_detach_range(region,tmp_cached_vma_d->range);
8749  if (!region->ranges)
8750  addrspace_detach_region(region->space,region);
8751 
8752  /* delete cached value stuff */
8753  value_free(tmp_cached_vma_d->vma);
8754  free(tmp_cached_vma_d);
8755  tmp_cached_vma_d = NULL;
8756  }
8757 
8758  cached_vma = tmp_cached_vma;
8759 
8760  /*
8761  * Now that we deleted any stale/dead mmaps, check if we
8762  * still have a cached vma. If we do, and it is ==
8763  * vma_addr, just continue the outer loop; handled by third
8764  * case of outer loop.
8765  */
8766  if (cached_vma && vma_addr == value_addr(cached_vma->vma)) {
8768  "continuing loop; cached_vma matches vma_addr (0x%"PRIxADDR");"
8769  " memrange(%s:%s:0x%"PRIxADDR",0x%"PRIxADDR","
8770  "%"PRIiOFFSET",%u)\n",
8771  vma_addr,cached_vma->range->region->name,
8772  REGION_TYPE(cached_vma->range->region->type),
8773  cached_vma->range->start,cached_vma->range->end,
8774  cached_vma->range->offset,cached_vma->range->prot_flags);
8775  continue;
8776  }
8777  /*
8778  * Otherwise, we need to add a new one (handled by first
8779  * case of main loop).
8780  */
8781  else if (cached_vma) {
8783  "continuing loop; cached_vma does not match vma_addr (0x%"PRIxADDR");"
8784  " cached_vma memrange(%s:%s:0x%"PRIxADDR",0x%"PRIxADDR","
8785  "%"PRIiOFFSET",%u)\n",
8786  vma_addr,cached_vma->range->region->name,
8787  REGION_TYPE(cached_vma->range->region->type),
8788  cached_vma->range->start,cached_vma->range->end,
8789  cached_vma->range->offset,cached_vma->range->prot_flags);
8790  continue;
8791  }
8792  else {
8794  "continuing loop; no more cached_vmas; all others will"
8795  " be new!\n");
8796  continue;
8797  }
8798  }
8799  }
8800 
8801  /*
8802  * Clear the new/existing/same/updated bits no matter what.
8803  */
8804 
8805  //zzz
8806 
8807  return olmm->space;
8808 
8809  err:
8810  // XXX cleanup the regions we added/modified??
8811  return NULL;
8812 
8813  err_vmiload:
8814  return NULL;
8815 }
8816 
8817 GHashTable *os_linux_processes_load(struct target *target) {
8818  return ((struct os_linux_state *)target->state)->processes;
8819 }
8820 
8821 struct target_process *os_linux_process_load(struct target *target,
8822  struct target_thread *tthread) {
8823  struct target_location_ctxt *tlctxt;
8824  struct os_linux_state *lstate;
8825  struct os_linux_thread_state *ltstate;
8826  struct target_process *process;
8827  int created = 0;
8828  struct target_thread *othread;
8829  struct addrspace *space;
8830 
8831  lstate = (struct os_linux_state *)target->personality_state;
8832  tlctxt = target_global_tlctxt(target);
8833 
8834  process = (struct target_process *) \
8835  g_hash_table_lookup(lstate->processes,(gpointer)(uintptr_t)tthread->tgid);
8836  if (process && OBJVALID(process))
8837  return process;
8838 
8839  othread = tthread;
8840 
8841  /* Make sure we have a valid thread, since we got a struct. */
8842  if (!OBJVALID(tthread))
8843  tthread = target_load_thread(target,tthread->tid,0);
8844  if (!tthread) {
8845  verror("thread %d no longer exists!\n",othread->tid);
8846  errno = EINVAL;
8847  return NULL;
8848  }
8849 
8850  /* Make sure we have the group leader. */
8851  if (tthread->tid != tthread->tgid)
8852  tthread = target_load_thread(target,tthread->tgid,0);
8853 
8854  ltstate = (struct os_linux_thread_state *)tthread->personality_state;
8855 
8856  /* First, if this is not a userspace thread, don't call it a process. */
8857  if (ltstate->mm_addr == 0) {
8858  errno = 0;
8859  return NULL;
8860  }
8861 
8862  if (!process) {
8863  space = os_linux_space_load(target,tthread);
8864  process = target_process_create(target,tthread,space);
8865  created = 1;
8866  if (!process) {
8867  verror("could not associate thread %d with process!\n",tthread->tid);
8868  errno = EFAULT;
8869  return NULL;
8870  }
8871  }
8872  else {
8873  /* If the process has changed mm, catch that. */
8874  space = os_linux_space_load(target,tthread);
8875  if (space != process->space) {
8876  REFCNT trefcnt;
8877  RPUT(space,addrspace,process,trefcnt);
8878 
8879  process->space = space;
8880  RHOLD(space,process);
8881  }
8882  }
8883 
8884  if (created) {
8885  g_hash_table_insert(lstate->processes,
8886  (gpointer)(uintptr_t)tthread->tgid,process);
8887  RHOLD(process,target);
8888  }
8889 
8890  goto out;
8891 
8892  errout:
8893  if (process && created) {
8894  /* It's not yet held; just free it. */
8895  target_process_free(process,1);
8896  }
8897  return NULL;
8898 
8899  out:
8900  OBJSVALID(process);
8901 
8902  /*
8903  * Autofill the threads in the process. If we just created this
8904  * process, we have to bootstrap it; otherwise, if thread tracking
8905  * is on, we can skip it.
8906  *
8907  * NB: this must come *after* the process is in the hashtable!
8908  * Otherwise this will loop infinitely.
8909  */
8910  if (created || !(target->ap_flags & APF_OS_THREAD_ENTRY
8911  && target->ap_flags & APF_OS_THREAD_EXIT)) {
8913  }
8914 
8915  return process;
8916 }
8917 
8918 static result_t os_linux_active_process_memory_post_handler(struct probe *probe,
8919  tid_t tid,
8920  void *handler_data,
8921  struct probe *trigger,
8922  struct probe *base) {
8923  struct target *target = (struct target *)handler_data;
8924  struct os_linux_state *lstate = (struct os_linux_state *)target->state;
8925  struct target_thread *tthread;
8926  struct target_process *process;
8927  struct addrspace *space;
8928 
8929  tthread = target_load_thread(target,tid,0);
8930 
8931  /*
8932  * Find or load the process for this thread...
8933  */
8934  process = (struct target_process *) \
8935  g_hash_table_lookup(lstate->processes,(gpointer)(uintptr_t)tthread->tgid);
8936 
8937  if (!process) {
8938  process = os_linux_process_load(target,tthread);
8939  if (!process) {
8940  verror("could not associate thread %d with process!\n",tthread->tid);
8941  return -1;
8942  }
8943  }
8944  else {
8945  space = os_linux_space_load(target,tthread);
8946 
8947  /* If the process has changed mm, catch that. */
8948  if (space != process->space) {
8949  REFCNT trefcnt;
8950  RPUT(space,addrspace,process,trefcnt);
8951 
8952  process->space = space;
8953  RHOLD(space,process);
8954  }
8955  }
8956 
8957  return RESULT_SUCCESS;
8958 }
8959 
8960 int os_linux_set_active_probing(struct target *target,
8961  active_probe_flags_t flags) {
8962  struct os_linux_state *lstate = \
8963  (struct os_linux_state *)target->personality_state;
8964  struct probe *probe;
8965  char *name;
8966  int forced_load = 0;
8967  int retval = 0;
8968  struct bsymbol *bs;
8969 
8970  if (!target->writeable && flags != AFP_NONE) {
8971  verror("target %s not writeable!\n",target->name);
8972  errno = EROFS;
8973  return -1;
8974  }
8975 
8976  /*
8977  * Filter out the default flags according to our personality.
8978  */
8979  if (flags & APF_THREAD_ENTRY) {
8980  flags &= ~APF_THREAD_ENTRY;
8981  flags |= APF_OS_THREAD_ENTRY;
8982  }
8983  if (flags & APF_THREAD_EXIT) {
8984  flags &= ~APF_THREAD_EXIT;
8985  flags |= APF_OS_THREAD_EXIT;
8986  }
8987  if (flags & APF_MEMORY) {
8988  flags &= ~APF_MEMORY;
8989  flags |= APF_OS_MEMORY;
8990  }
8991 
8992  if ((flags & APF_OS_MEMORY)
8993  != (target->ap_flags & APF_OS_MEMORY)) {
8994  if (flags & APF_OS_MEMORY) {
8995  probe = probe_create(target,TID_GLOBAL,NULL,
8997  os_linux_active_memory_handler,NULL,NULL,0,1);
8998  /* NB: always use this; it should be the default! */
9000  1,PROBEPOINT_SW,0,0)) {
9001  probe_free(probe,1);
9002  probe = NULL;
9003 
9004  vwarn("could not probe module_free; not enabling"
9005  " active memory updates!\n");
9006 
9007  lstate->active_memory_probe = NULL;
9008  target->ap_flags &= ~APF_OS_MEMORY;
9009 
9010  --retval;
9011  }
9012  else {
9013  lstate->active_memory_probe = probe;
9014  target->ap_flags |= APF_OS_MEMORY;
9015  }
9016  }
9017  else {
9018  if (lstate->active_memory_probe) {
9019  probe_free(lstate->active_memory_probe,0);
9020  lstate->active_memory_probe = NULL;
9021  }
9022  target->ap_flags &= ~APF_OS_MEMORY;
9023  }
9024  }
9025 
9026  if ((flags & APF_OS_THREAD_ENTRY)
9027  != (target->ap_flags & APF_OS_THREAD_ENTRY)) {
9028  if (flags & APF_OS_THREAD_ENTRY) {
9029  /*
9030  * Make sure all threads loaded first!
9031  */
9032  if (!forced_load) {
9034  forced_load = 1;
9035  }
9036 
9037  name = bsymbol_get_name(lstate->thread_entry_f_symbol);
9038  /*
9039  * Create it with only a post handler so that we only probe
9040  * on the RETs from copy_process().
9041  */
9042  probe = probe_create(target,TID_GLOBAL,NULL,name,
9044  NULL,0,1);
9045 #ifdef APF_TE_COPY_PROCESS
9046  if (!probe_register_function_ee(probe,PROBEPOINT_SW,
9047  lstate->thread_entry_f_symbol,0,0,1)) {
9048  probe_free(probe,1);
9049  probe = NULL;
9050 
9051  vwarn("could not probe %s entry/exits; not enabling"
9052  " active thread entry updates!\n",name);
9053 
9054  lstate->active_thread_entry_probe = NULL;
9055  target->ap_flags &= ~APF_OS_THREAD_ENTRY;
9056 
9057  --retval;
9058  }
9059 #else
9060  if (!probe_register_symbol(probe,lstate->thread_entry_f_symbol,
9061  PROBEPOINT_SW,0,0)) {
9062  probe_free(probe,1);
9063  probe = NULL;
9064 
9065  vwarn("could not probe %s entry; not enabling"
9066  " active thread entry updates!\n",name);
9067 
9068  lstate->active_thread_entry_probe = NULL;
9069  target->ap_flags &= ~APF_OS_THREAD_ENTRY;
9070 
9071  --retval;
9072  }
9073 #endif
9074  else {
9075  lstate->active_thread_entry_probe = probe;
9076  target->ap_flags |= APF_OS_THREAD_ENTRY;
9077  }
9078  }
9079  else {
9080  if (lstate->active_thread_entry_probe) {
9082  lstate->active_thread_entry_probe = NULL;
9083  }
9084  target->ap_flags &= ~APF_OS_THREAD_ENTRY;
9085  }
9086  }
9087 
9088  if ((flags & APF_OS_THREAD_EXIT)
9089  != (target->ap_flags & APF_OS_THREAD_EXIT)) {
9090  if (flags & APF_OS_THREAD_EXIT) {
9091  /*
9092  * Make sure all threads loaded first!
9093  */
9094  if (!forced_load) {
9096  forced_load = 1;
9097  }
9098 
9099  name = bsymbol_get_name(lstate->thread_exit_f_symbol);
9100  probe = probe_create(target,TID_GLOBAL,NULL,name,
9102  NULL,NULL,0,1);
9103  /* NB: always use this; it should be the default! */
9104  if (!probe_register_inlined_symbol(probe,
9105  lstate->thread_exit_f_symbol,
9106  1,PROBEPOINT_SW,0,0)) {
9107  probe_free(probe,1);
9108  probe = NULL;
9109 
9110  vwarn("could not probe %s; not enabling"
9111  " active thread exit updates!\n",name);
9112 
9113  lstate->active_thread_exit_probe = NULL;
9114  target->ap_flags &= ~APF_OS_THREAD_EXIT;
9115 
9116  --retval;
9117  }
9118  else {
9119  lstate->active_thread_exit_probe = probe;
9120  target->ap_flags |= APF_OS_THREAD_EXIT;
9121  }
9122  }
9123  else {
9124  if (lstate->active_thread_exit_probe) {
9126  lstate->active_thread_exit_probe = NULL;
9127  }
9128  target->ap_flags &= ~APF_OS_THREAD_EXIT;
9129  }
9130  }
9131 
9132  if ((flags & APF_PROCESS_MEMORY)
9133  != (target->ap_flags & APF_PROCESS_MEMORY)) {
9134  if (flags & APF_PROCESS_MEMORY) {
9135  /*
9136  if (!(lstate->active_memory_probe_mmap =
9137  linux_syscall_probe(target->base,target->tid,
9138  "sys_mmap",NULL,
9139  os_linux_active_process_memory_post_handler,
9140  target))) {
9141  if (errno != ENOSYS) {
9142  verror("could not register syscall probe on mmap;!\n");
9143  --retval;
9144  }
9145  }
9146  */
9147 
9148  /* mmap */
9149  bs = target_lookup_sym(target->base,"sys_mmap",NULL,NULL,
9151  if (!bs)
9152  goto unprobe;
9153  probe = probe_create(target->base,TID_GLOBAL,NULL,
9154  bsymbol_get_name(bs),
9155  NULL,os_linux_active_process_memory_post_handler,
9156  target,0,1);
9157  if (!probe_register_function_ee(probe,PROBEPOINT_SW,bs,
9158  0,1,1)) {
9159  verror("could not register function entry/exit probe on %s;"
9160  " aborting!\n",bsymbol_get_name(bs));
9161  probe_free(probe,0);
9162  probe = NULL;
9163  goto unprobe;
9164  }
9165  lstate->active_memory_probe_mmap = probe;
9166 
9167  /* munmap */
9168  bs = target_lookup_sym(target->base,"sys_munmap",NULL,NULL,
9170  if (!bs)
9171  goto unprobe;
9172  probe = probe_create(target->base,TID_GLOBAL,NULL,
9173  bsymbol_get_name(bs),
9174  NULL,os_linux_active_process_memory_post_handler,
9175  target,0,1);
9176  if (!probe_register_function_ee(probe,PROBEPOINT_SW,bs,
9177  0,1,1)) {
9178  verror("could not register function entry/exit probe on %s;"
9179  " aborting!\n",bsymbol_get_name(bs));
9180  probe_free(probe,0);
9181  probe = NULL;
9182  goto unprobe;
9183  }
9184  bsymbol_release(bs);
9185  lstate->active_memory_probe_munmap = probe;
9186 
9187  /* uselib */
9188  bs = target_lookup_sym(target->base,"sys_uselib",NULL,NULL,
9190  if (!bs)
9191  goto unprobe;
9192  probe = probe_create(target->base,TID_GLOBAL,NULL,
9193  bsymbol_get_name(bs),
9194  NULL,os_linux_active_process_memory_post_handler,
9195  target,0,1);
9196  if (!probe_register_function_ee(probe,PROBEPOINT_SW,bs,
9197  0,1,1)) {
9198  verror("could not register function entry/exit probe on %s;"
9199  " aborting!\n",bsymbol_get_name(bs));
9200  probe_free(probe,0);
9201  probe = NULL;
9202  goto unprobe;
9203  }
9204  bsymbol_release(bs);
9205  lstate->active_memory_probe_uselib = probe;
9206 
9207  /* mprotect */
9208  bs = target_lookup_sym(target->base,"sys_mprotect",NULL,NULL,
9210  if (!bs)
9211  goto unprobe;
9212  probe = probe_create(target->base,TID_GLOBAL,NULL,
9213  bsymbol_get_name(bs),
9214  NULL,os_linux_active_process_memory_post_handler,
9215  target,0,1);
9216  if (!probe_register_function_ee(probe,PROBEPOINT_SW,bs,
9217  0,1,1)) {
9218  verror("could not register function entry/exit probe on %s;"
9219  " aborting!\n",bsymbol_get_name(bs));
9220  probe_free(probe,0);
9221  probe = NULL;
9222  goto unprobe;
9223  }
9224  bsymbol_release(bs);
9225  lstate->active_memory_probe_mprotect = probe;
9226 
9227  /* mremap */
9228  bs = target_lookup_sym(target->base,"sys_mremap",NULL,NULL,
9230  if (!bs)
9231  goto unprobe;
9232  probe = probe_create(target->base,TID_GLOBAL,NULL,
9233  bsymbol_get_name(bs),
9234  NULL,os_linux_active_process_memory_post_handler,
9235  target,0,1);
9236  if (!probe_register_function_ee(probe,PROBEPOINT_SW,bs,
9237  0,1,1)) {
9238  verror("could not register function entry/exit probe on %s;"
9239  " aborting!\n",bsymbol_get_name(bs));
9240  probe_free(probe,0);
9241  probe = NULL;
9242  goto unprobe;
9243  }
9244  bsymbol_release(bs);
9245  lstate->active_memory_probe_mremap = probe;
9246 
9247  /* mmap_pgoff */
9248  bs = target_lookup_sym(target->base,"sys_mmap_pgoff",NULL,NULL,
9250  if (!bs)
9251  goto unprobe;
9252  probe = probe_create(target->base,TID_GLOBAL,NULL,
9253  bsymbol_get_name(bs),
9254  NULL,os_linux_active_process_memory_post_handler,
9255  target,0,1);
9256  if (!probe_register_function_ee(probe,PROBEPOINT_SW,bs,
9257  0,1,1)) {
9258  verror("could not register function entry/exit probe on %s;"
9259  " aborting!\n",bsymbol_get_name(bs));
9260  probe_free(probe,0);
9261  probe = NULL;
9262  goto unprobe;
9263  }
9264  bsymbol_release(bs);
9265  lstate->active_memory_probe_mmap_pgoff = probe;
9266 
9267  /* madvise */
9268  bs = target_lookup_sym(target->base,"sys_madvise",NULL,NULL,
9270  if (!bs)
9271  goto unprobe;
9272  probe = probe_create(target->base,TID_GLOBAL,NULL,
9273  bsymbol_get_name(bs),
9274  NULL,os_linux_active_process_memory_post_handler,
9275  target,0,1);
9276  if (!probe_register_function_ee(probe,PROBEPOINT_SW,bs,
9277  0,1,1)) {
9278  verror("could not register function entry/exit probe on %s;"
9279  " aborting!\n",bsymbol_get_name(bs));
9280  probe_free(probe,0);
9281  probe = NULL;
9282  goto unprobe;
9283  }
9284  bsymbol_release(bs);
9285  lstate->active_memory_probe_madvise = probe;
9286 
9287  target->ap_flags |= APF_PROCESS_MEMORY;
9288  }
9289  else {
9290  unprobe:
9291  if (lstate->active_memory_probe_uselib) {
9293  lstate->active_memory_probe_uselib = NULL;
9294  }
9295  if (lstate->active_memory_probe_munmap) {
9297  lstate->active_memory_probe_munmap = NULL;
9298  }
9299  if (lstate->active_memory_probe_mmap) {
9301  lstate->active_memory_probe_mmap = NULL;
9302  }
9303  if (lstate->active_memory_probe_mprotect) {
9305  lstate->active_memory_probe_mprotect = NULL;
9306  }
9307  if (lstate->active_memory_probe_mremap) {
9309  lstate->active_memory_probe_mremap = NULL;
9310  }
9311  if (lstate->active_memory_probe_mmap_pgoff) {
9313  lstate->active_memory_probe_mmap_pgoff = NULL;
9314  }
9315  if (lstate->active_memory_probe_madvise) {
9317  lstate->active_memory_probe_madvise = NULL;
9318  }
9319 
9320  target->ap_flags &= ~APF_PROCESS_MEMORY;
9321  }
9322  }
9323 
9324  return retval;
9325 }
9326 
9327 int os_linux_thread_singlestep(struct target *target,tid_t tid,int isbp,
9328  struct target *overlay,int force_emulate) {
9329  REGVAL eflags;
9330  int rc;
9331 
9332  /*
9333  * If this driver handles exceptions both in kernel and user spaces,
9334  * then we need to let it do the single step.
9335  */
9336  if (!g_hash_table_lookup(target->config,"OS_EMULATE_USERSPACE_EXCEPTIONS")
9337  && !force_emulate)
9338  return target->ops->singlestep(target,tid,isbp,overlay);
9339 
9340  if (!target->writeable) {
9341  verror("target %s not writeable!\n",target->name);
9342  errno = EROFS;
9343  return -1;
9344  }
9345 
9346  /*
9347  * We have to emulate the exception in the userspace part of the
9348  * target's thread.
9349  */
9350  if (target->arch->wordsize == 8)
9351  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
9353  else
9354  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
9355  REG_X86_EFLAGS);
9356  eflags |= X86_EF_TF;
9357  //if (isbp)
9358  // eflags |= X86_EF_RF;
9359  //eflags &= ~X86_EF_IF;
9360  if (target->arch->wordsize == 8)
9361  rc = target_write_reg_ctxt(target,tid,THREAD_CTXT_USER,
9362  REG_X86_64_RFLAGS,eflags);
9363  else
9364  rc = target_write_reg_ctxt(target,tid,THREAD_CTXT_USER,
9365  REG_X86_EFLAGS,eflags);
9366  //OBJSDIRTY(tthread);
9367 
9368  return 0;
9369 }
9370 
9371 int os_linux_thread_singlestep_end(struct target *target,tid_t tid,
9372  struct target *overlay,int force_emulate) {
9373  REGVAL eflags;
9374  int rc;
9375 
9376  /*
9377  * If this driver handles exceptions both in kernel and user spaces,
9378  * then we need to let it do the single step.
9379  */
9380  if (!g_hash_table_lookup(target->config,"OS_EMULATE_USERSPACE_EXCEPTIONS")
9381  && !force_emulate)
9382  return target->ops->singlestep_end(target,tid,overlay);
9383 
9384  if (!target->writeable) {
9385  verror("target %s not writeable!\n",target->name);
9386  errno = EROFS;
9387  return -1;
9388  }
9389 
9390  /*
9391  * If this is a userspace thread that is in the kernel, and our
9392  * hypervisor doesn't catch userspace debug exceptions, we have
9393  * to make sure we edit the *userspace* regs, not the kernel
9394  * ones!
9395  */
9396  if (target->arch->wordsize == 8)
9397  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
9399  else
9400  eflags = target_read_reg_ctxt(target,tid,THREAD_CTXT_USER,
9401  REG_X86_EFLAGS);
9402  eflags &= ~X86_EF_TF;
9403  if (target->arch->wordsize == 8)
9404  rc = target_write_reg_ctxt(target,tid,THREAD_CTXT_USER,
9405  REG_X86_64_RFLAGS,eflags);
9406  else
9407  rc = target_write_reg_ctxt(target,tid,THREAD_CTXT_USER,
9408  REG_X86_EFLAGS,eflags);
9409  //OBJSDIRTY(tthread);
9410 
9411  return 0;
9412 }
9413 
9416  .init = NULL,
9417  .fini = os_linux_fini,
9418 
9419  .postloadinit = os_linux_postloadinit,
9420  .postopened = os_linux_postopened,
9421  .set_active_probing = os_linux_set_active_probing,
9422 
9423  .free_thread_state = os_linux_free_thread_state,
9424  .list_available_tids = os_linux_list_available_tids,
9425  .load_thread = os_linux_load_thread,
9426  .load_current_thread = os_linux_load_current_thread,
9427  .load_available_threads = os_linux_load_available_threads,
9428 
9429  .flush_thread = os_linux_flush_thread,
9430  .flush_current_thread = os_linux_flush_current_thread,
9431  .invalidate_thread = os_linux_invalidate_thread,
9432 
9433  .thread_snprintf = os_linux_thread_snprintf,
9434 
9435  .readreg = target_regcache_readreg,
9436  .writereg = target_regcache_writereg,
9437  .copy_registers = target_regcache_copy_registers,
9438  .readreg_tidctxt = target_regcache_readreg_tidctxt,
9439  .writereg_tidctxt = target_regcache_writereg_tidctxt,
9440 };
9441 
9443  /* All this work is done in attach and fini of the personality_ops. */
9444  .init = NULL,
9445  .fini = NULL,
9446 
9447  .os_type = os_linux_type,
9448  .os_version = os_linux_version,
9449  .os_version_string = os_linux_version_string,
9450  .os_version_cmp = os_linux_version_cmp,
9451 
9452  .thread_get_pgd_phys = os_linux_thread_get_pgd_phys,
9453  .thread_is_user = os_linux_thread_is_user,
9454  .thread_get_leader = os_linux_thread_get_leader,
9455 
9456  .thread_singlestep = os_linux_thread_singlestep,
9457  .thread_singlestep_end = os_linux_thread_singlestep_end,
9458 
9459  .processes_get = os_linux_processes_load,
9460  .process_get = os_linux_process_load,
9461 
9462  .signal_enqueue = os_linux_signal_enqueue,
9463  .signal_to_name = os_linux_signal_to_name,
9464  .signal_from_name = os_linux_signal_from_name,
9465 
9466  .syscall_table_load = os_linux_syscall_table_load,
9467  .syscall_table_unload = os_linux_syscall_table_unload,
9468  .syscall_table_get = os_linux_syscall_table_get,
9469  .syscall_lookup_name = os_linux_syscall_lookup_name,
9470  .syscall_lookup_num = os_linux_syscall_lookup_num,
9471  .syscall_lookup_addr = os_linux_syscall_lookup_addr,
9472  .syscall_probe = os_linux_syscall_probe,
9473  .syscall_probe_all = os_linux_syscall_probe_all,
9474 };
9475 
9478  &os_linux_generic_personality_ops,
9479  &os_linux_generic_os_ops);
9480 }
unsigned int thread_struct_has_debugreg
arch_type_t type
Definition: arch.h:117
int target_thread_snprintf(struct target *target, tid_t tid, char *buf, int bufsiz, int detail, char *sep, char *kvsep)
Definition: target_api.c:1425
#define OBJSCLEAN(obj)
Definition: object.h:116
struct bsymbol * module_type
#define REG_X86_64_DS
Definition: arch_x86_64.h:105
#define OBJNEW(obj)
Definition: object.h:88
void * target_os_syscall_probe_summarize_tid(struct probe *probe, tid_t tid)
Definition: target_os.c:495
REFCNT binfile_instance_release(struct binfile_instance *bfi)
Definition: binfile.c:431
struct value * target_load_symbol(struct target *target, struct target_location_ctxt *tlctxt, struct bsymbol *bsymbol, load_flags_t flags)
Definition: target.c:3270
int addrspace_detach_region(struct addrspace *space, struct memregion *region)
Definition: memory.c:89
#define REG_X86_DS
Definition: arch_x86.h:86
struct symbol * task_struct_type
#define SYSCALL_GLOBAL_RET_PROBE
struct value * target_load_type(struct target *target, struct symbol *type, ADDR addr, load_flags_t flags)
Definition: target.c:2653
REGVAL target_regcache_readreg_tidctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg)
Definition: target.c:6885
#define REG_X86_64_RSP
Definition: arch_x86_64.h:43
#define REG_X86_EDI
Definition: arch_x86.h:44
OFFSET symbol_offsetof(struct symbol *symbol, const char *name, const char *delim)
Definition: debug.c:314
struct action * action_return(REGVAL retval)
Definition: probe.c:4457
#define vwarnopt(level, area, flags, format,...)
Definition: log.h:37
GHashTable * config
Definition: target_api.h:2622
result_t pre_handler(struct probe *probe, tid_t tid, void *data, struct probe *trigger, struct probe *base)
Definition: spf.c:903
int(* singlestep)(struct target *target, tid_t tid, int isbp, struct target *overlay)
Definition: target_api.h:3111
struct symbol * mm_struct_type
unsigned int thread_struct_has_fs
struct target_os_syscall_state * target_os_syscall_probe_last(struct target *target, tid_t tid)
Definition: target_os.c:531
struct addrspace * space
#define PRIiOFFSET
Definition: common.h:70
struct target * base
Definition: target_api.h:2654
void * state
Definition: target_api.h:2526
GSList * args
Definition: target_os.h:53
int value_update_i32(struct value *value, int32_t v)
Definition: value.c:537
void target_broadcast_event(struct target *target, struct target_event *event)
Definition: target_event.c:56
int os_linux_syscall_table_load(struct target *target)
int os_linux_set_active_probing(struct target *target, active_probe_flags_t flags)
#define REG_X86_ECX
Definition: arch_x86.h:38
uint8_t isstub
Definition: target_os.h:39
int target_regcache_zero(struct target *target, struct target_thread *tthread, thread_ctxt_t tctxt)
Definition: target.c:6717
struct binfile_instance * binfile_infer_instance(char *filename, char *root_prefix, ADDR base, GHashTable *config)
Definition: binfile.c:242
struct lsymbol * lsymbol
Definition: target.h:1017
int32_t tid_t
Definition: common.h:36
#define OBJSVALID(obj)
Definition: object.h:101
target_personality_t personality
Definition: target_api.h:2515
#define REG_X86_ES
Definition: arch_x86.h:83
char * os_linux_file_get_path(struct target *target, struct value *task, struct value *file, char *ibuf, int buflen)
REFCNT lsymbol_release(struct lsymbol *lsymbol)
Definition: debug.c:4805
#define REG_X86_64_RDI
Definition: arch_x86_64.h:41
int(* attach)(struct target *target)
Definition: target_api.h:3140
target_status_t
Definition: target_api.h:197
#define REG_X86_64_RIP
Definition: arch_x86_64.h:54
#define VLS(target, tlctxt, varstr, loadflags, outvarptr, outvalueptr, errlabel)
Definition: target_api.h:1677
int target_load_available_threads(struct target *target, int force)
Definition: target_api.c:1300
void * target_os_syscall_probe_summarize(struct probe *probe)
Definition: target_os.c:491
struct array_list * system_call_ret_idata_list
int target_personality_register(char *personality, target_personality_t pt, struct target_personality_ops *ptops, void *pops)
Definition: target.c:6040
#define REG_X86_64_SS
Definition: arch_x86_64.h:104
struct probe * active_memory_probe_mremap
OFFSET offset
Definition: disasm.h:100
struct symbol * symbol
Definition: dwdebug.h:1010
struct symbol * os_linux_get_task_struct_type(struct target *target)
struct probe * int3_probe
int addrspace_find_range_real(struct addrspace *space, ADDR addr, struct memregion **region_saveptr, struct memrange **range_saveptr)
Definition: memory.c:141
unsigned int hypervisor_ignores_userspace_exceptions
struct value * probe_value_get_function_ee(struct probe *probe, tid_t tid, char *name)
Definition: probe_value.c:467
Definition: probe.h:392
GHashTable * task_struct_addr_to_thread
#define REG_X86_DR2
Definition: arch_x86.h:107
#define HARDIRQ_COUNT(p)
int target_lsymbol_resolve_bounds(struct target *target, struct target_location_ctxt *tlctxt, struct lsymbol *lsymbol, ADDR base_addr, ADDR *start, ADDR *end, int *is_noncontiguous, ADDR *alt_start, ADDR *alt_end)
Definition: target.c:2418
char * name
Definition: probe.h:314
int os_linux_thread_get_pgd_phys(struct target *target, struct target_thread *tthread, ADDR *pgdp)
static uint64_t unsigned int i
#define OBJSNEW(obj)
Definition: object.h:133
struct bsymbol * target_lookup_sym(struct target *target, const char *name, const char *delim, char *srcfile, symbol_type_flag_t ftype)
Definition: target.c:2199
GHashTable * target_regcache_copy_registers(struct target *target, tid_t tid)
Definition: target.c:6862
Definition: log.h:171
#define REG_X86_64_R9
Definition: arch_x86_64.h:45
int memregion_detach_range(struct memregion *region, struct memrange *range)
Definition: memory.c:293
#define VLV(target, tlctxt, invalue, varstr, loadflags, outvarptr, outvalueptr, errlabel)
Definition: target_api.h:1739
void probe_values_free_stacked(struct probe *probe)
Definition: probe_value.c:149
REFCNT action_release(struct action *action)
Definition: probe.c:4576
inst_type_t type
Definition: disasm.h:99
#define REG_X86_64_RAX
Definition: arch_x86_64.h:36
#define REG_X86_64_DR3
Definition: arch_x86_64.h:149
int target_bsymbol_resolve_base(struct target *target, struct target_location_ctxt *tlctxt, struct bsymbol *bsymbol, ADDR *o_addr, struct memrange **o_range)
Definition: target.c:2618
struct symbol * pt_regs_type
#define v_g_slist_foreach(gslhead, gslcur, elm)
Definition: glib_wrapper.h:60
struct target_thread * os_linux_load_thread(struct target *target, tid_t tid, int force)
ADDR end
Definition: target.h:995
#define v_g_list_foreach(glhead, glcur, elm)
Definition: glib_wrapper.h:34
struct target_location_ctxt * target_location_ctxt_create_from_bsymbol(struct target *target, tid_t tid, struct bsymbol *bsymbol)
Definition: target.c:5325
struct probe_ops os_linux_syscall_ret_probe_ops
#define REG_X86_64_RDX
Definition: arch_x86_64.h:37
#define REG_X86_DR3
Definition: arch_x86.h:108
const char * task_uid_member_name
#define OBJSLIVE(obj, type)
Definition: object.h:121
struct bsymbol * thread_entry_f_symbol
ADDR target_addressof_symbol(struct target *target, struct target_location_ctxt *tlctxt, struct bsymbol *bsymbol, load_flags_t flags, struct memrange **o_range)
Definition: target.c:3490
const char * thread_sp0_member_name
active_probe_flags_t ap_flags
Definition: target_api.h:2477
struct target_personality_ops os_linux_generic_personality_ops
struct probe_ops os_linux_global_syscall_ret_probe_ops
int64_t num_t
Definition: common.h:87
REFCNT target_process_free(struct target_process *process, int force)
#define REG_X86_64_R8
Definition: arch_x86_64.h:44
struct probe * os_linux_syscall_probe(struct target *target, tid_t tid, struct target_os_syscall *syscall, probe_handler_t pre_handler, probe_handler_t post_handler, void *handler_data)
#define REG_X86_EBP
Definition: arch_x86.h:42
struct target_os_syscall_state * target_os_syscall_record_entry(struct target *target, tid_t tid, struct target_os_syscall *syscall)
Definition: target_os.c:543
#define REG_X86_64_GS
Definition: arch_x86_64.h:107
struct array_list * os_linux_list_available_tids(struct target *target)
struct target_thread * global_thread
Definition: target_api.h:2685
result_t os_linux_active_thread_entry_handler(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
int os_linux_load_available_threads(struct target *target, int force)
struct probe * active_thread_entry_probe
void * target_gkv_steal(struct target *target, char *key)
Definition: target.c:1436
char * name
Definition: target.h:939
#define REG_X86_SS
Definition: arch_x86.h:85
#define assert(x)
Definition: dlmalloc.c:1456
struct probe * active_memory_probe_madvise
uint32_t symbol_get_bytesize(struct symbol *symbol)
Definition: debug.c:3065
#define REG_X86_64_R12
Definition: arch_x86_64.h:48
result_t probe_do_sink_post_handlers(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
Definition: probe.c:109
int target_regcache_writereg_tidctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg, REGVAL value)
Definition: target.c:6907
char * bsymbol_get_name(struct bsymbol *bsymbol)
Definition: symbol.c:62
#define REG_X86_CR3
Definition: arch_x86.h:102
struct target_os_syscall * os_linux_syscall_lookup_name(struct target *target, char *name)
struct target_process * target_process_create(struct target *target, struct target_thread *tthread, struct addrspace *space)
int target_associate_debugfile(struct target *target, struct memregion *region, struct debugfile *debugfile)
Definition: target.c:1993
int32_t OFFSET
Definition: common.h:65
ADDR start
Definition: target.h:994
int value_update(struct value *value, const char *buf, int bufsiz)
Definition: value.c:432
struct list_head probe
Definition: probe.h:379
#define verror(format,...)
Definition: log.h:30
result_t probe_do_sink_pre_handlers(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
Definition: probe.c:48
unsigned char * target_read_addr(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_api.c:1053
int os_linux_list_for_each_struct(struct target *t, struct bsymbol *bsymbol, char *list_head_member_name, int nofree, os_linux_list_iterator_t iterator, void *data)
int bufsiz
Definition: target_api.h:3297
struct probe * probe_register_addr(struct probe *probe, ADDR addr, probepoint_type_t type, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize, struct bsymbol *bsymbol)
Definition: probe.c:1393
Definition: list.h:51
tid_t target_os_thread_get_leader(struct target *target, tid_t tid)
Definition: target_os.c:62
uint64_t target_os_version(struct target *target)
Definition: target_os.c:38
#define _LINUX_IS_ERR(x)
const char *(* gettype)(struct probe *probe)
Definition: probe_api.h:96
#define REG_X86_EAX
Definition: arch_x86.h:37
struct symbol * symbol_get_datatype(struct symbol *symbol)
Definition: debug.c:2989
struct probe * active_memory_probe_mmap_pgoff
void ** list
Definition: alist.h:34
#define REG_X86_GS
Definition: arch_x86.h:88
void target_detach_thread(struct target *target, struct target_thread *tthread)
Definition: target.c:4115
result_t(* probe_handler_t)(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
Definition: probe_api.h:70
target_os_type_t
Definition: target_os.h:27
symbol_type_t type
Definition: dwdebug_priv.h:833
#define OBJSINVALID(obj)
Definition: object.h:106
#define REG_X86_ESP
Definition: arch_x86.h:41
struct lsymbol * symbol_lookup_sym(struct symbol *symbol, const char *name, const char *delim)
Definition: debug.c:264
#define REG_X86_64_DR7
Definition: arch_x86_64.h:152
struct memregion * memregion_create(struct addrspace *space, region_type_t type, char *name)
Definition: memory.c:242
#define REG_X86_DR1
Definition: arch_x86.h:106
struct binfile * binfile_open(char *filename, char *root_prefix, struct binfile_instance *bfinst)
Definition: binfile.c:286
struct symbol * os_linux_get_task_struct_type_ptr(struct target *target)
REFCNT refcnt
Definition: target.h:911
#define vwarn(format,...)
Definition: log.h:33
struct bsymbol * module_free_symbol
#define REG_X86_EDX
Definition: arch_x86.h:39
#define RF
void free(void *ptr)
Definition: debugserver.c:207
REGVAL target_read_reg(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1132
int disasm_get_control_flow_offsets(struct target *target, inst_cf_flags_t flags, unsigned char *inst_buf, unsigned int buf_len, struct array_list **offset_list, ADDR base, int noabort)
Definition: disasm.c:131
GHashTable * os_linux_processes_load(struct target *target)
struct probe * active_thread_exit_probe
struct symbol * task_struct_type_ptr
struct target_thread * os_linux_load_current_thread(struct target *target, int force)
struct value * probe_value_get_last_function_ee(struct probe *probe, tid_t tid, char *name)
Definition: probe_value.c:477
#define REG_X86_64_R13
Definition: arch_x86_64.h:49
struct target_thread * os_linux_load_thread_from_value(struct target *target, struct value *taskv)
int target_write_reg_ctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg, REGVAL value)
Definition: target_api.c:1165
Definition: log.h:173
struct target_thread * target_load_thread(struct target *target, tid_t tid, int force)
Definition: target_api.c:1311
#define PRIiNUM
Definition: common.h:90
struct probe * probe_register_source(struct probe *sink, struct probe *src)
Definition: probe.c:1593
#define OBJLIVE(obj)
Definition: object.h:84
void * personality_state
Definition: target_api.h:2533
target_os_type_t os_linux_type(struct target *target)
int os_linux_handle_exception(struct target *target)
#define REG_X86_EFLAGS
Definition: arch_x86.h:46
#define array_list_foreach(alist, lpc, placeholder)
Definition: alist.h:371
char * os_linux_version_string(struct target *target)
int target_os_syscall_record_argv(struct target *target, tid_t tid, struct array_list *regvals, struct array_list *argvals)
Definition: target_os.c:562
struct arch * arch
Definition: binfile.h:216
num_t os_linux_get_preempt_count(struct target *target)
struct memrange * memrange_create(struct memregion *region, ADDR start, ADDR end, OFFSET offset, unsigned int prot_flags)
Definition: memory.c:538
int os_linux_get_task_pid(struct target *target, struct value *task)
#define THREAD_SIZE
REFCNT bsymbol_release(struct bsymbol *bsymbol)
Definition: symbol.c:90
struct target_thread * current_thread
Definition: target_api.h:2680
unsigned int thread_struct_has_ds_es
struct bsymbol * thread_exit_f_symbol
#define OBJDIRTY(obj)
Definition: object.h:80
GHashTable * mm_addr_to_mm_cache
unsigned int thread_struct_has_debugreg0
GHashTable * probe_value_get_raw_table_function_ee(struct probe *probe, tid_t tid)
Definition: probe_value.c:376
#define REG_X86_CS
Definition: arch_x86.h:84
struct target_memmod * target_memmod_lookup(struct target *target, tid_t tid, ADDR addr, int is_phys)
Definition: target.c:4897
region_type_t
struct array_list * target_list_tids(struct target *target)
Definition: target_api.c:1210
struct memrange * range
int target_os_syscall_table_load(struct target *target)
Definition: target_os.c:415
#define REG_X86_64_R15
Definition: arch_x86_64.h:51
struct symbol * thread_struct_type
struct bsymbol * wrapped_bsymbol
Definition: target_os.h:48
struct bsymbol * thread_entry_v_symbol
struct value * os_linux_load_current_thread_as_type(struct target *target, struct symbol *datatype, REGVAL kernel_esp)
tid_t base_tid
Definition: target_api.h:2657
#define REG_X86_64_DR2
Definition: arch_x86_64.h:148
int probe_free(struct probe *probe, int force)
Definition: probe.c:777
struct addrspace * space
Definition: target.h:937
struct target_os_syscall * target_os_syscall_lookup_num(struct target *target, int num)
Definition: target_os.c:443
char * buf
Definition: target_api.h:3298
result_t os_linux_active_thread_exit_handler(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
int value_refresh(struct value *value, int recursive)
Definition: value.c:329
#define PRIxNUM
Definition: common.h:91
int(* os_linux_list_iterator_t)(struct target *t, struct value *value, void *data)
unsigned int thread_struct_has_perf_debugreg
int os_linux_postopened(struct target *target)
#define REG_X86_64_DR6
Definition: arch_x86_64.h:151
#define THREAD_CTXT_USER
Definition: target_os.h:33
int target_symbol_resolve_bounds(struct target *target, struct target_location_ctxt *tlctxt, struct symbol *symbol, ADDR *start, ADDR *end, int *is_noncontiguous, ADDR *alt_start, ADDR *alt_end)
Definition: target.c:2403
#define PRIxOFFSET
Definition: common.h:71
#define REG_X86_DR0
Definition: arch_x86.h:105
Definition: log.h:178
struct probe * active_memory_probe
#define REG_X86_64_DR0
Definition: arch_x86_64.h:146
GHashTable * threads
Definition: target_api.h:2672
int(* fini)(struct probe *probe)
Definition: probe_api.h:187
#define REG_X86_EBX
Definition: arch_x86.h:40
#define REG_X86_64_CR1
Definition: arch_x86_64.h:137
GHashTable * probe_value_get_table_function_ee(struct probe *probe, tid_t tid)
Definition: probe_value.c:372
#define REG_X86_DR6
Definition: arch_x86.h:111
struct probe * active_memory_probe_mmap
#define ERRORDUMPSYMBOL_NL(s)
Definition: dwdebug_priv.h:54
void value_free(struct value *value)
Definition: value.c:282
const char * task_gid_member_name
int target_decoder_lib_bind(struct target *target, char *decoder_lib, char *decoder_lib_lib)
Definition: target.c:6206
struct target_location_ctxt * target_global_tlctxt(struct target *target)
Definition: target.c:5299
struct value * target_load_value_member(struct target *target, struct target_location_ctxt *tlctxt, struct value *old_value, const char *member, const char *delim, load_flags_t flags)
Definition: target.c:2942
int os_linux_invalidate_thread(struct target *target, struct target_thread *tthread)
#define SOFTIRQ_COUNT(p)
ADDR tag
Definition: target.h:897
#define REG_X86_64_CS
Definition: arch_x86_64.h:103
#define OBJSDEL(obj)
Definition: object.h:141
struct bsymbol * target_lookup_sym_addr(struct target *target, ADDR addr)
Definition: target.c:2093
int target_gkv_insert(struct target *target, char *key, void *value, target_gkv_dtor_t dtor)
Definition: target.c:1407
struct probe * active_memory_probe_uselib
#define THREAD_CTXT_KERNEL
Definition: target_os.h:32
int len
Definition: dumptarget.c:52
struct value * value_clone(struct value *in)
Definition: value.c:195
struct value * target_load_type_regval(struct target *target, struct symbol *type, tid_t tid, REG reg, REGVAL regval, load_flags_t flags)
Definition: target.c:2762
#define RHOLD(x, hx)
Definition: common.h:622
struct os_linux_vma * vma_cache
ADDR addr
Definition: target.h:392
struct target_memmod * emulating_debug_mmod
Definition: target_api.h:2179
#define REG_X86_64_RCX
Definition: arch_x86_64.h:38
int target_regcache_snprintf(struct target *target, struct target_thread *tthread, thread_ctxt_t tctxt, char *buf, int bufsiz, int detail, char *sep, char *kvsep, int flags)
Definition: target.c:6687
Definition: probe.h:308
GList * spaces
Definition: target_api.h:2643
char * os_linux_d_path(struct target *target, struct value *dentry, struct value *vfsmnt, struct value *root_dentry, struct value *root_vfsmnt, char *buf, int buflen)
const char * os_linux_signal_to_name(struct target *target, int signo)
result_t os_linux_debug_handler(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
struct probe * probe_create(struct target *target, tid_t tid, struct probe_ops *pops, const char *name, probe_handler_t pre_handler, probe_handler_t post_handler, void *handler_data, int autofree, int tracked)
Definition: probe.c:729
int target_store_value(struct target *target, struct value *value)
Definition: target.c:3548
#define VLA(target, addr, loadflags, outbufptr, outbuflen, outvalueptr, errlabel)
Definition: target_api.h:1840
GHashTable * os_linux_syscall_table_get(struct target *target)
REGVAL target_read_creg(struct target *target, tid_t tid, common_reg_t reg)
Definition: target_api.c:1178
struct target_thread * thread
Definition: probe.h:343
REFCNT symbol_release(struct symbol *symbol)
Definition: debug.c:4318
struct binfile * binfile
Definition: target_api.h:2649
int os_linux_thread_snprintf(struct target *target, struct target_thread *tthread, char *buf, int bufsiz, int detail, char *sep, char *kvsep)
#define LOCAL_TIF_SIGPENDING
struct bsymbol * modules
#define REG_X86_64_R14
Definition: arch_x86_64.h:50
#define v_g_list_foreach_safe(glhead, glcur, glnext, elm)
Definition: glib_wrapper.h:46
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
#define REG_X86_EIP
Definition: arch_x86.h:45
int target_regcache_readreg_ifdirty(struct target *target, struct target_thread *tthread, thread_ctxt_t tctxt, REG reg, REGVAL *regval)
Definition: target.c:6610
void probe_value_notify_phase_function_ee(struct probe *probe, tid_t tid, probe_handler_phase_t phase)
Definition: probe_value.c:96
ADDR v_addr(struct value *v)
Definition: value.c:429
int target_load_all_threads(struct target *target, int force)
Definition: target_api.c:1318
struct thread_probepoint_context * tpc
Definition: target_api.h:2168
#define OBJSMOD(obj)
Definition: object.h:137
struct probe * probe_register_inlined_symbol(struct probe *probe, struct bsymbol *bsymbol, int do_primary, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize)
Definition: probe_lib.c:1089
#define REG_X86_64_ES
Definition: arch_x86_64.h:102
#define REG_X86_64_RFLAGS
Definition: arch_x86_64.h:101
Definition: log.h:172
int target_regcache_init_reg_tidctxt(struct target *target, struct target_thread *tthread, thread_ctxt_t tctxt, REG reg, REGVAL regval)
Definition: target.c:6449
#define OBJVALID(obj)
Definition: object.h:76
struct memregion * region
Definition: target.h:992
struct arch * arch
Definition: target_api.h:2603
#define OBJSDIRTY(obj)
Definition: object.h:111
ADDR offset
Definition: target.h:996
unsigned int prot_flags
Definition: target.h:997
struct probe * debug_probe
unsigned int wordsize
Definition: arch.h:121
num_t v_num(struct value *v)
Definition: value.c:396
struct debugfile * debugfile_from_instance(struct binfile_instance *bfinst, struct array_list *debugfile_load_opts_list)
Definition: debug.c:1726
#define REG_X86_DR7
Definition: arch_x86.h:112
void * calloc(size_t nmemb, size_t size)
Definition: debugserver.c:200
result_t post_handler(struct probe *probe, tid_t tid, void *data, struct probe *trigger, struct probe *base)
Definition: spf.c:908
struct array_list * debugfile_load_opts_list
Definition: target_api.h:2237
struct binfile * binfile
Definition: dwdebug.h:808
int value_update_unum(struct value *value, unum_t v)
Definition: value.c:601
unsigned int thread_ctxt_t
Definition: target_api.h:300
void target_thread_set_status(struct target_thread *tthread, thread_status_t status)
Definition: target.c:4041
#define REGION_TYPE(n)
Definition: target_api.h:384
Definition: log.h:70
void os_linux_generic_register(void)
GList * ranges
Definition: target.h:947
int target_os_syscall_record_return(struct target *target, tid_t tid, REGVAL retval)
Definition: target_os.c:580
int os_linux_signal_enqueue(struct target *target, struct target_thread *tthread, int signo, void *data)
result_t
Definition: common.h:25
uint32_t REGVAL
Definition: common.h:66
struct addrspace * space
Definition: arch.h:102
#define REG_X86_64_RBX
Definition: arch_x86_64.h:39
struct value * probe_value_get_last_raw_function_ee(struct probe *probe, tid_t tid, char *name)
Definition: probe_value.c:482
void * personality_state
Definition: target_api.h:2106
struct target_process * os_linux_process_load(struct target *target, struct target_thread *tthread)
target_status_t target_status(struct target *target)
Definition: target_api.c:1046
struct bsymbol * thread_exit_v_symbol
int target_os_update_process_threads_generic(struct target_process *process, int no_event_send)
Definition: target_os.c:211
struct value * probe_value_get_raw_function_ee(struct probe *probe, tid_t tid, char *name)
Definition: probe_value.c:472
#define SYSCALL_GLOBAL_ENTRY_PROBE
#define PRIiTID
Definition: common.h:37
result_t os_linux_int3_handler(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
int target_regcache_isdirty(struct target *target, struct target_thread *tthread, thread_ctxt_t tctxt)
Definition: target.c:6626
char * v_string(struct value *v)
Definition: value.c:430
unsigned int task_struct_has_thread_info
int8_t exiting
Definition: target_api.h:2081
uint16_t v_u16(struct value *v)
Definition: value.c:389
int os_linux_syscall_table_unload(struct target *target)
int8_t REG
Definition: common.h:93
#define REG_X86_64_RSI
Definition: arch_x86_64.h:40
unsigned int breakpoint_instrs_len
Definition: arch.h:150
struct symbol * bsymbol_get_symbol(struct bsymbol *bsymbol)
Definition: symbol.c:66
struct target * target
Definition: probe.h:342
int os_linux_version_cmp(struct target *target, uint64_t vers)
#define REG_X86_64_R11
Definition: arch_x86_64.h:47
#define REG_X86_64_GS_BASE
Definition: arch_x86_64.h:110
const char * target_os_signal_to_name(struct target *target, int signo)
Definition: target_os.c:407
#define REG_X86_64_DR1
Definition: arch_x86_64.h:147
uint32_t ADDR
Definition: common.h:64
#define PAGE_SIZE
char * name
Definition: target_api.h:2521
struct target * target_lookup_overlay(struct target *target, tid_t tid)
Definition: target.c:4497
struct target_ops * ops
Definition: target_api.h:2548
int(* init)(struct target *target)
Definition: target_os.h:178
Definition: log.h:162
struct symbol * type
Definition: target_api.h:3287
REGVAL target_read_reg_ctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg)
Definition: target_api.c:1157
uint64_t v_u64(struct value *v)
Definition: value.c:391
struct value * target_load_addr_real(struct target *target, ADDR addr, load_flags_t flags, int len)
Definition: target.c:3758
char * name
Definition: target.h:898
target_status_t target_notify_overlay(struct target *overlay, target_exception_flags_t flags, tid_t tid, ADDR ipval, int *again)
Definition: target.c:4491
GSList * symbol_get_members(struct symbol *symbol, symbol_type_flag_t flags)
Definition: debug.c:3146
int os_linux_thread_singlestep_end(struct target *target, tid_t tid, struct target *overlay, int force_emulate)
REG spregno
Definition: target_api.h:2507
thread_ctxt_t tidctxt
Definition: target_api.h:2080
int os_linux_postloadinit(struct target *target)
int32_t v_i32(struct value *v)
Definition: value.c:394
unsigned int last_thread_count
struct value * target_load_symbol_member(struct target *target, struct target_location_ctxt *tlctxt, struct bsymbol *bsymbol, const char *member, const char *delim, load_flags_t flags)
Definition: target.c:2920
uint32_t REFCNT
Definition: common.h:124
#define PRIxADDR
Definition: common.h:67
void target_location_ctxt_free(struct target_location_ctxt *tlctxt)
Definition: target.c:5348
#define VL(target, tlctxt, invalue, varstr, loadflags, outvalueptr, errlabel)
Definition: target_api.h:1708
#define KERNEL_STACK_OFFSET
REGVAL target_regcache_readreg(struct target *target, tid_t tid, REG reg)
Definition: target.c:6528
#define REG_X86_64_R10
Definition: arch_x86_64.h:46
struct symbol * target_create_synthetic_type_pointer(struct target *target, struct symbol *type)
Definition: symbol.c:27
struct target_spec * spec
Definition: target_api.h:2605
const char * thread_sp_member_name
struct bsymbol * bsymbol
Definition: probe.h:389
#define DCACHE_UNHASHED
struct probe * active_memory_probe_munmap
unsigned int pt_regs_has_fs_gs
GHashTable * probe_value_get_last_raw_table_function_ee(struct probe *probe, tid_t tid)
Definition: probe_value.c:384
result_t target_os_emulate_ss_handler(struct target *target, tid_t tid, thread_ctxt_t tidctxt, struct target_memmod *mmod)
Definition: target_os.c:163
unsigned int task_struct_has_stack
char * symbol_get_name(struct symbol *symbol)
Definition: debug.c:2587
int os_linux_thread_is_user(struct target *target, struct target_thread *tthread)
const char * thread_info_preempt_count_name
#define RPUT(x, objtype, hx, rc)
Definition: common.h:624
#define REG_X86_ESI
Definition: arch_x86.h:43
int os_linux_thread_singlestep(struct target *target, tid_t tid, int isbp, struct target *overlay, int force_emulate)
struct bsymbol * module_free_mod_symbol
active_probe_flags_t
Definition: target_api.h:432
GHashTable * probe_value_get_last_table_function_ee(struct probe *probe, tid_t tid)
Definition: probe_value.c:380
result_t os_linux_active_memory_handler(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
unsigned int pt_regs_has_ds_es
int id
Definition: target_api.h:2514
void * malloc(size_t size)
Definition: debugserver.c:214
int value_update_zero(struct value *value, const char *buf, int bufsiz)
Definition: value.c:447
char * debugfile_root_prefix
Definition: target_api.h:2235
#define DRF
struct symbol * thread_info_type
region_type_t type
Definition: target.h:940
struct bsymbol * bsymbol
Definition: target_os.h:42
struct target_thread * os_linux_thread_get_leader(struct target *target, struct target_thread *tthread)
struct target_thread * target_lookup_thread(struct target *target, tid_t tid)
Definition: target.c:4023
struct os_linux_vma * next
ADDR base_load_addr
Definition: target.h:968
uint64_t unum_t
Definition: common.h:88
int os_linux_flush_current_thread(struct target *target)
struct target_event * target_create_event(struct target *target, struct target_thread *thread, target_event_t type, void *priv)
Definition: target_event.c:26
struct target_os_syscall * os_linux_syscall_lookup_num(struct target *target, int num)
endian_t endian
Definition: arch.h:120
uint32_t v_u32(struct value *v)
Definition: value.c:390
#define REG_X86_FS
Definition: arch_x86.h:87
int os_linux_list_for_each_entry(struct target *t, struct bsymbol *btype, struct bsymbol *list_head, char *list_head_member_name, int nofree, os_linux_list_iterator_t iterator, void *data)
int(* singlestep_end)(struct target *target, tid_t tid, struct target *overlay)
Definition: target_api.h:3113
Definition: log.h:177
unsigned int returned
Definition: target_os.h:57
void target_gkv_remove(struct target *target, char *key)
Definition: target.c:1454
int os_linux_fini(struct target *target)
struct bsymbol * init_task
void * target_gkv_lookup(struct target *target, char *key)
Definition: target.c:1425
ADDR os_linux_current_thread_ptr(struct target *target, REGVAL kernel_esp)
struct memregion * addrspace_find_region(struct addrspace *space, char *name)
Definition: memory.c:69
void os_linux_free_thread_state(struct target *target, void *state)
struct probe * active_memory_probe_mprotect
struct probe * probe_register_symbol(struct probe *probe, struct bsymbol *bsymbol, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize)
Definition: probe.c:1470
int target_addr_v2p(struct target *target, tid_t tid, ADDR vaddr, ADDR *paddr)
Definition: target_api.c:1072
int vdebug_is_on(int level, log_areas_t areas, log_flags_t flags)
Definition: log.c:335
int os_linux_flush_thread(struct target *target, tid_t tid)
struct binfile * binfile
Definition: target.h:961
#define REG_X86_64_RBP
Definition: arch_x86_64.h:42
int os_linux_attach(struct target *target)
unum_t v_unum(struct value *v)
Definition: value.c:411
struct target_thread * target_create_thread(struct target *target, tid_t tid, void *tstate, void *tpstate)
Definition: target.c:4063
ADDR value_addr(struct value *value)
Definition: value.c:302
struct array_list * target_list_threads(struct target *target)
Definition: target_api.c:1233
int action_sched(struct probe *probe, struct action *action, action_whence_t whence, action_handler_t handler, void *handler_data)
Definition: probe.c:4119
struct target_os_syscall * os_linux_syscall_lookup_addr(struct target *target, ADDR addr)
int target_regcache_writereg(struct target *target, tid_t tid, REG reg, REGVAL value)
Definition: target.c:6569
result_t target_os_emulate_bp_handler(struct target *target, tid_t tid, thread_ctxt_t tidctxt, struct target_memmod *mmod)
Definition: target_os.c:85
REG ipregno
Definition: target_api.h:2508
uint64_t os_linux_version(struct target *target)
Definition: arch.h:78
#define REG_X86_64_GS_BASE_USER
Definition: arch_x86_64.h:116
struct symbol * datatype
Definition: dwdebug_priv.h:925
#define TID_GLOBAL
Definition: target_api.h:145
struct probe * os_linux_syscall_probe_all(struct target *target, tid_t tid, probe_handler_t pre_handler, probe_handler_t post_handler, void *handler_data)
int os_linux_signal_from_name(struct target *target, const char *name)
#define X86_EF_TF
Definition: arch_x86.h:26
struct target * t
Definition: dumptarget.c:48
const char * thread_ip_member_name
struct value * os_linux_get_task(struct target *target, tid_t tid)
#define REG_X86_64_FS
Definition: arch_x86_64.h:106
struct target_os_ops os_linux_generic_os_ops
GList * regions
Definition: target.h:909
#define REG_X86_64_GS_BASE_KERNEL
Definition: arch_x86_64.h:115
#define OBJSDEAD(obj, type)
Definition: object.h:127
#define PRIxREGVAL
Definition: common.h:72
struct addrspace * addrspace_create(struct target *target, char *name, ADDR tag)
Definition: memory.c:41
uint32_t writeable
Definition: target_api.h:2465
target_type_t supported_overlay_types
Definition: target_api.h:2085
unsigned char * target_load_code(struct target *target, ADDR start, unsigned int len, int nocache, int force_copy, int *caller_free)
Definition: target.c:3909