| 1209 | |
| 1210 | = 11/09 = |
| 1211 | == change sys_clock_gettime to getnstimeofday == |
| 1212 | * http://hira-consulting.com/wiki/index.php?getnstimeofday%28%29%2Flinux2.6 |
| 1213 | |
| 1214 | * vim arch/x86/kernel/new_debug.c |
| 1215 | {{{#!diff |
| 1216 | Index: arch/x86/kernel/new_debug.c |
| 1217 | =================================================================== |
| 1218 | --- arch/x86/kernel/new_debug.c (リビジョン 1390) |
| 1219 | +++ arch/x86/kernel/new_debug.c (作業コピー) |
| 1220 | @@ -35,7 +35,7 @@ |
| 1221 | printk(KERN_DEBUG "%s\n", message); |
| 1222 | |
| 1223 | if (tp_user != NULL) { |
| 1224 | - sys_clock_gettime(CLOCK_REALTIME, &ts); |
| 1225 | + getnstimeofday(&ts); |
| 1226 | if (copy_to_user(tp_user, &ts, sizeof(ts)) != 0) { |
| 1227 | errno = -EFAULT; |
| 1228 | goto out; |
| 1229 | }}} |
| 1230 | |
| 1231 | * windell46:i386 s0711489$ ./build |
| 1232 | {{{ |
| 1233 | Kernel: arch/x86/boot/bzImage is ready (#12) |
| 1234 | }}} |
| 1235 | * s0711489@ubuntu-lucid:~$ sudo /mnt/hgfs/tools/install.sh |
| 1236 | |
| 1237 | |
| 1238 | * gdb |
| 1239 | {{{ |
| 1240 | (gdb) file vmlinux |
| 1241 | Reading symbols from /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/i386/vmlinux...(no debugging symbols found)...done. |
| 1242 | (gdb) b sys_new_debug |
| 1243 | Breakpoint 1 at 0xc101d89c: file arch/x86/kernel/new_debug.c, line 9. |
| 1244 | (gdb) target remote windell46:8832 |
| 1245 | Remote debugging using windell46:8832 |
| 1246 | 0xc1007cdf in native_safe_halt () |
| 1247 | at /home/ugrad/07/s0711489/coursework/KernelHack/linux-2.6.35.14/i386/arch/x86/include/asm/irqflags.h:49 |
| 1248 | 49 asm volatile("sti; hlt": : :"memory"); |
| 1249 | (gdb) c |
| 1250 | Continuing. |
| 1251 | |
| 1252 | Breakpoint 1, sys_new_debug (message_user=0x8048646 "new_debug()", tp_user=0x0) |
| 1253 | at arch/x86/kernel/new_debug.c:9 |
| 1254 | 9 SYSCALL_DEFINE2(new_debug, const char *, message_user, struct timespec*, tp_user) { |
| 1255 | (gdb) l |
| 1256 | 4 #include <linux/time.h> |
| 1257 | 5 |
| 1258 | 6 /* from kernel/printk.c */ |
| 1259 | 7 #define __LOG_BUF_LEN 1024 |
| 1260 | 8 |
| 1261 | 9 SYSCALL_DEFINE2(new_debug, const char *, message_user, struct timespec*, tp_user) { |
| 1262 | 10 int errno; |
| 1263 | 11 static char message[__LOG_BUF_LEN]; |
| 1264 | 12 struct timespec ts; |
| 1265 | 13 long len = 0; |
| 1266 | (gdb) |
| 1267 | 14 |
| 1268 | 15 if(tp_user != NULL && ! access_ok(VERIFY_WRITE, tp_user, sizeof(*tp_user)) ) { |
| 1269 | 16 errno = -EFAULT; |
| 1270 | 17 goto out; |
| 1271 | 18 } |
| 1272 | 19 |
| 1273 | 20 if (message_user == NULL) { |
| 1274 | 21 errno = -EINVAL; |
| 1275 | 22 goto out; |
| 1276 | 23 } |
| 1277 | (gdb) |
| 1278 | 24 len = strnlen_user(message_user, __LOG_BUF_LEN); |
| 1279 | 25 if (len == 0 || len > __LOG_BUF_LEN) { |
| 1280 | 26 errno = -EINVAL; |
| 1281 | 27 goto out; |
| 1282 | 28 } |
| 1283 | 29 if (strncpy_from_user(message, message_user, sizeof(message) - 1) < 0) { |
| 1284 | 30 errno = -EFAULT; |
| 1285 | 31 goto out; |
| 1286 | 32 } |
| 1287 | 33 message[sizeof(message) - 1] = '\0'; |
| 1288 | (gdb) |
| 1289 | 34 |
| 1290 | 35 printk(KERN_DEBUG "%s\n", message); |
| 1291 | 36 |
| 1292 | 37 if (tp_user != NULL) { |
| 1293 | 38 getnstimeofday(&ts); |
| 1294 | 39 if (copy_to_user(tp_user, &ts, sizeof(ts)) != 0) { |
| 1295 | 40 errno = -EFAULT; |
| 1296 | 41 goto out; |
| 1297 | 42 } |
| 1298 | 43 } |
| 1299 | (gdb) |
| 1300 | 44 |
| 1301 | 45 errno = 0; |
| 1302 | 46 |
| 1303 | 47 out: |
| 1304 | 48 return errno; |
| 1305 | 49 } |
| 1306 | (gdb) s |
| 1307 | 15 if(tp_user != NULL && ! access_ok(VERIFY_WRITE, tp_user, sizeof(*tp_user)) ) { |
| 1308 | (gdb) |
| 1309 | 20 if (message_user == NULL) { |
| 1310 | (gdb) |
| 1311 | 24 len = strnlen_user(message_user, __LOG_BUF_LEN); |
| 1312 | (gdb) |
| 1313 | strnlen_user (s=0x8048646 "new_debug()", n=1024) |
| 1314 | at arch/x86/lib/usercopy_32.c:196 |
| 1315 | 196 { |
| 1316 | (gdb) finish |
| 1317 | Run till exit from #0 strnlen_user (s=0x8048646 "new_debug()", n=1024) |
| 1318 | at arch/x86/lib/usercopy_32.c:196 |
| 1319 | sys_new_debug (message_user=0x8048646 "new_debug()", tp_user=0x0) |
| 1320 | at arch/x86/kernel/new_debug.c:25 |
| 1321 | 25 if (len == 0 || len > __LOG_BUF_LEN) { |
| 1322 | Value returned is $1 = 12 |
| 1323 | (gdb) s |
| 1324 | 29 if (strncpy_from_user(message, message_user, sizeof(message) - 1) < 0) { |
| 1325 | (gdb) |
| 1326 | strncpy_from_user (dst=0xc158da00 "", src=0x8048646 "new_debug()", count=1023) |
| 1327 | at arch/x86/lib/usercopy_32.c:114 |
| 1328 | 114 { |
| 1329 | (gdb) finish |
| 1330 | Run till exit from #0 strncpy_from_user (dst=0xc158da00 "", |
| 1331 | src=0x8048646 "new_debug()", count=1023) at arch/x86/lib/usercopy_32.c:114 |
| 1332 | 0xc101d8ea in sys_new_debug (message_user=0x8048646 "new_debug()", tp_user=0x0) |
| 1333 | at arch/x86/kernel/new_debug.c:29 |
| 1334 | 29 if (strncpy_from_user(message, message_user, sizeof(message) - 1) < 0) { |
| 1335 | Value returned is $2 = 11 |
| 1336 | (gdb) s |
| 1337 | 35 printk(KERN_DEBUG "%s\n", message); |
| 1338 | (gdb) |
| 1339 | 33 message[sizeof(message) - 1] = '\0'; |
| 1340 | (gdb) |
| 1341 | 35 printk(KERN_DEBUG "%s\n", message); |
| 1342 | (gdb) |
| 1343 | printk (fmt=0xc1449542 "<7>%s\n") at kernel/printk.c:614 |
| 1344 | 614 va_start(args, fmt); |
| 1345 | (gdb) finish |
| 1346 | Run till exit from #0 printk (fmt=0xc1449542 "<7>%s\n") at kernel/printk.c:614 |
| 1347 | sys_new_debug (message_user=0x8048646 "new_debug()", tp_user=0x0) |
| 1348 | at arch/x86/kernel/new_debug.c:37 |
| 1349 | 37 if (tp_user != NULL) { |
| 1350 | Value returned is $3 = 33 |
| 1351 | (gdb) s |
| 1352 | 49 } |
| 1353 | (gdb) finish |
| 1354 | Run till exit from #0 sys_new_debug (message_user=0x8048646 "new_debug()", |
| 1355 | tp_user=0x0) at arch/x86/kernel/new_debug.c:49 |
| 1356 | 0xc100288c in ?? () |
| 1357 | Value returned is $4 = 0 |
| 1358 | (gdb) c |
| 1359 | Continuing. |
| 1360 | }}} |
| 1361 | * s0711489@ubuntu-lucid:~$ ./a.out |
| 1362 | {{{ |
| 1363 | new_debug() |
| 1364 | }}} |
| 1365 | |
| 1366 | * gdb |
| 1367 | {{{ |
| 1368 | Breakpoint 1, sys_new_debug (message_user=0xbfb1b9b9 "1", tp_user=0xbfb1b324) |
| 1369 | at arch/x86/kernel/new_debug.c:9 |
| 1370 | 9 SYSCALL_DEFINE2(new_debug, const char *, message_user, struct timespec*, tp_user) { |
| 1371 | (gdb) l |
| 1372 | 4 #include <linux/time.h> |
| 1373 | 5 |
| 1374 | 6 /* from kernel/printk.c */ |
| 1375 | 7 #define __LOG_BUF_LEN 1024 |
| 1376 | 8 |
| 1377 | 9 SYSCALL_DEFINE2(new_debug, const char *, message_user, struct timespec*, tp_user) { |
| 1378 | 10 int errno; |
| 1379 | 11 static char message[__LOG_BUF_LEN]; |
| 1380 | 12 struct timespec ts; |
| 1381 | 13 long len = 0; |
| 1382 | (gdb) p message |
| 1383 | $5 = "new_debug()", '\000' <repeats 1012 times> |
| 1384 | (gdb) p message_user |
| 1385 | $6 = 0xbfb1b9b9 "1" |
| 1386 | (gdb) p tp_user |
| 1387 | $7 = (struct timespec *) 0xbfb1b324 |
| 1388 | (gdb) p ts |
| 1389 | $8 = {tv_sec = -570499584, tv_nsec = -1216356364} |
| 1390 | (gdb) n |
| 1391 | 15 if(tp_user != NULL && ! access_ok(VERIFY_WRITE, tp_user, sizeof(*tp_user)) ) { |
| 1392 | (gdb) |
| 1393 | 20 if (message_user == NULL) { |
| 1394 | (gdb) |
| 1395 | 24 len = strnlen_user(message_user, __LOG_BUF_LEN); |
| 1396 | (gdb) |
| 1397 | 25 if (len == 0 || len > __LOG_BUF_LEN) { |
| 1398 | (gdb) |
| 1399 | 29 if (strncpy_from_user(message, message_user, sizeof(message) - 1) < 0) { |
| 1400 | (gdb) |
| 1401 | 35 printk(KERN_DEBUG "%s\n", message); |
| 1402 | (gdb) |
| 1403 | 33 message[sizeof(message) - 1] = '\0'; |
| 1404 | (gdb) |
| 1405 | 35 printk(KERN_DEBUG "%s\n", message); |
| 1406 | (gdb) |
| 1407 | 37 if (tp_user != NULL) { |
| 1408 | (gdb) |
| 1409 | 38 getnstimeofday(&ts); |
| 1410 | (gdb) |
| 1411 | 39 if (copy_to_user(tp_user, &ts, sizeof(ts)) != 0) { |
| 1412 | (gdb) p ts |
| 1413 | $9 = {tv_sec = 1320815780, tv_nsec = 601392214} |
| 1414 | (gdb) p tp_user |
| 1415 | $10 = (struct timespec *) 0xbfb1b324 |
| 1416 | (gdb) n |
| 1417 | 49 } |
| 1418 | (gdb) p tp_user |
| 1419 | $11 = (struct timespec *) 0xbfb1b324 |
| 1420 | (gdb) finish |
| 1421 | Run till exit from #0 sys_new_debug (message_user=0xbfb1b9b9 "1", |
| 1422 | tp_user=0xbfb1b324) at arch/x86/kernel/new_debug.c:49 |
| 1423 | 0xc100288c in ?? () |
| 1424 | Value returned is $12 = 0 |
| 1425 | (gdb) c |
| 1426 | Continuing. |
| 1427 | }}} |
| 1428 | * s0711489@ubuntu-lucid:~$ ./a.out 1 |
| 1429 | {{{ |
| 1430 | [1320815780.601392214] 1 |
| 1431 | }}} |
| 1432 | |
| 1433 | * s0711489@ubuntu-lucid:~$ ./a.out 1 2 3 4 |
| 1434 | {{{ |
| 1435 | [1320815837.554212464] 1 |
| 1436 | [1320815837.554367435] 2 |
| 1437 | [1320815837.554373628] 3 |
| 1438 | [1320815837.554378429] 4 |
| 1439 | }}} |
| 1440 | * s0711489@ubuntu-lucid:~$ ./a.out 1 2 3 4 |
| 1441 | {{{ |
| 1442 | [1320815846.036561542] 1 |
| 1443 | [1320815846.036726240] 2 |
| 1444 | [1320815846.036732514] 3 |
| 1445 | [1320815846.036737329] 4 |
| 1446 | }}} |
| 1447 | * s0711489@ubuntu-lucid:~$ tail /var/log/debug |
| 1448 | {{{ |
| 1449 | Nov 9 14:14:21 ubuntu-lucid kernel: [ 187.486803] new_debug() |
| 1450 | Nov 9 14:16:38 ubuntu-lucid kernel: [ 364.616724] 1 |
| 1451 | Nov 9 14:17:17 ubuntu-lucid kernel: [ 425.607587] 1 |
| 1452 | Nov 9 14:17:17 ubuntu-lucid kernel: [ 425.607744] 2 |
| 1453 | Nov 9 14:17:17 ubuntu-lucid kernel: [ 425.607750] 3 |
| 1454 | Nov 9 14:17:17 ubuntu-lucid kernel: [ 425.607755] 4 |
| 1455 | Nov 9 14:17:26 ubuntu-lucid kernel: [ 434.089936] 1 |
| 1456 | Nov 9 14:17:26 ubuntu-lucid kernel: [ 434.090102] 2 |
| 1457 | Nov 9 14:17:26 ubuntu-lucid kernel: [ 434.090109] 3 |
| 1458 | Nov 9 14:17:26 ubuntu-lucid kernel: [ 434.090114] 4 |
| 1459 | }}} |
| 1460 | |
| 1461 | == fix macro for new_debug == |
| 1462 | * vim arch/x86/include/asm/new_debug.h |
| 1463 | {{{#!diff |
| 1464 | Index: arch/x86/include/asm/new_debug.h |
| 1465 | =================================================================== |
| 1466 | --- arch/x86/include/asm/new_debug.h (リビジョン 1381) |
| 1467 | +++ arch/x86/include/asm/new_debug.h (作業コピー) |
| 1468 | @@ -2,6 +2,6 @@ |
| 1469 | #define _ASM_X86_NEW_DEBUG_H |
| 1470 | |
| 1471 | #include <asm/unistd.h> |
| 1472 | -#define new_debug(x) syscall(__NR_new_debug, x) |
| 1473 | +#define new_debug(x,y) syscall(__NR_new_debug, x, y) |
| 1474 | |
| 1475 | #endif /* _ASM_X86_NEW_DEBUG_H */ |
| 1476 | }}} |
| 1477 | |
| 1478 | * windell46:i386 s0711489$ ./build |
| 1479 | {{{ |
| 1480 | Kernel: arch/x86/boot/bzImage is ready (#12) |
| 1481 | }}} |
| 1482 | |
| 1483 | == add new_debug to x86_64 == |
| 1484 | * vim arch/x86/include/asm/unistd_64.h |
| 1485 | {{{#!diff |
| 1486 | Index: arch/x86/include/asm/unistd_64.h |
| 1487 | =================================================================== |
| 1488 | --- arch/x86/include/asm/unistd_64.h (リビジョン 1397) |
| 1489 | +++ arch/x86/include/asm/unistd_64.h (作業コピー) |
| 1490 | @@ -665,6 +665,8 @@ |
| 1491 | __SYSCALL(__NR_recvmmsg, sys_recvmmsg) |
| 1492 | #define __NR_new_hello 300 |
| 1493 | __SYSCALL(__NR_new_hello, sys_new_hello) |
| 1494 | +#define __NR_new_debug 301 |
| 1495 | +__SYSCALL(__NR_new_debug, sys_new_debug) |
| 1496 | |
| 1497 | #ifndef __NO_STUBS |
| 1498 | #define __ARCH_WANT_OLD_READDIR |
| 1499 | }}} |
| 1500 | * viola06:x86_64 s0711489$ ./build |
| 1501 | {{{ |
| 1502 | Kernel: arch/x86/boot/bzImage is ready (#5) |
| 1503 | }}} |
| 1504 | * s0711489@ubuntu-lucid64:~$ sudo /mnt/hgfs/tools/install.sh |
| 1505 | |
| 1506 | * s0711489@ubuntu-lucid64:~$ gcc -I /lib/modules/2.6.35.14/build/arch/x86/include/ 03/new_debug-sys.c |
| 1507 | |
| 1508 | * s0711489@ubuntu-lucid64:~$ ./a.out |
| 1509 | {{{ |
| 1510 | new_debug() |
| 1511 | }}} |
| 1512 | * s0711489@ubuntu-lucid64:~$ ./a.out 1 2 3 4 5 6 7 8 |
| 1513 | {{{ |
| 1514 | [1320819346.718424802] 1 |
| 1515 | [1320819346.718761534] 2 |
| 1516 | [1320819346.718767685] 3 |
| 1517 | [1320819346.718772404] 4 |
| 1518 | [1320819346.718776934] 5 |
| 1519 | [1320819346.718781608] 6 |
| 1520 | [1320819346.718786123] 7 |
| 1521 | [1320819346.718790631] 8 |
| 1522 | }}} |
| 1523 | * s0711489@ubuntu-lucid64:~$ tail /var/log/kern.log |
| 1524 | {{{ |
| 1525 | Nov 9 15:15:24 ubuntu-lucid64 kernel: [ 27.316107] cc1 used greatest stack depth: 4368 bytes left |
| 1526 | Nov 9 15:15:36 ubuntu-lucid64 kernel: [ 39.137213] new_debug() |
| 1527 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.629638] 1 |
| 1528 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.629978] 2 |
| 1529 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.629985] 3 |
| 1530 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.629990] 4 |
| 1531 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.629994] 5 |
| 1532 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.629999] 6 |
| 1533 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.630004] 7 |
| 1534 | Nov 9 15:15:46 ubuntu-lucid64 kernel: [ 49.630008] 8 |
| 1535 | }}} |