4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/stack.h>
27 #include <sys/regset.h>
28 #include <sys/frame.h>
29 #include <sys/sysmacros.h>
31 #include <sys/machelf.h>
35 #include <sys/types.h>
43 static uchar_t int_syscall_instr
[] = { 0xCD, T_SYSCALLINT
};
44 static uchar_t syscall_instr
[] = { 0x0f, 0x05 };
47 Ppltdest(struct ps_prochandle
*P
, uintptr_t pltaddr
)
49 map_info_t
*mp
= Paddr2mptr(P
, pltaddr
);
54 if (mp
== NULL
|| (fp
= mp
->map_file
) == NULL
||
55 fp
->file_plt_base
== 0 ||
56 pltaddr
- fp
->file_plt_base
>= fp
->file_plt_size
) {
61 i
= (pltaddr
- fp
->file_plt_base
) / M_PLT_ENTSIZE
- M_PLT_XNumber
;
63 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
66 r_addr
= fp
->file_jmp_rel
+ i
* sizeof (r
);
68 if (Pread(P
, &r
, sizeof (r
), r_addr
) == sizeof (r
) &&
69 (i
= ELF64_R_SYM(r
.r_info
)) < fp
->file_dynsym
.sym_symn
) {
70 Elf_Data
*data
= fp
->file_dynsym
.sym_data_pri
;
71 Elf64_Sym
*symp
= &(((Elf64_Sym
*)data
->d_buf
)[i
]);
73 return (fp
->file_dynsym
.sym_strs
+ symp
->st_name
);
78 r_addr
= fp
->file_jmp_rel
+ i
* sizeof (r
);
80 if (Pread(P
, &r
, sizeof (r
), r_addr
) == sizeof (r
) &&
81 (i
= ELF32_R_SYM(r
.r_info
)) < fp
->file_dynsym
.sym_symn
) {
82 Elf_Data
*data
= fp
->file_dynsym
.sym_data_pri
;
83 Elf32_Sym
*symp
= &(((Elf32_Sym
*)data
->d_buf
)[i
]);
85 return (fp
->file_dynsym
.sym_strs
+ symp
->st_name
);
93 Pissyscall(struct ps_prochandle
*P
, uintptr_t addr
)
97 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
98 if (Pread(P
, instr
, sizeof (syscall_instr
), addr
) !=
99 sizeof (syscall_instr
) ||
100 memcmp(instr
, syscall_instr
, sizeof (syscall_instr
)) != 0)
106 if (Pread(P
, instr
, sizeof (int_syscall_instr
), addr
) !=
107 sizeof (int_syscall_instr
))
110 if (memcmp(instr
, int_syscall_instr
, sizeof (int_syscall_instr
)) == 0)
117 Pissyscall_prev(struct ps_prochandle
*P
, uintptr_t addr
, uintptr_t *dst
)
121 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
122 if (Pissyscall(P
, addr
- sizeof (syscall_instr
))) {
124 *dst
= addr
- sizeof (syscall_instr
);
130 if ((ret
= Pissyscall(P
, addr
- sizeof (int_syscall_instr
))) != 0) {
132 *dst
= addr
- sizeof (int_syscall_instr
);
140 Pissyscall_text(struct ps_prochandle
*P
, const void *buf
, size_t buflen
)
142 if (P
->status
.pr_dmodel
== PR_MODEL_LP64
) {
143 if (buflen
>= sizeof (syscall_instr
) &&
144 memcmp(buf
, syscall_instr
, sizeof (syscall_instr
)) == 0)
150 if (buflen
< sizeof (int_syscall_instr
))
153 if (memcmp(buf
, int_syscall_instr
, sizeof (int_syscall_instr
)) == 0)
159 #define TR_ARG_MAX 6 /* Max args to print, same as SPARC */
162 * Given a return address, determine the likely number of arguments
163 * that were pushed on the stack prior to its execution. We do this by
164 * expecting that a typical call sequence consists of pushing arguments on
165 * the stack, executing a call instruction, and then performing an add
166 * on %esp to restore it to the value prior to pushing the arguments for
167 * the call. We attempt to detect such an add, and divide the addend
168 * by the size of a word to determine the number of pushed arguments.
170 * If we do not find such an add, this does not necessarily imply that the
171 * function took no arguments. It is not possible to reliably detect such a
172 * void function because hand-coded assembler does not always perform an add
173 * to %esp immediately after the "call" instruction (eg. _sys_call()).
174 * Because of this, we default to returning MIN(sz, TR_ARG_MAX) instead of 0
175 * in the absence of an add to %esp.
178 argcount(struct ps_prochandle
*P
, uint32_t pc
, ssize_t sz
)
183 max
= MIN(sz
/ sizeof (uint32_t), TR_ARG_MAX
);
186 * Read the instruction at the return location.
188 if (Pread(P
, instr
, sizeof (instr
), (uintptr_t)pc
) != sizeof (instr
))
191 if (instr
[1] != 0xc4)
195 case 0x81: /* count is a longword */
196 count
= instr
[2]+(instr
[3]<<8)+(instr
[4]<<16)+(instr
[5]<<24);
198 case 0x83: /* count is a byte */
205 count
/= sizeof (uint32_t);
206 return (MIN(count
, max
));
210 ucontext_32_to_prgregs(const ucontext32_t
*uc
, prgregset_t dst
)
212 const greg32_t
*src
= &uc
->uc_mcontext
.gregs
[0];
214 dst
[REG_DS
] = (uint16_t)src
[DS
];
215 dst
[REG_ES
] = (uint16_t)src
[ES
];
217 dst
[REG_GS
] = (uint16_t)src
[GS
];
218 dst
[REG_FS
] = (uint16_t)src
[FS
];
219 dst
[REG_SS
] = (uint16_t)src
[SS
];
220 dst
[REG_RSP
] = (uint32_t)src
[UESP
];
221 dst
[REG_RFL
] = src
[EFL
];
222 dst
[REG_CS
] = (uint16_t)src
[CS
];
223 dst
[REG_RIP
] = (uint32_t)src
[EIP
];
224 dst
[REG_ERR
] = (uint32_t)src
[ERR
];
225 dst
[REG_TRAPNO
] = (uint32_t)src
[TRAPNO
];
226 dst
[REG_RAX
] = (uint32_t)src
[EAX
];
227 dst
[REG_RCX
] = (uint32_t)src
[ECX
];
228 dst
[REG_RDX
] = (uint32_t)src
[EDX
];
229 dst
[REG_RBX
] = (uint32_t)src
[EBX
];
230 dst
[REG_RBP
] = (uint32_t)src
[EBP
];
231 dst
[REG_RSI
] = (uint32_t)src
[ESI
];
232 dst
[REG_RDI
] = (uint32_t)src
[EDI
];
236 Pstack_iter32(struct ps_prochandle
*P
, const prgregset_t regs
,
237 proc_stack_f
*func
, void *arg
)
239 prgreg_t
*prevfp
= NULL
;
250 uint32_t fp
, pfp
, pc
;
256 * Type definition for a structure corresponding to an IA32
257 * signal frame. Refer to the comments in Pstack.c for more info
271 init_uclist(&ucl
, P
);
272 (void) memcpy(gregs
, regs
, sizeof (gregs
));
277 while (fp
!= 0 || pc
!= 0) {
278 if (stack_loop(fp
, &prevfp
, &nfp
, &pfpsize
))
282 (sz
= Pread(P
, &frame
, sizeof (frame
), (uintptr_t)fp
)
283 >= (ssize_t
)(2* sizeof (uint32_t)))) {
285 * One more trick for signal frames: the kernel sets
286 * the return pc of the signal frame to 0xffffffff on
287 * Intel IA32, so argcount won't work.
289 if (frame
.pc
!= -1L) {
290 sz
-= 2* sizeof (uint32_t);
291 argc
= argcount(P
, (uint32_t)frame
.pc
, sz
);
293 argc
= 3; /* sighandler(signo, sip, ucp) */
295 (void) memset(&frame
, 0, sizeof (frame
));
302 for (i
= 0; i
< argc
; i
++)
303 args
[i
] = (uint32_t)frame
.args
[i
];
305 if ((rv
= func(arg
, gregs
, argc
, args
)) != 0)
309 * In order to allow iteration over java frames (which can have
310 * their own frame pointers), we allow the iterator to change
311 * the contents of gregs. If we detect a change, then we assume
312 * that the new values point to the next frame.
314 if (gregs
[R_FP
] != fp
|| gregs
[R_PC
] != pc
) {
324 if (find_uclink(&ucl
, pfp
+ sizeof (sf_t
)))
325 uc_addr
= pfp
+ sizeof (sf_t
);
329 if (uc_addr
!= NULL
&&
330 Pread(P
, &uc
, sizeof (uc
), uc_addr
) == sizeof (uc
)) {
331 ucontext_32_to_prgregs(&uc
, gregs
);
345 ucontext_n_to_prgregs(const ucontext_t
*src
, prgregset_t dst
)
347 (void) memcpy(dst
, src
->uc_mcontext
.gregs
, sizeof (gregset_t
));
351 * Read arguments from the frame indicated by regs into args, return the
352 * number of arguments successfully read
355 read_args(struct ps_prochandle
*P
, uintptr_t fp
, uintptr_t pc
, prgreg_t
*args
,
359 ctf_file_t
*ctfp
= NULL
;
360 ctf_funcinfo_t finfo
;
361 prsyminfo_t si
= {0};
362 uint8_t ins
[SAVEARGS_INSN_SEQ_LEN
];
369 ctf_id_t args_types
[5];
371 if (Pxlookup_by_addr(P
, pc
, NULL
, 0, &sym
, &si
) != 0)
374 if ((ctfp
= Paddr_to_ctf(P
, pc
)) == NULL
)
377 if (ctf_func_info(ctfp
, si
.prs_id
, &finfo
) == CTF_ERR
)
380 argc
= finfo
.ctc_argc
;
385 rettype
= ctf_type_kind(ctfp
, finfo
.ctc_return
);
388 * If the function returns a structure or union greater than 16 bytes
389 * in size %rdi contains the address in which to store the return
390 * value rather than for an argument.
392 if (((rettype
== CTF_K_STRUCT
) || (rettype
== CTF_K_UNION
)) &&
393 ctf_type_size(ctfp
, finfo
.ctc_return
) > 16)
399 * If any of the first 5 arguments are a structure less than 16 bytes
400 * in size, it will be passed spread across two argument registers,
401 * and we will not cope.
403 if (ctf_func_args(ctfp
, si
.prs_id
, 5, args_types
) == CTF_ERR
)
406 for (i
= 0; i
< MIN(5, finfo
.ctc_argc
); i
++) {
407 int t
= ctf_type_kind(ctfp
, args_types
[i
]);
409 if (((t
== CTF_K_STRUCT
) || (t
== CTF_K_UNION
)) &&
410 ctf_type_size(ctfp
, args_types
[i
]) <= 16)
415 * The number of instructions to search for argument saving is limited
416 * such that only instructions prior to %pc are considered and we
417 * never read arguments from a function where the saving code has not
418 * in fact yet executed.
420 insnsize
= MIN(MIN(sym
.st_size
, SAVEARGS_INSN_SEQ_LEN
),
423 if (Pread(P
, ins
, insnsize
, sym
.st_value
) != insnsize
)
427 ((args_style
= saveargs_has_args(ins
, insnsize
, argc
,
428 start_index
)) != SAVEARGS_NO_ARGS
)) {
429 int regargs
= MIN((6 - start_index
), argc
);
430 size_t size
= regargs
* sizeof (long);
434 * If Studio pushed a structure return address as an argument,
435 * we need to read one more argument than actually exists (the
436 * addr) to make everything line up.
438 if (args_style
== SAVEARGS_STRUCT_ARGS
)
439 size
+= sizeof (long);
441 if (Pread(P
, args
, size
, (fp
- size
)) != size
)
444 for (i
= 0; i
< (regargs
/ 2); i
++) {
445 prgreg_t t
= args
[i
];
447 args
[i
] = args
[regargs
- i
- 1];
448 args
[regargs
- i
- 1] = t
;
451 if (argc
> regargs
) {
452 size
= MIN((argc
- regargs
) * sizeof (long),
453 argsize
- (regargs
* sizeof (long)));
455 if (Pread(P
, &args
[regargs
], size
, fp
+
456 (sizeof (uintptr_t) * 2)) != size
)
467 Pstack_iter(struct ps_prochandle
*P
, const prgregset_t regs
,
468 proc_stack_f
*func
, void *arg
)
476 prgreg_t
*prevfp
= NULL
;
491 * Type definition for a structure corresponding to an IA32
492 * signal frame. Refer to the comments in Pstack.c for more info
500 prgreg_t args
[32] = {0};
502 if (P
->status
.pr_dmodel
!= PR_MODEL_LP64
)
503 return (Pstack_iter32(P
, regs
, func
, arg
));
505 init_uclist(&ucl
, P
);
506 (void) memcpy(gregs
, regs
, sizeof (gregs
));
511 while (fp
!= 0 || pc
!= 0) {
513 if (stack_loop(fp
, &prevfp
, &nfp
, &pfpsize
))
517 Pread(P
, &frame
, sizeof (frame
), (uintptr_t)fp
) ==
519 if (frame
.pc
== -1) {
521 args
[2] = fp
+ sizeof (sigframe_t
);
522 if (Pread(P
, &args
, 2 * sizeof (prgreg_t
),
523 fp
+ 2 * sizeof (prgreg_t
)) !=
524 2 * sizeof (prgreg_t
))
527 argc
= read_args(P
, fp
, pc
, args
,
531 (void) memset(&frame
, 0, sizeof (frame
));
538 if ((rv
= func(arg
, gregs
, argc
, args
)) != 0)
545 if (pc
== -1 && find_uclink(&ucl
, pfp
+ sizeof (sigframe_t
))) {
546 uc_addr
= pfp
+ sizeof (sigframe_t
);
548 if (Pread(P
, &uc
, sizeof (uc
), uc_addr
)
550 ucontext_n_to_prgregs(&uc
, gregs
);
566 Psyscall_setup(struct ps_prochandle
*P
, int nargs
, int sysindex
, uintptr_t sp
)
568 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
569 sp
-= sizeof (int) * (nargs
+2);
571 P
->status
.pr_lwp
.pr_reg
[REG_RAX
] = sysindex
;
572 P
->status
.pr_lwp
.pr_reg
[REG_RSP
] = sp
;
573 P
->status
.pr_lwp
.pr_reg
[REG_RIP
] = P
->sysaddr
;
575 int pusharg
= (nargs
> 6) ? nargs
- 6: 0;
577 sp
-= sizeof (int64_t) * (pusharg
+2);
579 P
->status
.pr_lwp
.pr_reg
[REG_RAX
] = sysindex
;
580 P
->status
.pr_lwp
.pr_reg
[REG_RSP
] = sp
;
581 P
->status
.pr_lwp
.pr_reg
[REG_RIP
] = P
->sysaddr
;
588 Psyscall_copyinargs(struct ps_prochandle
*P
, int nargs
, argdes_t
*argp
,
591 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
592 int32_t arglist
[MAXARGS
+2];
596 for (i
= 0, adp
= argp
; i
< nargs
; i
++, adp
++)
597 arglist
[1 + i
] = (int32_t)adp
->arg_value
;
599 arglist
[0] = P
->status
.pr_lwp
.pr_reg
[REG_RIP
];
600 if (Pwrite(P
, &arglist
[0], sizeof (int) * (nargs
+1),
601 (uintptr_t)ap
) != sizeof (int) * (nargs
+1))
604 int64_t arglist
[MAXARGS
+2];
607 int pusharg
= (nargs
> 6) ? nargs
- 6: 0;
609 for (i
= 0, adp
= argp
; i
< nargs
; i
++, adp
++) {
612 (void) Pputareg(P
, REG_RDI
, adp
->arg_value
);
615 (void) Pputareg(P
, REG_RSI
, adp
->arg_value
);
618 (void) Pputareg(P
, REG_RDX
, adp
->arg_value
);
621 (void) Pputareg(P
, REG_RCX
, adp
->arg_value
);
624 (void) Pputareg(P
, REG_R8
, adp
->arg_value
);
627 (void) Pputareg(P
, REG_R9
, adp
->arg_value
);
630 arglist
[i
- 5] = (uint64_t)adp
->arg_value
;
635 arglist
[0] = P
->status
.pr_lwp
.pr_reg
[REG_RIP
];
637 if (Pwrite(P
, &arglist
[0],
638 sizeof (int64_t) * (pusharg
+ 1), ap
) !=
639 sizeof (int64_t) * (pusharg
+ 1))
647 Psyscall_copyoutargs(struct ps_prochandle
*P
, int nargs
, argdes_t
*argp
,
650 if (P
->status
.pr_dmodel
== PR_MODEL_ILP32
) {
651 uint32_t arglist
[MAXARGS
+ 2];
655 if (Pread(P
, &arglist
[0], sizeof (int) * (nargs
+1),
656 (uintptr_t)ap
) != sizeof (int) * (nargs
+1))
659 for (i
= 0, adp
= argp
; i
< nargs
; i
++, adp
++)
660 adp
->arg_value
= arglist
[i
];
662 int pusharg
= (nargs
> 6) ? nargs
- 6: 0;
663 int64_t arglist
[MAXARGS
+2];
668 Pread(P
, &arglist
[0], sizeof (int64_t) * (pusharg
+ 1),
669 ap
) != sizeof (int64_t) * (pusharg
+ 1))
672 for (i
= 0, adp
= argp
; i
< nargs
; i
++, adp
++) {
676 P
->status
.pr_lwp
.pr_reg
[REG_RDI
];
680 P
->status
.pr_lwp
.pr_reg
[REG_RSI
];
684 P
->status
.pr_lwp
.pr_reg
[REG_RDX
];
688 P
->status
.pr_lwp
.pr_reg
[REG_RCX
];
692 P
->status
.pr_lwp
.pr_reg
[REG_R8
];
696 P
->status
.pr_lwp
.pr_reg
[REG_R9
];
699 adp
->arg_value
= arglist
[i
- 6];