1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
356 static void detach_all_pids(struct ps_prochandle* ph) {
357 thread_info* thr = ph->threads;
358 while (thr) {
359 ptrace_detach(thr->lwp_id);
360 thr = thr->next;
361 }
362 }
363
364 static void process_cleanup(struct ps_prochandle* ph) {
365 detach_all_pids(ph);
366 }
367
368 static ps_prochandle_ops process_ops = {
369 .release= process_cleanup,
370 .p_pread= process_read_data,
371 .p_pwrite= process_write_data,
372 .get_lwp_regs= process_get_lwp_regs
373 };
374
375 // attach to the process. One and only one exposed stuff
376 struct ps_prochandle* Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) {
377 struct ps_prochandle* ph = NULL;
378 thread_info* thr = NULL;
379
380 if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
381 snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
382 print_debug("%s\n", err_buf);
383 return NULL;
384 }
385
386 if (ptrace_attach(pid, err_buf, err_buf_len) != true) {
387 free(ph);
388 return NULL;
389 }
390
391 // initialize ps_prochandle
392 ph->pid = pid;
393
394 // initialize vtable
395 ph->ops = &process_ops;
396
|
1 /*
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
356 static void detach_all_pids(struct ps_prochandle* ph) {
357 thread_info* thr = ph->threads;
358 while (thr) {
359 ptrace_detach(thr->lwp_id);
360 thr = thr->next;
361 }
362 }
363
364 static void process_cleanup(struct ps_prochandle* ph) {
365 detach_all_pids(ph);
366 }
367
368 static ps_prochandle_ops process_ops = {
369 .release= process_cleanup,
370 .p_pread= process_read_data,
371 .p_pwrite= process_write_data,
372 .get_lwp_regs= process_get_lwp_regs
373 };
374
375 // attach to the process. One and only one exposed stuff
376 JNIEXPORT struct ps_prochandle* JNICALL
377 Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) {
378 struct ps_prochandle* ph = NULL;
379 thread_info* thr = NULL;
380
381 if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
382 snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
383 print_debug("%s\n", err_buf);
384 return NULL;
385 }
386
387 if (ptrace_attach(pid, err_buf, err_buf_len) != true) {
388 free(ph);
389 return NULL;
390 }
391
392 // initialize ps_prochandle
393 ph->pid = pid;
394
395 // initialize vtable
396 ph->ops = &process_ops;
397
|