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