comparison agent/src/os/linux/ps_proc.c @ 23475:d06b64fc150f

8145099: Better error message when SA can't attach to a process Reviewed-by: jbachorik, stuefe
author sla
date Thu, 10 Dec 2015 16:09:36 +0100
parents 7e2e246df4e9
children
comparison
equal deleted inserted replaced
23474:7c5babab479b 23475:d06b64fc150f
213 } 213 }
214 } 214 }
215 } 215 }
216 216
217 // attach to a process/thread specified by "pid" 217 // attach to a process/thread specified by "pid"
218 static bool ptrace_attach(pid_t pid) { 218 static bool ptrace_attach(pid_t pid, char* err_buf, size_t err_buf_len) {
219 if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) { 219 if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
220 print_debug("ptrace(PTRACE_ATTACH, ..) failed for %d\n", pid); 220 char buf[200];
221 char* msg = strerror_r(errno, buf, sizeof(buf));
222 snprintf(err_buf, err_buf_len, "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, msg);
223 print_debug("%s\n", err_buf);
221 return false; 224 return false;
222 } else { 225 } else {
223 return ptrace_waitpid(pid); 226 return ptrace_waitpid(pid);
224 } 227 }
225 } 228 }
337 .p_pwrite= process_write_data, 340 .p_pwrite= process_write_data,
338 .get_lwp_regs= process_get_lwp_regs 341 .get_lwp_regs= process_get_lwp_regs
339 }; 342 };
340 343
341 // attach to the process. One and only one exposed stuff 344 // attach to the process. One and only one exposed stuff
342 struct ps_prochandle* Pgrab(pid_t pid) { 345 struct ps_prochandle* Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) {
343 struct ps_prochandle* ph = NULL; 346 struct ps_prochandle* ph = NULL;
344 thread_info* thr = NULL; 347 thread_info* thr = NULL;
345 348
346 if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) { 349 if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
347 print_debug("can't allocate memory for ps_prochandle\n"); 350 snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
351 print_debug("%s\n", err_buf);
348 return NULL; 352 return NULL;
349 } 353 }
350 354
351 if (ptrace_attach(pid) != true) { 355 if (ptrace_attach(pid, err_buf, err_buf_len) != true) {
352 free(ph); 356 free(ph);
353 return NULL; 357 return NULL;
354 } 358 }
355 359
356 // initialize ps_prochandle 360 // initialize ps_prochandle
369 373
370 // attach to the threads 374 // attach to the threads
371 thr = ph->threads; 375 thr = ph->threads;
372 while (thr) { 376 while (thr) {
373 // don't attach to the main thread again 377 // don't attach to the main thread again
374 if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id) != true) { 378 if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id, err_buf, err_buf_len) != true) {
375 // even if one attach fails, we get return NULL 379 // even if one attach fails, we get return NULL
376 Prelease(ph); 380 Prelease(ph);
377 return NULL; 381 return NULL;
378 } 382 }
379 thr = thr->next; 383 thr = thr->next;